Index: third_party/WebKit/LayoutTests/http/tests/resources/geolocation-mock.js |
diff --git a/third_party/WebKit/LayoutTests/http/tests/resources/geolocation-mock.js b/third_party/WebKit/LayoutTests/http/tests/resources/geolocation-mock.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..caab365fa8654502513d4cf8d5837abcdf462945 |
--- /dev/null |
+++ b/third_party/WebKit/LayoutTests/http/tests/resources/geolocation-mock.js |
@@ -0,0 +1,138 @@ |
+/* |
+ * geolocation-mock contains a mock implementation of GeolocationService and |
+ * PermissionService. |
+ */ |
+ |
+"use strict"; |
+ |
+let geolocationServiceMock = loadMojoModules( |
+ 'geolocationServiceMock', |
+ ['third_party/WebKit/public/platform/modules/geolocation/geolocation.mojom', |
+ 'third_party/WebKit/public/platform/modules/permissions/permission.mojom', |
+ 'third_party/WebKit/public/platform/modules/permissions/permission_status.mojom', |
+ 'mojo/public/js/router', |
+ ]).then(mojo => { |
+ let [geolocation, permission, permissionStatus, router] = |
+ mojo.modules; |
+ |
+ class GeolocationServiceMock { |
Michael van Ouwerkerk
2016/05/04 13:52:16
Duplicating this mock seems less than ideal. Could
Sam McNally
2016/05/05 11:50:23
I found the file of magical path hacks so we can s
|
+ constructor(serviceRegistry) { |
+ serviceRegistry.addServiceOverrideForTesting( |
+ geolocation.GeolocationService.name, |
+ handle => this.connectGeolocation(handle)); |
+ |
+ serviceRegistry.addServiceOverrideForTesting( |
+ permission.PermissionService.name, |
+ handle => this.connectPermission(handle)); |
+ |
+ this.serviceRegistry_ = serviceRegistry; |
+ this.pendingRequests_ = []; |
+ this.geoposition_ = null; |
+ this.pendingPermissionRequests_ = []; |
+ this.allowed_ = null; |
+ this.rejectPermissionConnections_ = false; |
+ this.rejectGeolocationConnections_ = false; |
+ } |
+ |
+ connectGeolocation(handle) { |
+ if (this.rejectGeolocationConnections_) { |
+ mojo.core.close(handle); |
+ return; |
+ } |
+ this.geolocationStub_ = new geolocation.GeolocationService.stubClass( |
+ this); |
+ this.geolocationRouter_ = new router.Router(handle); |
+ this.geolocationRouter_.setIncomingReceiver(this.geolocationStub_); |
+ } |
+ |
+ connectPermission(handle) { |
+ if (this.rejectPermissionConnections_) { |
+ mojo.core.close(handle); |
+ return; |
+ } |
+ this.permissionStub_ = new permission.PermissionService.stubClass(this); |
+ this.permissionRouter_ = new router.Router(handle); |
+ this.permissionRouter_.setIncomingReceiver(this.permissionStub_); |
+ } |
+ |
+ setHighAccuracy(highAccuracy) { |
+ } |
+ |
+ queryNextPosition() { |
+ let result = new Promise( |
+ (resolve, reject) => this.pendingRequests_.push(resolve)); |
+ this.runPositionCallbacks_(); |
+ return result; |
+ } |
+ |
+ setGeolocationPosition(latitude, longitude, accuracy, altitude, |
+ altitudeAccuracy, heading, speed) { |
+ this.geoposition_ = new geolocation.Geoposition(); |
+ this.geoposition_.latitude = latitude; |
+ this.geoposition_.longitude = longitude; |
+ this.geoposition_.accuracy = accuracy; |
+ this.geoposition_.altitude = altitude; |
+ this.geoposition_.altitude_accuracy = altitudeAccuracy; |
+ this.geoposition_.heading = heading; |
+ this.geoposition_.speed = speed; |
+ this.geoposition_.timestamp = new Date().getTime() / 1000; |
+ this.geoposition_.error_message = ''; |
+ this.geoposition_.valid = true; |
+ this.runPositionCallbacks_(); |
+ } |
+ |
+ setGeolocationPositionUnavailableError(message) { |
+ this.geoposition_ = new geolocation.Geoposition(); |
+ this.geoposition_.valid = false; |
+ this.geoposition_.error_message = message; |
+ this.geoposition_.error_code = |
+ geolocation.Geoposition.ErrorCode.POSITION_UNAVAILABLE; |
+ this.runPositionCallbacks_(); |
+ } |
+ |
+ rejectPermissionConnections() { |
+ this.rejectPermissionConnections_ = true; |
+ } |
+ |
+ rejectGeolocationConnections() { |
+ this.rejectGeolocationConnections_ = true; |
+ } |
+ |
+ requestPermission(permission) { |
+ let result = new Promise( |
+ (resolve, reject) => this.pendingPermissionRequests_.push(resolve)); |
+ this.runPermissionCallbacks_(); |
+ return result; |
+ } |
+ |
+ runPositionCallbacks_() { |
+ if (!this.geoposition_ || !this.pendingRequests_.length) |
+ return; |
+ |
+ while (this.pendingRequests_.length) { |
+ this.pendingRequests_.pop()({geoposition: this.geoposition_}); |
+ } |
+ this.geoposition_ = null; |
+ } |
+ |
+ runPermissionCallbacks_() { |
+ if (this.allowed_ === null || !this.pendingPermissionRequests_.length) |
+ return; |
+ |
+ while (this.pendingPermissionRequests_.length) { |
+ this.pendingPermissionRequests_.pop()( |
+ {status: this.allowed_ ? |
+ permissionStatus.PermissionStatus.ALLOWED: |
+ permissionStatus.PermissionStatus.DENIED}); |
+ } |
+ this.allowed_ = null; |
+ } |
+ |
+ setGeolocationPermission(allowed) { |
+ this.allowed_ = allowed; |
+ this.runPermissionCallbacks_(); |
+ } |
+ |
+ } |
+ return new GeolocationServiceMock(mojo.frameServiceRegistry); |
+}); |