michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const { utils: Cu } = Components; michael@0: michael@0: Cu.import("resource://gre/modules/HomeProvider.jsm"); michael@0: Cu.import("resource://gre/modules/osfile.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/Sqlite.jsm"); michael@0: Cu.import("resource://gre/modules/Task.jsm"); michael@0: michael@0: const TEST_DATASET_ID = "test-dataset-id"; michael@0: const TEST_URL = "http://test.com"; michael@0: const TEST_TITLE = "Test"; michael@0: michael@0: const PREF_SYNC_CHECK_INTERVAL_SECS = "home.sync.checkIntervalSecs"; michael@0: const TEST_INTERVAL_SECS = 1; michael@0: michael@0: const DB_PATH = OS.Path.join(OS.Constants.Path.profileDir, "home.sqlite"); michael@0: michael@0: add_test(function test_request_sync() { michael@0: // The current implementation of requestSync is synchronous. michael@0: let success = HomeProvider.requestSync(TEST_DATASET_ID, function callback(datasetId) { michael@0: do_check_eq(datasetId, TEST_DATASET_ID); michael@0: }); michael@0: michael@0: do_check_true(success); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_periodic_sync() { michael@0: do_register_cleanup(function cleanup() { michael@0: Services.prefs.clearUserPref(PREF_SYNC_CHECK_INTERVAL_SECS); michael@0: HomeProvider.removePeriodicSync(TEST_DATASET_ID); michael@0: }); michael@0: michael@0: // Lower the check interval for testing purposes. michael@0: Services.prefs.setIntPref(PREF_SYNC_CHECK_INTERVAL_SECS, TEST_INTERVAL_SECS); michael@0: michael@0: HomeProvider.addPeriodicSync(TEST_DATASET_ID, TEST_INTERVAL_SECS, function callback(datasetId) { michael@0: do_check_eq(datasetId, TEST_DATASET_ID); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_task(function test_save_and_delete() { michael@0: // Use the HomeProvider API to save some data. michael@0: let storage = HomeProvider.getStorage(TEST_DATASET_ID); michael@0: yield storage.save([{ title: TEST_TITLE, url: TEST_URL }]); michael@0: michael@0: // Peek in the DB to make sure we have the right data. michael@0: let db = yield Sqlite.openConnection({ path: DB_PATH }); michael@0: michael@0: // Make sure the items table was created. michael@0: do_check_true(yield db.tableExists("items")); michael@0: michael@0: // Make sure the correct values for the item ended up in there. michael@0: let result = yield db.execute("SELECT * FROM items", null, function onRow(row){ michael@0: do_check_eq(row.getResultByName("dataset_id"), TEST_DATASET_ID); michael@0: do_check_eq(row.getResultByName("url"), TEST_URL); michael@0: }); michael@0: michael@0: // Use the HomeProvider API to delete the data. michael@0: yield storage.deleteAll(); michael@0: michael@0: // Make sure the data was deleted. michael@0: let result = yield db.execute("SELECT * FROM items"); michael@0: do_check_eq(result.length, 0); michael@0: michael@0: db.close(); michael@0: }); michael@0: michael@0: add_task(function test_row_validation() { michael@0: // Use the HomeProvider API to save some data. michael@0: let storage = HomeProvider.getStorage(TEST_DATASET_ID); michael@0: michael@0: let invalidRows = [ michael@0: { url: "url" }, michael@0: { title: "title" }, michael@0: { description: "description" }, michael@0: { image_url: "image_url" } michael@0: ]; michael@0: michael@0: // None of these save calls should save anything michael@0: for (let row of invalidRows) { michael@0: try { michael@0: yield storage.save([row]); michael@0: } catch (e if e instanceof HomeProvider.ValidationError) { michael@0: // Just catch and ignore validation errors michael@0: } michael@0: } michael@0: michael@0: // Peek in the DB to make sure we have the right data. michael@0: let db = yield Sqlite.openConnection({ path: DB_PATH }); michael@0: michael@0: // Make sure no data has been saved. michael@0: let result = yield db.execute("SELECT * FROM items"); michael@0: do_check_eq(result.length, 0); michael@0: michael@0: db.close(); michael@0: }); michael@0: michael@0: add_task(function test_save_transaction() { michael@0: // Use the HomeProvider API to save some data. michael@0: let storage = HomeProvider.getStorage(TEST_DATASET_ID); michael@0: michael@0: // One valid, one invalid michael@0: let rows = [ michael@0: { title: TEST_TITLE, url: TEST_URL }, michael@0: { image_url: "image_url" } michael@0: ]; michael@0: michael@0: // Try to save all the rows at once michael@0: try { michael@0: yield storage.save(rows); michael@0: } catch (e if e instanceof HomeProvider.ValidationError) { michael@0: // Just catch and ignore validation errors michael@0: } michael@0: michael@0: // Peek in the DB to make sure we have the right data. michael@0: let db = yield Sqlite.openConnection({ path: DB_PATH }); michael@0: michael@0: // Make sure no data has been saved. michael@0: let result = yield db.execute("SELECT * FROM items"); michael@0: do_check_eq(result.length, 0); michael@0: michael@0: db.close(); michael@0: }); michael@0: michael@0: run_next_test();