Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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.chrome.browser.searchwidget; | |
| 6 | |
| 7 import android.app.Activity; | |
| 8 import android.app.Instrumentation; | |
| 9 import android.app.Instrumentation.ActivityMonitor; | |
| 10 import android.content.Context; | |
| 11 import android.content.Intent; | |
| 12 import android.support.test.InstrumentationRegistry; | |
| 13 import android.support.test.filters.SmallTest; | |
| 14 import android.util.Pair; | |
| 15 import android.view.View; | |
| 16 import android.widget.FrameLayout; | |
| 17 import android.widget.RemoteViews; | |
| 18 import android.widget.TextView; | |
| 19 | |
| 20 import org.junit.Assert; | |
| 21 import org.junit.Before; | |
| 22 import org.junit.Test; | |
| 23 import org.junit.runner.RunWith; | |
| 24 | |
| 25 import org.chromium.base.ContextUtils; | |
| 26 import org.chromium.base.ThreadUtils; | |
| 27 import org.chromium.base.test.util.AdvancedMockContext; | |
| 28 import org.chromium.chrome.R; | |
| 29 import org.chromium.chrome.browser.util.IntentUtils; | |
| 30 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | |
| 31 | |
| 32 import java.util.ArrayList; | |
| 33 import java.util.List; | |
| 34 | |
| 35 /** | |
| 36 * Tests for the SearchWidgetProvider. | |
| 37 */ | |
| 38 @RunWith(ChromeJUnit4ClassRunner.class) | |
| 39 public class SearchWidgetProviderTest { | |
| 40 private static final class TestDelegate extends SearchWidgetProvider.Delegat e { | |
| 41 public static final int[] ALL_IDS = {11684, 20170525}; | |
| 42 | |
| 43 public final List<Pair<Integer, RemoteViews>> mViews = | |
| 44 new ArrayList<Pair<Integer, RemoteViews>>(); | |
|
Ted C
2017/04/18 04:33:02
I think the second part can just be "new ArrayList
gone
2017/04/18 17:14:33
Done.
| |
| 45 | |
| 46 private TestDelegate(Context context) { | |
| 47 super(context); | |
| 48 } | |
| 49 | |
| 50 @Override | |
| 51 protected int[] getAllSearchWidgetIds() { | |
| 52 return ALL_IDS; | |
| 53 } | |
| 54 | |
| 55 @Override | |
| 56 protected void updateAppWidget(int id, RemoteViews views) { | |
| 57 mViews.add(new Pair<Integer, RemoteViews>(id, views)); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 private static final class TestContext extends AdvancedMockContext { | |
| 62 public TestContext() { | |
| 63 // Wrapping the application context allows the ContextUtils to avoid writing to the real | |
| 64 // SharedPreferences file. | |
| 65 super(InstrumentationRegistry.getInstrumentation() | |
| 66 .getTargetContext() | |
|
Ted C
2017/04/18 04:33:02
this indentation looks quite wonky...clang format
gone
2017/04/18 17:14:33
Yep. Rejoice.
| |
| 67 .getApplicationContext()); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 private static final String TEXT_GENERIC = "Search"; | |
| 72 private static final String TEXT_SEARCH_ENGINE = "Stuff and Thangs"; | |
| 73 private static final String TEXT_SEARCH_ENGINE_FULL = "Search with Stuff and Thangs"; | |
| 74 | |
| 75 private TestContext mContext; | |
| 76 private TestDelegate mDelegate; | |
| 77 | |
| 78 @Before | |
| 79 public void setUp() throws Exception { | |
| 80 SearchActivity.disableForTests(); | |
| 81 | |
| 82 mContext = new TestContext(); | |
| 83 ContextUtils.initApplicationContextForTests(mContext); | |
| 84 | |
| 85 mDelegate = new TestDelegate(mContext); | |
| 86 SearchWidgetProvider.setDelegateForTest(mDelegate); | |
| 87 } | |
| 88 | |
| 89 @Test | |
| 90 @SmallTest | |
| 91 public void testUpdateAll() { | |
| 92 SearchWidgetProvider.handleAction(SearchWidgetProvider.ACTION_UPDATE_ALL _WIDGETS); | |
| 93 | |
| 94 // Without any idea of what the default search engine is, widgets should default to saying | |
| 95 // just "Search". | |
| 96 checkWidgetStates(TEXT_GENERIC, View.VISIBLE); | |
| 97 | |
| 98 // The microphone icon should disappear if voice queries are unavailable . | |
| 99 mDelegate.mViews.clear(); | |
| 100 SearchWidgetProvider.updateCachedVoiceSearchAvailability(false); | |
| 101 checkWidgetStates(TEXT_GENERIC, View.GONE); | |
| 102 | |
| 103 // After recording that the default search engine is "X", it should say "Search with X". | |
| 104 mDelegate.mViews.clear(); | |
| 105 SearchWidgetProvider.updateCachedEngineName(TEXT_SEARCH_ENGINE); | |
| 106 checkWidgetStates(TEXT_SEARCH_ENGINE_FULL, View.GONE); | |
| 107 | |
| 108 // The microphone icon should appear if voice queries are available. | |
| 109 mDelegate.mViews.clear(); | |
| 110 SearchWidgetProvider.updateCachedVoiceSearchAvailability(true); | |
| 111 checkWidgetStates(TEXT_SEARCH_ENGINE_FULL, View.VISIBLE); | |
| 112 } | |
| 113 | |
| 114 private void checkWidgetStates(final String expectedString, final int expect edMicrophoneState) { | |
| 115 // Confirm that all the widgets got updated. | |
| 116 assert mDelegate.mViews.size() == TestDelegate.ALL_IDS.length; | |
|
Ted C
2017/04/18 04:33:02
s/assert/assertEquals
gone
2017/04/18 17:14:33
Done.
| |
| 117 for (int i = 0; i < TestDelegate.ALL_IDS.length; i++) { | |
| 118 Assert.assertEquals(TestDelegate.ALL_IDS[i], mDelegate.mViews.get(i) .first.intValue()); | |
| 119 } | |
| 120 | |
| 121 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
| 122 @Override | |
| 123 public void run() { | |
| 124 // Check the contents of the RemoteViews by inflating them. | |
| 125 for (int i = 0; i < mDelegate.mViews.size(); i++) { | |
| 126 FrameLayout parentView = new FrameLayout(mContext); | |
| 127 RemoteViews views = mDelegate.mViews.get(i).second; | |
| 128 View view = views.apply(mContext, parentView); | |
| 129 parentView.addView(view); | |
| 130 | |
| 131 // Confirm that the string is correct. | |
| 132 TextView titleView = (TextView) view.findViewById(R.id.title ); | |
| 133 Assert.assertEquals(View.VISIBLE, titleView.getVisibility()) ; | |
| 134 Assert.assertEquals(expectedString, titleView.getText()); | |
| 135 | |
| 136 // Confirm the visibility of the microphone. | |
| 137 View microphoneView = view.findViewById(R.id.microphone_icon ); | |
| 138 Assert.assertEquals(expectedMicrophoneState, microphoneView. getVisibility()); | |
| 139 } | |
| 140 } | |
| 141 }); | |
| 142 } | |
| 143 | |
| 144 @Test | |
| 145 @SmallTest | |
| 146 public void testMicrophoneClick() { | |
| 147 SearchWidgetProvider.handleAction(SearchWidgetProvider.ACTION_UPDATE_ALL _WIDGETS); | |
| 148 for (int i = 0; i < mDelegate.mViews.size(); i++) { | |
| 149 RemoteViews views = mDelegate.mViews.get(i).second; | |
| 150 clickOnWidget(views, R.id.microphone_icon); | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 @Test | |
| 155 @SmallTest | |
| 156 public void testTextClick() { | |
| 157 SearchWidgetProvider.handleAction(SearchWidgetProvider.ACTION_UPDATE_ALL _WIDGETS); | |
| 158 for (int i = 0; i < mDelegate.mViews.size(); i++) { | |
| 159 RemoteViews views = mDelegate.mViews.get(i).second; | |
| 160 clickOnWidget(views, R.id.text_container); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 private void clickOnWidget(final RemoteViews views, final int clickTarget) { | |
| 165 final Instrumentation instrumentation = InstrumentationRegistry.getInstr umentation(); | |
| 166 final ActivityMonitor monitor = | |
| 167 new ActivityMonitor(SearchActivity.class.getName(), null, false) ; | |
| 168 instrumentation.addMonitor(monitor); | |
| 169 | |
| 170 // Click on the widget. | |
| 171 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
| 172 @Override | |
| 173 public void run() { | |
| 174 FrameLayout parentView = new FrameLayout(mContext); | |
| 175 View view = views.apply(mContext, parentView); | |
| 176 parentView.addView(view); | |
| 177 view.findViewById(clickTarget).performClick(); | |
| 178 } | |
| 179 }); | |
| 180 | |
| 181 // Check that the Activity was launched in the right mode. | |
| 182 Activity activity = instrumentation.waitForMonitor(monitor); | |
| 183 Intent intent = activity.getIntent(); | |
| 184 boolean microphoneState = IntentUtils.safeGetBooleanExtra( | |
| 185 intent, SearchWidgetProvider.EXTRA_START_VOICE_SEARCH, false); | |
| 186 Assert.assertEquals(clickTarget == R.id.microphone_icon, microphoneState ); | |
| 187 } | |
| 188 } | |
| OLD | NEW |