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 final String test_url = mTestServer.getURL(url); | |
Bernhard Bauer
2017/05/25 09:57:17
Nit: The `final` doesn't do much here. Or maybe ju
Timothy Loh
2017/05/26 05:08:03
Done.
| |
143 loadUrl(test_url); | |
144 } | |
145 | |
139 /** | 146 /** |
140 * Simulates clicking a button on an AlertDialog. | 147 * Simulates clicking a button on an AlertDialog. |
141 */ | 148 */ |
142 private void clickButton(final AlertDialog dialog, final int button) { | 149 private void clickButton(final AlertDialog dialog, final int button) { |
143 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 150 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
144 @Override | 151 @Override |
145 public void run() { | 152 public void run() { |
146 dialog.getButton(button).performClick(); | 153 dialog.getButton(button).performClick(); |
147 } | 154 } |
148 }); | 155 }); |
149 } | 156 } |
150 | 157 |
151 /** | 158 /** |
152 * Runs a permission prompt test that grants the permission and expects the page title to be | 159 * Runs a permission prompt test that grants the permission and expects the page title to be |
153 * updated in response. | 160 * updated in response. |
154 * @param updateWaiter The update waiter to wait for callbacks. Should be a dded as an observer | 161 * @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. | 162 * 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 | 163 * @param javascript The JS function to run in the current tab to execute the test and update |
157 * the page title. | 164 * the page title. |
158 * @param nUpdates How many updates of the page title to wait for. | 165 * @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. | 166 * @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. | 167 * @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. | 168 * @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. | 169 * @param toggleSwitch True if we should toggle the switch off, false other wise. |
163 * @throws Exception | 170 * @throws Exception |
164 */ | 171 */ |
165 protected void runAllowTest(PermissionUpdateWaiter updateWaiter, final Strin g url, | 172 protected void runAllowTest(PermissionUpdateWaiter updateWaiter, final Strin g url, |
166 String javascript, int nUpdates, boolean withGesture, boolean isDial og, | 173 String javascript, int nUpdates, boolean withGesture, boolean isDial og, |
167 boolean hasSwitch, boolean toggleSwitch) throws Exception { | 174 boolean hasSwitch, boolean toggleSwitch) throws Exception { |
168 final String test_url = mTestServer.getURL(url); | 175 setUpUrl(url); |
169 loadUrl(test_url); | |
170 | 176 |
171 if (withGesture) { | 177 if (withGesture) { |
172 runJavaScriptCodeInCurrentTab("functionToRun = '" + javascript + "'" ); | 178 runJavaScriptCodeInCurrentTab("functionToRun = '" + javascript + "'" ); |
173 singleClickView(getActivity().getActivityTab().getView()); | 179 singleClickView(getActivity().getActivityTab().getView()); |
174 } else { | 180 } else { |
175 runJavaScriptCodeInCurrentTab(javascript); | 181 runJavaScriptCodeInCurrentTab(javascript); |
176 } | 182 } |
177 | 183 |
178 if (isDialog) { | 184 if (isDialog) { |
179 DialogShownCriteria criteria = new DialogShownCriteria("Dialog not s hown"); | 185 DialogShownCriteria criteria = new DialogShownCriteria("Dialog not s hown", true); |
180 CriteriaHelper.pollUiThread(criteria); | 186 CriteriaHelper.pollUiThread(criteria); |
181 replyToDialogAndWaitForUpdates( | 187 replyToDialogAndWaitForUpdates( |
182 updateWaiter, criteria.getDialog(), nUpdates, true, hasSwitc h, toggleSwitch); | 188 updateWaiter, criteria.getDialog(), nUpdates, true, hasSwitc h, toggleSwitch); |
183 } else { | 189 } else { |
184 replyToInfoBarAndWaitForUpdates(updateWaiter, nUpdates, true, hasSwi tch, toggleSwitch); | 190 replyToInfoBarAndWaitForUpdates(updateWaiter, nUpdates, true, hasSwi tch, toggleSwitch); |
185 } | 191 } |
186 } | 192 } |
187 | 193 |
188 /** | 194 /** |
189 * Replies to an infobar permission prompt, optionally checking for the pres ence of a | 195 * 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 } | 249 } |
244 }); | 250 }); |
245 } | 251 } |
246 } | 252 } |
247 | 253 |
248 @Override | 254 @Override |
249 public void startMainActivity() throws InterruptedException { | 255 public void startMainActivity() throws InterruptedException { |
250 startMainActivityOnBlankPage(); | 256 startMainActivityOnBlankPage(); |
251 } | 257 } |
252 } | 258 } |
OLD | NEW |