OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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.partnercustomizations; |
| 6 |
| 7 import android.content.SharedPreferences; |
| 8 import android.net.Uri; |
| 9 import android.preference.PreferenceManager; |
| 10 import android.test.suitebuilder.annotation.MediumTest; |
| 11 import android.view.View; |
| 12 import android.widget.CheckBox; |
| 13 import android.widget.Checkable; |
| 14 import android.widget.EditText; |
| 15 |
| 16 import com.google.android.apps.chrome.R; |
| 17 |
| 18 import org.chromium.base.ThreadUtils; |
| 19 import org.chromium.base.test.util.CommandLineFlags; |
| 20 import org.chromium.base.test.util.Feature; |
| 21 import org.chromium.chrome.browser.ChromeSwitches; |
| 22 import org.chromium.chrome.browser.ChromeTabbedActivity; |
| 23 import org.chromium.chrome.browser.Tab; |
| 24 import org.chromium.chrome.browser.preferences.HomepagePreferences; |
| 25 import org.chromium.chrome.browser.preferences.Preferences; |
| 26 import org.chromium.chrome.browser.tabmodel.EmptyTabModelObserver; |
| 27 import org.chromium.chrome.browser.tabmodel.TabList; |
| 28 import org.chromium.chrome.browser.tabmodel.TabModel; |
| 29 import org.chromium.chrome.browser.widget.ChromeSwitchCompat; |
| 30 import org.chromium.chrome.test.partnercustomizations.TestPartnerBrowserCustomiz
ationsProvider; |
| 31 import org.chromium.chrome.test.util.ChromeTabUtils; |
| 32 import org.chromium.chrome.test.util.TestHttpServerClient; |
| 33 import org.chromium.content.browser.test.util.CallbackHelper; |
| 34 import org.chromium.content.browser.test.util.Criteria; |
| 35 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 36 import org.chromium.content.browser.test.util.TouchCommon; |
| 37 import org.chromium.content.browser.test.util.UiUtils; |
| 38 |
| 39 import java.util.concurrent.TimeoutException; |
| 40 |
| 41 /** |
| 42 * Integration test suite for partner homepage. |
| 43 */ |
| 44 public class PartnerHomepageIntegrationTest extends BasePartnerBrowserCustomizat
ionIntegrationTest { |
| 45 private static final String TEST_URL = |
| 46 TestHttpServerClient.getUrl("chrome/test/data/android/about.html"); |
| 47 |
| 48 @Override |
| 49 public void startMainActivity() throws InterruptedException { |
| 50 ThreadUtils.runOnUiThreadBlocking(new Runnable(){ |
| 51 @Override |
| 52 public void run() { |
| 53 // TODO(newt): Remove this once SharedPreferences is cleared aut
omatically at the |
| 54 // beginning of every test. http://crbug.com/441859 |
| 55 SharedPreferences sp = PreferenceManager.getDefaultSharedPrefere
nces( |
| 56 getInstrumentation().getTargetContext()); |
| 57 sp.edit().clear().apply(); |
| 58 } |
| 59 }); |
| 60 |
| 61 startMainActivityFromLauncher(); |
| 62 } |
| 63 |
| 64 /** |
| 65 * Homepage is loaded on startup. |
| 66 */ |
| 67 @MediumTest |
| 68 @Feature({"Homepage" }) |
| 69 public void testHomepageInitialLoading() { |
| 70 assertEquals(Uri.parse(TestPartnerBrowserCustomizationsProvider.HOMEPAGE
_URI), |
| 71 Uri.parse(getActivity().getActivityTab().getUrl())); |
| 72 } |
| 73 |
| 74 /** |
| 75 * Clicking the homepage button should load homepage in the current tab. |
| 76 */ |
| 77 @MediumTest |
| 78 @Feature({"Homepage"}) |
| 79 public void testHomepageButtonClick() throws InterruptedException { |
| 80 // Load non-homepage URL. |
| 81 loadUrl(TEST_URL); |
| 82 UiUtils.settleDownUI(getInstrumentation()); |
| 83 assertNotSame(Uri.parse(TestPartnerBrowserCustomizationsProvider.HOMEPAG
E_URI), |
| 84 Uri.parse(getActivity().getActivityTab().getUrl())); |
| 85 |
| 86 // Click homepage button. |
| 87 ChromeTabUtils.waitForTabPageLoaded(getActivity().getActivityTab(), new
Runnable() { |
| 88 @Override |
| 89 public void run() { |
| 90 View homeButton = getActivity().findViewById(R.id.home_button); |
| 91 assertEquals("Homepage button is not shown", |
| 92 View.VISIBLE, homeButton.getVisibility()); |
| 93 singleClickView(homeButton); |
| 94 } |
| 95 }); |
| 96 assertEquals(Uri.parse(TestPartnerBrowserCustomizationsProvider.HOMEPAGE
_URI), |
| 97 Uri.parse(getActivity().getActivityTab().getUrl())); |
| 98 } |
| 99 |
| 100 /** |
| 101 * Homepage button visibility should be updated by enabling and disabling ho
mepage in settings. |
| 102 * @throws InterruptedException |
| 103 */ |
| 104 @MediumTest |
| 105 @Feature({"Homepage"}) |
| 106 public void testHomepageButtonEnableDisable() throws InterruptedException { |
| 107 // Disable homepage. |
| 108 Preferences homepagePreferenceActivity = |
| 109 startPreferences(HomepagePreferences.class.getName()); |
| 110 ChromeSwitchCompat homepageSwitch = |
| 111 (ChromeSwitchCompat) homepagePreferenceActivity.findViewById(R.i
d.homepage_switch); |
| 112 assertNotNull(homepageSwitch); |
| 113 TouchCommon.singleClickView(homepageSwitch); |
| 114 waitForCheckedState(homepageSwitch, false); |
| 115 homepagePreferenceActivity.finish(); |
| 116 |
| 117 // Assert no homepage button. |
| 118 assertFalse(HomepageManager.isHomepageEnabled(getActivity())); |
| 119 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 120 @Override |
| 121 public void run() { |
| 122 assertEquals("Homepage button is shown", View.GONE, |
| 123 getActivity().findViewById(R.id.home_button).getVisibili
ty()); |
| 124 } |
| 125 }); |
| 126 |
| 127 // Enable homepage. |
| 128 homepagePreferenceActivity = startPreferences(HomepagePreferences.class.
getName()); |
| 129 homepageSwitch = |
| 130 (ChromeSwitchCompat) homepagePreferenceActivity.findViewById(R.i
d.homepage_switch); |
| 131 assertNotNull(homepageSwitch); |
| 132 TouchCommon.singleClickView(homepageSwitch); |
| 133 waitForCheckedState(homepageSwitch, true); |
| 134 homepagePreferenceActivity.finish(); |
| 135 |
| 136 // Assert homepage button. |
| 137 assertTrue(HomepageManager.isHomepageEnabled(getActivity())); |
| 138 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 139 @Override |
| 140 public void run() { |
| 141 assertEquals("Homepage button is shown", View.VISIBLE, |
| 142 getActivity().findViewById(R.id.home_button).getVisibili
ty()); |
| 143 } |
| 144 }); |
| 145 } |
| 146 |
| 147 private boolean waitForCheckedState(final Checkable view, final boolean isCh
ecked) |
| 148 throws InterruptedException { |
| 149 return CriteriaHelper.pollForUIThreadCriteria(new Criteria() { |
| 150 @Override |
| 151 public boolean isSatisfied() { |
| 152 return view.isChecked() == isChecked; |
| 153 } |
| 154 }); |
| 155 } |
| 156 |
| 157 /** |
| 158 * Custom homepage URI should be fixed (e.g., "chrome.com" -> "http://chrome
.com/") |
| 159 * on exiting Homepage settings page. |
| 160 */ |
| 161 @MediumTest |
| 162 @Feature({"Homepage"}) |
| 163 public void testPreferenceCustomUriFixup() throws InterruptedException { |
| 164 // Change homepage custom URI on homepage settings. |
| 165 final Preferences homepagePreferenceActivity = |
| 166 startPreferences(HomepagePreferences.class.getName()); |
| 167 CheckBox checkBox = |
| 168 (CheckBox) homepagePreferenceActivity.findViewById(R.id.default_
checkbox); |
| 169 assertTrue(checkBox.isChecked()); |
| 170 TouchCommon.singleClickView(checkBox); |
| 171 TouchCommon.singleClickView(homepagePreferenceActivity.findViewById(R.id
.custom_uri)); |
| 172 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 173 @Override |
| 174 public void run() { |
| 175 ((EditText) homepagePreferenceActivity.findViewById(R.id.custom_
uri)) |
| 176 .setText("chrome.com"); |
| 177 } |
| 178 }); |
| 179 homepagePreferenceActivity.finish(); |
| 180 |
| 181 UiUtils.settleDownUI(getInstrumentation()); |
| 182 assertEquals("http://chrome.com/", HomepageManager.getHomepageUri(getAct
ivity())); |
| 183 } |
| 184 |
| 185 /** |
| 186 * Closing the last tab should also close Chrome on Tabbed mode. |
| 187 */ |
| 188 @CommandLineFlags.Add(ChromeSwitches.DISABLE_DOCUMENT_MODE) |
| 189 @MediumTest |
| 190 @Feature({"Homepage" }) |
| 191 public void testLastTabClosed() throws InterruptedException { |
| 192 ChromeTabUtils.closeCurrentTab(getInstrumentation(), (ChromeTabbedActivi
ty) getActivity()); |
| 193 assertTrue("Activity was not closed.", |
| 194 getActivity().isFinishing() || getActivity().isDestroyed()); |
| 195 } |
| 196 |
| 197 /** |
| 198 * Closing all tabs should finalize all tab closures and close Chrome on Tab
bed mode. |
| 199 */ |
| 200 @CommandLineFlags.Add(ChromeSwitches.DISABLE_DOCUMENT_MODE) |
| 201 @MediumTest |
| 202 @Feature({"Homepage" }) |
| 203 public void testCloseAllTabs() throws InterruptedException { |
| 204 final CallbackHelper tabClosed = new CallbackHelper(); |
| 205 final TabModel tabModel = getActivity().getCurrentTabModel(); |
| 206 getActivity().getCurrentTabModel().addObserver(new EmptyTabModelObserver
() { |
| 207 @Override |
| 208 public void didCloseTab(Tab tab) { |
| 209 if (tabModel.getCount() == 0) tabClosed.notifyCalled(); |
| 210 } |
| 211 }); |
| 212 getInstrumentation().runOnMainSync(new Runnable() { |
| 213 @Override |
| 214 public void run() { |
| 215 getActivity().getTabModelSelector().closeAllTabs(); |
| 216 } |
| 217 }); |
| 218 |
| 219 try { |
| 220 tabClosed.waitForCallback(0); |
| 221 } catch (TimeoutException e) { |
| 222 fail("Never closed all of the tabs"); |
| 223 } |
| 224 assertEquals("Expected no tabs to be present", |
| 225 0, getActivity().getCurrentTabModel().getCount()); |
| 226 TabList fullModel = getActivity().getCurrentTabModel().getComprehensiveM
odel(); |
| 227 // By the time TAB_CLOSED event is received, all tab closures should be
finalized |
| 228 assertEquals("Expected no tabs to be present in the comprehensive model"
, |
| 229 0, fullModel.getCount()); |
| 230 |
| 231 getInstrumentation().waitForIdleSync(); |
| 232 assertTrue("Activity was not closed.", |
| 233 getActivity().isFinishing() || getActivity().isDestroyed()); |
| 234 } |
| 235 } |
OLD | NEW |