OLD | NEW |
---|---|
(Empty) | |
1 <!DOCTYPE html> | |
2 <html> | |
3 <head> | |
4 <title>Repeater</title> | |
5 <script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base .js"></script> | |
James Hawkins
2011/11/02 05:39:12
nit: 80 cols.
James Hawkins
2011/11/02 05:39:12
Does this work if the test machine is offline?
kevers
2011/11/02 15:38:34
Pushed closing tag to the next line, but address i
kevers
2011/11/02 15:38:34
Appears that the test machine needs to be online.
| |
6 <script src="../../cr.js"></script> | |
7 <script src="../ui.js"></script> | |
8 <script src="repeating_button.js"></script> | |
9 <script> | |
10 goog.require('goog.testing.jsunit'); | |
11 goog.require('goog.testing.MockClock'); | |
12 </script> | |
13 </head> | |
14 <body> | |
15 <script> | |
16 var mockClock; | |
17 var value; | |
18 var button; | |
19 var repeatDelay; | |
20 var repeatInterval; | |
21 | |
22 /** | |
23 * Prepare running the tests. | |
24 */ | |
25 function setUp() { | |
26 mockClock = new goog.testing.MockClock(); | |
27 mockClock.install(); | |
28 button = new cr.ui.RepeatingButton(); | |
29 repeatDelay = button.repeatDelay; | |
30 repeatInterval = button.repeatInterval; | |
31 button.addEventListener( | |
32 cr.ui.RepeatingButton.Event.BUTTON_HELD, | |
33 function(e) { | |
34 value++; | |
35 }); | |
36 } | |
37 | |
38 /** | |
39 * Post-test cleanup. | |
40 */ | |
41 function tearDown() { | |
42 mockClock.uninstall(); | |
43 } | |
44 | |
45 /** | |
46 * Simulates a mouse or touch event to the repeating button. | |
47 * @param {string} type The type of event. | |
48 */ | |
49 function mockEvent(type) { | |
50 cr.dispatchSimpleEvent(button, type); | |
51 } | |
52 | |
53 /** | |
54 * Simulates a sequence of events. | |
55 * @param {Array.<string>} events List of event types. | |
56 * @param {Array.<number>} timeIncrements List of time increments between | |
57 * events. | |
58 * @param {number} expectedValue Expected result. | |
59 */ | |
60 function mockEventSequence(events, timeIncrements, expectedValue) { | |
61 assertEquals(events.length, timeIncrements.length); | |
62 value = 0; | |
63 for (var i = 0; i < events.length; i++) { | |
64 mockEvent(events[i]); | |
65 mockClock.tick(timeIncrements[i]); | |
66 } | |
67 assertEquals(expectedValue, value); | |
68 mockClock.tick(repeatDelay); | |
69 assertEquals(expectedValue, value); | |
70 } | |
71 | |
72 /** | |
73 * Simulates a tap or touch and hold gesture. | |
74 * @param {number} time Duration of the hold. | |
75 * @param {number} expectedValue Expected result. | |
76 */ | |
77 function mockTouchHold(time, expectedValue) { | |
78 mockEventSequence(['touchstart', 'touchend'], [time, 0], expectedValue); | |
79 } | |
80 | |
81 /** | |
82 * Simulates a mouse click or mouse press and hold. | |
83 * @param {number} time Duration of the hold. | |
84 * @param {number} expectedValue Expected result. | |
85 */ | |
86 function mockMouseHold(time, expectedValue) { | |
87 mockEventSequence(['mousedown', 'mouseup', 'mouseclick'], | |
88 [time, 0, 0], | |
89 expectedValue); | |
90 } | |
91 | |
92 /** | |
93 * Simulates a mouse press and drag off of the button. | |
94 * @param {number} time1 Duration that the mouse button is pressed and the | |
95 * mouse is over the button. | |
96 * @param {number} time2 Duration that the mouse button is pressed but the | |
97 * mouse is outside the boundary of the button. | |
98 * @param {number} expectedValue Expected result. | |
99 */ | |
100 function mockMouseOut(time1, time2, expectedValue) { | |
101 mockEventSequence(['mousedown', 'mouseout', 'mouseup'], | |
102 [time1, time2, 0], | |
103 expectedValue); | |
104 } | |
105 | |
106 /** | |
107 * Runs a series of tests with increasing button hold time. | |
108 * @param {function} fn Testing function. | |
109 * @param {Object} opt_arg Optional additional argument for the test. | |
110 */ | |
111 function runButtonTests(fn, opt_arg) { | |
112 var holdTime = repeatDelay - repeatInterval / 2; | |
113 for (var i = 0; i < 3; i++, holdTime += repeatInterval) { | |
114 var args = opt_arg ? [holdTime, opt_arg, i + 1] : [holdTime, i + 1]; | |
115 fn.apply(this, args); | |
116 } | |
117 } | |
118 | |
119 /** | |
120 * Simulates a short tap on the button. | |
121 */ | |
122 function testTap() { | |
123 mockTouchHold(repeatDelay / 2, 1); | |
124 } | |
125 | |
126 /** | |
127 * Simulates a long press of the button. | |
128 */ | |
129 function testTouchHold() { | |
130 runButtonTests(mockTouchHold); | |
131 } | |
132 | |
133 /** | |
134 * Simulates a quick mouse click of the button. | |
135 */ | |
136 function testClick() { | |
137 mockMouseHold(repeatDelay / 2, 1); | |
138 } | |
139 | |
140 /** | |
141 * Simulates a mouse press and hold on the button. | |
142 */ | |
143 function testMousePressHold() { | |
144 runButtonTests(mockMouseHold); | |
145 } | |
146 | |
147 /** | |
148 * Simulates draging the mouse off of the button while pressed. | |
149 */ | |
150 function testMouseOut() { | |
151 runButtonTests(mockMouseOut, repeatDelay); | |
152 } | |
153 </script> | |
154 </body> | |
155 </html> | |
OLD | NEW |