| 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.android_webview.test; |
| 6 | |
| 7 | import android.test.suitebuilder.annotation.SmallTest; |
| 8 | |
| 9 | import java.util.regex.Matcher; |
| 10 | import java.util.regex.Pattern; |
| 11 | |
| 12 | import org.chromium.android_webview.AwContents; |
| 13 | import org.chromium.base.test.util.Feature; |
| 14 | |
| 15 | public class UserAgentTest extends AwTestBase { |
| 16 | |
| 17 | private TestAwContentsClient mContentsClient; |
| 18 | private AwContents mAwContents; |
| 19 | |
| 20 | @Override |
| 21 | public void setUp() throws Exception { |
| 22 | super.setUp(); |
| 23 | mContentsClient = new TestAwContentsClient(); |
| 24 | mAwContents = createAwTestContainerViewOnMainSync(mContentsClient).getAwContents(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Test for b/6404375. Verify that the UA string doesn't contain |
| 29 | * two spaces before the Android build name. |
| 30 | */ |
| 31 | @SmallTest |
| 32 | @Feature({"AndroidWebView"}) |
| 33 | public void testNoExtraSpaceBeforeBuildName() throws Throwable { |
| 34 | getAwSettingsOnUiThread(mAwContents).setJavaScriptEnabled(true); |
| 35 | loadDataSync( |
| 36 | mAwContents, |
| 37 | mContentsClient.getOnPageFinishedHelper(), |
| 38 | // Spaces are replaced with underscores to avoid consecutive spaces compression. |
| 39 | "<html>" + |
| 40 | "<body onload='document.title=navigator.userAgent.replace(/ /g, \"_\")'></body>" + |
| 41 | "</html>", |
| 42 | "text/html", false); |
| 43 | final String ua = getTitleOnUiThread(mAwContents); |
| 44 | Matcher matcher = Pattern.compile("Android_[^;]+;_[^_]").matcher(ua); |
| 45 | assertTrue(matcher.find()); |
| 46 | } |
| 47 | } |