6
6
7
7
import android .os .Build ;
8
8
import android .test .suitebuilder .annotation .MediumTest ;
9
+ import android .test .suitebuilder .annotation .SmallTest ;
9
10
import android .webkit .GeolocationPermissions ;
10
11
11
12
import org .chromium .android_webview .AwContents ;
13
+ import org .chromium .base .annotations .SuppressFBWarnings ;
12
14
import org .chromium .base .test .util .Feature ;
13
15
import org .chromium .base .test .util .MinAndroidSdkLevel ;
14
16
import org .chromium .content .browser .LocationProviderFactory ;
@@ -37,52 +39,65 @@ public class GeolocationTest extends AwTestBase {
37
39
+ " function gotPos(position) {\n "
38
40
+ " positionCount++;\n "
39
41
+ " }\n "
42
+ + " function errorCallback(error){"
43
+ + " window.document.title = 'deny';"
44
+ + " console.log('navigator.getCurrentPosition error: ', error);"
45
+ + " }"
40
46
+ " function initiate_getCurrentPosition() {\n "
41
47
+ " navigator.geolocation.getCurrentPosition(\n "
42
- + " gotPos, function() { } , { });\n "
48
+ + " gotPos, errorCallback , { });\n "
43
49
+ " }\n "
44
50
+ " function initiate_watchPosition() {\n "
45
51
+ " navigator.geolocation.watchPosition(\n "
46
- + " gotPos, function() { } , { });\n "
52
+ + " gotPos, errorCallback , { });\n "
47
53
+ " }\n "
48
54
+ " </script>\n "
49
55
+ " </head>\n "
50
56
+ " <body>\n "
51
57
+ " </body>\n "
52
58
+ "</html>" ;
53
59
54
- @ Override
55
- public void setUp () throws Exception {
56
- super .setUp ();
57
- mContentsClient = new TestAwContentsClient () {
58
- @ Override
59
- public void onGeolocationPermissionsShowPrompt (String origin ,
60
- GeolocationPermissions .Callback callback ) {
61
- callback .invoke (origin , true , true );
62
- }
63
- };
64
- mAwContents = createAwTestContainerViewOnMainSync (mContentsClient ).getAwContents ();
65
- enableJavaScriptOnUiThread (mAwContents );
66
- setupGeolocation ();
60
+ private static class GrantPermisionAwContentClient extends TestAwContentsClient {
61
+ @ Override
62
+ public void onGeolocationPermissionsShowPrompt (String origin ,
63
+ GeolocationPermissions .Callback callback ) {
64
+ callback .invoke (origin , true , true );
65
+ }
67
66
}
68
67
69
- @ Override
70
- public void tearDown () throws Exception {
71
- mMockLocationProvider .stopUpdates ();
72
- super .tearDown ();
68
+ private static class DefaultPermisionAwContentClient extends TestAwContentsClient {
69
+ @ Override
70
+ public void onGeolocationPermissionsShowPrompt (String origin ,
71
+ GeolocationPermissions .Callback callback ) {
72
+ // This method is empty intentionally to simulate callback is not referenced.
73
+ }
73
74
}
74
75
75
- private void setupGeolocation () {
76
+ private void initAwContents (TestAwContentsClient contentsClient ) throws Exception {
77
+ mContentsClient = contentsClient ;
78
+ mAwContents = createAwTestContainerViewOnMainSync (mContentsClient ).getAwContents ();
79
+ enableJavaScriptOnUiThread (mAwContents );
76
80
getInstrumentation ().runOnMainSync (new Runnable () {
77
81
@ Override
78
82
public void run () {
79
83
mAwContents .getSettings ().setGeolocationEnabled (true );
80
84
}
81
85
});
86
+ }
87
+
88
+ @ Override
89
+ public void setUp () throws Exception {
90
+ super .setUp ();
82
91
mMockLocationProvider = new MockLocationProvider ();
83
92
LocationProviderFactory .setLocationProviderImpl (mMockLocationProvider );
84
93
}
85
94
95
+ @ Override
96
+ public void tearDown () throws Exception {
97
+ mMockLocationProvider .stopUpdates ();
98
+ super .tearDown ();
99
+ }
100
+
86
101
private int getPositionCountFromJS () {
87
102
int result = -1 ;
88
103
try {
@@ -110,6 +125,7 @@ public Boolean call() throws Exception {
110
125
@ MediumTest
111
126
@ Feature ({"AndroidWebView" })
112
127
public void testGetPosition () throws Throwable {
128
+ initAwContents (new GrantPermisionAwContentClient ());
113
129
loadDataSync (mAwContents , mContentsClient .getOnPageFinishedHelper (),
114
130
RAW_HTML , "text/html" , false );
115
131
@@ -137,6 +153,7 @@ public Boolean call() throws Exception {
137
153
@ MediumTest
138
154
@ Feature ({"AndroidWebView" })
139
155
public void testWatchPosition () throws Throwable {
156
+ initAwContents (new GrantPermisionAwContentClient ());
140
157
loadDataSync (mAwContents , mContentsClient .getOnPageFinishedHelper (),
141
158
RAW_HTML , "text/html" , false );
142
159
@@ -153,6 +170,7 @@ public Boolean call() throws Exception {
153
170
@ MediumTest
154
171
@ Feature ({"AndroidWebView" })
155
172
public void testPauseGeolocationOnPause () throws Throwable {
173
+ initAwContents (new GrantPermisionAwContentClient ());
156
174
// Start a watch going.
157
175
loadDataSync (mAwContents , mContentsClient .getOnPageFinishedHelper (),
158
176
RAW_HTML , "text/html" , false );
@@ -204,6 +222,7 @@ public Boolean call() throws Exception {
204
222
@ MediumTest
205
223
@ Feature ({"AndroidWebView" })
206
224
public void testPauseAwContentsBeforeNavigating () throws Throwable {
225
+ initAwContents (new GrantPermisionAwContentClient ());
207
226
getInstrumentation ().runOnMainSync (new Runnable () {
208
227
@ Override
209
228
public void run () {
@@ -241,6 +260,7 @@ public Boolean call() throws Exception {
241
260
@ MediumTest
242
261
@ Feature ({"AndroidWebView" })
243
262
public void testResumeWhenNotStarted () throws Throwable {
263
+ initAwContents (new GrantPermisionAwContentClient ());
244
264
getInstrumentation ().runOnMainSync (new Runnable () {
245
265
@ Override
246
266
public void run () {
@@ -261,4 +281,23 @@ public void run() {
261
281
ensureGeolocationRunning (false );
262
282
}
263
283
284
+ @ Feature ({"AndroidWebView" })
285
+ @ SmallTest
286
+ public void testDenyAccessByDefault () throws Throwable {
287
+ initAwContents (new DefaultPermisionAwContentClient ());
288
+ loadDataSync (mAwContents , mContentsClient .getOnPageFinishedHelper (),
289
+ RAW_HTML , "text/html" , false );
290
+
291
+ mAwContents .evaluateJavaScriptForTests ("initiate_getCurrentPosition();" , null );
292
+
293
+ poll (new Callable <Boolean >() {
294
+ @ SuppressFBWarnings ("DM_GC" )
295
+ @ Override
296
+ public Boolean call () throws Exception {
297
+ Runtime .getRuntime ().gc ();
298
+ return "deny" .equals (getTitleOnUiThread (mAwContents ));
299
+ }
300
+ });
301
+ }
302
+
264
303
}
0 commit comments