Chromium Code Reviews| Index: third_party/WebKit/LayoutTests/vibration/resources/vibration-helpers.js |
| diff --git a/third_party/WebKit/LayoutTests/vibration/resources/vibration-helpers.js b/third_party/WebKit/LayoutTests/vibration/resources/vibration-helpers.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2882af90a95a55030b40d275e8142f6fb72240c7 |
| --- /dev/null |
| +++ b/third_party/WebKit/LayoutTests/vibration/resources/vibration-helpers.js |
| @@ -0,0 +1,65 @@ |
| +'use strict'; |
| + |
| +function vibration_mocks(mojo) { |
| + return define('VibrationManager mocks', [ |
| + 'mojo/public/js/bindings', |
| + 'device/vibration/vibration_manager.mojom', |
| + ], (bindings, vibrationManager) => { |
| + class MockVibrationManager { |
| + constructor() { |
| + this.bindingSet = new bindings.BindingSet(vibrationManager.VibrationManager); |
| + |
| + this.vibrate_milliseconds_ = -1; |
| + this.cancelled_ = false; |
| + } |
| + |
| + vibrate(milliseconds) { |
| + this.vibrate_milliseconds_ = milliseconds; |
| + window.postMessage(milliseconds, '*'); |
|
leonhsl(Using Gerrit)
2017/03/07 15:00:41
I'm not sure whether this is the best practice to
|
| + return Promise.resolve(); |
| + } |
| + |
| + cancel() { |
| + this.cancelled_ = true; |
| + } |
| + |
| + getDuration() { |
| + return this.vibrate_milliseconds_; |
| + } |
| + |
| + isCancelled() { |
| + return this.cancelled_; |
| + } |
| + |
| + reset() { |
| + this.vibrate_milliseconds_ = -1; |
| + this.cancelled_ = false; |
| + } |
| + } |
| + |
| + let mockVibrationManager = new MockVibrationManager; |
| + mojo.frameInterfaces.addInterfaceOverrideForTesting( |
| + vibrationManager.VibrationManager.name, |
| + handle => { |
| + mockVibrationManager.bindingSet.addBinding(mockVibrationManager, handle); |
| + }); |
| + |
| + return Promise.resolve({ |
| + // Interface instance bound to main frame. |
| + mockVibrationManager: mockVibrationManager, |
| + // Constructor for mock VibrationManager class. |
| + MockVibrationManager: MockVibrationManager, |
| + // Loaded mojom interface. |
| + VibrationManager: vibrationManager.VibrationManager, |
| + }); |
| + }); |
| +} |
| + |
| +function vibration_test(func, name, properties) { |
| + mojo_test(mojo => vibration_mocks(mojo).then(vibration => { |
| + let result = Promise.resolve(func(vibration)); |
| + let cleanUp = () => vibration.mockVibrationManager.reset(); |
| + result.then(cleanUp, cleanUp); |
| + return result; |
| + }), name, properties); |
| +} |