diff options
author | Android Merger <noreply-android-build-merger@google.com> | 2019-02-26 23:42:20 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2019-02-26 23:42:20 +0000 |
commit | a8bf3188888dbf3a3baf441c20aea954f981d9fa (patch) | |
tree | c92b049b69865cf7ec9ef586468ee6b422d408ba | |
parent | dffe3e0fb28d44828bb30cc3200043ce6e7ce778 (diff) | |
parent | 22963b80381260b6f5d31e8154a1f8c269c833c7 (diff) | |
download | cts-a8bf3188888dbf3a3baf441c20aea954f981d9fa.tar.gz |
Merge "Merge "resolve merge conflicts of ed8ae7dd1222afdb78eec81b0c6dc2b9fefc41de to oreo-mr1-cts-dev" into oreo-mr1-cts-dev am: f2b5b9f8fb" into pie-cts-dev
-rw-r--r-- | tests/tests/webkit/src/android/webkit/cts/WebSettingsTest.java | 79 |
1 files changed, 63 insertions, 16 deletions
diff --git a/tests/tests/webkit/src/android/webkit/cts/WebSettingsTest.java b/tests/tests/webkit/src/android/webkit/cts/WebSettingsTest.java index a9b0d9ed427..d1993dcc5e0 100644 --- a/tests/tests/webkit/src/android/webkit/cts/WebSettingsTest.java +++ b/tests/tests/webkit/src/android/webkit/cts/WebSettingsTest.java @@ -105,7 +105,7 @@ public class WebSettingsTest extends ActivityInstrumentationTestCase2<WebViewCts * compatibility definition (tokens in angle brackets are variables, tokens in square * brackets are optional): * <p/> - * Mozilla/5.0 (Linux; Android <version>; [<devicemodel>;] [Build/<buildID>;] wv) + * Mozilla/5.0 (Linux; Android <version>; [<devicemodel>] [Build/<buildID>]; wv) * AppleWebKit/<major>.<minor> (KHTML, like Gecko) Version/<major>.<minor> * Chrome/<major>.<minor>.<branch>.<build>[ Mobile] Safari/<major>.<minor> */ @@ -113,36 +113,83 @@ public class WebSettingsTest extends ActivityInstrumentationTestCase2<WebViewCts if (!NullWebViewUtils.isWebViewAvailable()) { return; } - final String actualUserAgentString = mSettings.getUserAgentString(); - Log.i(LOG_TAG, String.format("Checking user agent string %s", actualUserAgentString)); + checkUserAgentStringHelper(mSettings.getUserAgentString(), true); + } + + /** + * Verifies that the useragent testing regex is actually correct, because it's very complex. + */ + public void testUserAgentStringTest() { + // All test UAs share the same prefix and suffix; only the middle part varies. + final String prefix = "Mozilla/5.0 (Linux; Android " + Build.VERSION.RELEASE + "; "; + final String suffix = "wv) AppleWebKit/0.0 (KHTML, like Gecko) Version/4.0 Chrome/0.0.0.0 Safari/0.0"; + + // Valid cases: + // Both model and build present + checkUserAgentStringHelper(prefix + Build.MODEL + " Build/" + Build.ID + "; " + suffix, true); + // Just model + checkUserAgentStringHelper(prefix + Build.MODEL + "; " + suffix, true); + // Just build + checkUserAgentStringHelper(prefix + "Build/" + Build.ID + "; " + suffix, true); + // Neither + checkUserAgentStringHelper(prefix + suffix, true); + + // Invalid cases: + // No space between model and build + checkUserAgentStringHelper(prefix + Build.MODEL + "Build/" + Build.ID + "; " + suffix, false); + // No semicolon after model and/or build + checkUserAgentStringHelper(prefix + Build.MODEL + " Build/" + Build.ID + suffix, false); + checkUserAgentStringHelper(prefix + Build.MODEL + suffix, false); + checkUserAgentStringHelper(prefix + "Build/" + Build.ID + suffix, false); + // Double semicolon when both omitted + checkUserAgentStringHelper(prefix + "; " + suffix, false); + } - String expectedRelease, expectedModel; + /** + * Helper function to validate that a given useragent string is or is not valid. + */ + private void checkUserAgentStringHelper(final String useragent, boolean shouldMatch) { + String expectedRelease; if ("REL".equals(Build.VERSION.CODENAME)) { expectedRelease = Pattern.quote(Build.VERSION.RELEASE); - expectedModel = Pattern.quote(Build.MODEL); } else { - // Non-release builds don't include real release version/model, be lenient. - expectedRelease = expectedModel = "[^;]+"; + // Non-release builds don't include real release version, be lenient. + expectedRelease = "[^;]+"; } // Build expected regex inserting the appropriate variables, as this is easier to // understand and get right than matching any possible useragent and comparing the // variables afterward. final String patternString = - Pattern.quote("Mozilla/5.0 (Linux; Android ") + expectedRelease + "; " + - "(" + expectedModel + "; )?" + // Optional - "(Build/" + Pattern.quote(Build.ID) + "; )?" + // Optional - Pattern.quote("wv) ") + + // Release version always has a semicolon after it: + Pattern.quote("Mozilla/5.0 (Linux; Android ") + expectedRelease + ";" + + // Model is optional, but if present must have a space first: + "( " + Pattern.quote(Build.MODEL) + ")?" + + // Build is optional, but if present must have a space first: + "( Build/" + Pattern.quote(Build.ID) + ")?" + + // We want a semicolon before the wv token, but we don't want to have two in a row + // if both model and build are omitted. Lookbehind assertions ensure either: + // - the previous character is a semicolon + // - or the previous character is NOT a semicolon AND a semicolon is added here. + "((?<=;)|(?<!;);)" + + // After that we can just check for " wv)" to finish the platform section: + Pattern.quote(" wv) ") + + // The rest of the expression is browser tokens and is fairly simple: "AppleWebKit/\\d+\\.\\d+ " + Pattern.quote("(KHTML, like Gecko) Version/4.0 ") + "Chrome/\\d+\\.\\d+\\.\\d+\\.\\d+ " + "(Mobile )?Safari/\\d+\\.\\d+"; - Log.i(LOG_TAG, String.format("Trying to match pattern %s", patternString)); final Pattern userAgentExpr = Pattern.compile(patternString); - Matcher patternMatcher = userAgentExpr.matcher(actualUserAgentString); - assertTrue(String.format("User agent string did not match expected pattern. \nExpected " + - "pattern:\n%s\nActual:\n%s", patternString, actualUserAgentString), - patternMatcher.find()); + Matcher patternMatcher = userAgentExpr.matcher(useragent); + if (shouldMatch) { + assertTrue(String.format("CDD(3.4.1/C-1-3) User agent string did not match expected pattern. \n" + + "Expected pattern:\n%s\nActual:\n%s", patternString, useragent), + patternMatcher.find()); + } else { + assertFalse(String.format("Known-bad user agent string incorrectly matched. \n" + + "Expected pattern:\n%s\nActual:\n%s", patternString, useragent), + patternMatcher.find()); + } } public void testAccessUserAgentString() throws Exception { |