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 ['device/geolocation/public/interfaces/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/bindings', | |
14 ]).then(mojo => { | |
15 let [geolocation, permission, permissionStatus, bindings] = | |
16 mojo.modules; | |
17 | |
18 class GeolocationServiceMock { | |
19 constructor(interfaceProvider) { | |
20 interfaceProvider.addInterfaceOverrideForTesting( | |
21 geolocation.GeolocationService.name, | |
22 handle => this.connectGeolocation_(handle)); | |
23 | |
24 interfaceProvider.addInterfaceOverrideForTesting( | |
25 permission.PermissionService.name, | |
26 handle => this.connectPermission_(handle)); | |
27 | |
28 this.interfaceProvider_ = interfaceProvider; | |
29 | |
30 /** | |
31 * The next geoposition to return in response to a queryNextPosition() | |
32 * call. | |
33 */ | |
34 this.geoposition_ = null; | |
35 | |
36 /** | |
37 * A pending request for permission awaiting a decision to be set via a | |
38 * setGeolocationPermission call. | |
39 * | |
40 * @type {?Function} | |
41 */ | |
42 this.pendingPermissionRequest_ = null; | |
43 | |
44 /** | |
45 * The status to respond to permission requests with. If set to ASK, then | |
46 * permission requests will block until setGeolocationPermission is called | |
47 * to allow or deny permission requests. | |
48 * | |
49 * @type {!permissionStatus.PermissionStatus} | |
50 */ | |
51 this.permissionStatus_ = permissionStatus.PermissionStatus.ASK; | |
52 this.rejectPermissionConnections_ = false; | |
53 this.rejectGeolocationConnections_ = false; | |
54 | |
55 this.geolocationBindingSet_ = new bindings.BindingSet( | |
56 geolocation.GeolocationService); | |
57 this.permissionBindingSet_ = new bindings.BindingSet( | |
58 permission.PermissionService); | |
59 } | |
60 | |
61 connectGeolocation_(handle) { | |
62 if (this.rejectGeolocationConnections_) { | |
63 mojo.core.close(handle); | |
64 return; | |
65 } | |
66 this.geolocationBindingSet_.addBinding(this, handle); | |
67 } | |
68 | |
69 connectPermission_(handle) { | |
70 if (this.rejectPermissionConnections_) { | |
71 mojo.core.close(handle); | |
72 return; | |
73 } | |
74 this.permissionBindingSet_.addBinding(this, handle); | |
75 } | |
76 | |
77 setHighAccuracy(highAccuracy) { | |
78 // FIXME: We need to add some tests regarding "high accuracy" mode. | |
79 // See https://bugs.webkit.org/show_bug.cgi?id=49438 | |
80 } | |
81 | |
82 /** | |
83 * A mock implementation of GeolocationService.queryNextPosition(). This | |
84 * returns the position set by a call to setGeolocationPosition() or | |
85 * setGeolocationPositionUnavailableError(). | |
86 */ | |
87 queryNextPosition() { | |
88 if (!this.geoposition_) { | |
89 this.setGeolocationPositionUnavailableError( | |
90 'Test error: position not set before call to queryNextPosition()'); | |
91 } | |
92 let geoposition = this.geoposition_; | |
93 this.geoposition_ = null; | |
94 return Promise.resolve({geoposition}); | |
95 } | |
96 | |
97 /** | |
98 * Sets the position to return to the next queryNextPosition() call. If any | |
99 * queryNextPosition() requests are outstanding, they will all receive the | |
100 * position set by this call. | |
101 */ | |
102 setGeolocationPosition(latitude, longitude, accuracy, altitude, | |
103 altitudeAccuracy, heading, speed) { | |
104 this.geoposition_ = new geolocation.Geoposition(); | |
105 this.geoposition_.latitude = latitude; | |
106 this.geoposition_.longitude = longitude; | |
107 this.geoposition_.accuracy = accuracy; | |
108 this.geoposition_.altitude = altitude; | |
109 this.geoposition_.altitude_accuracy = altitudeAccuracy; | |
110 this.geoposition_.heading = heading; | |
111 this.geoposition_.speed = speed; | |
112 this.geoposition_.timestamp = new Date().getTime() / 1000; | |
113 this.geoposition_.error_message = ''; | |
114 this.geoposition_.valid = true; | |
115 } | |
116 | |
117 /** | |
118 * Sets the error message to return to the next queryNextPosition() call. If | |
119 * any queryNextPosition() requests are outstanding, they will all receive | |
120 * the error set by this call. | |
121 */ | |
122 setGeolocationPositionUnavailableError(message) { | |
123 this.geoposition_ = new geolocation.Geoposition(); | |
124 this.geoposition_.valid = false; | |
125 this.geoposition_.error_message = message; | |
126 this.geoposition_.error_code = | |
127 geolocation.Geoposition.ErrorCode.POSITION_UNAVAILABLE; | |
128 } | |
129 | |
130 /** | |
131 * Reject any connection requests for the permission service. This will | |
132 * trigger a connection error in the client. | |
133 */ | |
134 rejectPermissionConnections() { | |
135 this.rejectPermissionConnections_ = true; | |
136 } | |
137 | |
138 /** | |
139 * Reject any connection requests for the geolocation service. This will | |
140 * trigger a connection error in the client. | |
141 */ | |
142 rejectGeolocationConnections() { | |
143 this.rejectGeolocationConnections_ = true; | |
144 } | |
145 | |
146 /** | |
147 * A mock implementation of PermissionService.requestPermission(). This | |
148 * returns the result set by a call to setGeolocationPermission(), waiting | |
149 * for a call if necessary. Any permission request that is not for | |
150 * geolocation is always denied. | |
151 */ | |
152 requestPermission(permissionDescriptor) { | |
153 if (permissionDescriptor.name != permission.PermissionName.GEOLOCATION) | |
154 return Promise.resolve(permissionStatus.PermissionStatus.DENIED); | |
155 | |
156 return new Promise(resolve => { | |
157 if (this.pendingPermissionRequest_) | |
158 this.pendingPermissionRequest_(permissionStatus.PermissionStatus.ASK); | |
159 this.pendingPermissionRequest_ = resolve; | |
160 this.runPermissionCallback_(); | |
161 }); | |
162 } | |
163 | |
164 runPermissionCallback_() { | |
165 if (this.permissionStatus_ == permissionStatus.PermissionStatus.ASK || | |
166 !this.pendingPermissionRequest_) | |
167 return; | |
168 | |
169 this.pendingPermissionRequest_({status: this.permissionStatus_}); | |
170 this.permissionStatus_ = permissionStatus.PermissionStatus.ASK ; | |
171 this.pendingPermissionRequest_ = null; | |
172 } | |
173 | |
174 /** | |
175 * Sets whether the next geolocation permission request should be allowed. | |
176 */ | |
177 setGeolocationPermission(allowed) { | |
178 this.permissionStatus_ = allowed ? | |
179 permissionStatus.PermissionStatus.GRANTED : | |
180 permissionStatus.PermissionStatus.DENIED; | |
181 this.runPermissionCallback_(); | |
182 } | |
183 | |
184 } | |
185 return new GeolocationServiceMock(mojo.frameInterfaces); | |
186 }); | |
OLD | NEW |