summaryrefslogtreecommitdiff
path: root/toolkit/components/contentprefs/tests/unit/test_stringGroups.js
blob: afce3b64a3b693e84644f23a383d60480045acd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

function run_test() {

  var cps = new ContentPrefInstance(null);

  // Make sure disk synchronization checking is turned off by default.
  var statement = cps.DBConnection.createStatement("PRAGMA synchronous");
  statement.executeStep();
  do_check_eq(0, statement.getInt32(0));

  // These are the different types of aGroup arguments we'll test.
  var anObject = {"foo":"bar"};                                // a simple object
  var uri = ContentPrefTest.getURI("http://www.example.com/"); // nsIURI
  var stringURI = "www.example.com";                           // typeof = "string"
  var stringObjectURI = new String("www.example.com");         // typeof = "object"

  {
    // First check that all the methods work or don't work.
    function simple_test_methods(aGroup, shouldThrow) {
      var prefName = "test.pref.0";
      var prefValue = Math.floor(Math.random() * 100);

      if (shouldThrow) {
        do_check_thrown(function () { cps.getPref(aGroup, prefName); });
        do_check_thrown(function () { cps.setPref(aGroup, prefName, prefValue); });
        do_check_thrown(function () { cps.hasPref(aGroup, prefName); });
        do_check_thrown(function () { cps.removePref(aGroup, prefName); });
        do_check_thrown(function () { cps.getPrefs(aGroup); });
      } else {
        do_check_eq(cps.setPref(aGroup, prefName, prefValue), undefined);
        do_check_true(cps.hasPref(aGroup, prefName));
        do_check_eq(cps.getPref(aGroup, prefName), prefValue);
        do_check_eq(cps.removePref(aGroup, prefName), undefined);
        do_check_false(cps.hasPref(aGroup, prefName));
      }
    }

    simple_test_methods(cps, true); // arbitrary nsISupports object, should throw too
    simple_test_methods(anObject, true);
    simple_test_methods(uri, false);
    simple_test_methods(stringURI, false);
    simple_test_methods(stringObjectURI, false);
  }

  {
    // Now we'll check that each argument produces the same result.
    function complex_test_methods(aGroup) {
      var prefName = "test.pref.1";
      var prefValue = Math.floor(Math.random() * 100);

      do_check_eq(cps.setPref(aGroup, prefName, prefValue), undefined);

      do_check_true(cps.hasPref(uri, prefName));
      do_check_true(cps.hasPref(stringURI, prefName));
      do_check_true(cps.hasPref(stringObjectURI, prefName));

      do_check_eq(cps.getPref(uri, prefName), prefValue);
      do_check_eq(cps.getPref(stringURI, prefName), prefValue);
      do_check_eq(cps.getPref(stringObjectURI, prefName), prefValue);

      do_check_eq(cps.removePref(aGroup, prefName), undefined);

      do_check_false(cps.hasPref(uri, prefName));
      do_check_false(cps.hasPref(stringURI, prefName));
      do_check_false(cps.hasPref(stringObjectURI, prefName));
    }

    complex_test_methods(uri);
    complex_test_methods(stringURI);
    complex_test_methods(stringObjectURI);
  }

  {
    // test getPrefs returns the same prefs
    do_check_eq(cps.setPref(stringObjectURI, "test.5", 5), undefined);
    do_check_eq(cps.setPref(stringURI, "test.2", 2), undefined);
    do_check_eq(cps.setPref(uri, "test.1", 1), undefined);

    enumerateAndCheck(cps.getPrefs(uri), 8, ["test.1", "test.2", "test.5"]);
    enumerateAndCheck(cps.getPrefs(stringURI), 8, ["test.1", "test.2", "test.5"]);
    enumerateAndCheck(cps.getPrefs(stringObjectURI), 8, ["test.1", "test.2", "test.5"]);

    do_check_eq(cps.setPref(uri, "test.4", 4), undefined);
    do_check_eq(cps.setPref(stringObjectURI, "test.0", 0), undefined);

    enumerateAndCheck(cps.getPrefs(uri), 12, ["test.0", "test.1", "test.2", "test.4", "test.5"]);
    enumerateAndCheck(cps.getPrefs(stringURI), 12, ["test.0", "test.1", "test.2", "test.4", "test.5"]);
    enumerateAndCheck(cps.getPrefs(stringObjectURI), 12, ["test.0", "test.1", "test.2", "test.4", "test.5"]);

    do_check_eq(cps.setPref(stringURI, "test.3", 3), undefined);

    enumerateAndCheck(cps.getPrefs(uri), 15, ["test.0", "test.1", "test.2", "test.3", "test.4", "test.5"]);
    enumerateAndCheck(cps.getPrefs(stringURI), 15, ["test.0", "test.1", "test.2", "test.3", "test.4", "test.5"]);
    enumerateAndCheck(cps.getPrefs(stringObjectURI), 15, ["test.0", "test.1", "test.2", "test.3", "test.4", "test.5"]);
  }
}

function do_check_thrown (aCallback) {
  var exThrown = false;
  try {
    aCallback();
    do_throw("NS_ERROR_ILLEGAL_VALUE should have been thrown here");
  } catch (e) {
    do_check_eq(e.result, Cr.NS_ERROR_ILLEGAL_VALUE);
    exThrown = true;
  }
  do_check_true(exThrown);
}

function enumerateAndCheck(prefs, expectedSum, expectedNames) {
  var enumerator = prefs.enumerator;
  var sum = 0;
  while (enumerator.hasMoreElements()) {
    var property = enumerator.getNext().QueryInterface(Components.interfaces.nsIProperty);
    sum += parseInt(property.value);

    // remove the pref name from the list of expected names
    var index = expectedNames.indexOf(property.name);
    do_check_true(index >= 0);
    expectedNames.splice(index, 1);
  }
  do_check_eq(sum, expectedSum);
  // check all pref names have been removed from the array
  do_check_eq(expectedNames.length, 0);
}