| 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.content.BroadcastReceiver; |
| 8 | import android.content.Context; |
| 9 | import android.content.Intent; |
| 10 | import android.content.IntentFilter; |
| 11 | import android.net.Proxy; |
| 12 | import android.test.FlakyTest; |
| 13 | import android.test.mock.MockContext; |
| 14 | import android.test.suitebuilder.annotation.SmallTest; |
| 15 | |
| 16 | import org.chromium.android_webview.AwContents; |
| 17 | import org.chromium.base.test.util.DisabledTest; |
| 18 | import org.chromium.base.test.util.Feature; |
| 19 | import org.chromium.base.test.util.UrlUtils; |
| 20 | import org.chromium.base.ThreadUtils; |
| 21 | import org.chromium.content.browser.ContentViewCore; |
| 22 | import org.chromium.content.browser.ContentViewStatics; |
| 23 | import org.chromium.net.ProxyChangeListener; |
| 24 | |
| 25 | import java.util.concurrent.atomic.AtomicBoolean; |
| 26 | import java.util.concurrent.atomic.AtomicReference; |
| 27 | import java.util.concurrent.Callable; |
| 28 | |
| 29 | /** |
| 30 | * Tests for ContentView methods that don't fall into any other category. |
| 31 | */ |
| 32 | public class ContentViewMiscTest extends AwTestBase { |
| 33 | |
| 34 | private TestAwContentsClient mContentsClient; |
| 35 | private AwContents mAwContents; |
| 36 | private ContentViewCore mContentViewCore; |
| 37 | |
| 38 | @Override |
| 39 | public void setUp() throws Exception { |
| 40 | super.setUp(); |
| 41 | mContentsClient = new TestAwContentsClient(); |
| 42 | final AwTestContainerView testContainerView = |
| 43 | createAwTestContainerViewOnMainSync(mContentsClient); |
| 44 | mAwContents = testContainerView.getAwContents(); |
| 45 | mContentViewCore = testContainerView.getContentViewCore(); |
| 46 | } |
| 47 | |
| 48 | @SmallTest |
| 49 | @Feature({"AndroidWebView"}) |
| 50 | public void testFindAddress() { |
| 51 | assertNull(ContentViewStatics.findAddress("This is some random text")); |
| 52 | |
| 53 | String googleAddr = "1600 Amphitheatre Pkwy, Mountain View, CA 94043"; |
| 54 | assertEquals(googleAddr, ContentViewStatics.findAddress(googleAddr)); |
| 55 | } |
| 56 | |
| 57 | @SmallTest |
| 58 | @Feature({"AndroidWebView"}) |
| 59 | public void testEnableDisablePlatformNotifications() { |
| 60 | |
| 61 | // Set up mock contexts to use with the listener |
| 62 | final AtomicReference<BroadcastReceiver> receiverRef = |
| 63 | new AtomicReference<BroadcastReceiver>(); |
| 64 | final MockContext appContext = new MockContext() { |
| 65 | @Override |
| 66 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 67 | receiverRef.set(receiver); |
| 68 | return null; |
| 69 | } |
| 70 | }; |
| 71 | final MockContext context = new MockContext() { |
| 72 | @Override |
| 73 | public Context getApplicationContext() { |
| 74 | return appContext; |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | // Set up a delegate so we know when native code is about to get |
| 79 | // informed of a proxy change. |
| 80 | final AtomicBoolean proxyChanged = new AtomicBoolean(); |
| 81 | final ProxyChangeListener.Delegate delegate = new ProxyChangeListener.Delegate() { |
| 82 | @Override |
| 83 | public void proxySettingsChanged() { |
| 84 | proxyChanged.set(true); |
| 85 | } |
| 86 | }; |
| 87 | Intent intent = new Intent(); |
| 88 | intent.setAction(Proxy.PROXY_CHANGE_ACTION); |
| 89 | |
| 90 | // Create the listener that's going to be used for the test |
| 91 | ProxyChangeListener listener = ProxyChangeListener.create(context); |
| 92 | listener.setDelegateForTesting(delegate); |
| 93 | listener.start(0); |
| 94 | |
| 95 | // Start the actual tests |
| 96 | |
| 97 | // Make sure everything works by default |
| 98 | proxyChanged.set(false); |
| 99 | receiverRef.get().onReceive(context, intent); |
| 100 | assertEquals(true, proxyChanged.get()); |
| 101 | |
| 102 | // Now disable platform notifications and make sure we don't notify |
| 103 | // native code. |
| 104 | proxyChanged.set(false); |
| 105 | ContentViewStatics.disablePlatformNotifications(); |
| 106 | receiverRef.get().onReceive(context, intent); |
| 107 | assertEquals(false, proxyChanged.get()); |
| 108 | |
| 109 | // Now re-enable notifications to make sure they work again. |
| 110 | ContentViewStatics.enablePlatformNotifications(); |
| 111 | receiverRef.get().onReceive(context, intent); |
| 112 | assertEquals(true, proxyChanged.get()); |
| 113 | } |
| 114 | } |