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