1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.sdksandbox.webkit.cts; 17 18 import android.app.sdksandbox.testutils.testscenario.SdkSandboxScenarioRule; 19 import android.os.Bundle; 20 import android.webkit.cts.SharedWebViewTest; 21 import android.webkit.cts.SharedWebViewTestEnvironment; 22 23 import androidx.test.core.app.ApplicationProvider; 24 25 import com.android.compatibility.common.util.NullWebViewUtils; 26 27 import org.junit.Assume; 28 import org.junit.runner.Description; 29 import org.junit.runners.model.Statement; 30 31 /** 32 * This rule is used to invoke webview tests inside a test sdk. 33 * This rule is a wrapper for using the 34 * {@link WebViewSandboxTestSdk}, for detailed implementation 35 * details please refer to its parent class 36 * {@link SdkSandboxScenarioRule}. 37 */ 38 public class WebViewSandboxTestRule extends SdkSandboxScenarioRule { 39 WebViewSandboxTestRule(String webViewTestClassName)40 public WebViewSandboxTestRule(String webViewTestClassName) { 41 super( 42 "com.android.cts.sdk.webviewsandboxtest", 43 getSetupParams(webViewTestClassName), 44 SharedWebViewTestEnvironment.createHostAppInvoker( 45 ApplicationProvider.getApplicationContext(), true), 46 ENABLE_LIFE_CYCLE_ANNOTATIONS); 47 } 48 getSetupParams(String webViewTestClassName)49 private static Bundle getSetupParams(String webViewTestClassName) { 50 Bundle params = new Bundle(); 51 params.putString(SharedWebViewTest.WEB_VIEW_TEST_CLASS_NAME, webViewTestClassName); 52 return params; 53 } 54 55 @Override apply(final Statement base, final Description description)56 public Statement apply(final Statement base, final Description description) { 57 // If WebView is not available, simply skip loading the SDK and then throw an assumption 58 // failure for each test run attempt. 59 // We can't throw the assumptions in the apply because WebViewSandboxTestRule can be used as 60 // a class rule. 61 if (!NullWebViewUtils.isWebViewAvailable()) { 62 return base; 63 } 64 65 return super.apply(base, description); 66 } 67 68 @Override assertSdkTestRunPasses(String testMethodName, Bundle params)69 public void assertSdkTestRunPasses(String testMethodName, Bundle params) throws Throwable { 70 // This will prevent shared webview tests from running if a WebView provider does not exist. 71 Assume.assumeTrue("WebView is not available", NullWebViewUtils.isWebViewAvailable()); 72 super.assertSdkTestRunPasses(testMethodName, params); 73 } 74 } 75