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 17 package com.android.webview.tests; 18 19 import com.android.tradefed.util.AaptParser; 20 21 import org.junit.Assert; 22 23 import java.nio.file.Path; 24 import java.util.regex.Matcher; 25 import java.util.regex.Pattern; 26 27 public class WebviewPackage implements Comparable<WebviewPackage> { 28 private final String mPackageName; 29 private final String mVersion; 30 private final long mVersionCode; 31 private Path mApkPath; 32 WebviewPackage(String packageName, String version, long versionCode, Path apkPath)33 public WebviewPackage(String packageName, String version, long versionCode, Path apkPath) { 34 this(packageName, version, versionCode); 35 mApkPath = apkPath; 36 } 37 WebviewPackage(String packageName, String version, long versionCode)38 public WebviewPackage(String packageName, String version, long versionCode) { 39 mPackageName = packageName; 40 mVersion = version; 41 mVersionCode = versionCode; 42 } 43 buildFromApk(Path apkPath)44 public static WebviewPackage buildFromApk(Path apkPath) { 45 AaptParser aaptParser = AaptParser.parse(apkPath.toFile()); 46 return new WebviewPackage( 47 aaptParser.getPackageName(), 48 aaptParser.getVersionName(), 49 Long.parseLong(aaptParser.getVersionCode()), 50 apkPath); 51 } 52 buildFromDumpsys(String dumpsys)53 public static WebviewPackage buildFromDumpsys(String dumpsys) { 54 String regexPattern = "Current WebView package \\(name, version\\): \\((.*), (.*)\\)"; 55 Pattern pattern = Pattern.compile(regexPattern); 56 Matcher matcher = pattern.matcher(dumpsys); 57 Assert.assertTrue( 58 String.format( 59 "Cannot find a sub string matching the regex in the dumpsys\n%s", dumpsys), 60 matcher.find()); 61 return buildFromDumpsys(matcher.group(1), dumpsys); 62 } 63 buildFromDumpsys(String webviewPackage, String dumpsys)64 public static WebviewPackage buildFromDumpsys(String webviewPackage, String dumpsys) { 65 String regexPattern = 66 String.format( 67 "Valid package %s \\(versionName: (.*), versionCode: (\\d+)," 68 + " targetSdkVersion: (\\d+)\\)", 69 webviewPackage.replace(".", "\\.")); 70 Pattern pattern = Pattern.compile(regexPattern); 71 Matcher matcher = pattern.matcher(dumpsys); 72 Assert.assertTrue( 73 String.format( 74 "Cannot find a sub string matching the regex in the dumpsys\n%s", dumpsys), 75 matcher.find()); 76 return new WebviewPackage( 77 webviewPackage, matcher.group(1), Long.parseLong(matcher.group(2))); 78 } 79 getPath()80 public Path getPath() { 81 Assert.assertTrue( 82 "The apk path was not set for this WebviewPackage instance", mApkPath != null); 83 return mApkPath; 84 } 85 getPackageName()86 public String getPackageName() { 87 return mPackageName; 88 } 89 getVersion()90 public String getVersion() { 91 return mVersion; 92 } 93 getVersionCode()94 public long getVersionCode() { 95 return mVersionCode; 96 } 97 98 @Override compareTo(WebviewPackage otherWebviewPkg)99 public int compareTo(WebviewPackage otherWebviewPkg) { 100 return Long.compare(this.getVersionCode(), otherWebviewPkg.getVersionCode()); 101 } 102 103 @Override equals(final Object obj)104 public boolean equals(final Object obj) { 105 final WebviewPackage otherWebviewPkg = (WebviewPackage) obj; 106 return this.getPackageName().equals(otherWebviewPkg.getPackageName()) 107 && this.getVersion().equals(otherWebviewPkg.getVersion()); 108 } 109 } 110