| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 | 81 |
| 82 public void waitForNumUpdates(int numUpdates) throws Exception { | 82 public void waitForNumUpdates(int numUpdates) throws Exception { |
| 83 mExpectedCount = numUpdates; | 83 mExpectedCount = numUpdates; |
| 84 mCallbackHelper.waitForCallback(0); | 84 mCallbackHelper.waitForCallback(0); |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 /** | 88 /** |
| 89 * Criteria class to detect whether the permission dialog is shown. | 89 * Criteria class to detect whether the permission dialog is shown. |
| 90 */ | 90 */ |
| 91 private static class DialogShownCriteria extends Criteria { | 91 protected static class DialogShownCriteria extends Criteria { |
| 92 private AlertDialog mDialog; | 92 private AlertDialog mDialog; |
| 93 private boolean mExpectDialog; |
| 93 | 94 |
| 94 public DialogShownCriteria(String error) { | 95 public DialogShownCriteria(String error, boolean expectDialog) { |
| 95 super(error); | 96 super(error); |
| 97 mExpectDialog = expectDialog; |
| 96 } | 98 } |
| 97 | 99 |
| 98 public AlertDialog getDialog() { | 100 public AlertDialog getDialog() { |
| 99 return mDialog; | 101 return mDialog; |
| 100 } | 102 } |
| 101 | 103 |
| 102 @Override | 104 @Override |
| 103 public boolean isSatisfied() { | 105 public boolean isSatisfied() { |
| 104 try { | 106 try { |
| 105 return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>()
{ | 107 return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>()
{ |
| 106 @Override | 108 @Override |
| 107 public Boolean call() { | 109 public Boolean call() { |
| 108 mDialog = PermissionDialogController.getInstance() | 110 mDialog = PermissionDialogController.getInstance() |
| 109 .getCurrentDialogForTesting(); | 111 .getCurrentDialogForTesting(); |
| 110 return mDialog != null; | 112 return (mDialog != null) == mExpectDialog; |
| 111 } | 113 } |
| 112 }); | 114 }); |
| 113 } catch (ExecutionException e) { | 115 } catch (ExecutionException e) { |
| 114 return false; | 116 return false; |
| 115 } | 117 } |
| 116 } | 118 } |
| 117 } | 119 } |
| 118 | 120 |
| 119 public PermissionTestCaseBase() { | 121 public PermissionTestCaseBase() { |
| 120 super(ChromeActivity.class); | 122 super(ChromeActivity.class); |
| 121 } | 123 } |
| 122 | 124 |
| 123 @Override | 125 @Override |
| 124 protected void setUp() throws Exception { | 126 protected void setUp() throws Exception { |
| 125 super.setUp(); | 127 super.setUp(); |
| 126 InfoBarContainer container = | 128 InfoBarContainer container = |
| 127 getActivity().getTabModelSelector().getCurrentTab().getInfoBarCo
ntainer(); | 129 getActivity().getTabModelSelector().getCurrentTab().getInfoBarCo
ntainer(); |
| 128 mListener = new InfoBarTestAnimationListener(); | 130 mListener = new InfoBarTestAnimationListener(); |
| 129 container.addAnimationListener(mListener); | 131 container.addAnimationListener(mListener); |
| 130 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation
().getContext()); | 132 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation
().getContext()); |
| 131 } | 133 } |
| 132 | 134 |
| 133 @Override | 135 @Override |
| 134 protected void tearDown() throws Exception { | 136 protected void tearDown() throws Exception { |
| 135 mTestServer.stopAndDestroyServer(); | 137 mTestServer.stopAndDestroyServer(); |
| 136 super.tearDown(); | 138 super.tearDown(); |
| 137 } | 139 } |
| 138 | 140 |
| 141 protected void setUpUrl(final String url) throws InterruptedException { |
| 142 loadUrl(mTestServer.getURL(url)); |
| 143 } |
| 144 |
| 139 /** | 145 /** |
| 140 * Simulates clicking a button on an AlertDialog. | 146 * Simulates clicking a button on an AlertDialog. |
| 141 */ | 147 */ |
| 142 private void clickButton(final AlertDialog dialog, final int button) { | 148 private void clickButton(final AlertDialog dialog, final int button) { |
| 143 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 149 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 144 @Override | 150 @Override |
| 145 public void run() { | 151 public void run() { |
| 146 dialog.getButton(button).performClick(); | 152 dialog.getButton(button).performClick(); |
| 147 } | 153 } |
| 148 }); | 154 }); |
| 149 } | 155 } |
| 150 | 156 |
| 151 /** | 157 /** |
| 152 * Runs a permission prompt test that grants the permission and expects the
page title to be | 158 * Runs a permission prompt test that grants the permission and expects the
page title to be |
| 153 * updated in response. | 159 * updated in response. |
| 154 * @param updateWaiter The update waiter to wait for callbacks. Should be a
dded as an observer | 160 * @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. | 161 * 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 | 162 * @param javascript The JS function to run in the current tab to execute
the test and update |
| 157 * the page title. | 163 * the page title. |
| 158 * @param nUpdates How many updates of the page title to wait for. | 164 * @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. | 165 * @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. | 166 * @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. | 167 * @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. | 168 * @param toggleSwitch True if we should toggle the switch off, false other
wise. |
| 163 * @throws Exception | 169 * @throws Exception |
| 164 */ | 170 */ |
| 165 protected void runAllowTest(PermissionUpdateWaiter updateWaiter, final Strin
g url, | 171 protected void runAllowTest(PermissionUpdateWaiter updateWaiter, final Strin
g url, |
| 166 String javascript, int nUpdates, boolean withGesture, boolean isDial
og, | 172 String javascript, int nUpdates, boolean withGesture, boolean isDial
og, |
| 167 boolean hasSwitch, boolean toggleSwitch) throws Exception { | 173 boolean hasSwitch, boolean toggleSwitch) throws Exception { |
| 168 final String test_url = mTestServer.getURL(url); | 174 setUpUrl(url); |
| 169 loadUrl(test_url); | |
| 170 | 175 |
| 171 if (withGesture) { | 176 if (withGesture) { |
| 172 runJavaScriptCodeInCurrentTab("functionToRun = '" + javascript + "'"
); | 177 runJavaScriptCodeInCurrentTab("functionToRun = '" + javascript + "'"
); |
| 173 singleClickView(getActivity().getActivityTab().getView()); | 178 singleClickView(getActivity().getActivityTab().getView()); |
| 174 } else { | 179 } else { |
| 175 runJavaScriptCodeInCurrentTab(javascript); | 180 runJavaScriptCodeInCurrentTab(javascript); |
| 176 } | 181 } |
| 177 | 182 |
| 178 if (isDialog) { | 183 if (isDialog) { |
| 179 DialogShownCriteria criteria = new DialogShownCriteria("Dialog not s
hown"); | 184 DialogShownCriteria criteria = new DialogShownCriteria("Dialog not s
hown", true); |
| 180 CriteriaHelper.pollUiThread(criteria); | 185 CriteriaHelper.pollUiThread(criteria); |
| 181 replyToDialogAndWaitForUpdates( | 186 replyToDialogAndWaitForUpdates( |
| 182 updateWaiter, criteria.getDialog(), nUpdates, true, hasSwitc
h, toggleSwitch); | 187 updateWaiter, criteria.getDialog(), nUpdates, true, hasSwitc
h, toggleSwitch); |
| 183 } else { | 188 } else { |
| 184 replyToInfoBarAndWaitForUpdates(updateWaiter, nUpdates, true, hasSwi
tch, toggleSwitch); | 189 replyToInfoBarAndWaitForUpdates(updateWaiter, nUpdates, true, hasSwi
tch, toggleSwitch); |
| 185 } | 190 } |
| 186 } | 191 } |
| 187 | 192 |
| 188 /** | 193 /** |
| 189 * Replies to an infobar permission prompt, optionally checking for the pres
ence of a | 194 * Replies to an infobar permission prompt, optionally checking for the pres
ence of a |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 } | 248 } |
| 244 }); | 249 }); |
| 245 } | 250 } |
| 246 } | 251 } |
| 247 | 252 |
| 248 @Override | 253 @Override |
| 249 public void startMainActivity() throws InterruptedException { | 254 public void startMainActivity() throws InterruptedException { |
| 250 startMainActivityOnBlankPage(); | 255 startMainActivityOnBlankPage(); |
| 251 } | 256 } |
| 252 } | 257 } |
| OLD | NEW |