mobile/android/base/tests/testHomeProvider.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/tests/testHomeProvider.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +const { utils: Cu } = Components;
    1.10 +
    1.11 +Cu.import("resource://gre/modules/HomeProvider.jsm");
    1.12 +Cu.import("resource://gre/modules/osfile.jsm");
    1.13 +Cu.import("resource://gre/modules/Services.jsm");
    1.14 +Cu.import("resource://gre/modules/Sqlite.jsm");
    1.15 +Cu.import("resource://gre/modules/Task.jsm");
    1.16 +
    1.17 +const TEST_DATASET_ID = "test-dataset-id";
    1.18 +const TEST_URL = "http://test.com";
    1.19 +const TEST_TITLE = "Test";
    1.20 +
    1.21 +const PREF_SYNC_CHECK_INTERVAL_SECS = "home.sync.checkIntervalSecs";
    1.22 +const TEST_INTERVAL_SECS = 1;
    1.23 +
    1.24 +const DB_PATH = OS.Path.join(OS.Constants.Path.profileDir, "home.sqlite");
    1.25 +
    1.26 +add_test(function test_request_sync() {
    1.27 +  // The current implementation of requestSync is synchronous.
    1.28 +  let success = HomeProvider.requestSync(TEST_DATASET_ID, function callback(datasetId) {
    1.29 +    do_check_eq(datasetId, TEST_DATASET_ID);
    1.30 +  });
    1.31 +
    1.32 +  do_check_true(success);
    1.33 +  run_next_test();
    1.34 +});
    1.35 +
    1.36 +add_test(function test_periodic_sync() {
    1.37 +  do_register_cleanup(function cleanup() {
    1.38 +    Services.prefs.clearUserPref(PREF_SYNC_CHECK_INTERVAL_SECS);
    1.39 +    HomeProvider.removePeriodicSync(TEST_DATASET_ID);
    1.40 +  });
    1.41 +
    1.42 +  // Lower the check interval for testing purposes.
    1.43 +  Services.prefs.setIntPref(PREF_SYNC_CHECK_INTERVAL_SECS, TEST_INTERVAL_SECS);
    1.44 +
    1.45 +  HomeProvider.addPeriodicSync(TEST_DATASET_ID, TEST_INTERVAL_SECS, function callback(datasetId) {
    1.46 +    do_check_eq(datasetId, TEST_DATASET_ID);
    1.47 +    run_next_test();
    1.48 +  });
    1.49 +});
    1.50 +
    1.51 +add_task(function test_save_and_delete() {
    1.52 +  // Use the HomeProvider API to save some data.
    1.53 +  let storage = HomeProvider.getStorage(TEST_DATASET_ID);
    1.54 +  yield storage.save([{ title: TEST_TITLE, url: TEST_URL }]);
    1.55 +
    1.56 +  // Peek in the DB to make sure we have the right data.
    1.57 +  let db = yield Sqlite.openConnection({ path: DB_PATH });
    1.58 +
    1.59 +  // Make sure the items table was created.
    1.60 +  do_check_true(yield db.tableExists("items"));
    1.61 +
    1.62 +  // Make sure the correct values for the item ended up in there.
    1.63 +  let result = yield db.execute("SELECT * FROM items", null, function onRow(row){
    1.64 +    do_check_eq(row.getResultByName("dataset_id"), TEST_DATASET_ID);
    1.65 +    do_check_eq(row.getResultByName("url"), TEST_URL);
    1.66 +  });
    1.67 +
    1.68 +  // Use the HomeProvider API to delete the data.
    1.69 +  yield storage.deleteAll();
    1.70 +
    1.71 +  // Make sure the data was deleted.
    1.72 +  let result = yield db.execute("SELECT * FROM items");
    1.73 +  do_check_eq(result.length, 0);
    1.74 +
    1.75 +  db.close();
    1.76 +});
    1.77 +
    1.78 +add_task(function test_row_validation() {
    1.79 +  // Use the HomeProvider API to save some data.
    1.80 +  let storage = HomeProvider.getStorage(TEST_DATASET_ID);
    1.81 +
    1.82 +  let invalidRows = [
    1.83 +    { url: "url" },
    1.84 +    { title: "title" },
    1.85 +    { description: "description" },
    1.86 +    { image_url: "image_url" }
    1.87 +  ];
    1.88 +
    1.89 +  // None of these save calls should save anything
    1.90 +  for (let row of invalidRows) {
    1.91 +    try {
    1.92 +      yield storage.save([row]);
    1.93 +    } catch (e if e instanceof HomeProvider.ValidationError) {
    1.94 +      // Just catch and ignore validation errors
    1.95 +    }
    1.96 +  }
    1.97 +
    1.98 +  // Peek in the DB to make sure we have the right data.
    1.99 +  let db = yield Sqlite.openConnection({ path: DB_PATH });
   1.100 +
   1.101 +  // Make sure no data has been saved.
   1.102 +  let result = yield db.execute("SELECT * FROM items");
   1.103 +  do_check_eq(result.length, 0);
   1.104 +
   1.105 +  db.close();
   1.106 +});
   1.107 +
   1.108 +add_task(function test_save_transaction() {
   1.109 +  // Use the HomeProvider API to save some data.
   1.110 +  let storage = HomeProvider.getStorage(TEST_DATASET_ID);
   1.111 +
   1.112 +  // One valid, one invalid
   1.113 +  let rows = [
   1.114 +    { title: TEST_TITLE, url: TEST_URL },
   1.115 +    { image_url: "image_url" }
   1.116 +  ];
   1.117 +
   1.118 +  // Try to save all the rows at once
   1.119 +  try {
   1.120 +    yield storage.save(rows);
   1.121 +  } catch (e if e instanceof HomeProvider.ValidationError) {
   1.122 +    // Just catch and ignore validation errors
   1.123 +  }
   1.124 +
   1.125 +  // Peek in the DB to make sure we have the right data.
   1.126 +  let db = yield Sqlite.openConnection({ path: DB_PATH });
   1.127 +
   1.128 +  // Make sure no data has been saved.
   1.129 +  let result = yield db.execute("SELECT * FROM items");
   1.130 +  do_check_eq(result.length, 0);
   1.131 +
   1.132 +  db.close();
   1.133 +});
   1.134 +
   1.135 +run_next_test();

mercurial