blob: 90f8d679baec3e17bd5e299f7a20c4434b1702e4 [file] [log] [blame]
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.android_webview.test;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Proxy;
import android.os.Looper;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.android_webview.AwContents;
import org.chromium.android_webview.AwContentsStatics;
import org.chromium.base.ContextUtils;
import org.chromium.base.test.util.AdvancedMockContext;
import org.chromium.base.test.util.Feature;
import org.chromium.content_public.browser.ContentViewStatics;
import org.chromium.net.ProxyChangeListener;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
/**
* Tests for ContentView methods that don't fall into any other category.
*/
@RunWith(AwJUnit4ClassRunner.class)
public class ContentViewMiscTest {
@Rule
public AwActivityTestRule mActivityTestRule = new AwActivityTestRule();
private TestAwContentsClient mContentsClient;
private AwContents mAwContents;
@Before
public void setUp() throws Exception {
mContentsClient = new TestAwContentsClient();
final AwTestContainerView testContainerView =
mActivityTestRule.createAwTestContainerViewOnMainSync(mContentsClient);
mAwContents = testContainerView.getAwContents();
}
@Test
@SmallTest
@Feature({"AndroidWebView"})
public void testFindAddress() {
Assert.assertNull(AwContentsStatics.findAddress("This is some random text"));
String googleAddr = "1600 Amphitheatre Pkwy, Mountain View, CA 94043";
String testString = "Address: " + googleAddr + " in a string";
Assert.assertEquals(googleAddr, AwContentsStatics.findAddress(testString));
}
@Test
@SmallTest
@Feature({"AndroidWebView"})
public void testEnableDisablePlatformNotifications() {
Looper.prepare();
// Set up mock contexts to use with the listener
final AtomicReference<BroadcastReceiver> receiverRef =
new AtomicReference<BroadcastReceiver>();
final AdvancedMockContext appContext = new AdvancedMockContext(
InstrumentationRegistry.getInstrumentation()
.getTargetContext()
.getApplicationContext()) {
@Override
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
receiverRef.set(receiver);
return null;
}
};
ContextUtils.initApplicationContextForTests(appContext);
// Set up a delegate so we know when native code is about to get
// informed of a proxy change.
final AtomicBoolean proxyChanged = new AtomicBoolean();
final ProxyChangeListener.Delegate delegate = () -> proxyChanged.set(true);
Intent intent = new Intent();
intent.setAction(Proxy.PROXY_CHANGE_ACTION);
// Create the listener that's going to be used for the test
ProxyChangeListener listener = ProxyChangeListener.create();
listener.setDelegateForTesting(delegate);
listener.start(0);
// Start the actual tests
// Make sure everything works by default
proxyChanged.set(false);
receiverRef.get().onReceive(appContext, intent);
Assert.assertEquals(true, proxyChanged.get());
// Now disable platform notifications and make sure we don't notify
// native code.
proxyChanged.set(false);
ContentViewStatics.disablePlatformNotifications();
receiverRef.get().onReceive(appContext, intent);
Assert.assertEquals(false, proxyChanged.get());
// Now re-enable notifications to make sure they work again.
ContentViewStatics.enablePlatformNotifications();
receiverRef.get().onReceive(appContext, intent);
Assert.assertEquals(true, proxyChanged.get());
}
}