Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/preferences/PreferencesTest.java

Issue 2367373003: [Android] Allow setting recently visited search engines as default search engine (Closed)
Patch Set: Update based on Peter and Dan's comments. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.preferences; 5 package org.chromium.chrome.browser.preferences;
6 6
7 import android.app.Activity; 7 import android.app.Activity;
8 import android.app.Instrumentation; 8 import android.app.Instrumentation;
9 import android.content.Context; 9 import android.content.Context;
10 import android.content.Intent; 10 import android.content.Intent;
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 /** 77 /**
78 * Change search engine and make sure it works correctly. 78 * Change search engine and make sure it works correctly.
79 */ 79 */
80 @SmallTest 80 @SmallTest
81 @Feature({"Preferences"}) 81 @Feature({"Preferences"})
82 @DisableIf.Build(hardware_is = "sprout", message = "crashes on android-one: crbug.com/540720") 82 @DisableIf.Build(hardware_is = "sprout", message = "crashes on android-one: crbug.com/540720")
83 @RetryOnFailure 83 @RetryOnFailure
84 public void testSearchEnginePreference() throws Exception { 84 public void testSearchEnginePreference() throws Exception {
85 ensureTemplateUrlServiceLoaded(); 85 ensureTemplateUrlServiceLoaded();
86 86
87 final Preferences prefActivity =
88 startPreferences(getInstrumentation(), SearchEnginePreference.cl ass.getName());
89
87 // Set the second search engine as the default using TemplateUrlService. 90 // Set the second search engine as the default using TemplateUrlService.
88 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 91 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
89 @Override 92 @Override
90 public void run() { 93 public void run() {
91 TemplateUrlService.getInstance().setSearchEngine(1); 94 SearchEnginePreference pref =
95 (SearchEnginePreference) prefActivity.getFragmentForTest ();
96 pref.setValueForTesting("1");
92 } 97 }
93 }); 98 });
94 99
95 final Preferences prefActivity =
96 startPreferences(getInstrumentation(), SearchEnginePreference.cl ass.getName());
97 100
98 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 101 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
99 @Override 102 @Override
100 public void run() { 103 public void run() {
101 // Ensure that the second search engine in the list is selected. 104 // Ensure that the second search engine in the list is selected.
102 SearchEnginePreference pref = 105 SearchEnginePreference pref =
103 (SearchEnginePreference) prefActivity.getFragmentForTest (); 106 (SearchEnginePreference) prefActivity.getFragmentForTest ();
104 assertNotNull(pref); 107 assertNotNull(pref);
105 assertEquals("1", pref.getValueForTesting()); 108 assertEquals("1", pref.getValueForTesting());
106 109
107 // Simulate selecting the third search engine, ensure that Templ ateUrlService is 110 // Simulate selecting the third search engine, ensure that Templ ateUrlService is
108 // updated, but location permission not granted for the new engi ne. 111 // updated, but location permission not granted for the new engi ne.
109 pref.setValueForTesting("2"); 112 String keyword2 = pref.setValueForTesting("2");
110 TemplateUrlService templateUrlService = TemplateUrlService.getIn stance(); 113 TemplateUrlService templateUrlService = TemplateUrlService.getIn stance();
111 assertEquals(2, templateUrlService.getDefaultSearchEngineIndex() ); 114 assertEquals(keyword2,
112 assertEquals(ContentSetting.ASK, locationPermissionForSearchEngi ne(2)); 115 templateUrlService.getDefaultSearchEngineTemplateUrl().g etKeyword());
116 assertEquals(ContentSetting.ASK, locationPermissionForSearchEngi ne(keyword2));
113 117
114 // Simulate selecting the fourth search engine and but set a blo cked permission 118 // Simulate selecting the fourth search engine and but set a blo cked permission
115 // first and ensure that location permission is NOT granted. 119 // first and ensure that location permission is NOT granted.
116 String url = templateUrlService.getSearchEngineUrlFromTemplateUr l(3); 120 String keyword3 = pref.getKeywordFromIndexForTesting(3);
121 String url = templateUrlService.getSearchEngineUrlFromTemplateUr l(keyword3);
117 WebsitePreferenceBridge.nativeSetGeolocationSettingForOrigin( 122 WebsitePreferenceBridge.nativeSetGeolocationSettingForOrigin(
118 url, url, ContentSetting.BLOCK.toInt(), false); 123 url, url, ContentSetting.BLOCK.toInt(), false);
119 pref.setValueForTesting("3"); 124 keyword3 = pref.setValueForTesting("3");
120 assertEquals(3, TemplateUrlService.getInstance().getDefaultSearc hEngineIndex()); 125 assertEquals(keyword3, TemplateUrlService.getInstance()
121 assertEquals(ContentSetting.BLOCK, locationPermissionForSearchEn gine(3)); 126 .getDefaultSearchEngineTemplateUr l()
122 assertEquals(ContentSetting.ASK, locationPermissionForSearchEngi ne(2)); 127 .getKeyword());
128 assertEquals(ContentSetting.BLOCK, locationPermissionForSearchEn gine(keyword3));
129 assertEquals(ContentSetting.ASK, locationPermissionForSearchEngi ne(keyword2));
123 130
124 // Make sure a pre-existing ALLOW value does not get deleted whe n switching away 131 // Make sure a pre-existing ALLOW value does not get deleted whe n switching away
125 // from a search engine. 132 // from a search engine.
126 url = templateUrlService.getSearchEngineUrlFromTemplateUrl(4); 133 String keyword4 = pref.getKeywordFromIndexForTesting(4);
134 url = templateUrlService.getSearchEngineUrlFromTemplateUrl(keywo rd4);
127 WebsitePreferenceBridge.nativeSetGeolocationSettingForOrigin( 135 WebsitePreferenceBridge.nativeSetGeolocationSettingForOrigin(
128 url, url, ContentSetting.ALLOW.toInt(), false); 136 url, url, ContentSetting.ALLOW.toInt(), false);
129 pref.setValueForTesting("4"); 137 keyword4 = pref.setValueForTesting("4");
130 assertEquals(4, TemplateUrlService.getInstance().getDefaultSearc hEngineIndex()); 138 assertEquals(keyword4, TemplateUrlService.getInstance()
131 assertEquals(ContentSetting.ALLOW, locationPermissionForSearchEn gine(4)); 139 .getDefaultSearchEngineTemplateUr l()
140 .getKeyword());
141 assertEquals(ContentSetting.ALLOW, locationPermissionForSearchEn gine(keyword4));
132 pref.setValueForTesting("3"); 142 pref.setValueForTesting("3");
133 assertEquals(ContentSetting.ALLOW, locationPermissionForSearchEn gine(4)); 143 assertEquals(ContentSetting.ALLOW, locationPermissionForSearchEn gine(keyword4));
134 } 144 }
135 }); 145 });
136 } 146 }
137 147
138 /** 148 /**
139 * Make sure that when a user switches to a search engine that uses HTTP, th e location 149 * Make sure that when a user switches to a search engine that uses HTTP, th e location
140 * permission is not added. 150 * permission is not added.
141 */ 151 */
142 /* 152 /*
143 * @SmallTest 153 * @SmallTest
144 * @Feature({"Preferences"}) 154 * @Feature({"Preferences"})
145 * BUG=crbug.com/540706 155 * BUG=crbug.com/540706
146 */ 156 */
147 @FlakyTest 157 @FlakyTest
148 @DisableIf.Build(hardware_is = "sprout", message = "fails on android-one: cr bug.com/540706") 158 @DisableIf.Build(hardware_is = "sprout", message = "fails on android-one: cr bug.com/540706")
149 public void testSearchEnginePreferenceHttp() throws Exception { 159 public void testSearchEnginePreferenceHttp() throws Exception {
150 ensureTemplateUrlServiceLoaded(); 160 ensureTemplateUrlServiceLoaded();
151 161
162 final Preferences prefActivity =
163 startPreferences(getInstrumentation(), SearchEnginePreference.cl ass.getName());
164
152 // Set the first search engine as the default using TemplateUrlService. 165 // Set the first search engine as the default using TemplateUrlService.
153 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 166 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
154 @Override 167 @Override
155 public void run() { 168 public void run() {
156 TemplateUrlService.getInstance().setSearchEngine(0); 169 SearchEnginePreference pref =
170 (SearchEnginePreference) prefActivity.getFragmentForTest ();
171 pref.setValueForTesting("0");
157 } 172 }
158 }); 173 });
159 174
160 final Preferences prefActivity =
161 startPreferences(getInstrumentation(), SearchEnginePreference.cl ass.getName());
162
163 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 175 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
164 @Override 176 @Override
165 public void run() { 177 public void run() {
166 // Ensure that the first search engine in the list is selected. 178 // Ensure that the first search engine in the list is selected.
167 SearchEnginePreference pref = 179 SearchEnginePreference pref =
168 (SearchEnginePreference) prefActivity.getFragmentForTest (); 180 (SearchEnginePreference) prefActivity.getFragmentForTest ();
169 assertNotNull(pref); 181 assertNotNull(pref);
170 assertEquals("0", pref.getValueForTesting()); 182 assertEquals("0", pref.getValueForTesting());
171 183
172 // Simulate selecting a search engine that uses HTTP. 184 // Simulate selecting a search engine that uses HTTP.
173 int index = indexOfFirstHttpSearchEngine(); 185 int index = indexOfFirstHttpSearchEngine(pref);
174 pref.setValueForTesting(Integer.toString(index)); 186 String keyword = pref.setValueForTesting(Integer.toString(index) );
175 187
176 TemplateUrlService templateUrlService = TemplateUrlService.getIn stance(); 188 TemplateUrlService templateUrlService = TemplateUrlService.getIn stance();
177 assertEquals(index, templateUrlService.getDefaultSearchEngineInd ex()); 189 assertEquals(keyword,
178 assertEquals(ContentSetting.ASK, locationPermissionForSearchEngi ne(index)); 190 templateUrlService.getDefaultSearchEngineTemplateUrl().g etKeyword());
191 assertEquals(ContentSetting.ASK, locationPermissionForSearchEngi ne(keyword));
179 } 192 }
180 }); 193 });
181 } 194 }
182 195
183 private int indexOfFirstHttpSearchEngine() { 196 private int indexOfFirstHttpSearchEngine(SearchEnginePreference pref) {
184 TemplateUrlService templateUrlService = TemplateUrlService.getInstance() ; 197 TemplateUrlService templateUrlService = TemplateUrlService.getInstance() ;
185 List<TemplateUrl> urls = templateUrlService.getLocalizedSearchEngines(); 198 List<TemplateUrl> urls = templateUrlService.getSearchEngines();
186 int index; 199 int index;
187 for (index = 0; index < urls.size(); ++index) { 200 for (index = 0; index < urls.size(); ++index) {
188 String url = templateUrlService.getSearchEngineUrlFromTemplateUrl(in dex); 201 String keyword = pref.getKeywordFromIndexForTesting(index);
202 String url = templateUrlService.getSearchEngineUrlFromTemplateUrl(ke yword);
189 if (url.startsWith("http:")) { 203 if (url.startsWith("http:")) {
190 return index; 204 return index;
191 } 205 }
192 } 206 }
193 fail(); 207 fail();
194 return index; 208 return index;
195 } 209 }
196 210
197 private void ensureTemplateUrlServiceLoaded() throws Exception { 211 private void ensureTemplateUrlServiceLoaded() throws Exception {
198 // Make sure the template_url_service is loaded. 212 // Make sure the template_url_service is loaded.
(...skipping 10 matching lines...) Expand all
209 onTemplateUrlServiceLoadedHelper.notifyCalled(); 223 onTemplateUrlServiceLoadedHelper.notifyCalled();
210 } 224 }
211 }); 225 });
212 TemplateUrlService.getInstance().load(); 226 TemplateUrlService.getInstance().load();
213 } 227 }
214 } 228 }
215 }); 229 });
216 onTemplateUrlServiceLoadedHelper.waitForCallback(0); 230 onTemplateUrlServiceLoadedHelper.waitForCallback(0);
217 } 231 }
218 232
219 private ContentSetting locationPermissionForSearchEngine(int index) { 233 private ContentSetting locationPermissionForSearchEngine(String keyword) {
220 String url = TemplateUrlService.getInstance().getSearchEngineUrlFromTemp lateUrl(index); 234 String url = TemplateUrlService.getInstance().getSearchEngineUrlFromTemp lateUrl(keyword);
221 GeolocationInfo locationSettings = new GeolocationInfo(url, null, false) ; 235 GeolocationInfo locationSettings = new GeolocationInfo(url, null, false) ;
222 ContentSetting locationPermission = locationSettings.getContentSetting() ; 236 ContentSetting locationPermission = locationSettings.getContentSetting() ;
223 return locationPermission; 237 return locationPermission;
224 } 238 }
225 239
226 /** 240 /**
227 * Tests setting FontScaleFactor and ForceEnableZoom in AccessibilityPrefere nces and ensures 241 * Tests setting FontScaleFactor and ForceEnableZoom in AccessibilityPrefere nces and ensures
228 * that ForceEnableZoom changes corresponding to FontScaleFactor. 242 * that ForceEnableZoom changes corresponding to FontScaleFactor.
229 */ 243 */
230 @SmallTest 244 @SmallTest
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 private static void userSetForceEnableZoom(final AccessibilityPreferences ac cessibilityPref, 314 private static void userSetForceEnableZoom(final AccessibilityPreferences ac cessibilityPref,
301 final SeekBarLinkedCheckBoxPreference forceEnableZoomPref, final boo lean enabled) { 315 final SeekBarLinkedCheckBoxPreference forceEnableZoomPref, final boo lean enabled) {
302 ThreadUtils.runOnUiThread(new Runnable() { 316 ThreadUtils.runOnUiThread(new Runnable() {
303 @Override 317 @Override
304 public void run() { 318 public void run() {
305 accessibilityPref.onPreferenceChange(forceEnableZoomPref, enable d); 319 accessibilityPref.onPreferenceChange(forceEnableZoomPref, enable d);
306 } 320 }
307 }); 321 });
308 } 322 }
309 } 323 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698