aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/tweak/registry_test.html
blob: edc172b56fd632e744e45ad4002b656ea625c64c (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>

<!--
Author: agrieve@google.com (Andrew Grieve)
-->

<html>
<!--
Copyright 2009 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Closure Unit Tests - goog.tweak.Registry</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.testing.jsunit');
    goog.require('goog.tweak.Registry');
    goog.require('goog.tweak.testhelpers');
  </script>
</head>
<body>
<script>

var registry;

function setUp() {
  createRegistryEntries('');
  registry = goog.tweak.getRegistry();
}

function tearDown() {
  goog.tweak.registry_ = null;
}

function testGetBaseEntry() {
  // Initial values
  assertEquals('wrong bool1 object', boolEntry,
      registry.getBooleanSetting('Bool'));
  assertEquals('wrong string object', strEntry,
      registry.getStringSetting('Str'));
  assertEquals('wrong numeric object', numEntry,
      registry.getNumericSetting('Num'));
  assertEquals('wrong button object', buttonEntry,
      registry.getEntry('Button'));
  assertEquals('wrong button object', boolGroup,
      registry.getEntry('BoolGroup'));
}

function testInitializeFromQueryParams() {
  var testCase = 0;
  function assertQuery(queryStr, boolValue, enumValue, strValue, subBoolValue,
      subBoolValue2) {
    createRegistryEntries(queryStr);
    assertEquals('Wrong bool value for query: ' + queryStr, boolValue,
        boolEntry.getValue());
    assertEquals('Wrong enum value for query: ' + queryStr, enumValue,
        strEnumEntry.getValue());
    assertEquals('Wrong str value for query: ' + queryStr, strValue,
        strEntry.getValue());
    assertEquals('Wrong BoolOne value for query: ' + queryStr, subBoolValue,
        boolOneEntry.getValue());
    assertEquals('Wrong BoolTwo value for query: ' + queryStr, subBoolValue2,
        boolTwoEntry.getValue());
  }
  assertQuery('?dummy=1&bool=&enum=&s=', false, '', '', false, true);
  assertQuery('?bool=0&enum=A&s=a', false, 'A', 'a', false, true);
  assertQuery('?bool=1&enum=A', true, 'A', '', false, true);
  assertQuery('?bool=true&enum=B&s=as+df', true, 'B', 'as df', false, true);
  assertQuery('?enum=C', false, 'C', '', false, true);
  assertQuery('?enum=C&boolgroup=-booltwo', false, 'C', '', false, false);
  assertQuery('?enum=C&boolgroup=b1,-booltwo', false, 'C', '', true, false);
  assertQuery('?enum=C&boolgroup=b1', false, 'C', '', true, true);
  assertQuery('?s=a+b%20c%26', false, 'A', 'a b c&', false, true);
}

function testMakeUrlQuery() {
  assertEquals('All values are default.',
      '', registry.makeUrlQuery(''));
  assertEquals('All values are default - with existing params.',
      '?super=pudu', registry.makeUrlQuery('?super=pudu'));

  boolEntry.setValue(true);
  numEnumEntry.setValue(2);
  strEntry.setValue('f o&o');
  assertEquals('Wrong query string 1.',
      '?bool=1&enum2=2&s=f+o%26o',
      registry.makeUrlQuery('?bool=1'));
  assertEquals('Wrong query string 1 - with existing params.',
      '?super=pudu&bool=1&enum2=2&s=f+o%26o',
      registry.makeUrlQuery('?bool=0&s=g&super=pudu'));

  boolOneEntry.setValue(true);
  assertEquals('Wrong query string 2.',
      '?bool=1&boolgroup=B1&enum2=2&s=f+o%26o',
      registry.makeUrlQuery(''));

  boolTwoEntry.setValue(false);
  assertEquals('Wrong query string 3.',
      '?bool=1&boolgroup=B1,-booltwo&enum2=2&s=f+o%26o',
      registry.makeUrlQuery(''));
}

function testOverrideDefaultValue_calledBefore() {
  registry.overrideDefaultValue('b', false);
  registry.overrideDefaultValue('b', true);
  goog.tweak.registerBoolean('b', 'b desc');
  var bEntry = registry.getEntry('b');
  assertTrue('Default value should be true.', bEntry.getDefaultValue());
  assertTrue('Value should be true.', bEntry.getValue());
}

function testOverrideDefaultValue_calledAfter() {
  var exception = assertThrows('Should assert.', function() {
    registry.overrideDefaultValue('Bool2', false);
  });
  assertTrue('Wrong exception',
      exception instanceof goog.asserts.AssertionError);
}

function testCompilerOverrideDefaultValue() {
  createRegistryEntries('', { 'b': true });
  registry = goog.tweak.getRegistry();
  goog.tweak.registerBoolean('b', 'b desc');
  var bEntry = registry.getEntry('b');
  assertTrue('Default value should be true.', bEntry.getDefaultValue());
  assertTrue('Value should be true.', bEntry.getValue());
}

function testCompilerAndJsOverrideDefaultValue() {
  createRegistryEntries('', { 'b': false });
  registry = goog.tweak.getRegistry();
  registry.overrideDefaultValue('b', true);
  goog.tweak.registerBoolean('b', 'b desc', true);
  var bEntry = registry.getEntry('b');
  assertFalse('Default value should be false.', bEntry.getDefaultValue());
  assertFalse('Value should be false.', bEntry.getValue());
}

</script>
</body>
</html>