mobile/android/base/tests/testHomeProvider.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 const { utils: Cu } = Components;
michael@0 7
michael@0 8 Cu.import("resource://gre/modules/HomeProvider.jsm");
michael@0 9 Cu.import("resource://gre/modules/osfile.jsm");
michael@0 10 Cu.import("resource://gre/modules/Services.jsm");
michael@0 11 Cu.import("resource://gre/modules/Sqlite.jsm");
michael@0 12 Cu.import("resource://gre/modules/Task.jsm");
michael@0 13
michael@0 14 const TEST_DATASET_ID = "test-dataset-id";
michael@0 15 const TEST_URL = "http://test.com";
michael@0 16 const TEST_TITLE = "Test";
michael@0 17
michael@0 18 const PREF_SYNC_CHECK_INTERVAL_SECS = "home.sync.checkIntervalSecs";
michael@0 19 const TEST_INTERVAL_SECS = 1;
michael@0 20
michael@0 21 const DB_PATH = OS.Path.join(OS.Constants.Path.profileDir, "home.sqlite");
michael@0 22
michael@0 23 add_test(function test_request_sync() {
michael@0 24 // The current implementation of requestSync is synchronous.
michael@0 25 let success = HomeProvider.requestSync(TEST_DATASET_ID, function callback(datasetId) {
michael@0 26 do_check_eq(datasetId, TEST_DATASET_ID);
michael@0 27 });
michael@0 28
michael@0 29 do_check_true(success);
michael@0 30 run_next_test();
michael@0 31 });
michael@0 32
michael@0 33 add_test(function test_periodic_sync() {
michael@0 34 do_register_cleanup(function cleanup() {
michael@0 35 Services.prefs.clearUserPref(PREF_SYNC_CHECK_INTERVAL_SECS);
michael@0 36 HomeProvider.removePeriodicSync(TEST_DATASET_ID);
michael@0 37 });
michael@0 38
michael@0 39 // Lower the check interval for testing purposes.
michael@0 40 Services.prefs.setIntPref(PREF_SYNC_CHECK_INTERVAL_SECS, TEST_INTERVAL_SECS);
michael@0 41
michael@0 42 HomeProvider.addPeriodicSync(TEST_DATASET_ID, TEST_INTERVAL_SECS, function callback(datasetId) {
michael@0 43 do_check_eq(datasetId, TEST_DATASET_ID);
michael@0 44 run_next_test();
michael@0 45 });
michael@0 46 });
michael@0 47
michael@0 48 add_task(function test_save_and_delete() {
michael@0 49 // Use the HomeProvider API to save some data.
michael@0 50 let storage = HomeProvider.getStorage(TEST_DATASET_ID);
michael@0 51 yield storage.save([{ title: TEST_TITLE, url: TEST_URL }]);
michael@0 52
michael@0 53 // Peek in the DB to make sure we have the right data.
michael@0 54 let db = yield Sqlite.openConnection({ path: DB_PATH });
michael@0 55
michael@0 56 // Make sure the items table was created.
michael@0 57 do_check_true(yield db.tableExists("items"));
michael@0 58
michael@0 59 // Make sure the correct values for the item ended up in there.
michael@0 60 let result = yield db.execute("SELECT * FROM items", null, function onRow(row){
michael@0 61 do_check_eq(row.getResultByName("dataset_id"), TEST_DATASET_ID);
michael@0 62 do_check_eq(row.getResultByName("url"), TEST_URL);
michael@0 63 });
michael@0 64
michael@0 65 // Use the HomeProvider API to delete the data.
michael@0 66 yield storage.deleteAll();
michael@0 67
michael@0 68 // Make sure the data was deleted.
michael@0 69 let result = yield db.execute("SELECT * FROM items");
michael@0 70 do_check_eq(result.length, 0);
michael@0 71
michael@0 72 db.close();
michael@0 73 });
michael@0 74
michael@0 75 add_task(function test_row_validation() {
michael@0 76 // Use the HomeProvider API to save some data.
michael@0 77 let storage = HomeProvider.getStorage(TEST_DATASET_ID);
michael@0 78
michael@0 79 let invalidRows = [
michael@0 80 { url: "url" },
michael@0 81 { title: "title" },
michael@0 82 { description: "description" },
michael@0 83 { image_url: "image_url" }
michael@0 84 ];
michael@0 85
michael@0 86 // None of these save calls should save anything
michael@0 87 for (let row of invalidRows) {
michael@0 88 try {
michael@0 89 yield storage.save([row]);
michael@0 90 } catch (e if e instanceof HomeProvider.ValidationError) {
michael@0 91 // Just catch and ignore validation errors
michael@0 92 }
michael@0 93 }
michael@0 94
michael@0 95 // Peek in the DB to make sure we have the right data.
michael@0 96 let db = yield Sqlite.openConnection({ path: DB_PATH });
michael@0 97
michael@0 98 // Make sure no data has been saved.
michael@0 99 let result = yield db.execute("SELECT * FROM items");
michael@0 100 do_check_eq(result.length, 0);
michael@0 101
michael@0 102 db.close();
michael@0 103 });
michael@0 104
michael@0 105 add_task(function test_save_transaction() {
michael@0 106 // Use the HomeProvider API to save some data.
michael@0 107 let storage = HomeProvider.getStorage(TEST_DATASET_ID);
michael@0 108
michael@0 109 // One valid, one invalid
michael@0 110 let rows = [
michael@0 111 { title: TEST_TITLE, url: TEST_URL },
michael@0 112 { image_url: "image_url" }
michael@0 113 ];
michael@0 114
michael@0 115 // Try to save all the rows at once
michael@0 116 try {
michael@0 117 yield storage.save(rows);
michael@0 118 } catch (e if e instanceof HomeProvider.ValidationError) {
michael@0 119 // Just catch and ignore validation errors
michael@0 120 }
michael@0 121
michael@0 122 // Peek in the DB to make sure we have the right data.
michael@0 123 let db = yield Sqlite.openConnection({ path: DB_PATH });
michael@0 124
michael@0 125 // Make sure no data has been saved.
michael@0 126 let result = yield db.execute("SELECT * FROM items");
michael@0 127 do_check_eq(result.length, 0);
michael@0 128
michael@0 129 db.close();
michael@0 130 });
michael@0 131
michael@0 132 run_next_test();

mercurial