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.Context; |
| 8 import android.net.Uri; |
| 9 import android.os.Bundle; |
| 10 import android.test.suitebuilder.annotation.MediumTest; |
| 11 import android.view.Menu; |
| 12 import android.view.MenuItem; |
| 13 import android.widget.PopupMenu; |
| 14 |
| 15 import com.google.android.apps.chrome.R; |
| 16 |
| 17 import org.chromium.base.ThreadUtils; |
| 18 import org.chromium.base.test.util.Feature; |
| 19 import org.chromium.chrome.browser.preferences.PrefServiceBridge; |
| 20 import org.chromium.chrome.test.partnercustomizations.TestPartnerBrowserCustomiz
ationsProvider; |
| 21 import org.chromium.chrome.test.util.TestHttpServerClient; |
| 22 import org.chromium.content.browser.test.util.Criteria; |
| 23 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 24 |
| 25 import java.util.concurrent.Callable; |
| 26 import java.util.concurrent.ExecutionException; |
| 27 |
| 28 /** |
| 29 * Integration tests for the partner disabling incognito mode feature. |
| 30 */ |
| 31 public class PartnerDisableIncognitoModeIntegrationTest extends |
| 32 BasePartnerBrowserCustomizationIntegrationTest { |
| 33 |
| 34 private static final String TEST_URLS[] = { |
| 35 TestHttpServerClient.getUrl("chrome/test/data/android/about.html"), |
| 36 TestHttpServerClient.getUrl("chrome/test/data/android/ok.txt"), |
| 37 TestHttpServerClient.getUrl("chrome/test/data/android/test.html") |
| 38 }; |
| 39 |
| 40 @Override |
| 41 public void startMainActivity() throws InterruptedException { |
| 42 // Each test will launch main activity, so purposefully omit here. |
| 43 } |
| 44 |
| 45 private void setParentalControlsEnabled(boolean enabled) { |
| 46 Uri uri = PartnerBrowserCustomizations.buildQueryUri( |
| 47 PartnerBrowserCustomizations.PARTNER_DISABLE_INCOGNITO_MODE_PATH
); |
| 48 Bundle bundle = new Bundle(); |
| 49 bundle.putBoolean( |
| 50 TestPartnerBrowserCustomizationsProvider.INCOGNITO_MODE_DISABLED
_KEY, enabled); |
| 51 Context context = getInstrumentation().getTargetContext(); |
| 52 context.getContentResolver().call(uri, "setIncognitoModeDisabled", null,
bundle); |
| 53 } |
| 54 |
| 55 private void assertIncognitoMenuItemEnabled(boolean enabled) throws Executio
nException { |
| 56 Menu menu = ThreadUtils.runOnUiThreadBlocking(new Callable<Menu>() { |
| 57 @Override |
| 58 public Menu call() throws Exception { |
| 59 // PopupMenu is a convenient way of building a temp menu. |
| 60 PopupMenu tempMenu = new PopupMenu( |
| 61 getActivity(), getActivity().findViewById(R.id.menu_anch
or_stub)); |
| 62 tempMenu.inflate(R.menu.main_menu); |
| 63 Menu menu = tempMenu.getMenu(); |
| 64 |
| 65 getActivity().prepareMenu(menu); |
| 66 return menu; |
| 67 } |
| 68 }); |
| 69 for (int i = 0; i < menu.size(); ++i) { |
| 70 MenuItem item = menu.getItem(i); |
| 71 if (item.getItemId() == R.id.new_incognito_tab_menu_id && item.isVis
ible()) { |
| 72 assertEquals("Menu item enabled state is not correct.", enabled,
item.isEnabled()); |
| 73 } |
| 74 } |
| 75 } |
| 76 |
| 77 private boolean waitForParentalControlsEnabledState(final boolean parentalCo
ntrolsEnabled) |
| 78 throws InterruptedException { |
| 79 return CriteriaHelper.pollForCriteria(new Criteria() { |
| 80 @Override |
| 81 public boolean isSatisfied() { |
| 82 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable
<Boolean>() { |
| 83 @Override |
| 84 public Boolean call() throws Exception { |
| 85 // areParentalControlsEnabled is updated on a background
thread, so we |
| 86 // also wait on the isIncognitoModeEnabled to ensure the
updates on the |
| 87 // UI thread have also triggered. |
| 88 boolean retVal = parentalControlsEnabled |
| 89 == PartnerBrowserCustomizations.isIncognitoDisab
led(); |
| 90 retVal &= parentalControlsEnabled |
| 91 != PrefServiceBridge.getInstance().isIncognitoMo
deEnabled(); |
| 92 return retVal; |
| 93 } |
| 94 }); |
| 95 } |
| 96 }); |
| 97 } |
| 98 |
| 99 private void toggleActivityForegroundState() { |
| 100 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 101 @Override |
| 102 public void run() { |
| 103 getActivity().onPause(); |
| 104 } |
| 105 }); |
| 106 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 107 @Override |
| 108 public void run() { |
| 109 getActivity().onStop(); |
| 110 } |
| 111 }); |
| 112 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 113 @Override |
| 114 public void run() { |
| 115 getActivity().onStart(); |
| 116 } |
| 117 }); |
| 118 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 119 @Override |
| 120 public void run() { |
| 121 getActivity().onResume(); |
| 122 } |
| 123 }); |
| 124 } |
| 125 |
| 126 @MediumTest |
| 127 @Feature({"DisableIncognitoMode"}) |
| 128 public void testIncognitoEnabledIfNoParentalControls() throws InterruptedExc
eption { |
| 129 setParentalControlsEnabled(false); |
| 130 startMainActivityOnBlankPage(); |
| 131 assertTrue(waitForParentalControlsEnabledState(false)); |
| 132 newIncognitoTabFromMenu(); |
| 133 } |
| 134 |
| 135 @MediumTest |
| 136 @Feature({"DisableIncognitoMode"}) |
| 137 public void testIncognitoMenuItemEnabledBasedOnParentalControls() |
| 138 throws InterruptedException, ExecutionException { |
| 139 setParentalControlsEnabled(true); |
| 140 startMainActivityOnBlankPage(); |
| 141 assertTrue(waitForParentalControlsEnabledState(true)); |
| 142 assertIncognitoMenuItemEnabled(false); |
| 143 |
| 144 setParentalControlsEnabled(false); |
| 145 toggleActivityForegroundState(); |
| 146 assertTrue(waitForParentalControlsEnabledState(false)); |
| 147 assertIncognitoMenuItemEnabled(true); |
| 148 } |
| 149 |
| 150 @MediumTest |
| 151 @Feature({"DisableIncognitoMode"}) |
| 152 public void testEnabledParentalControlsClosesIncognitoTabs() throws Interrup
tedException { |
| 153 setParentalControlsEnabled(false); |
| 154 startMainActivityOnBlankPage(); |
| 155 assertTrue(waitForParentalControlsEnabledState(false)); |
| 156 |
| 157 loadUrlInNewTab(TEST_URLS[0], true); |
| 158 loadUrlInNewTab(TEST_URLS[1], true); |
| 159 loadUrlInNewTab(TEST_URLS[2], true); |
| 160 loadUrlInNewTab(TEST_URLS[0], false); |
| 161 |
| 162 setParentalControlsEnabled(true); |
| 163 toggleActivityForegroundState(); |
| 164 assertTrue(waitForParentalControlsEnabledState(true)); |
| 165 |
| 166 assertTrue("Incognito tabs did not close as expected", |
| 167 CriteriaHelper.pollForCriteria(new Criteria() { |
| 168 @Override |
| 169 public boolean isSatisfied() { |
| 170 return incognitoTabsCount() == 0; |
| 171 } |
| 172 })); |
| 173 } |
| 174 } |
OLD | NEW |