diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-03-17 03:32:47 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-03-17 03:32:47 +0000 |
commit | 819938121e1799bb8070fae63c3197cd2a35b778 (patch) | |
tree | 4d1c150ffa1720efabb23ae87c6225d7276ef420 | |
parent | 73ebb1f7baafc6f708ef0ff2e1f28f4e8b63098e (diff) | |
parent | aff87353780fdf65864b779c55671c44882d5850 (diff) | |
download | csuite-819938121e1799bb8070fae63c3197cd2a35b778.tar.gz |
Snap for 9762093 from aff87353780fdf65864b779c55671c44882d5850 to udc-release
Change-Id: I85806f66e8eab7749ec142c8a563d5b2b799965e
-rw-r--r-- | test_scripts/src/main/java/com/android/webview/lib/WebviewUtils.java | 135 | ||||
-rw-r--r-- | test_scripts/src/main/java/com/android/webview/tests/WebviewAppLaunchTest.java | 113 |
2 files changed, 142 insertions, 106 deletions
diff --git a/test_scripts/src/main/java/com/android/webview/lib/WebviewUtils.java b/test_scripts/src/main/java/com/android/webview/lib/WebviewUtils.java new file mode 100644 index 0000000..0774b23 --- /dev/null +++ b/test_scripts/src/main/java/com/android/webview/lib/WebviewUtils.java @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.webview.tests; + +import com.android.tradefed.device.DeviceNotAvailableException; +import com.android.tradefed.device.ITestDevice; +import com.android.tradefed.invoker.TestInformation; +import com.android.tradefed.log.LogUtil.CLog; +import com.android.tradefed.util.CommandResult; +import com.android.tradefed.util.CommandStatus; + +import org.junit.Assert; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class WebviewUtils { + private ITestDevice mTestDevice; + private TestInformation mTestInformation; + + public WebviewUtils(ITestDevice device, TestInformation testInformation) { + mTestDevice = device; + mTestInformation = testInformation; + } + + public WebviewPackage installWebview(String webviewVersion, String releaseChannel) + throws IOException, InterruptedException, DeviceNotAvailableException { + List<String> extraArgs = new ArrayList<>(); + if (webviewVersion == null + && Arrays.asList("beta", "stable").contains(releaseChannel.toLowerCase())) { + // Get current version of WebView in the stable or beta release channels. + CLog.i( + "Getting the latest nightly official release version of the %s branch", + releaseChannel); + String releaseChannelVersion = getNightlyBranchBuildVersion(releaseChannel); + Assert.assertNotNull( + String.format( + "Could not retrieve the latest " + + "nightly release version of the %s channel", + releaseChannel), + releaseChannelVersion); + // Install the latest official build compiled for the beta or stable branches. + extraArgs.addAll( + Arrays.asList("--milestone", releaseChannelVersion.split("\\.", 2)[0])); + } + CommandResult commandResult = + WebviewInstallerToolPreparer.runWebviewInstallerToolCommand( + mTestInformation, mTestDevice, webviewVersion, releaseChannel, extraArgs); + + Assert.assertEquals( + "The WebView installer tool failed to install WebView:\n" + + commandResult.toString(), + commandResult.getStatus(), + CommandStatus.SUCCESS); + + printWebviewVersion(); + return getCurrentWebviewPackage(); + } + + private static String getNightlyBranchBuildVersion(String releaseChannel) + throws IOException, MalformedURLException { + final URL omahaProxyUrl = new URL("https://omahaproxy.appspot.com/all?os=webview"); + try (BufferedReader bufferedReader = + new BufferedReader( + new InputStreamReader(omahaProxyUrl.openConnection().getInputStream()))) { + String csvLine = null; + while ((csvLine = bufferedReader.readLine()) != null) { + String[] csvLineValues = csvLine.split(","); + if (csvLineValues[1].toLowerCase().equals(releaseChannel.toLowerCase())) { + return csvLineValues[2]; + } + } + } + return null; + } + + public void uninstallWebview( + WebviewPackage webviewPackage, WebviewPackage preInstalledWebviewPackage) + throws DeviceNotAvailableException { + Assert.assertNotEquals( + "Test is attempting to uninstall the preinstalled WebView provider", + webviewPackage, + preInstalledWebviewPackage); + updateWebviewImplementation(preInstalledWebviewPackage.getPackageName()); + mTestDevice.executeAdbCommand("uninstall", webviewPackage.getPackageName()); + printWebviewVersion(); + } + + private void updateWebviewImplementation(String webviewPackageName) + throws DeviceNotAvailableException { + CommandResult res = + mTestDevice.executeShellV2Command( + String.format( + "cmd webviewupdate set-webview-implementation %s", + webviewPackageName)); + Assert.assertEquals( + "Failed to set webview update: " + res, res.getStatus(), CommandStatus.SUCCESS); + } + + public WebviewPackage getCurrentWebviewPackage() throws DeviceNotAvailableException { + String dumpsys = mTestDevice.executeShellCommand("dumpsys webviewupdate"); + return WebviewPackage.buildFromDumpsys(dumpsys); + } + + public void printWebviewVersion() throws DeviceNotAvailableException { + WebviewPackage currentWebview = getCurrentWebviewPackage(); + printWebviewVersion(currentWebview); + } + + public void printWebviewVersion(WebviewPackage currentWebview) + throws DeviceNotAvailableException { + CLog.i("Current webview implementation: %s", currentWebview.getPackageName()); + CLog.i("Current webview version: %s", currentWebview.getVersion()); + } +} diff --git a/test_scripts/src/main/java/com/android/webview/tests/WebviewAppLaunchTest.java b/test_scripts/src/main/java/com/android/webview/tests/WebviewAppLaunchTest.java index 186a9c1..c285308 100644 --- a/test_scripts/src/main/java/com/android/webview/tests/WebviewAppLaunchTest.java +++ b/test_scripts/src/main/java/com/android/webview/tests/WebviewAppLaunchTest.java @@ -28,8 +28,6 @@ import com.android.tradefed.log.LogUtil.CLog; import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData; import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; -import com.android.tradefed.util.CommandResult; -import com.android.tradefed.util.CommandStatus; import com.android.tradefed.util.RunUtil; import org.junit.After; @@ -39,14 +37,9 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import java.io.BufferedReader; import java.io.File; import java.io.IOException; -import java.io.InputStreamReader; -import java.net.MalformedURLException; -import java.net.URL; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; @@ -56,6 +49,7 @@ public class WebviewAppLaunchTest extends BaseHostJUnit4Test { @Rule public TestLogData mLogData = new TestLogData(); private static final long COMMAND_TIMEOUT_MILLIS = 5 * 60 * 1000; + private WebviewUtils mWebviewUtils; private WebviewPackage mPreInstalledWebview; private ApkInstaller mApkInstaller; @@ -99,7 +93,8 @@ public class WebviewAppLaunchTest extends BaseHostJUnit4Test { mWebviewVersionToTest != null || mReleaseChannel != null); mApkInstaller = ApkInstaller.getInstance(getDevice()); - mPreInstalledWebview = getCurrentWebviewPackage(); + mWebviewUtils = new WebviewUtils(getDevice(), getTestInformation()); + mPreInstalledWebview = mWebviewUtils.getCurrentWebviewPackage(); for (File apkPath : mApkPaths) { CLog.d("Installing " + apkPath); @@ -107,7 +102,7 @@ public class WebviewAppLaunchTest extends BaseHostJUnit4Test { } DeviceUtils.getInstance(getDevice()).freezeRotation(); - printWebviewVersion(); + mWebviewUtils.printWebviewVersion(); } @Test @@ -116,14 +111,14 @@ public class WebviewAppLaunchTest extends BaseHostJUnit4Test { IOException { AssertionError lastError = null; WebviewPackage lastWebviewInstalled = - installWebview(mWebviewVersionToTest, mReleaseChannel); + mWebviewUtils.installWebview(mWebviewVersionToTest, mReleaseChannel); try { assertAppLaunchNoCrash(); } catch (AssertionError e) { lastError = e; } finally { - uninstallWebview(lastWebviewInstalled); + mWebviewUtils.uninstallWebview(lastWebviewInstalled, mPreInstalledWebview); } // If the app doesn't crash, complete the test. @@ -159,101 +154,7 @@ public class WebviewAppLaunchTest extends BaseHostJUnit4Test { deviceUtils.unfreezeRotation(); mApkInstaller.uninstallAllInstalledPackages(); - printWebviewVersion(); - } - - private void printWebviewVersion(WebviewPackage currentWebview) - throws DeviceNotAvailableException { - CLog.i("Current webview implementation: %s", currentWebview.getPackageName()); - CLog.i("Current webview version: %s", currentWebview.getVersion()); - } - - private void printWebviewVersion() throws DeviceNotAvailableException { - WebviewPackage currentWebview = getCurrentWebviewPackage(); - printWebviewVersion(currentWebview); - } - - private WebviewPackage installWebview(String webviewVersion, String releaseChannel) - throws IOException, InterruptedException, DeviceNotAvailableException { - List<String> extraArgs = new ArrayList<>(); - if (webviewVersion == null - && Arrays.asList("beta", "stable").contains(releaseChannel.toLowerCase())) { - // Get current version of WebView in the stable or beta release channels. - CLog.i( - "Getting the latest nightly official release version of the %s branch", - releaseChannel); - String releaseChannelVersion = getNightlyBranchBuildVersion(releaseChannel); - Assert.assertNotNull( - String.format( - "Could not retrieve the latest " - + "nightly release version of the %s channel", - releaseChannel), - releaseChannelVersion); - // Install the latest official build compiled for the beta or stable branches. - extraArgs.addAll( - Arrays.asList("--milestone", releaseChannelVersion.split("\\.", 2)[0])); - } - CommandResult commandResult = - WebviewInstallerToolPreparer.runWebviewInstallerToolCommand( - getTestInformation(), - getDevice(), - webviewVersion, - releaseChannel, - extraArgs); - - Assert.assertEquals( - "The WebView installer tool failed to install WebView:\n" - + commandResult.toString(), - commandResult.getStatus(), - CommandStatus.SUCCESS); - - printWebviewVersion(); - return getCurrentWebviewPackage(); - } - - private String getNightlyBranchBuildVersion(String releaseChannel) - throws IOException, MalformedURLException { - final URL omahaProxyUrl = new URL("https://omahaproxy.appspot.com/all?os=webview"); - try (BufferedReader bufferedReader = - new BufferedReader( - new InputStreamReader(omahaProxyUrl.openConnection().getInputStream()))) { - String csvLine = null; - while ((csvLine = bufferedReader.readLine()) != null) { - String[] csvLineValues = csvLine.split(","); - if (csvLineValues[1].toLowerCase().equals(releaseChannel.toLowerCase())) { - return csvLineValues[2]; - } - } - } - return null; - } - - private void uninstallWebview(WebviewPackage webviewPackage) - throws DeviceNotAvailableException { - Assert.assertNotEquals( - "Test is attempting to uninstall the preinstalled WebView provider", - webviewPackage, - mPreInstalledWebview); - updateWebviewImplementation(mPreInstalledWebview.getPackageName()); - getDevice().executeAdbCommand("uninstall", webviewPackage.getPackageName()); - printWebviewVersion(); - } - - private void updateWebviewImplementation(String webviewPackageName) - throws DeviceNotAvailableException { - CommandResult res = - getDevice() - .executeShellV2Command( - String.format( - "cmd webviewupdate set-webview-implementation %s", - webviewPackageName)); - Assert.assertEquals( - "Failed to set webview update: " + res, res.getStatus(), CommandStatus.SUCCESS); - } - - private WebviewPackage getCurrentWebviewPackage() throws DeviceNotAvailableException { - String dumpsys = getDevice().executeShellCommand("dumpsys webviewupdate"); - return WebviewPackage.buildFromDumpsys(dumpsys); + mWebviewUtils.printWebviewVersion(); } private void assertAppLaunchNoCrash() throws DeviceNotAvailableException { |