storage/test/unit/test_storage_service.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/test/unit/test_storage_service.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,141 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// This file tests the functions of mozIStorageService except for
     1.9 +// openUnsharedDatabase, which is tested by test_storage_service_unshared.js.
    1.10 +
    1.11 +const BACKUP_FILE_NAME = "test_storage.sqlite.backup";
    1.12 +
    1.13 +function test_openSpecialDatabase_invalid_arg()
    1.14 +{
    1.15 +  try {
    1.16 +    getService().openSpecialDatabase("abcd");
    1.17 +    do_throw("We should not get here!");
    1.18 +  } catch (e) {
    1.19 +    print(e);
    1.20 +    print("e.result is " + e.result);
    1.21 +    do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
    1.22 +  }
    1.23 +}
    1.24 +
    1.25 +function test_openDatabase_null_file()
    1.26 +{
    1.27 +  try {
    1.28 +    getService().openDatabase(null);
    1.29 +    do_throw("We should not get here!");
    1.30 +  } catch (e) {
    1.31 +    print(e);
    1.32 +    print("e.result is " + e.result);
    1.33 +    do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
    1.34 +  }
    1.35 +}
    1.36 +
    1.37 +function test_openUnsharedDatabase_null_file()
    1.38 +{
    1.39 +  try {
    1.40 +    getService().openUnsharedDatabase(null);
    1.41 +    do_throw("We should not get here!");
    1.42 +  } catch (e) {
    1.43 +    print(e);
    1.44 +    print("e.result is " + e.result);
    1.45 +    do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
    1.46 +  }
    1.47 +}
    1.48 +
    1.49 +function test_openDatabase_file_DNE()
    1.50 +{
    1.51 +  // the file should be created after calling
    1.52 +  var db = getTestDB();
    1.53 +  do_check_false(db.exists());
    1.54 +  getService().openDatabase(db);
    1.55 +  do_check_true(db.exists());
    1.56 +}
    1.57 +
    1.58 +function test_openDatabase_file_exists()
    1.59 +{
    1.60 +  // it should already exist from our last test
    1.61 +  var db = getTestDB();
    1.62 +  do_check_true(db.exists());
    1.63 +  getService().openDatabase(db);
    1.64 +  do_check_true(db.exists());
    1.65 +}
    1.66 +
    1.67 +function test_corrupt_db_throws_with_openDatabase()
    1.68 +{
    1.69 +  try {
    1.70 +    getDatabase(getCorruptDB());
    1.71 +    do_throw("should not be here");
    1.72 +  }
    1.73 +  catch (e) {
    1.74 +    do_check_eq(Cr.NS_ERROR_FILE_CORRUPTED, e.result);
    1.75 +  }
    1.76 +}
    1.77 +
    1.78 +function test_fake_db_throws_with_openDatabase()
    1.79 +{
    1.80 +  try {
    1.81 +    getDatabase(getFakeDB());
    1.82 +    do_throw("should not be here");
    1.83 +  }
    1.84 +  catch (e) {
    1.85 +    do_check_eq(Cr.NS_ERROR_FILE_CORRUPTED, e.result);
    1.86 +  }
    1.87 +}
    1.88 +
    1.89 +function test_backup_not_new_filename()
    1.90 +{
    1.91 +  const fname = getTestDB().leafName;
    1.92 +
    1.93 +  var backup = getService().backupDatabaseFile(getTestDB(), fname);
    1.94 +  do_check_neq(fname, backup.leafName);
    1.95 +
    1.96 +  backup.remove(false);
    1.97 +}
    1.98 +
    1.99 +function test_backup_new_filename()
   1.100 +{
   1.101 +  var backup = getService().backupDatabaseFile(getTestDB(), BACKUP_FILE_NAME);
   1.102 +  do_check_eq(BACKUP_FILE_NAME, backup.leafName);
   1.103 +  
   1.104 +  backup.remove(false);
   1.105 +}
   1.106 +
   1.107 +function test_backup_new_folder()
   1.108 +{
   1.109 +  var parentDir = getTestDB().parent;
   1.110 +  parentDir.append("test_storage_temp");
   1.111 +  if (parentDir.exists())
   1.112 +    parentDir.remove(true);
   1.113 +  parentDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
   1.114 +  do_check_true(parentDir.exists());
   1.115 +
   1.116 +  var backup = getService().backupDatabaseFile(getTestDB(), BACKUP_FILE_NAME,
   1.117 +                                               parentDir);
   1.118 +  do_check_eq(BACKUP_FILE_NAME, backup.leafName);
   1.119 +  do_check_true(parentDir.equals(backup.parent));
   1.120 +
   1.121 +  parentDir.remove(true);
   1.122 +}
   1.123 +
   1.124 +var tests = [
   1.125 +  test_openSpecialDatabase_invalid_arg,
   1.126 +  test_openDatabase_null_file,
   1.127 +  test_openUnsharedDatabase_null_file,
   1.128 +  test_openDatabase_file_DNE,
   1.129 +  test_openDatabase_file_exists,
   1.130 +  test_corrupt_db_throws_with_openDatabase,
   1.131 +  test_fake_db_throws_with_openDatabase,
   1.132 +  test_backup_not_new_filename,
   1.133 +  test_backup_new_filename,
   1.134 +  test_backup_new_folder,
   1.135 +];
   1.136 +
   1.137 +function run_test()
   1.138 +{
   1.139 +  for (var i = 0; i < tests.length; i++)
   1.140 +    tests[i]();
   1.141 +    
   1.142 +  cleanup();
   1.143 +}
   1.144 +

mercurial