OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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.media.ui; |
| 6 |
| 7 import android.os.Environment; |
| 8 import android.support.v7.widget.SwitchCompat; |
| 9 import android.test.suitebuilder.annotation.MediumTest; |
| 10 import android.test.suitebuilder.annotation.Smoke; |
| 11 |
| 12 import org.chromium.base.test.util.CommandLineFlags; |
| 13 import org.chromium.base.test.util.Feature; |
| 14 import org.chromium.chrome.R; |
| 15 import org.chromium.chrome.browser.ChromeActivity; |
| 16 import org.chromium.chrome.browser.ChromeSwitches; |
| 17 import org.chromium.chrome.browser.infobar.InfoBar; |
| 18 import org.chromium.chrome.browser.infobar.InfoBarContainer; |
| 19 import org.chromium.chrome.browser.tab.EmptyTabObserver; |
| 20 import org.chromium.chrome.browser.tab.Tab; |
| 21 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
| 22 import org.chromium.chrome.test.util.InfoBarTestAnimationListener; |
| 23 import org.chromium.chrome.test.util.InfoBarUtil; |
| 24 import org.chromium.content.browser.test.util.CallbackHelper; |
| 25 import org.chromium.content.browser.test.util.Criteria; |
| 26 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 27 import org.chromium.content.browser.test.util.TouchCommon; |
| 28 import org.chromium.net.test.EmbeddedTestServer; |
| 29 |
| 30 import java.util.concurrent.Callable; |
| 31 |
| 32 /** |
| 33 * Test suite for media permissions requests. |
| 34 */ |
| 35 public class MediaPermissionsTest extends ChromeActivityTestCaseBase<ChromeActiv
ity> { |
| 36 private static final String TEST_FILE = "/content/test/data/android/media_pe
rmissions.html"; |
| 37 |
| 38 private InfoBarTestAnimationListener mListener; |
| 39 private EmbeddedTestServer mTestServer; |
| 40 |
| 41 /** |
| 42 * Waits till the media JavaScript callback is called the specified number o
f times. |
| 43 */ |
| 44 private class MediaPermissionsUpdateWaiter extends EmptyTabObserver { |
| 45 private CallbackHelper mCallbackHelper; |
| 46 private int mExpectedCount; |
| 47 private String mPrefix; |
| 48 |
| 49 public MediaPermissionsUpdateWaiter(String prefix) { |
| 50 mCallbackHelper = new CallbackHelper(); |
| 51 mPrefix = prefix; |
| 52 } |
| 53 |
| 54 @Override |
| 55 public void onTitleUpdated(Tab tab) { |
| 56 String expectedTitle = mPrefix + mExpectedCount; |
| 57 if (getActivity().getActivityTab().getTitle().equals(expectedTitle))
{ |
| 58 mCallbackHelper.notifyCalled(); |
| 59 } |
| 60 } |
| 61 |
| 62 public void waitForNumUpdates(int numUpdates) throws Exception { |
| 63 mExpectedCount = numUpdates; |
| 64 mCallbackHelper.waitForCallback(0); |
| 65 } |
| 66 } |
| 67 |
| 68 public MediaPermissionsTest() { |
| 69 super(ChromeActivity.class); |
| 70 } |
| 71 |
| 72 @Override |
| 73 protected void setUp() throws Exception { |
| 74 super.setUp(); |
| 75 |
| 76 InfoBarContainer container = |
| 77 getActivity().getTabModelSelector().getCurrentTab().getInfoBarCo
ntainer(); |
| 78 mListener = new InfoBarTestAnimationListener(); |
| 79 container.setAnimationListener(mListener); |
| 80 |
| 81 mTestServer = EmbeddedTestServer.createAndStartFileServer( |
| 82 getInstrumentation().getContext(), Environment.getExternalStorag
eDirectory()); |
| 83 } |
| 84 |
| 85 @Override |
| 86 protected void tearDown() throws Exception { |
| 87 mTestServer.stopAndDestroyServer(); |
| 88 super.tearDown(); |
| 89 } |
| 90 |
| 91 /** |
| 92 * Verify asking for microphone creates an InfoBar and works when the permis
sion is granted. |
| 93 * @throws Exception |
| 94 */ |
| 95 @Smoke |
| 96 @MediumTest |
| 97 @Feature({"MediaPermissions", "Main"}) |
| 98 @CommandLineFlags.Add(ChromeSwitches.USE_FAKE_DEVICE_FOR_MEDIA_STREAM) |
| 99 public void testMicrophoneMediaPermissionsPlumbing() throws Exception { |
| 100 testMediaPermissionsPlumbing("Mic count:", "initiate_getMicrophone()", 1
, false, false); |
| 101 } |
| 102 |
| 103 /** |
| 104 * Verify asking for camera creates an InfoBar and works when the permission
is granted. |
| 105 * @throws Exception |
| 106 */ |
| 107 @MediumTest |
| 108 @Feature({"MediaPermissions", "Main"}) |
| 109 @CommandLineFlags.Add(ChromeSwitches.USE_FAKE_DEVICE_FOR_MEDIA_STREAM) |
| 110 public void testCameraMediaPermissionsPlumbing() throws Exception { |
| 111 testMediaPermissionsPlumbing("Camera count:", "initiate_getCamera()", 1,
false, false); |
| 112 } |
| 113 |
| 114 /** |
| 115 * Verify asking for both mic and camera creates a combined InfoBar and work
s when the |
| 116 * permissions are granted. |
| 117 * @throws Exception |
| 118 */ |
| 119 @MediumTest |
| 120 @Feature({"MediaPermissions", "Main"}) |
| 121 @CommandLineFlags.Add(ChromeSwitches.USE_FAKE_DEVICE_FOR_MEDIA_STREAM) |
| 122 public void testCombinedMediaPermissionsPlumbing() throws Exception { |
| 123 testMediaPermissionsPlumbing("Combined count:", "initiate_getCombined()"
, 1, false, false); |
| 124 } |
| 125 |
| 126 /** |
| 127 * Verify microphone prompts with a persistence toggle if that feature is en
abled. Check the |
| 128 * switch appears and that permission is granted with it toggled on. |
| 129 * @throws Exception |
| 130 */ |
| 131 @MediumTest |
| 132 @CommandLineFlags.Add("enable-features=DisplayPersistenceToggleInPermissionP
rompts") |
| 133 @Feature({"MediaPermissions"}) |
| 134 public void testMicrophonePersistenceOn() throws Exception { |
| 135 testMediaPermissionsPlumbing("Mic count:", "initiate_getMicrophone()", 1
, true, false); |
| 136 } |
| 137 |
| 138 /** |
| 139 * Verify microphone prompts with a persistence toggle if that feature is en
abled. Check the |
| 140 * switch appears and that permission is granted with it toggled off. |
| 141 * @throws Exception |
| 142 */ |
| 143 @MediumTest |
| 144 @CommandLineFlags.Add("enable-features=DisplayPersistenceToggleInPermissionP
rompts") |
| 145 @Feature({"MediaPermissions"}) |
| 146 public void testMicrophonePersistenceOff() throws Exception { |
| 147 testMediaPermissionsPlumbing("Mic count:", "initiate_getMicrophone()", 1
, true, true); |
| 148 } |
| 149 |
| 150 /** |
| 151 * Verify camera prompts with a persistence toggle if that feature is enable
d. Check the |
| 152 * switch appears and that permission is granted with it toggled on. |
| 153 * @throws Exception |
| 154 */ |
| 155 @MediumTest |
| 156 @CommandLineFlags.Add("enable-features=DisplayPersistenceToggleInPermissionP
rompts") |
| 157 @Feature({"MediaPermissions"}) |
| 158 public void testCameraPersistenceOn() throws Exception { |
| 159 testMediaPermissionsPlumbing("Camera count:", "initiate_getCamera()", 1,
true, false); |
| 160 } |
| 161 |
| 162 /** |
| 163 * Verify camera prompts with a persistence toggle if that feature is enable
d. Check the |
| 164 * switch appears and that permission is granted with it toggled off. |
| 165 * @throws Exception |
| 166 */ |
| 167 @MediumTest |
| 168 @CommandLineFlags.Add("enable-features=DisplayPersistenceToggleInPermissionP
rompts") |
| 169 @Feature({"MediaPermissions"}) |
| 170 public void testCameraPersistenceOff() throws Exception { |
| 171 testMediaPermissionsPlumbing("Camera count:", "initiate_getCamera()", 1,
true, true); |
| 172 } |
| 173 |
| 174 /** |
| 175 * Verify combined prompts show a persistence toggle if that feature is enab
led. Check the |
| 176 * switch appears and that permission is granted with it toggled on. |
| 177 * @throws Exception |
| 178 */ |
| 179 @MediumTest |
| 180 @CommandLineFlags.Add("enable-features=DisplayPersistenceToggleInPermissionP
rompts") |
| 181 @Feature({"MediaPermissions"}) |
| 182 public void testCombinedPersistenceOn() throws Exception { |
| 183 testMediaPermissionsPlumbing("Combined count:", "initiate_getCombined()"
, 1, true, false); |
| 184 } |
| 185 |
| 186 /** |
| 187 * Verify combined prompts show a persistence toggle if that feature is enab
led. Check the |
| 188 * switch appears and that permission is granted with it toggled of. |
| 189 * @throws Exception |
| 190 */ |
| 191 @MediumTest |
| 192 @CommandLineFlags.Add("enable-features=DisplayPersistenceToggleInPermissionP
rompts") |
| 193 @Feature({"MediaPermissions"}) |
| 194 public void testCombinedPersistenceOff() throws Exception { |
| 195 testMediaPermissionsPlumbing("Combined count:", "initiate_getCombined()"
, 1, true, true); |
| 196 } |
| 197 |
| 198 @Override |
| 199 public void startMainActivity() throws InterruptedException { |
| 200 startMainActivityOnBlankPage(); |
| 201 } |
| 202 |
| 203 private void testMediaPermissionsPlumbing(String prefix, String script, int
numUpdates, |
| 204 boolean expectsSwitch, boolean toggleSwitch) throws Exception { |
| 205 final String url = mTestServer.getURL(TEST_FILE); |
| 206 |
| 207 Tab tab = getActivity().getActivityTab(); |
| 208 MediaPermissionsUpdateWaiter updateWaiter = new MediaPermissionsUpdateWa
iter(prefix); |
| 209 tab.addObserver(updateWaiter); |
| 210 |
| 211 loadUrl(url); |
| 212 runJavaScriptCodeInCurrentTab(script); |
| 213 assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished()
); |
| 214 InfoBar infobar = getInfoBars().get(0); |
| 215 |
| 216 SwitchCompat persistSwitch = (SwitchCompat) infobar.getView().findViewBy
Id( |
| 217 R.id.permission_infobar_persist_toggle); |
| 218 if (expectsSwitch) { |
| 219 assertNotNull(persistSwitch); |
| 220 assertTrue(persistSwitch.isChecked()); |
| 221 if (toggleSwitch) { |
| 222 TouchCommon.singleClickView(persistSwitch); |
| 223 waitForCheckedState(persistSwitch, false); |
| 224 } |
| 225 } else { |
| 226 assertNull(persistSwitch); |
| 227 } |
| 228 |
| 229 assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(info
bar)); |
| 230 updateWaiter.waitForNumUpdates(numUpdates); |
| 231 |
| 232 tab.removeObserver(updateWaiter); |
| 233 } |
| 234 |
| 235 private void waitForCheckedState(final SwitchCompat persistSwitch, boolean i
sChecked) |
| 236 throws InterruptedException { |
| 237 CriteriaHelper.pollUiThread(Criteria.equals(isChecked, new Callable<Bool
ean>() { |
| 238 @Override |
| 239 public Boolean call() { |
| 240 return persistSwitch.isChecked(); |
| 241 } |
| 242 })); |
| 243 } |
| 244 } |
OLD | NEW |