/
permission_request_chip_browsertest.cc
404 lines (336 loc) · 15.6 KB
/
permission_request_chip_browsertest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#include "chrome/browser/ui/views/permissions/chip_controller.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/permissions/permission_request_manager_test_api.h"
#include "components/metrics/content/subprocess_metrics_provider.h"
#include "components/omnibox/browser/omnibox_edit_model.h"
#include "components/omnibox/browser/omnibox_view.h"
#include "components/omnibox/browser/open_tab_provider.h"
#include "components/permissions/features.h"
#include "components/permissions/test/permission_request_observer.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "ui/gfx/animation/animation_test_api.h"
namespace {
void RequestPermission(Browser* browser) {
test::PermissionRequestManagerTestApi test_api(browser);
permissions::PermissionRequestObserver observer(
browser->tab_strip_model()->GetActiveWebContents());
EXPECT_NE(nullptr, test_api.manager());
test_api.AddSimpleRequest(
browser->tab_strip_model()->GetActiveWebContents()->GetPrimaryMainFrame(),
permissions::RequestType::kGeolocation);
observer.Wait();
}
LocationBarView* GetLocationBarView(Browser* browser) {
return BrowserView::GetBrowserViewForBrowser(browser)
->toolbar()
->location_bar();
}
} // namespace
class PermissionRequestChipGestureSensitiveBrowserTest
: public InProcessBrowserTest {
public:
void SetUp() override {
feature_list_.InitWithFeatures(
{permissions::features::kPermissionChip,
permissions::features::kPermissionChipGestureSensitive,
permissions::features::kPermissionChipRequestTypeSensitive},
{});
InProcessBrowserTest::SetUp();
}
private:
base::test::ScopedFeatureList feature_list_;
};
IN_PROC_BROWSER_TEST_F(PermissionRequestChipGestureSensitiveBrowserTest,
ChipFinalizedWhenInteractingWithOmnibox) {
RequestPermission(browser());
LocationBarView* lbv = GetLocationBarView(browser());
auto* animation = lbv->chip_controller()->chip()->animation_for_testing();
// Animate the chip expand.
gfx::AnimationTestApi animation_api(animation);
base::TimeTicks now = base::TimeTicks::Now();
animation_api.SetStartTime(now);
animation_api.Step(now + animation->GetSlideDuration());
// After animation ended, the chip is expanded and the bubble is shown because
// the gesture sensitive request feature is enabled.
EXPECT_TRUE(lbv->chip_controller()->IsPermissionPromptChipVisible());
EXPECT_TRUE(lbv->chip_controller()->IsBubbleShowing());
// Because the bubble is shown, callback timers should be abandoned
EXPECT_FALSE(lbv->chip_controller()->is_collapse_timer_running_for_testing());
EXPECT_FALSE(lbv->chip_controller()->is_dismiss_timer_running_for_testing());
// Type something in the omnibox.
auto* omnibox_view = lbv->GetOmniboxView();
omnibox_view->SetUserText(u"search query");
omnibox_view->model()->SetInputInProgress(true);
base::RunLoop().RunUntilIdle();
// While the user is interacting with the omnibox, the chip is hidden, the
// location icon isn't offset by the chip and the bubble is hidden.
EXPECT_FALSE(lbv->chip_controller()->IsPermissionPromptChipVisible());
EXPECT_FALSE(lbv->chip_controller()->IsBubbleShowing());
EXPECT_EQ(lbv->location_icon_view()->bounds().x(),
GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING));
// Ensure no callbacks are pending.
EXPECT_FALSE(lbv->chip_controller()->is_collapse_timer_running_for_testing());
EXPECT_FALSE(lbv->chip_controller()->is_dismiss_timer_running_for_testing());
}
IN_PROC_BROWSER_TEST_F(PermissionRequestChipGestureSensitiveBrowserTest,
ChipIsNotShownWhenInteractingWithOmnibox) {
LocationBarView* lbv = GetLocationBarView(browser());
// The chip is not shown because there is no active permission request.
EXPECT_FALSE(lbv->chip_controller()->IsPermissionPromptChipVisible());
// Type something in the omnibox.
auto* omnibox_view = lbv->GetOmniboxView();
omnibox_view->SetUserText(u"search query");
omnibox_view->model()->SetInputInProgress(true);
RequestPermission(browser());
// While the user is interacting with the omnibox, an incoming permission
// request will be automatically ignored. The chip is not shown.
EXPECT_FALSE(lbv->chip_controller()->IsPermissionPromptChipVisible());
}
// This is an end-to-end test that verifies that a permission prompt bubble will
// not be shown because of the empty address bar. Under the normal conditions
// such a test should be placed in PermissionsSecurityModelInteractiveUITest but
// due to dependency issues (see crbug.com/1112591) `//chrome/browser` is not
// allowed to have dependencies on `//chrome/browser/ui/views/*`.
IN_PROC_BROWSER_TEST_F(PermissionRequestChipGestureSensitiveBrowserTest,
PermissionRequestIsAutoIgnored) {
ASSERT_TRUE(embedded_test_server()->Start());
base::HistogramTester histograms;
content::WebContents* embedder_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(embedder_contents);
const GURL url(embedded_test_server()->GetURL("/empty.html"));
content::RenderFrameHost* main_rfh =
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url,
1);
content::WebContents::FromRenderFrameHost(main_rfh)->Focus();
ASSERT_TRUE(main_rfh);
constexpr char kCheckMicrophone[] = R"(
new Promise(async resolve => {
const PermissionStatus =
await navigator.permissions.query({name: 'microphone'});
resolve(PermissionStatus.state === 'granted');
})
)";
constexpr char kRequestMicrophone[] = R"(
new Promise(async resolve => {
var constraints = { audio: true };
window.focus();
try {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
resolve('granted');
} catch(error) {
resolve('denied')
}
})
)";
EXPECT_FALSE(content::EvalJs(main_rfh, kCheckMicrophone,
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS, 1)
.value.GetBool());
LocationBarView* location_bar =
BrowserView::GetBrowserViewForBrowser(browser())
->toolbar()
->location_bar();
// Type something in the omnibox.
OmniboxView* omnibox_view = location_bar->GetOmniboxView();
omnibox_view->SetUserText(u"search query");
omnibox_view->model()->SetInputInProgress(true);
auto* manager =
permissions::PermissionRequestManager::FromWebContents(embedder_contents);
permissions::PermissionRequestObserver observer(embedder_contents);
EXPECT_FALSE(manager->IsRequestInProgress());
EXPECT_TRUE(content::ExecJs(
main_rfh, kRequestMicrophone,
content::EvalJsOptions::EXECUTE_SCRIPT_NO_RESOLVE_PROMISES));
// Wait until a permission request is shown or finalized.
observer.Wait();
// Permission request was is in progress without showing a prompt bubble.
EXPECT_TRUE(manager->IsRequestInProgress());
EXPECT_FALSE(observer.request_shown());
EXPECT_TRUE(observer.is_view_recreate_failed());
EXPECT_FALSE(manager->view_for_testing());
EXPECT_FALSE(content::EvalJs(main_rfh, kCheckMicrophone,
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS, 1)
.value.GetBool());
metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting();
histograms.ExpectBucketCount(
"Permissions.Prompt.AudioCapture.Gesture.Attempt", true, 1);
}
IN_PROC_BROWSER_TEST_F(PermissionRequestChipGestureSensitiveBrowserTest,
ShouldUpdateActiverPRMAndObservations) {
constexpr char kRequestNotifications[] = R"(
new Promise(resolve => {
Notification.requestPermission().then(function (permission) {
resolve(permission)
});
})
)";
ASSERT_TRUE(embedded_test_server()->Start());
const GURL url(embedded_test_server()->GetURL("/empty.html"));
// Setup: open 2 tabs at the same origin
TabStripModel* tab_strip = browser()->tab_strip_model();
content::WebContents* embedder_contents_tab_0 =
tab_strip->GetActiveWebContents();
ASSERT_TRUE(embedder_contents_tab_0);
content::RenderFrameHost* rfh_tab_0 =
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url,
1);
content::WebContents::FromRenderFrameHost(rfh_tab_0)->Focus();
embedder_contents_tab_0 = tab_strip->GetActiveWebContents();
auto* manager_tab_0 = permissions::PermissionRequestManager::FromWebContents(
embedder_contents_tab_0);
permissions::PermissionRequestObserver observer_tab_0(
embedder_contents_tab_0);
chrome::NewTabToRight(browser());
EXPECT_EQ(2, tab_strip->count());
tab_strip->ActivateTabAt(1);
content::RenderFrameHost* rfh_tab_1 =
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url,
1);
content::WebContents* embedder_contents_tab_1 =
browser()->tab_strip_model()->GetActiveWebContents();
auto* manager_tab_1 = permissions::PermissionRequestManager::FromWebContents(
embedder_contents_tab_1);
permissions::PermissionRequestObserver observer_tab_1(
embedder_contents_tab_1);
tab_strip->ActivateTabAt(0);
// Obtain the chip controller instance. The chip controller instance is tied
// to the location bar view instance. Since the location bar view instance is
// reused across multiple tabs, this in turn also means that the chip
// controller instance is the same across multiple tabs.
LocationBarView* location_bar =
BrowserView::GetBrowserViewForBrowser(browser())->GetLocationBarView();
ASSERT_TRUE(location_bar);
ChipController* chip_controller = location_bar->chip_controller();
// Trigger permission request on first tab
EXPECT_FALSE(manager_tab_0->IsRequestInProgress());
EXPECT_TRUE(content::ExecJs(
rfh_tab_0, kRequestNotifications,
content::EvalJsOptions::EXECUTE_SCRIPT_NO_RESOLVE_PROMISES));
observer_tab_0.Wait();
EXPECT_TRUE(manager_tab_0->IsRequestInProgress());
// Close first tab
tab_strip->CloseWebContentsAt(0, TabCloseTypes::CLOSE_USER_GESTURE);
EXPECT_FALSE(manager_tab_1->IsRequestInProgress());
// After closing the first tab, the chip controller should no longer be
// observing any permission request manager. It should also no longer hold a
// reference to a Permission Request Manager instance.
ASSERT_FALSE(chip_controller->IsInObserverList());
ASSERT_FALSE(chip_controller->active_permission_request_manager_for_testing()
.has_value());
// Trigger a request on the second (the now only remaining) tab.
EXPECT_TRUE(content::ExecJs(
rfh_tab_1, kRequestNotifications,
content::EvalJsOptions::EXECUTE_SCRIPT_NO_RESOLVE_PROMISES));
observer_tab_1.Wait();
// During the request, the chip controller should be observing the correct
// permission request manager instance, and have a reference to the same.
EXPECT_TRUE(manager_tab_1->IsRequestInProgress());
ASSERT_TRUE(chip_controller->active_permission_request_manager_for_testing()
.has_value());
ASSERT_EQ(
chip_controller->active_permission_request_manager_for_testing().value(),
manager_tab_1);
ASSERT_TRUE(manager_tab_1->get_observer_list_for_testing()->HasObserver(
chip_controller));
}
class PermissionRequestChipGestureInsensitiveBrowserTest
: public InProcessBrowserTest {
public:
void SetUp() override {
feature_list_.InitWithFeatures(
{permissions::features::kPermissionChip,
permissions::features::kPermissionChipRequestTypeSensitive},
{});
InProcessBrowserTest::SetUp();
}
private:
base::test::ScopedFeatureList feature_list_;
};
IN_PROC_BROWSER_TEST_F(PermissionRequestChipGestureInsensitiveBrowserTest,
CallbacksResetWhenInteractingWithOmnibox) {
RequestPermission(browser());
LocationBarView* lbv = GetLocationBarView(browser());
auto* animation = lbv->chip_controller()->chip()->animation_for_testing();
// Animate the chip expand.
gfx::AnimationTestApi animation_api(animation);
base::TimeTicks now = base::TimeTicks::Now();
animation_api.SetStartTime(now);
animation_api.Step(now + animation->GetSlideDuration());
// After animation ended, the chip is expanded and a bubble is shown.
EXPECT_TRUE(lbv->chip_controller()->IsPermissionPromptChipVisible());
EXPECT_TRUE(lbv->chip_controller()->IsBubbleShowing());
// Because a bubble is shown, the collapse callback timer should not be
// running.
EXPECT_FALSE(lbv->chip_controller()->is_collapse_timer_running_for_testing());
EXPECT_FALSE(lbv->chip_controller()->is_dismiss_timer_running_for_testing());
// Type something in the omnibox.
auto* omnibox_view = lbv->GetOmniboxView();
omnibox_view->SetUserText(u"search query");
omnibox_view->model()->SetInputInProgress(true);
base::RunLoop().RunUntilIdle();
// Ensure chip is no longer visible and callbacks are no longer running.
EXPECT_FALSE(lbv->chip_controller()->IsPermissionPromptChipVisible());
EXPECT_FALSE(lbv->chip_controller()->is_collapse_timer_running_for_testing());
EXPECT_FALSE(lbv->chip_controller()->is_dismiss_timer_running_for_testing());
}
class PermissionRequestChipDialogBrowserTest : public UiBrowserTest {
public:
PermissionRequestChipDialogBrowserTest() {
feature_list_.InitAndEnableFeature(permissions::features::kPermissionChip);
}
PermissionRequestChipDialogBrowserTest(
const PermissionRequestChipDialogBrowserTest&) = delete;
PermissionRequestChipDialogBrowserTest& operator=(
const PermissionRequestChipDialogBrowserTest&) = delete;
// UiBrowserTest:
void ShowUi(const std::string& name) override {
RequestPermission(browser());
LocationBarView* lbv = GetLocationBarView(browser());
lbv->GetFocusManager()->ClearFocus();
lbv->chip_controller()->chip()->SetForceExpandedForTesting(true);
}
bool VerifyUi() override {
LocationBarView* lbv = GetLocationBarView(browser());
OmniboxChipButton* chip = lbv->chip_controller()->chip();
if (!chip)
return false;
// TODO(olesiamrukhno): VerifyPixelUi works only for these platforms, revise
// this if supported platforms change.
#if BUILDFLAG(IS_WIN) || (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS))
auto* test_info = testing::UnitTest::GetInstance()->current_test_info();
const std::string screenshot_name =
base::StrCat({test_info->test_case_name(), "_", test_info->name()});
return VerifyPixelUi(chip, "BrowserUi", screenshot_name);
#else
return true;
#endif
}
void WaitForUserDismissal() override {
// Consider closing the browser to be dismissal.
ui_test_utils::WaitForBrowserToClose();
}
private:
base::test::ScopedFeatureList feature_list_;
};
// Temporarily disabled per https://crbug.com/1197280
IN_PROC_BROWSER_TEST_F(PermissionRequestChipDialogBrowserTest,
DISABLED_InvokeUi_geolocation) {
ShowAndVerifyUi();
}