mobile/android/base/tests/testHomeProvider.js

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

mercurial