Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * geolocation-mock contains a mock implementation of GeolocationService and | |
| 3 * PermissionService. | |
| 4 */ | |
| 5 | |
| 6 "use strict"; | |
| 7 | |
| 8 let geolocationServiceMock = loadMojoModules( | |
| 9 'geolocationServiceMock', | |
| 10 ['third_party/WebKit/public/platform/modules/geolocation/geolocation.mojom', | |
| 11 'third_party/WebKit/public/platform/modules/permissions/permission.mojom', | |
| 12 'third_party/WebKit/public/platform/modules/permissions/permission_status.m ojom', | |
| 13 'mojo/public/js/router', | |
| 14 ]).then(mojo => { | |
| 15 let [geolocation, permission, permissionStatus, router] = | |
| 16 mojo.modules; | |
| 17 | |
| 18 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
| |
| 19 constructor(serviceRegistry) { | |
| 20 serviceRegistry.addServiceOverrideForTesting( | |
| 21 geolocation.GeolocationService.name, | |
| 22 handle => this.connectGeolocation(handle)); | |
| 23 | |
| 24 serviceRegistry.addServiceOverrideForTesting( | |
| 25 permission.PermissionService.name, | |
| 26 handle => this.connectPermission(handle)); | |
| 27 | |
| 28 this.serviceRegistry_ = serviceRegistry; | |
| 29 this.pendingRequests_ = []; | |
| 30 this.geoposition_ = null; | |
| 31 this.pendingPermissionRequests_ = []; | |
| 32 this.allowed_ = null; | |
| 33 this.rejectPermissionConnections_ = false; | |
| 34 this.rejectGeolocationConnections_ = false; | |
| 35 } | |
| 36 | |
| 37 connectGeolocation(handle) { | |
| 38 if (this.rejectGeolocationConnections_) { | |
| 39 mojo.core.close(handle); | |
| 40 return; | |
| 41 } | |
| 42 this.geolocationStub_ = new geolocation.GeolocationService.stubClass( | |
| 43 this); | |
| 44 this.geolocationRouter_ = new router.Router(handle); | |
| 45 this.geolocationRouter_.setIncomingReceiver(this.geolocationStub_); | |
| 46 } | |
| 47 | |
| 48 connectPermission(handle) { | |
| 49 if (this.rejectPermissionConnections_) { | |
| 50 mojo.core.close(handle); | |
| 51 return; | |
| 52 } | |
| 53 this.permissionStub_ = new permission.PermissionService.stubClass(this); | |
| 54 this.permissionRouter_ = new router.Router(handle); | |
| 55 this.permissionRouter_.setIncomingReceiver(this.permissionStub_); | |
| 56 } | |
| 57 | |
| 58 setHighAccuracy(highAccuracy) { | |
| 59 } | |
| 60 | |
| 61 queryNextPosition() { | |
| 62 let result = new Promise( | |
| 63 (resolve, reject) => this.pendingRequests_.push(resolve)); | |
| 64 this.runPositionCallbacks_(); | |
| 65 return result; | |
| 66 } | |
| 67 | |
| 68 setGeolocationPosition(latitude, longitude, accuracy, altitude, | |
| 69 altitudeAccuracy, heading, speed) { | |
| 70 this.geoposition_ = new geolocation.Geoposition(); | |
| 71 this.geoposition_.latitude = latitude; | |
| 72 this.geoposition_.longitude = longitude; | |
| 73 this.geoposition_.accuracy = accuracy; | |
| 74 this.geoposition_.altitude = altitude; | |
| 75 this.geoposition_.altitude_accuracy = altitudeAccuracy; | |
| 76 this.geoposition_.heading = heading; | |
| 77 this.geoposition_.speed = speed; | |
| 78 this.geoposition_.timestamp = new Date().getTime() / 1000; | |
| 79 this.geoposition_.error_message = ''; | |
| 80 this.geoposition_.valid = true; | |
| 81 this.runPositionCallbacks_(); | |
| 82 } | |
| 83 | |
| 84 setGeolocationPositionUnavailableError(message) { | |
| 85 this.geoposition_ = new geolocation.Geoposition(); | |
| 86 this.geoposition_.valid = false; | |
| 87 this.geoposition_.error_message = message; | |
| 88 this.geoposition_.error_code = | |
| 89 geolocation.Geoposition.ErrorCode.POSITION_UNAVAILABLE; | |
| 90 this.runPositionCallbacks_(); | |
| 91 } | |
| 92 | |
| 93 rejectPermissionConnections() { | |
| 94 this.rejectPermissionConnections_ = true; | |
| 95 } | |
| 96 | |
| 97 rejectGeolocationConnections() { | |
| 98 this.rejectGeolocationConnections_ = true; | |
| 99 } | |
| 100 | |
| 101 requestPermission(permission) { | |
| 102 let result = new Promise( | |
| 103 (resolve, reject) => this.pendingPermissionRequests_.push(resolve)); | |
| 104 this.runPermissionCallbacks_(); | |
| 105 return result; | |
| 106 } | |
| 107 | |
| 108 runPositionCallbacks_() { | |
| 109 if (!this.geoposition_ || !this.pendingRequests_.length) | |
| 110 return; | |
| 111 | |
| 112 while (this.pendingRequests_.length) { | |
| 113 this.pendingRequests_.pop()({geoposition: this.geoposition_}); | |
| 114 } | |
| 115 this.geoposition_ = null; | |
| 116 } | |
| 117 | |
| 118 runPermissionCallbacks_() { | |
| 119 if (this.allowed_ === null || !this.pendingPermissionRequests_.length) | |
| 120 return; | |
| 121 | |
| 122 while (this.pendingPermissionRequests_.length) { | |
| 123 this.pendingPermissionRequests_.pop()( | |
| 124 {status: this.allowed_ ? | |
| 125 permissionStatus.PermissionStatus.ALLOWED: | |
| 126 permissionStatus.PermissionStatus.DENIED}); | |
| 127 } | |
| 128 this.allowed_ = null; | |
| 129 } | |
| 130 | |
| 131 setGeolocationPermission(allowed) { | |
| 132 this.allowed_ = allowed; | |
| 133 this.runPermissionCallbacks_(); | |
| 134 } | |
| 135 | |
| 136 } | |
| 137 return new GeolocationServiceMock(mojo.frameServiceRegistry); | |
| 138 }); | |
| OLD | NEW |