forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write instrumentation tests for Webview JS Sandbox
These tests are similar to UI tests such that they compile and install a local version of WebView and set it as provider before beginning the tests. The test uses actual IPC calls to the webview provider package and is a more complete integration test as compared to webview_instrumentation_test_apk test. Bug: 1309447 Change-Id: I0b6fb009e48b07ca6615052ee20fa69f9971e1d0 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3550196 Reviewed-by: Richard Coles <torne@chromium.org> Reviewed-by: John Abd-El-Malek <jam@chromium.org> Commit-Queue: Abhijith Nair <abhijithnair@chromium.org> Cr-Commit-Position: refs/heads/main@{#986120}
- Loading branch information
Abhijith Nair
authored and
Chromium LUCI CQ
committed
Mar 28, 2022
1 parent
04c0dad
commit dd793b1
Showing
7 changed files
with
273 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright 2022 The Chromium Authors.All rights reserved. | ||
# Use of this source code is governed by a BSD - style license that can be | ||
# found in the LICENSE file. | ||
|
||
import("//build/config/android/rules.gni") | ||
import("//testing/test.gni") | ||
|
||
group("webview_js_sandbox_test_app") { | ||
testonly = true | ||
deps = [ | ||
":webview_js_sandbox_test_app_apk", | ||
":webview_js_sandbox_test_app_test_apk", | ||
] | ||
} | ||
|
||
android_apk("webview_js_sandbox_test_app_apk") { | ||
apk_name = "WebViewJsSandboxTestApp" | ||
android_manifest = "java/AndroidManifest.xml" | ||
deps = [] | ||
} | ||
|
||
instrumentation_test_apk("webview_js_sandbox_test_app_test_apk") { | ||
apk_name = "WebViewJsSandboxTestAppTest" | ||
apk_under_test = ":webview_js_sandbox_test_app_apk" | ||
android_manifest = "javatests/AndroidManifest.xml" | ||
sources = [ "javatests/src/org/chromium/webview_js_sandbox_test/test/WebViewJsSandboxTest.java" ] | ||
deps = [ | ||
"//android_webview:js_sandbox_java", | ||
"//base:base_java_test_support", | ||
"//third_party/androidx:androidx_test_runner_java", | ||
"//third_party/junit:junit", | ||
] | ||
use_webview_provider = system_webview_apk_target | ||
} |
18 changes: 18 additions & 0 deletions
18
android_webview/tools/js_sandbox_tests/java/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!-- | ||
* Copyright 2022 The Chromium Authors. All rights reserved. | ||
* Use of this source code is governed by a BSD-style license that can be | ||
* found in the LICENSE file. | ||
--> | ||
|
||
<manifest | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="org.chromium.webview_js_sandbox_test" | ||
android:versionCode="1" | ||
android:versionName="1.0" > | ||
|
||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
|
||
<application> | ||
</application> | ||
</manifest> |
23 changes: 23 additions & 0 deletions
23
android_webview/tools/js_sandbox_tests/javatests/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!-- | ||
* Copyright 2022 The Chromium Authors. All rights reserved. | ||
* Use of this source code is governed by a BSD-style license that can be | ||
* found in the LICENSE file. | ||
--> | ||
|
||
<manifest | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="org.chromium.webview_js_sandbox_test.test"> | ||
|
||
<uses-permission android:name="android.permission.RUN_INSTRUMENTATION" /> | ||
|
||
<!-- We add an application tag here just so that we can indicate that this | ||
package needs to link against the android.test library, which is | ||
needed when building test cases. --> | ||
<application> | ||
<uses-library android:name="android.test.runner" /> | ||
</application> | ||
|
||
<instrumentation android:name="org.chromium.base.test.BaseChromiumAndroidJUnitRunner" | ||
android:targetPackage="org.chromium.webview_js_sandbox_test" | ||
android:label="Tests for org.chromium.webview_js_sandbox_test"/> | ||
</manifest> |
163 changes: 163 additions & 0 deletions
163
...x_tests/javatests/src/org/chromium/webview_js_sandbox_test/test/WebViewJsSandboxTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.webview_js_sandbox_test.test; | ||
|
||
import androidx.test.filters.MediumTest; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import org.chromium.android_webview.js.browser.AwJsContext; | ||
import org.chromium.android_webview.js.browser.AwJsSandbox; | ||
import org.chromium.base.test.BaseJUnit4ClassRunner; | ||
import org.chromium.base.test.util.CallbackHelper; | ||
|
||
/** Instrumentation test for JsSandboxService. */ | ||
@RunWith(BaseJUnit4ClassRunner.class) | ||
public class WebViewJsSandboxTest { | ||
private class TestExecutionCallback implements AwJsContext.ExecutionCallback { | ||
public CallbackHelper helper = new CallbackHelper(); | ||
public String result; | ||
public String error; | ||
|
||
@Override | ||
public void reportResult(String result) { | ||
this.result = result; | ||
helper.notifyCalled(); | ||
} | ||
|
||
@Override | ||
public void reportError(String error) { | ||
this.error = error; | ||
helper.notifyCalled(); | ||
} | ||
} | ||
|
||
@Test | ||
@MediumTest | ||
public void testSimpleJsEvaluation() throws Throwable { | ||
final String code = "'PASS'"; | ||
final String expected = "PASS"; | ||
TestExecutionCallback callback = new TestExecutionCallback(); | ||
|
||
AwJsSandbox.newConnectedInstance((AwJsSandbox jsSandbox) -> { | ||
AwJsContext jsContext = jsSandbox.createContext(); | ||
jsContext.evaluateJavascript(code, callback); | ||
}); | ||
|
||
callback.helper.waitForCallback("Timed out waiting for reportResult() to be called", 0); | ||
Assert.assertEquals(expected, callback.result); | ||
} | ||
|
||
@Test | ||
@MediumTest | ||
public void testClosingOneContext() throws Throwable { | ||
final String code = "'PASS'"; | ||
final String expected = "PASS"; | ||
TestExecutionCallback callback = new TestExecutionCallback(); | ||
|
||
AwJsSandbox.newConnectedInstance((AwJsSandbox jsSandbox) -> { | ||
AwJsContext jsContext1 = jsSandbox.createContext(); | ||
AwJsContext jsContext2 = jsSandbox.createContext(); | ||
jsContext1.close(); | ||
jsContext2.evaluateJavascript(code, callback); | ||
jsContext2.close(); | ||
}); | ||
|
||
callback.helper.waitForCallback("Timed out waiting for reportResult() to be called", 0); | ||
Assert.assertEquals(expected, callback.result); | ||
} | ||
|
||
@Test | ||
@MediumTest | ||
public void testEvaluationInTwoContexts() throws Throwable { | ||
final String code1 = "this.x = 'PASS';\n"; | ||
final String expected1 = "PASS"; | ||
final String code2 = "this.x = 'SUPER_PASS';\n"; | ||
final String expected2 = "SUPER_PASS"; | ||
TestExecutionCallback callback1 = new TestExecutionCallback(); | ||
TestExecutionCallback callback2 = new TestExecutionCallback(); | ||
|
||
AwJsSandbox.newConnectedInstance((AwJsSandbox jsSandbox) -> { | ||
AwJsContext jsContext1 = jsSandbox.createContext(); | ||
jsContext1.evaluateJavascript(code1, callback1); | ||
AwJsContext jsContext2 = jsSandbox.createContext(); | ||
jsContext2.evaluateJavascript(code2, callback2); | ||
}); | ||
callback1.helper.waitForCallback( | ||
"Timed out waiting for reportResult() to be called for first case", 0); | ||
callback2.helper.waitForCallback( | ||
"Timed out waiting for reportResult() to be called for second case", 0); | ||
|
||
Assert.assertEquals(expected1, callback1.result); | ||
Assert.assertEquals(expected2, callback2.result); | ||
} | ||
|
||
@Test | ||
@MediumTest | ||
public void testTwoContextsDoNotShareEnvironment() throws Throwable { | ||
final String code1 = "this.y = 'PASS';\n"; | ||
final String expected1 = "PASS"; | ||
final String code2 = "this.y = this.y + ' PASS';\n"; | ||
final String expected2 = "undefined PASS"; | ||
TestExecutionCallback callback1 = new TestExecutionCallback(); | ||
TestExecutionCallback callback2 = new TestExecutionCallback(); | ||
|
||
AwJsSandbox.newConnectedInstance((AwJsSandbox jsSandbox) -> { | ||
AwJsContext jsContext1 = jsSandbox.createContext(); | ||
jsContext1.evaluateJavascript(code1, callback1); | ||
AwJsContext jsContext2 = jsSandbox.createContext(); | ||
jsContext2.evaluateJavascript(code2, callback2); | ||
}); | ||
callback1.helper.waitForCallback( | ||
"Timed out waiting for reportResult() to be called for first case", 0); | ||
callback2.helper.waitForCallback( | ||
"Timed out waiting for reportResult() to be called for second case", 0); | ||
|
||
Assert.assertEquals(expected1, callback1.result); | ||
Assert.assertEquals(expected2, callback2.result); | ||
} | ||
|
||
@Test | ||
@MediumTest | ||
public void testTwoExecutionsShareEnvironment() throws Throwable { | ||
final String code1 = "this.z = 'PASS';\n"; | ||
final String expected1 = "PASS"; | ||
final String code2 = "this.z = this.z + ' PASS';\n"; | ||
final String expected2 = "PASS PASS"; | ||
TestExecutionCallback callback1 = new TestExecutionCallback(); | ||
TestExecutionCallback callback2 = new TestExecutionCallback(); | ||
|
||
AwJsSandbox.newConnectedInstance((AwJsSandbox jsSandbox) -> { | ||
AwJsContext jsContext1 = jsSandbox.createContext(); | ||
jsContext1.evaluateJavascript(code1, callback1); | ||
jsContext1.evaluateJavascript(code2, callback2); | ||
}); | ||
callback1.helper.waitForCallback( | ||
"Timed out waiting for reportResult() to be called for first case", 0); | ||
callback2.helper.waitForCallback( | ||
"Timed out waiting for reportResult() to be called for second case", 0); | ||
|
||
Assert.assertEquals(expected1, callback1.result); | ||
Assert.assertEquals(expected2, callback2.result); | ||
} | ||
|
||
@Test | ||
@MediumTest | ||
public void testJsEvaluationError() throws Throwable { | ||
final String code = "."; | ||
final String contains = "SyntaxError"; | ||
TestExecutionCallback callback = new TestExecutionCallback(); | ||
|
||
AwJsSandbox.newConnectedInstance(jsSandbox -> { | ||
AwJsContext jsContext = jsSandbox.createContext(); | ||
jsContext.evaluateJavascript(code, callback); | ||
}); | ||
|
||
callback.helper.waitForCallback("Timed out waiting for reportError() to be called", 0); | ||
Assert.assertTrue(callback.error.contains(contains)); | ||
} | ||
} |