| <!DOCTYPE html> |
| <html> |
| <head> |
| <title>Repeater</title> |
| <script src="mock_timer.js"></script> |
| </head> |
| <body> |
| <script> |
| |
| /** |
| * Controller for mocking behavior or timer-based callbacks. |
| * @type {!MockTimer} |
| */ |
| var mockTimer; |
| |
| /** |
| * Number of callbacks fired during simulated button press. |
| * @type {number} |
| */ |
| var buttonHeldCount; |
| |
| /** |
| * Repeating button. The button automatically refires is long-pressed. |
| * @type {!Element} |
| */ |
| var button; |
| |
| /** |
| * Inital delay before the first refire during a button press. |
| * @type {number} |
| */ |
| var repeatDelay; |
| |
| /** |
| * Interval between refires during a button press. |
| * @type {number} |
| */ |
| var repeatInterval; |
| |
| /** Prepare running the tests. */ |
| function setUp() { |
| mockTimer = new MockTimer(); |
| mockTimer.install(); |
| button = new cr.ui.RepeatingButton(); |
| repeatDelay = button.repeatDelay; |
| repeatInterval = button.repeatInterval; |
| button.addEventListener( |
| cr.ui.RepeatingButton.Event.BUTTON_HELD, |
| function(e) { |
| buttonHeldCount++; |
| }); |
| } |
| |
| /** |
| * Post-test cleanup. |
| */ |
| function tearDown() { |
| mockTimer.uninstall(); |
| } |
| |
| /** |
| * Simulates a mouse or touch event to the repeating button. |
| * @param {string} type The type of event. |
| */ |
| function mockEvent(type) { |
| cr.dispatchSimpleEvent(button, type); |
| } |
| |
| /** |
| * Simulates a sequence of events. |
| * @param {!Array<string>} events List of event types. |
| * @param {!Array<number>} timeIncrements List of time increments between |
| * events. |
| * @param {number} expectedValue Expected result. |
| */ |
| function mockEventSequence(events, timeIncrements, expectedValue) { |
| assertEquals(events.length, timeIncrements.length); |
| buttonHeldCount = 0; |
| for (var i = 0; i < events.length; i++) { |
| mockEvent(events[i]); |
| mockTimer.tick(timeIncrements[i]); |
| } |
| assertEquals(expectedValue, buttonHeldCount); |
| // Ensure that the button stops repeated firing at the end of the event |
| // sequence. |
| mockTimer.tick(repeatDelay); |
| assertEquals(expectedValue, buttonHeldCount); |
| } |
| |
| /** |
| * Simulates a tap or touch and hold gesture. |
| * @param {number} time Duration of the hold. |
| * @param {number} expectedValue Expected result. |
| */ |
| function mockTouchHold(time, expectedValue) { |
| mockEventSequence(['touchstart', 'touchend'], [time, 0], expectedValue); |
| } |
| |
| /** |
| * Simulates a mouse click or mouse press and hold. |
| * @param {number} time Duration of the hold. |
| * @param {number} expectedValue Expected result. |
| */ |
| function mockMouseHold(time, expectedValue) { |
| mockEventSequence(['mousedown', 'mouseup', 'mouseclick'], |
| [time, 0, 0], |
| expectedValue); |
| } |
| |
| /** |
| * Simulates a mouse press and drag off of the button. |
| * @param {number} time1 Duration that the mouse button is pressed and the |
| * mouse is over the button. |
| * @param {number} time2 Duration that the mouse button is pressed but the |
| * mouse is outside the boundary of the button. |
| * @param {number} expectedValue Expected result. |
| */ |
| function mockMouseOut(time1, time2, expectedValue) { |
| mockEventSequence(['mousedown', 'mouseout', 'mouseup'], |
| [time1, time2, 0], |
| expectedValue); |
| } |
| |
| /** |
| * Runs a series of tests with increasing button hold time. |
| * @param {!Function} fn Testing function. |
| * @param {number=} opt_secondaryDelay Optional additional delay used in |
| * tests that simulate a secondary delay after the initial button press |
| * and hold time. |
| */ |
| function runButtonTests(fn, opt_secondaryDelay) { |
| var holdTime = repeatDelay - repeatInterval / 2; |
| |
| for (var i = 0; i < 3; i++, holdTime += repeatInterval) { |
| // One or two time delays followed by the expected click count. |
| var args = [holdTime, i + 1]; |
| if (opt_secondaryDelay) |
| args.splice(1, 0, opt_secondaryDelay); |
| fn.apply(this, args); |
| } |
| } |
| |
| /** |
| * Simulates a short tap on the button. |
| */ |
| function testTap() { |
| mockTouchHold(repeatDelay / 2, 1); |
| } |
| |
| /** |
| * Simulates a long press of the button. |
| */ |
| function testTouchHold() { |
| runButtonTests(mockTouchHold); |
| } |
| |
| /** |
| * Simulates a quick mouse click of the button. |
| */ |
| function testClick() { |
| mockMouseHold(repeatDelay / 2, 1); |
| } |
| |
| /** |
| * Simulates a mouse press and hold on the button. |
| */ |
| function testMousePressHold() { |
| runButtonTests(mockMouseHold); |
| } |
| |
| /** |
| * Simulates draging the mouse off of the button while pressed. |
| */ |
| function testMouseOut() { |
| runButtonTests(mockMouseOut, repeatDelay); |
| } |
| |
| /** |
| * Repeat tests with new delay and interval times. |
| */ |
| function testUpdateDelayTimes() { |
| var oldDelay = repeatDelay; |
| var oldInterval = repeatInterval; |
| repeatDelay = button.repeatDelay = 2 * repeatDelay; |
| repeatInterval = button.repeatInterval = 2 * repeatInterval; |
| testTouchHold(); |
| testMousePressHold(); |
| testMouseOut(); |
| testClick(); |
| testTap(); |
| repeatDelay = button.repeatDelay = oldDelay; |
| repeatInterval = button.repeatInterval = oldInterval; |
| } |
| |
| /** |
| * Runs mouse and touch hold tests with a repeat interval that is longer |
| * than the initial repeat delay. |
| */ |
| function testLongRepeat() { |
| var oldInterval = repeatInterval; |
| repeatInterval = button.repeatInterval = 3 * button.repeatDelay; |
| testTouchHold(); |
| testMousePressHold(); |
| repeatInterval = button.repeatInterval = oldInterval; |
| } |
| </script> |
| </body> |
| </html> |