Commit 30f568cb authored by Yoland Yan's avatar Yoland Yan Committed by Commit Bot

Convert SearchWidgetProviderTest to JUnit4

For more on JUnit4 migration, please check
src/testing/android/docs/junit4.md

Bug: 640116
Change-Id: I6a1857ab3322176c03256fba003f5a1677b5899e
Reviewed-on: https://chromium-review.googlesource.com/701639Reviewed-by: default avatarYusuf Ozuysal <yusufo@chromium.org>
Commit-Queue: Yoland Yan <yolandyan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#506779}
parent 1ebe145d
...@@ -10,15 +10,19 @@ import android.app.Instrumentation.ActivityMonitor; ...@@ -10,15 +10,19 @@ import android.app.Instrumentation.ActivityMonitor;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.test.InstrumentationTestCase;
import android.util.Pair; import android.util.Pair;
import android.view.View; import android.view.View;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import android.widget.TextView; import android.widget.TextView;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.AdvancedMockContext; import org.chromium.base.test.util.AdvancedMockContext;
...@@ -30,6 +34,7 @@ import org.chromium.chrome.browser.firstrun.FirstRunActivity; ...@@ -30,6 +34,7 @@ import org.chromium.chrome.browser.firstrun.FirstRunActivity;
import org.chromium.chrome.browser.locale.LocaleManager; import org.chromium.chrome.browser.locale.LocaleManager;
import org.chromium.chrome.browser.searchwidget.SearchActivity.SearchActivityDelegate; import org.chromium.chrome.browser.searchwidget.SearchActivity.SearchActivityDelegate;
import org.chromium.chrome.browser.util.IntentUtils; import org.chromium.chrome.browser.util.IntentUtils;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.ApplicationTestUtils; import org.chromium.chrome.test.util.ApplicationTestUtils;
import org.chromium.content.browser.test.util.CriteriaHelper; import org.chromium.content.browser.test.util.CriteriaHelper;
...@@ -41,8 +46,9 @@ import java.util.concurrent.ExecutionException; ...@@ -41,8 +46,9 @@ import java.util.concurrent.ExecutionException;
/** /**
* Tests for the SearchWidgetProvider. * Tests for the SearchWidgetProvider.
*/ */
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add(ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE) @CommandLineFlags.Add(ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE)
public class SearchWidgetProviderTest extends InstrumentationTestCase { public class SearchWidgetProviderTest {
private static class TestSearchDelegate extends SearchActivityDelegate { private static class TestSearchDelegate extends SearchActivityDelegate {
@Override @Override
public boolean isActivityDisabledForTests() { public boolean isActivityDisabledForTests() {
...@@ -85,9 +91,11 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase { ...@@ -85,9 +91,11 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase {
} }
} }
private final class TestContext extends AdvancedMockContext { private final static class TestContext extends AdvancedMockContext {
public TestContext() { public TestContext() {
super(getInstrumentation().getTargetContext().getApplicationContext()); super(InstrumentationRegistry.getInstrumentation()
.getTargetContext()
.getApplicationContext());
} }
} }
...@@ -98,10 +106,10 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase { ...@@ -98,10 +106,10 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase {
private TestContext mContext; private TestContext mContext;
private TestDelegate mDelegate; private TestDelegate mDelegate;
@Override @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); ApplicationTestUtils.setUp(
ApplicationTestUtils.setUp(getInstrumentation().getTargetContext(), true); InstrumentationRegistry.getInstrumentation().getTargetContext(), true);
SearchActivity.setDelegateForTests(new TestSearchDelegate()); SearchActivity.setDelegateForTests(new TestSearchDelegate());
mContext = new TestContext(); mContext = new TestContext();
...@@ -109,12 +117,13 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase { ...@@ -109,12 +117,13 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase {
SearchWidgetProvider.setActivityDelegateForTest(mDelegate); SearchWidgetProvider.setActivityDelegateForTest(mDelegate);
} }
@Override @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
ApplicationTestUtils.tearDown(getInstrumentation().getTargetContext()); ApplicationTestUtils.tearDown(
super.tearDown(); InstrumentationRegistry.getInstrumentation().getTargetContext());
} }
@Test
@SmallTest @SmallTest
public void testUpdateAll() { public void testUpdateAll() {
SearchWidgetProvider.handleAction( SearchWidgetProvider.handleAction(
...@@ -170,10 +179,11 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase { ...@@ -170,10 +179,11 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase {
checkWidgetStates(TEXT_SEARCH_ENGINE_FULL, View.VISIBLE); checkWidgetStates(TEXT_SEARCH_ENGINE_FULL, View.VISIBLE);
} }
@Test
@SmallTest @SmallTest
@CommandLineFlags.Remove(ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE) @CommandLineFlags.Remove(ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE)
public void testUpdateCachedEngineNameBeforeFirstRun() throws ExecutionException { public void testUpdateCachedEngineNameBeforeFirstRun() throws ExecutionException {
assertFalse(ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() { Assert.assertFalse(ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() {
@Override @Override
public Boolean call() throws Exception { public Boolean call() throws Exception {
return SearchWidgetProvider.shouldShowFullString(); return SearchWidgetProvider.shouldShowFullString();
...@@ -202,7 +212,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase { ...@@ -202,7 +212,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase {
SearchWidgetProvider.updateCachedEngineName(TEXT_SEARCH_ENGINE); SearchWidgetProvider.updateCachedEngineName(TEXT_SEARCH_ENGINE);
} }
}); });
assertEquals(0, mDelegate.mViews.size()); Assert.assertEquals(0, mDelegate.mViews.size());
// Manually set the preference, then update the cached engine name again. The // Manually set the preference, then update the cached engine name again. The
// SearchWidgetProvider should now believe that its widgets are displaying branding when it // SearchWidgetProvider should now believe that its widgets are displaying branding when it
...@@ -251,6 +261,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase { ...@@ -251,6 +261,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase {
}); });
} }
@Test
@SmallTest @SmallTest
public void testMicrophoneClick() { public void testMicrophoneClick() {
SearchWidgetProvider.handleAction( SearchWidgetProvider.handleAction(
...@@ -261,6 +272,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase { ...@@ -261,6 +272,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase {
} }
} }
@Test
@SmallTest @SmallTest
public void testTextClick() { public void testTextClick() {
SearchWidgetProvider.handleAction( SearchWidgetProvider.handleAction(
...@@ -271,6 +283,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase { ...@@ -271,6 +283,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase {
} }
} }
@Test
@SmallTest @SmallTest
@CommandLineFlags.Remove(ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE) @CommandLineFlags.Remove(ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE)
public void testOnboardingRequired() { public void testOnboardingRequired() {
...@@ -288,7 +301,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase { ...@@ -288,7 +301,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase {
: FirstRunActivity.class.getName(); : FirstRunActivity.class.getName();
ActivityMonitor monitor = new ActivityMonitor(className, null, false); ActivityMonitor monitor = new ActivityMonitor(className, null, false);
Instrumentation instrumentation = getInstrumentation(); Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
instrumentation.addMonitor(monitor); instrumentation.addMonitor(monitor);
// Click on the widget. // Click on the widget.
...@@ -304,7 +317,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase { ...@@ -304,7 +317,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase {
Activity activity = instrumentation.waitForMonitorWithTimeout( Activity activity = instrumentation.waitForMonitorWithTimeout(
monitor, CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL); monitor, CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL);
assertNotNull(activity); Assert.assertNotNull(activity);
if (isFirstRunComplete) { if (isFirstRunComplete) {
// Check that the Activity was launched in the right mode. // Check that the Activity was launched in the right mode.
Intent intent = activity.getIntent(); Intent intent = activity.getIntent();
...@@ -314,6 +327,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase { ...@@ -314,6 +327,7 @@ public class SearchWidgetProviderTest extends InstrumentationTestCase {
} }
} }
@Test
@SmallTest @SmallTest
public void testCrashAbsorption() { public void testCrashAbsorption() {
Runnable crashingRunnable = new Runnable() { Runnable crashingRunnable = new Runnable() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment