3 <script src=
"http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script>
4 <script src=
"../../../../../ui/webui/resources/js/cr.js"></script>
5 <script src=
"screen_context.js"></script>
8 goog
.require('goog.testing.jsunit');
14 function callSuccessful(f
) {
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);
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);
47 rv
= context
.set('key0', false);
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);
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);
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);
82 assertEquals(3, context
.get('key0', 3));
83 assertTrue(context
.hasChanges());
85 rv
= context
.set('key0', 2);
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());
130 var keys
= context
.applyChanges(changes
);
132 assertEquals(3, keys
.length
);
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
;
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();
167 checkNotThrows(function() {
168 var c
= createContext();
172 checkThrows(function() {
173 createContext().hasKey(20);
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'});