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 static android.app.sdksandbox.testutils.testscenario.SdkSandboxScenarioRule.ENABLE_LIFE_CYCLE_ANNOTATIONS; 19 20 import android.app.sdksandbox.testutils.testscenario.SdkSandboxScenarioRule; 21 import android.os.Bundle; 22 import android.webkit.cts.SharedWebViewTest; 23 import android.webkit.cts.SharedWebViewTestEnvironment; 24 25 import androidx.test.core.app.ApplicationProvider; 26 27 import com.android.compatibility.common.util.NullWebViewUtils; 28 29 import org.junit.Assume; 30 import org.junit.runner.Description; 31 import org.junit.runners.model.Statement; 32 33 /** 34 * This rule is used to invoke webview tests inside a test sdk. 35 * This rule is a wrapper for using the 36 * {@link WebViewSandboxTestSdk}, for detailed implementation 37 * details please refer to its parent class 38 * {@link SdkSandboxScenarioRule}. 39 */ 40 public class WebViewSandboxTestRule extends SdkSandboxScenarioRule { 41 WebViewSandboxTestRule(String webViewTestClassName)42 public WebViewSandboxTestRule(String webViewTestClassName) { 43 super( 44 "com.android.cts.sdk.webviewsandboxtest", 45 getSetupParams(webViewTestClassName), 46 SharedWebViewTestEnvironment.createHostAppInvoker( 47 ApplicationProvider.getApplicationContext(), true), 48 ENABLE_LIFE_CYCLE_ANNOTATIONS); 49 } 50 getSetupParams(String webViewTestClassName)51 private static Bundle getSetupParams(String webViewTestClassName) { 52 Bundle params = new Bundle(); 53 params.putString(SharedWebViewTest.WEB_VIEW_TEST_CLASS_NAME, webViewTestClassName); 54 return params; 55 } 56 57 @Override apply(final Statement base, final Description description)58 public Statement apply(final Statement base, final Description description) { 59 // This will prevent shared webview tests from running if a WebView provider does not exist. 60 Assume.assumeTrue("WebView is not available", NullWebViewUtils.isWebViewAvailable()); 61 return super.apply(base, description); 62 } 63 } 64