mobile/android/base/tests/testHomeProvider.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     6 const { utils: Cu } = Components;
     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");
    14 const TEST_DATASET_ID = "test-dataset-id";
    15 const TEST_URL = "http://test.com";
    16 const TEST_TITLE = "Test";
    18 const PREF_SYNC_CHECK_INTERVAL_SECS = "home.sync.checkIntervalSecs";
    19 const TEST_INTERVAL_SECS = 1;
    21 const DB_PATH = OS.Path.join(OS.Constants.Path.profileDir, "home.sqlite");
    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   });
    29   do_check_true(success);
    30   run_next_test();
    31 });
    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   });
    39   // Lower the check interval for testing purposes.
    40   Services.prefs.setIntPref(PREF_SYNC_CHECK_INTERVAL_SECS, TEST_INTERVAL_SECS);
    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 });
    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 }]);
    53   // Peek in the DB to make sure we have the right data.
    54   let db = yield Sqlite.openConnection({ path: DB_PATH });
    56   // Make sure the items table was created.
    57   do_check_true(yield db.tableExists("items"));
    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   });
    65   // Use the HomeProvider API to delete the data.
    66   yield storage.deleteAll();
    68   // Make sure the data was deleted.
    69   let result = yield db.execute("SELECT * FROM items");
    70   do_check_eq(result.length, 0);
    72   db.close();
    73 });
    75 add_task(function test_row_validation() {
    76   // Use the HomeProvider API to save some data.
    77   let storage = HomeProvider.getStorage(TEST_DATASET_ID);
    79   let invalidRows = [
    80     { url: "url" },
    81     { title: "title" },
    82     { description: "description" },
    83     { image_url: "image_url" }
    84   ];
    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   }
    95   // Peek in the DB to make sure we have the right data.
    96   let db = yield Sqlite.openConnection({ path: DB_PATH });
    98   // Make sure no data has been saved.
    99   let result = yield db.execute("SELECT * FROM items");
   100   do_check_eq(result.length, 0);
   102   db.close();
   103 });
   105 add_task(function test_save_transaction() {
   106   // Use the HomeProvider API to save some data.
   107   let storage = HomeProvider.getStorage(TEST_DATASET_ID);
   109   // One valid, one invalid
   110   let rows = [
   111     { title: TEST_TITLE, url: TEST_URL },
   112     { image_url: "image_url" }
   113   ];
   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   }
   122   // Peek in the DB to make sure we have the right data.
   123   let db = yield Sqlite.openConnection({ path: DB_PATH });
   125   // Make sure no data has been saved.
   126   let result = yield db.execute("SELECT * FROM items");
   127   do_check_eq(result.length, 0);
   129   db.close();
   130 });
   132 run_next_test();

mercurial