Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / resources / chromeos / login / screen_context_test.html
blobbc77e4bb158b0086d25058d9c0f069ae4401db7f
1 <!doctype html>
2 <title></title>
3 <script src="https://cdn.rawgit.com/google/closure-library/master/closure/goog/base.js"></script>
4 <script src="../../../../../ui/webui/resources/js/cr.js"></script>
5 <script src="screen_context.js"></script>
6 <script>
8 goog.require('goog.testing.jsunit');
10 </script>
11 <body>
12 <script>
14 function callSuccessful(f) {
15 try {
16 f();
17 } catch (e) {
18 return false;
20 return true;
23 function checkThrows(f) {
24 assertFalse(callSuccessful(f));
27 function checkNotThrows(f) {
28 assertTrue(callSuccessful(f));
31 function testSimple() {
32 var context = new login.ScreenContext;
33 assertFalse(context.hasChanges());
35 assertFalse(context.hasKey('key0'));
37 var rv = context.set('key0', true);
38 assertTrue(rv);
39 assertTrue(context.hasKey('key0'));
40 assertTrue(context.get('key0'));
41 assertTrue(context.get('key0', false));
42 assertTrue(context.hasChanges());
44 rv = context.set('key0', true);
45 assertFalse(rv);
47 rv = context.set('key0', false);
48 assertTrue(rv);
49 assertTrue(context.hasKey('key0'));
50 assertFalse(context.get('key0'));
51 assertFalse(context.get('key0', true));
52 assertTrue(context.hasChanges());
54 assertFalse(context.hasKey('key1'));
56 assertEquals(1, context.get('key1', 1));
57 rv = context.set('key1', 2);
58 assertTrue(rv);
59 assertTrue(context.hasKey('key1'));
60 assertEquals(2, context.get('key1'));
61 assertEquals(2, context.get('key1', 1));
64 function testChanges() {
65 var context = new login.ScreenContext;
66 assertFalse(context.hasChanges());
68 var rv = context.set('key0', 2);
69 assertTrue(rv);
70 assertEquals(2, context.get('key0'));
71 assertTrue(context.hasChanges());
73 var changes = context.getChangesAndReset();
74 assertFalse(context.hasChanges());
76 assertEquals(1, Object.keys(changes).length);
78 assertEquals(2, changes['key0']);
80 rv = context.set('key0', 3);
81 assertTrue(rv);
82 assertEquals(3, context.get('key0', 3));
83 assertTrue(context.hasChanges());
85 rv = context.set('key0', 2);
86 assertTrue(rv);
87 assertTrue(context.hasChanges());
90 function testComplexChanges() {
91 var context = new login.ScreenContext;
92 assertFalse(context.hasChanges());
94 context.set('key0', 'value0');
95 context.set('key1', true);
96 context.set('key2', 3.14159);
97 assertTrue(context.hasChanges());
99 // Get all changes and verify them.
100 var changes = context.getChangesAndReset();
101 assertFalse(context.hasChanges());
102 assertEquals(3, Object.keys(changes).length);
104 assertEquals('value0', changes['key0']);
105 assertEquals(true, changes['key1']);
106 assertEquals(3.14159, changes['key2']);
108 context.set('key0', 'value1');
109 assertTrue(context.hasChanges());
111 context.set('key0', 'value0');
112 assertTrue(context.hasChanges());
114 changes = context.getChangesAndReset();
115 assertFalse(context.hasChanges());
116 assertEquals(1, Object.keys(changes).length);
117 assertEquals('value0', changes['key0']);
120 function testApplyChanges() {
121 var context = new login.ScreenContext;
122 assertFalse(context.hasChanges());
124 var changes = {
125 key0: 'value0',
126 key1: 1,
127 key2: true
130 var keys = context.applyChanges(changes);
132 assertEquals(3, keys.length);
133 keys.sort();
134 assertEquals('key0', keys[0]);
135 assertEquals('key1', keys[1]);
136 assertEquals('key2', keys[2]);
138 assertFalse(context.hasChanges());
139 assertEquals('value0', context.get('key0'));
140 assertEquals(1, context.get('key1'));
141 assertTrue(context.get('key2'));
144 function testErrorHandling() {
145 function createContext() {
146 return new login.ScreenContext;
149 /* Invalid keys. */
150 checkThrows(function() {
151 createContext().set(true, 10);
153 checkThrows(function() {
154 createContext().set(new Object, 10);
156 checkThrows(function() {
157 createContext().set(10, 10);
159 checkNotThrows(function() {
160 createContext().set('key0', 10);
162 checkThrows(function() {
163 var c = createContext();
164 c.set('10', 10);
165 c.get(10);
167 checkNotThrows(function() {
168 var c = createContext();
169 c.set('10', 10);
170 c.get('10');
172 checkThrows(function() {
173 createContext().hasKey(20);
176 /* Missing key. */
177 checkThrows(function() {
178 createContext().get('key');
180 checkNotThrows(function() {
181 createContext().get('key', 10);
184 /* Invalid values. */
185 checkThrows(function() {
186 createContext().set('key0', new Object());
188 checkThrows(function() {
189 createContext().set('key0', new Boolean(true));
192 /* Applying diff to context with changes. */
193 checkThrows(function() {
194 var c = createContext();
195 c.set('key', 'value');
196 c.applyChanges({'key1': 'value1'});
198 checkNotThrows(function() {
199 var c = createContext();
200 c.set('key', 'value');
201 c.getChangesAndReset();
202 c.applyChanges({'key1': 'value1'});
206 </script>
207 </body>