• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 
17 package com.android.webview.tests;
18 
19 import com.android.tradefed.device.DeviceNotAvailableException;
20 import com.android.tradefed.invoker.TestInformation;
21 import com.android.tradefed.log.LogUtil.CLog;
22 import com.android.tradefed.util.CommandResult;
23 import com.android.tradefed.util.CommandStatus;
24 
25 import org.junit.Assert;
26 
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.net.MalformedURLException;
31 import java.net.URL;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35 
36 public class WebviewUtils {
37     private TestInformation mTestInformation;
38 
WebviewUtils(TestInformation testInformation)39     public WebviewUtils(TestInformation testInformation) {
40         mTestInformation = testInformation;
41     }
42 
installWebview(String webviewVersion, String releaseChannel)43     public WebviewPackage installWebview(String webviewVersion, String releaseChannel)
44             throws IOException, InterruptedException, DeviceNotAvailableException {
45         List<String> extraArgs = new ArrayList<>();
46         if (webviewVersion == null
47                 && Arrays.asList("beta", "stable").contains(releaseChannel.toLowerCase())) {
48             // Get current version of WebView in the stable or beta release channels.
49             CLog.i(
50                     "Getting the latest nightly official release version of the %s branch",
51                     releaseChannel);
52             String releaseChannelVersion = getNightlyBranchBuildVersion(releaseChannel);
53             Assert.assertNotNull(
54                     String.format(
55                             "Could not retrieve the latest "
56                                     + "nightly release version of the %s channel",
57                             releaseChannel),
58                     releaseChannelVersion);
59             // Install the latest official build compiled for the beta or stable branches.
60             extraArgs.addAll(
61                     Arrays.asList("--milestone", releaseChannelVersion.split("\\.", 2)[0]));
62         }
63         CommandResult commandResult =
64                 WebviewInstallerToolPreparer.runWebviewInstallerToolCommand(
65                         mTestInformation, webviewVersion, releaseChannel, extraArgs);
66 
67         Assert.assertEquals(
68                 "The WebView installer tool failed to install WebView:\n"
69                         + commandResult.toString(),
70                 commandResult.getStatus(),
71                 CommandStatus.SUCCESS);
72 
73         printWebviewVersion();
74         return getCurrentWebviewPackage();
75     }
76 
getNightlyBranchBuildVersion(String releaseChannel)77     private static String getNightlyBranchBuildVersion(String releaseChannel)
78             throws IOException, MalformedURLException {
79         final URL omahaProxyUrl = new URL("https://omahaproxy.appspot.com/all?os=webview");
80         try (BufferedReader bufferedReader =
81                 new BufferedReader(
82                         new InputStreamReader(omahaProxyUrl.openConnection().getInputStream()))) {
83             String csvLine = null;
84             while ((csvLine = bufferedReader.readLine()) != null) {
85                 String[] csvLineValues = csvLine.split(",");
86                 if (csvLineValues[1].toLowerCase().equals(releaseChannel.toLowerCase())) {
87                     return csvLineValues[2];
88                 }
89             }
90         }
91         return null;
92     }
93 
uninstallWebview( WebviewPackage webviewPackage, WebviewPackage preInstalledWebviewPackage)94     public void uninstallWebview(
95             WebviewPackage webviewPackage, WebviewPackage preInstalledWebviewPackage)
96             throws DeviceNotAvailableException {
97         Assert.assertNotEquals(
98                 "Test is attempting to uninstall the preinstalled WebView provider",
99                 webviewPackage,
100                 preInstalledWebviewPackage);
101         updateWebviewImplementation(preInstalledWebviewPackage.getPackageName());
102         mTestInformation
103                 .getDevice()
104                 .executeAdbCommand("uninstall", webviewPackage.getPackageName());
105         printWebviewVersion();
106     }
107 
updateWebviewImplementation(String webviewPackageName)108     private void updateWebviewImplementation(String webviewPackageName)
109             throws DeviceNotAvailableException {
110         CommandResult res =
111                 mTestInformation
112                         .getDevice()
113                         .executeShellV2Command(
114                                 String.format(
115                                         "cmd webviewupdate set-webview-implementation %s",
116                                         webviewPackageName));
117         Assert.assertEquals(
118                 "Failed to set webview update: " + res, res.getStatus(), CommandStatus.SUCCESS);
119     }
120 
getCurrentWebviewPackage()121     public WebviewPackage getCurrentWebviewPackage() throws DeviceNotAvailableException {
122         String dumpsys = mTestInformation.getDevice().executeShellCommand("dumpsys webviewupdate");
123         return WebviewPackage.buildFromDumpsys(dumpsys);
124     }
125 
printWebviewVersion()126     public void printWebviewVersion() throws DeviceNotAvailableException {
127         WebviewPackage currentWebview = getCurrentWebviewPackage();
128         printWebviewVersion(currentWebview);
129     }
130 
printWebviewVersion(WebviewPackage currentWebview)131     public void printWebviewVersion(WebviewPackage currentWebview)
132             throws DeviceNotAvailableException {
133         CLog.i("Current webview implementation: %s", currentWebview.getPackageName());
134         CLog.i("Current webview version: %s", currentWebview.getVersion());
135     }
136 }
137