Index: chrome/browser/resources/shared/js/cr/ui/repeating_button_test.html |
diff --git a/chrome/browser/resources/shared/js/cr/ui/repeating_button_test.html b/chrome/browser/resources/shared/js/cr/ui/repeating_button_test.html |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c6d7a45f2736fae1e228b170e0629eb22b7f880d |
--- /dev/null |
+++ b/chrome/browser/resources/shared/js/cr/ui/repeating_button_test.html |
@@ -0,0 +1,155 @@ |
+<!DOCTYPE html> |
+<html> |
+<head> |
+ <title>Repeater</title> |
+ <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.
|
+ <script src="../../cr.js"></script> |
+ <script src="../ui.js"></script> |
+ <script src="repeating_button.js"></script> |
+ <script> |
+ goog.require('goog.testing.jsunit'); |
+ goog.require('goog.testing.MockClock'); |
+ </script> |
+</head> |
+<body> |
+ <script> |
+ var mockClock; |
+ var value; |
+ var button; |
+ var repeatDelay; |
+ var repeatInterval; |
+ |
+ /** |
+ * Prepare running the tests. |
+ */ |
+ function setUp() { |
+ mockClock = new goog.testing.MockClock(); |
+ mockClock.install(); |
+ button = new cr.ui.RepeatingButton(); |
+ repeatDelay = button.repeatDelay; |
+ repeatInterval = button.repeatInterval; |
+ button.addEventListener( |
+ cr.ui.RepeatingButton.Event.BUTTON_HELD, |
+ function(e) { |
+ value++; |
+ }); |
+ } |
+ |
+ /** |
+ * Post-test cleanup. |
+ */ |
+ function tearDown() { |
+ mockClock.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); |
+ value = 0; |
+ for (var i = 0; i < events.length; i++) { |
+ mockEvent(events[i]); |
+ mockClock.tick(timeIncrements[i]); |
+ } |
+ assertEquals(expectedValue, value); |
+ mockClock.tick(repeatDelay); |
+ assertEquals(expectedValue, value); |
+ } |
+ |
+ /** |
+ * 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 {Object} opt_arg Optional additional argument for the test. |
+ */ |
+ function runButtonTests(fn, opt_arg) { |
+ var holdTime = repeatDelay - repeatInterval / 2; |
+ for (var i = 0; i < 3; i++, holdTime += repeatInterval) { |
+ var args = opt_arg ? [holdTime, opt_arg, i + 1] : [holdTime, i + 1]; |
+ 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); |
+ } |
+ </script> |
+</body> |
+</html> |