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