| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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.permissions; | 5 package org.chromium.chrome.browser.permissions; |
| 6 | 6 |
| 7 import android.content.DialogInterface; | 7 import android.content.DialogInterface; |
| 8 import android.support.v7.app.AlertDialog; | 8 import android.support.v7.app.AlertDialog; |
| 9 import android.support.v7.widget.SwitchCompat; | 9 import android.support.v7.widget.SwitchCompat; |
| 10 | 10 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 container.addAnimationListener(mListener); | 129 container.addAnimationListener(mListener); |
| 130 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation
().getContext()); | 130 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation
().getContext()); |
| 131 } | 131 } |
| 132 | 132 |
| 133 @Override | 133 @Override |
| 134 protected void tearDown() throws Exception { | 134 protected void tearDown() throws Exception { |
| 135 mTestServer.stopAndDestroyServer(); | 135 mTestServer.stopAndDestroyServer(); |
| 136 super.tearDown(); | 136 super.tearDown(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 protected void setUpUrl(final String url) throws InterruptedException { |
| 140 final String test_url = mTestServer.getURL(url); |
| 141 loadUrl(test_url); |
| 142 } |
| 143 |
| 144 /** |
| 145 * Checks permission status via the navigator.permissions.query API |
| 146 */ |
| 147 protected void checkPermissionStatus(final String permission, final String e
xpected) |
| 148 throws Exception { |
| 149 final CallbackHelper callbackHelper = new CallbackHelper(); |
| 150 EmptyTabObserver updateWaiter = new EmptyTabObserver() { |
| 151 @Override |
| 152 public void onTitleUpdated(Tab tab) { |
| 153 String title = getActivity().getActivityTab().getTitle(); |
| 154 String prefix = permission + " status: "; |
| 155 if (!title.startsWith(prefix)) return; |
| 156 String actual = title.substring(prefix.length()); |
| 157 assertEquals(expected, actual); |
| 158 callbackHelper.notifyCalled(); |
| 159 } |
| 160 }; |
| 161 |
| 162 Tab tab = getActivity().getActivityTab(); |
| 163 tab.addObserver(updateWaiter); |
| 164 runJavaScriptCodeInCurrentTab("navigator.permissions.query({ name: '" +
permission |
| 165 + "' }).then(({state}) => { window.document.title = '" + permiss
ion |
| 166 + " status: ' + state; });"); |
| 167 callbackHelper.waitForCallback(0); |
| 168 tab.removeObserver(updateWaiter); |
| 169 runJavaScriptCodeInCurrentTab("window.document.title = 'banana';"); |
| 170 } |
| 171 |
| 139 /** | 172 /** |
| 140 * Simulates clicking a button on an AlertDialog. | 173 * Simulates clicking a button on an AlertDialog. |
| 141 */ | 174 */ |
| 142 private void clickButton(final AlertDialog dialog, final int button) { | 175 private void clickButton(final AlertDialog dialog, final int button) { |
| 143 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 176 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 144 @Override | 177 @Override |
| 145 public void run() { | 178 public void run() { |
| 146 dialog.getButton(button).performClick(); | 179 dialog.getButton(button).performClick(); |
| 147 } | 180 } |
| 148 }); | 181 }); |
| 149 } | 182 } |
| 150 | 183 |
| 151 /** | 184 /** |
| 185 * Waits for an infobar to have been added since to the last call to this fu
nction. |
| 186 */ |
| 187 protected void waitForInfoBar() throws Exception { |
| 188 mListener.addInfoBarAnimationFinished("InfoBar not added."); |
| 189 } |
| 190 |
| 191 /** |
| 152 * Runs a permission prompt test that grants the permission and expects the
page title to be | 192 * Runs a permission prompt test that grants the permission and expects the
page title to be |
| 153 * updated in response. | 193 * updated in response. |
| 154 * @param updateWaiter The update waiter to wait for callbacks. Should be a
dded as an observer | 194 * @param updateWaiter The update waiter to wait for callbacks. Should be a
dded as an observer |
| 155 * to the current tab prior to calling this method. | 195 * to the current tab prior to calling this method. |
| 156 * @param javascript The JS function to run in the current tab to execute
the test and update | 196 * @param javascript The JS function to run in the current tab to execute
the test and update |
| 157 * the page title. | 197 * the page title. |
| 158 * @param nUpdates How many updates of the page title to wait for. | 198 * @param nUpdates How many updates of the page title to wait for. |
| 159 * @param withGeature True if we require a user gesture to trigger the pro
mpt. | 199 * @param withGeature True if we require a user gesture to trigger the pro
mpt. |
| 160 * @param isDialog True if we are expecting a permission dialog, false
for an infobar. | 200 * @param isDialog True if we are expecting a permission dialog, false
for an infobar. |
| 161 * @param hasSwitch True if we are expecting a persistence switch, false
otherwise. | 201 * @param hasSwitch True if we are expecting a persistence switch, false
otherwise. |
| 162 * @param toggleSwitch True if we should toggle the switch off, false other
wise. | 202 * @param toggleSwitch True if we should toggle the switch off, false other
wise. |
| 163 * @throws Exception | 203 * @throws Exception |
| 164 */ | 204 */ |
| 165 protected void runAllowTest(PermissionUpdateWaiter updateWaiter, final Strin
g url, | 205 protected void runAllowTest(PermissionUpdateWaiter updateWaiter, final Strin
g url, |
| 166 String javascript, int nUpdates, boolean withGesture, boolean isDial
og, | 206 String javascript, int nUpdates, boolean withGesture, boolean isDial
og, |
| 167 boolean hasSwitch, boolean toggleSwitch) throws Exception { | 207 boolean hasSwitch, boolean toggleSwitch) throws Exception { |
| 168 final String test_url = mTestServer.getURL(url); | 208 setUpUrl(url); |
| 169 loadUrl(test_url); | |
| 170 | 209 |
| 171 if (withGesture) { | 210 if (withGesture) { |
| 172 runJavaScriptCodeInCurrentTab("functionToRun = '" + javascript + "'"
); | 211 runJavaScriptCodeInCurrentTab("functionToRun = '" + javascript + "'"
); |
| 173 singleClickView(getActivity().getActivityTab().getView()); | 212 singleClickView(getActivity().getActivityTab().getView()); |
| 174 } else { | 213 } else { |
| 175 runJavaScriptCodeInCurrentTab(javascript); | 214 runJavaScriptCodeInCurrentTab(javascript); |
| 176 } | 215 } |
| 177 | 216 |
| 178 if (isDialog) { | 217 if (isDialog) { |
| 179 DialogShownCriteria criteria = new DialogShownCriteria("Dialog not s
hown"); | 218 DialogShownCriteria criteria = new DialogShownCriteria("Dialog not s
hown"); |
| 180 CriteriaHelper.pollUiThread(criteria); | 219 CriteriaHelper.pollUiThread(criteria); |
| 181 replyToDialogAndWaitForUpdates( | 220 replyToDialog(criteria.getDialog(), true, hasSwitch, toggleSwitch); |
| 182 updateWaiter, criteria.getDialog(), nUpdates, true, hasSwitc
h, toggleSwitch); | |
| 183 } else { | 221 } else { |
| 184 replyToInfoBarAndWaitForUpdates(updateWaiter, nUpdates, true, hasSwi
tch, toggleSwitch); | 222 waitForInfoBar(); |
| 223 replyToInfoBar(true, hasSwitch, toggleSwitch); |
| 185 } | 224 } |
| 225 updateWaiter.waitForNumUpdates(nUpdates); |
| 186 } | 226 } |
| 187 | 227 |
| 188 /** | 228 /** |
| 189 * Replies to an infobar permission prompt, optionally checking for the pres
ence of a | 229 * Replies to an infobar permission prompt, optionally checking for the pres
ence of a |
| 190 * persistence switch and toggling it. Waits for a provided number of update
s to the page title | 230 * persistence switch and toggling it. |
| 191 * in response. | |
| 192 */ | 231 */ |
| 193 private void replyToInfoBarAndWaitForUpdates(PermissionUpdateWaiter updateWa
iter, int nUpdates, | 232 protected void replyToInfoBar(boolean allow, boolean hasSwitch, boolean togg
leSwitch) |
| 194 boolean allow, boolean hasSwitch, boolean toggleSwitch) throws Excep
tion { | 233 throws Exception { |
| 195 mListener.addInfoBarAnimationFinished("InfoBar not added."); | |
| 196 InfoBar infobar = getInfoBars().get(0); | 234 InfoBar infobar = getInfoBars().get(0); |
| 197 assertNotNull(infobar); | 235 assertNotNull(infobar); |
| 198 | 236 |
| 199 if (hasSwitch) { | 237 if (hasSwitch) { |
| 200 SwitchCompat persistSwitch = (SwitchCompat) infobar.getView().findVi
ewById( | 238 SwitchCompat persistSwitch = (SwitchCompat) infobar.getView().findVi
ewById( |
| 201 R.id.permission_infobar_persist_toggle); | 239 R.id.permission_infobar_persist_toggle); |
| 202 checkAndToggleSwitch(persistSwitch, toggleSwitch); | 240 checkAndToggleSwitch(persistSwitch, toggleSwitch); |
| 203 } | 241 } |
| 204 | 242 |
| 205 if (allow) { | 243 if (allow) { |
| 206 assertTrue("Allow button wasn't found", InfoBarUtil.clickPrimaryButt
on(infobar)); | 244 assertTrue("Allow button wasn't found", InfoBarUtil.clickPrimaryButt
on(infobar)); |
| 207 } else { | 245 } else { |
| 208 assertTrue("Block button wasn't found", InfoBarUtil.clickSecondaryBu
tton(infobar)); | 246 assertTrue("Block button wasn't found", InfoBarUtil.clickSecondaryBu
tton(infobar)); |
| 209 } | 247 } |
| 210 updateWaiter.waitForNumUpdates(nUpdates); | |
| 211 } | 248 } |
| 212 | 249 |
| 213 /** | 250 /** |
| 214 * Replies to a dialog permission prompt, optionally checking for the presen
ce of a | 251 * Replies to a dialog permission prompt, optionally checking for the presen
ce of a |
| 215 * persistence switch and toggling it. Waits for a provided number of update
s to the page title | 252 * persistence switch and toggling it. |
| 216 * in response. | |
| 217 */ | 253 */ |
| 218 private void replyToDialogAndWaitForUpdates(PermissionUpdateWaiter updateWai
ter, | 254 private void replyToDialog(AlertDialog dialog, boolean allow, boolean hasSwi
tch, |
| 219 AlertDialog dialog, int nUpdates, boolean allow, boolean hasSwitch, | |
| 220 boolean toggleSwitch) throws Exception { | 255 boolean toggleSwitch) throws Exception { |
| 221 if (hasSwitch) { | 256 if (hasSwitch) { |
| 222 SwitchCompat persistSwitch = | 257 SwitchCompat persistSwitch = |
| 223 (SwitchCompat) dialog.findViewById(R.id.permission_dialog_pe
rsist_toggle); | 258 (SwitchCompat) dialog.findViewById(R.id.permission_dialog_pe
rsist_toggle); |
| 224 checkAndToggleSwitch(persistSwitch, toggleSwitch); | 259 checkAndToggleSwitch(persistSwitch, toggleSwitch); |
| 225 } | 260 } |
| 226 | 261 |
| 227 if (allow) { | 262 if (allow) { |
| 228 clickButton(dialog, DialogInterface.BUTTON_POSITIVE); | 263 clickButton(dialog, DialogInterface.BUTTON_POSITIVE); |
| 229 } else { | 264 } else { |
| 230 clickButton(dialog, DialogInterface.BUTTON_NEGATIVE); | 265 clickButton(dialog, DialogInterface.BUTTON_NEGATIVE); |
| 231 } | 266 } |
| 232 updateWaiter.waitForNumUpdates(nUpdates); | |
| 233 } | 267 } |
| 234 | 268 |
| 235 private void checkAndToggleSwitch(final SwitchCompat persistSwitch, boolean
toggleSwitch) { | 269 private void checkAndToggleSwitch(final SwitchCompat persistSwitch, boolean
toggleSwitch) { |
| 236 assertNotNull(persistSwitch); | 270 assertNotNull(persistSwitch); |
| 237 assertTrue(persistSwitch.isChecked()); | 271 assertTrue(persistSwitch.isChecked()); |
| 238 if (toggleSwitch) { | 272 if (toggleSwitch) { |
| 239 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 273 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 240 @Override | 274 @Override |
| 241 public void run() { | 275 public void run() { |
| 242 persistSwitch.toggle(); | 276 persistSwitch.toggle(); |
| 243 } | 277 } |
| 244 }); | 278 }); |
| 245 } | 279 } |
| 246 } | 280 } |
| 247 | 281 |
| 248 @Override | 282 @Override |
| 249 public void startMainActivity() throws InterruptedException { | 283 public void startMainActivity() throws InterruptedException { |
| 250 startMainActivityOnBlankPage(); | 284 startMainActivityOnBlankPage(); |
| 251 } | 285 } |
| 252 } | 286 } |
| OLD | NEW |