1*b7c941bbSAndroid Build Coastguard Worker# Shared Webkit Environment 2*b7c941bbSAndroid Build Coastguard Worker 3*b7c941bbSAndroid Build Coastguard Worker## Overview 4*b7c941bbSAndroid Build Coastguard Worker 5*b7c941bbSAndroid Build Coastguard WorkerThis helper lib makes a test suite extendable to run in both an activity based environment and 6*b7c941bbSAndroid Build Coastguard Workerwithin the SDK Runtime. 7*b7c941bbSAndroid Build Coastguard Worker 8*b7c941bbSAndroid Build Coastguard Worker[design](go/shared-sdk-sandbox-webview-tests) (*only visible to Googlers) 9*b7c941bbSAndroid Build Coastguard Worker 10*b7c941bbSAndroid Build Coastguard Worker## Expected prior knowledge 11*b7c941bbSAndroid Build Coastguard Worker 12*b7c941bbSAndroid Build Coastguard WorkerRead the test scenario documentation to get a better understanding of how we invoke tests inside 13*b7c941bbSAndroid Build Coastguard Workerthe SDK Runtime: 14*b7c941bbSAndroid Build Coastguard Worker`//packages/modules/AdServices/sdksandbox/tests/testutils/testscenario/README.md` 15*b7c941bbSAndroid Build Coastguard Worker 16*b7c941bbSAndroid Build Coastguard Worker## Tutorial 17*b7c941bbSAndroid Build Coastguard Worker 18*b7c941bbSAndroid Build Coastguard Worker*** aside 19*b7c941bbSAndroid Build Coastguard Worker**Tip:** If your test suite already extends SharedWebViewTest, you can skip to section 20*b7c941bbSAndroid Build Coastguard Worker3, "Converting a test to shared" 21*b7c941bbSAndroid Build Coastguard Worker*** 22*b7c941bbSAndroid Build Coastguard Worker 23*b7c941bbSAndroid Build Coastguard Worker### 1. Making a test suite sharable with the SDK Runtime 24*b7c941bbSAndroid Build Coastguard Worker 25*b7c941bbSAndroid Build Coastguard WorkerIf you want to share webkit tests inside the SDK runtime, you will need to make your 26*b7c941bbSAndroid Build Coastguard Workertest suite inherit from `android.webkit.cts.SharedWebViewTest`. This is used to indicate 27*b7c941bbSAndroid Build Coastguard Workerthat a test suite has a configurable environment. 28*b7c941bbSAndroid Build Coastguard Worker 29*b7c941bbSAndroid Build Coastguard WorkerEg: 30*b7c941bbSAndroid Build Coastguard Worker```java 31*b7c941bbSAndroid Build Coastguard Worker- public class WebViewTest 32*b7c941bbSAndroid Build Coastguard Worker+ public class WebViewTest extends SharedWebViewTest 33*b7c941bbSAndroid Build Coastguard Worker``` 34*b7c941bbSAndroid Build Coastguard Worker 35*b7c941bbSAndroid Build Coastguard Worker*** aside 36*b7c941bbSAndroid Build Coastguard Worker**Note:** Some WebView tests still use the JUnit 3 style, so you may need to 37*b7c941bbSAndroid Build Coastguard Workerfirst migrate the test suite from `ActivityInstrumentationTestCase2` to use 38*b7c941bbSAndroid Build Coastguard Worker`ActivityScenarioRule` (which is for JUnit 4 style). See 39*b7c941bbSAndroid Build Coastguard Worker[b/112773416](http://b/112773416) for details. 40*b7c941bbSAndroid Build Coastguard Worker*** 41*b7c941bbSAndroid Build Coastguard Worker 42*b7c941bbSAndroid Build Coastguard WorkerThis abstract class requires you to implement the method `createTestEnvironment` that 43*b7c941bbSAndroid Build Coastguard Workerdefines the test environment for your test suite. Think of the test environment as a 44*b7c941bbSAndroid Build Coastguard Workerconcrete description of where this test suite will execute. `createTestEnvironment` should 45*b7c941bbSAndroid Build Coastguard Workerhave all the references your test has to an activity. This will be overridden later on by the 46*b7c941bbSAndroid Build Coastguard WorkerSDK tests using the API method `setTestEnvironment`. 47*b7c941bbSAndroid Build Coastguard Worker 48*b7c941bbSAndroid Build Coastguard WorkerEg: 49*b7c941bbSAndroid Build Coastguard Worker```java 50*b7c941bbSAndroid Build Coastguard Worker@Override 51*b7c941bbSAndroid Build Coastguard Workerprotected SharedWebViewTestEnvironment createTestEnvironment() { 52*b7c941bbSAndroid Build Coastguard Worker return new SharedWebViewTestEnvironment.Builder() 53*b7c941bbSAndroid Build Coastguard Worker .setContext(mActivity) 54*b7c941bbSAndroid Build Coastguard Worker .setWebView(mWebView) 55*b7c941bbSAndroid Build Coastguard Worker // This allows SDK methods to invoke APIs from outside the SDK. 56*b7c941bbSAndroid Build Coastguard Worker // The Activity based tests don't need to worry about this so you can 57*b7c941bbSAndroid Build Coastguard Worker // just provide the host app invoker directly to this environment. 58*b7c941bbSAndroid Build Coastguard Worker .setHostAppInvoker(SharedWebViewTestEnvironment.createHostAppInvoker()) 59*b7c941bbSAndroid Build Coastguard Worker .build(); 60*b7c941bbSAndroid Build Coastguard Worker} 61*b7c941bbSAndroid Build Coastguard Worker``` 62*b7c941bbSAndroid Build Coastguard Worker 63*b7c941bbSAndroid Build Coastguard WorkerYour test suite is now sharable with an SDK runtime test suite! 64*b7c941bbSAndroid Build Coastguard Worker 65*b7c941bbSAndroid Build Coastguard Worker### 2. Sharing your tests with the SDK Runtime 66*b7c941bbSAndroid Build Coastguard Worker 67*b7c941bbSAndroid Build Coastguard WorkerThe SDK Runtime tests for webkit live under `//cts/tests/tests/sdksandbox/webkit`. 68*b7c941bbSAndroid Build Coastguard Worker 69*b7c941bbSAndroid Build Coastguard WorkerYou need a test SDK that will actually have your tests, and a JUnit test suite that JUnit will have to invoke your tests from an activity. 70*b7c941bbSAndroid Build Coastguard Worker 71*b7c941bbSAndroid Build Coastguard WorkerYou can follow the "Creating new SDK Runtime tests" section under the SDK testscenario 72*b7c941bbSAndroid Build Coastguard Workerguide (store these SDK tests in `//cts/tests/tests/sdksandbox/webkit`): 73*b7c941bbSAndroid Build Coastguard Worker`//packages/modules/AdServices/sdksandbox/tests/testutils/testscenario/README.md` 74*b7c941bbSAndroid Build Coastguard Worker 75*b7c941bbSAndroid Build Coastguard Worker*** aside 76*b7c941bbSAndroid Build Coastguard Worker**Note:** If you reuse the WebViewSandboxTestSdk below you will only need to follow the last step of the "Invoke from a JUnit test suite" section from the guide above. 77*b7c941bbSAndroid Build Coastguard Worker*** 78*b7c941bbSAndroid Build Coastguard Worker 79*b7c941bbSAndroid Build Coastguard WorkerHowever, instead of creating a new test SDK as per the guide above, you can reuse the WebViewSandboxTestSdk 80*b7c941bbSAndroid Build Coastguard Worker`//cts/tests/tests/sdksandbox/webkit/sdksidetests/WebViewSandboxTest/src/com/android/cts/sdksidetests/webviewsandboxtest/WebViewSandboxTestSdk.java` 81*b7c941bbSAndroid Build Coastguard WorkerTo do this use the `android.sdksandbox.webkit.cts.WebViewSandboxTestRule` and pass in the fully qualified name of your test class. 82*b7c941bbSAndroid Build Coastguard Worker 83*b7c941bbSAndroid Build Coastguard WorkerCongratulations! Your webkit tests are now shared with your SDK Runtime tests! 84*b7c941bbSAndroid Build Coastguard Worker 85*b7c941bbSAndroid Build Coastguard Worker### 3. Converting a test to shared 86*b7c941bbSAndroid Build Coastguard Worker 87*b7c941bbSAndroid Build Coastguard WorkerYou need to do two things when you are making a test shared: 88*b7c941bbSAndroid Build Coastguard Worker1. Update the test suite to use the `SharedWebViewTestEnvironment` 89*b7c941bbSAndroid Build Coastguard Worker2. Update the SDK JUnit Test Suite to invoke the test 90*b7c941bbSAndroid Build Coastguard Worker 91*b7c941bbSAndroid Build Coastguard WorkerWe will use `WebViewTest` as an example: 92*b7c941bbSAndroid Build Coastguard Worker`//cts/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java` 93*b7c941bbSAndroid Build Coastguard Worker 94*b7c941bbSAndroid Build Coastguard WorkerSearch for `getTestEnvironment()`. This method returns a `SharedWebViewTestEnvironment`. 95*b7c941bbSAndroid Build Coastguard WorkerWhenever your test needs to refer to anything that is not available in the SDK runtime, 96*b7c941bbSAndroid Build Coastguard Workeror needs to be shared between the SDK runtime and the activity based tests, 97*b7c941bbSAndroid Build Coastguard Workeruse this class. 98*b7c941bbSAndroid Build Coastguard Worker 99*b7c941bbSAndroid Build Coastguard WorkerOpen `SharedWebViewTestEnvironment` to familiarize yourself with what is available: 100*b7c941bbSAndroid Build Coastguard Worker`//cts/libs/webkit-shared/src/android/webkit/cts/SharedWebViewTestEnvironment.java` 101*b7c941bbSAndroid Build Coastguard Worker 102*b7c941bbSAndroid Build Coastguard WorkerFirst convert any direct references to any variable that should come from the shared test 103*b7c941bbSAndroid Build Coastguard Workerenvironment. 104*b7c941bbSAndroid Build Coastguard Worker 105*b7c941bbSAndroid Build Coastguard WorkerEg: 106*b7c941bbSAndroid Build Coastguard Worker```java 107*b7c941bbSAndroid Build Coastguard Worker@Test 108*b7c941bbSAndroid Build Coastguard Workerpublic void testExample() throws Throwable { 109*b7c941bbSAndroid Build Coastguard Worker - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 110*b7c941bbSAndroid Build Coastguard Worker 111*b7c941bbSAndroid Build Coastguard Worker + getTestEnvironment().waitForIdleSync(); 112*b7c941bbSAndroid Build Coastguard Worker ... 113*b7c941bbSAndroid Build Coastguard Worker} 114*b7c941bbSAndroid Build Coastguard Worker``` 115*b7c941bbSAndroid Build Coastguard Worker 116*b7c941bbSAndroid Build Coastguard Worker*** note 117*b7c941bbSAndroid Build Coastguard Worker**Tip:** You can likely just update your setup method to pull from the test environment for 118*b7c941bbSAndroid Build Coastguard Workerminimal refactoring. Eg: 119*b7c941bbSAndroid Build Coastguard Worker 120*b7c941bbSAndroid Build Coastguard Worker```java 121*b7c941bbSAndroid Build Coastguard Worker@Test 122*b7c941bbSAndroid Build Coastguard Workerpublic void setup() { 123*b7c941bbSAndroid Build Coastguard Worker mWebView = getTestEnvironment().getWebView(); 124*b7c941bbSAndroid Build Coastguard Worker ... 125*b7c941bbSAndroid Build Coastguard Worker} 126*b7c941bbSAndroid Build Coastguard Worker``` 127*b7c941bbSAndroid Build Coastguard Worker*** 128*b7c941bbSAndroid Build Coastguard Worker 129*b7c941bbSAndroid Build Coastguard WorkerNext you will invoke this shared test from your SDK JUnit test suite. An example of a JUnit 130*b7c941bbSAndroid Build Coastguard Workertest suite can be found here: 131*b7c941bbSAndroid Build Coastguard Worker`//cts/tests/tests/sdksandbox/webkit/src/android/sdksandbox/webkit/cts/WebViewSandboxTest.java` 132*b7c941bbSAndroid Build Coastguard Worker 133*b7c941bbSAndroid Build Coastguard WorkerYou can see in this file that we use `SdkSandboxScenarioRule#assertSdkTestRunPasses` to invoke 134*b7c941bbSAndroid Build Coastguard Workertest methods. 135*b7c941bbSAndroid Build Coastguard Worker 136*b7c941bbSAndroid Build Coastguard WorkerAnd that's it! Your test should now run! You can test that your method was added with `atest`: 137*b7c941bbSAndroid Build Coastguard Worker 138*b7c941bbSAndroid Build Coastguard Worker```sh 139*b7c941bbSAndroid Build Coastguard Worker# Confirm the test runs in the sandbox 140*b7c941bbSAndroid Build Coastguard Workeratest CtsSdkSandboxWebkitTestCases:WebViewSandboxTest#<shared_test> 141*b7c941bbSAndroid Build Coastguard Worker# Confirm the test still runs normally 142*b7c941bbSAndroid Build Coastguard Workeratest CtsWebkitTestCases:WebViewTest#<shared_test> 143*b7c941bbSAndroid Build Coastguard Worker``` 144*b7c941bbSAndroid Build Coastguard Worker 145*b7c941bbSAndroid Build Coastguard Worker## Invoking behavior in the Activity 146*b7c941bbSAndroid Build Coastguard Worker 147*b7c941bbSAndroid Build Coastguard WorkerThere are some things you won't be able to initiate from within the SDK runtime 148*b7c941bbSAndroid Build Coastguard Workerthat are needed to write tests. For example, you cannot start a local server 149*b7c941bbSAndroid Build Coastguard Workerin the SDK runtime, but this would be useful for testing against. 150*b7c941bbSAndroid Build Coastguard Worker 151*b7c941bbSAndroid Build Coastguard WorkerYou can use the 152*b7c941bbSAndroid Build Coastguard WorkerActivityInvoker (`//cts/libs/webkit-shared/src/android/webkit/cts/IActivityInvoker.aidl`) 153*b7c941bbSAndroid Build Coastguard Workerto add this functionality. 154*b7c941bbSAndroid Build Coastguard WorkerThe activity invoker allows SDK runtime tests to initiate events in the activity driving 155*b7c941bbSAndroid Build Coastguard Workerthe tests. 156*b7c941bbSAndroid Build Coastguard Worker 157*b7c941bbSAndroid Build Coastguard WorkerOnce you have added a new ActivityInvoker API, provide a wrapper API to SharedWebViewTestEnvironment 158*b7c941bbSAndroid Build Coastguard Workerto abstract these APIs away from test authors. 159