File: intent_picker_bubble_view_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (728 lines) | stat: -rw-r--r-- 28,088 bytes parent folder | download | duplicates (4)
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/intent_picker_bubble_view.h"

#include <string>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "build/build_config.h"
#include "chrome/browser/apps/link_capturing/link_capturing_feature_test_support.h"
#include "chrome/browser/ui/actions/chrome_action_id.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/intent_picker_tab_helper.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.h"
#include "chrome/browser/ui/views/location_bar/intent_chip_button.h"
#include "chrome/browser/ui/views/location_bar/intent_chip_button_test_base.h"
#include "chrome/browser/ui/views/page_action/page_action_icon_view.h"
#include "chrome/browser/ui/views/page_action/page_action_view.h"
#include "chrome/browser/ui/views/web_apps/web_app_link_capturing_test_utils.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/ui/web_applications/test/web_app_navigation_browsertest.h"
#include "chrome/browser/web_applications/link_capturing_features.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/prerender_test_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "ui/views/test/button_test_api.h"
#include "ui/views/widget/any_widget_observer.h"
#include "url/gurl.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/intent_helper/preferred_apps_test_util.h"
#include "components/services/app_service/public/cpp/preferred_apps_list_handle.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/view_utils.h"
#endif

using LinkCapturingFeatureVersion = apps::test::LinkCapturingFeatureVersion;

namespace {

std::string GetLinkCapturingTestName(
    const testing::TestParamInfo<
        std::tuple<std::string, LinkCapturingFeatureVersion, bool>>& info) {
  std::string test_name;
  test_name = std::get<std::string>(info.param);
  test_name.append("_");
  test_name.append(
      apps::test::ToString(std::get<LinkCapturingFeatureVersion>(info.param)));
  test_name.append(std::get<bool>(info.param) ? "MigrationEnabled"
                                              : "MigrationNotEnabled");
  return test_name;
}

}  // namespace

class IntentPickerBrowserTest : public web_app::WebAppNavigationBrowserTest {
 public:
  IntentPickerBrowserTest() {
    if (IsMigrationEnabled()) {
      scoped_feature_list_.InitAndEnableFeatureWithParameters(
          features::kPageActionsMigration,
          {{features::kPageActionsMigrationIntentPicker.name, "true"}});
    } else {
      scoped_feature_list_.InitWithFeatures(
          {}, {::features::kPageActionsMigration});
    }
  }

  template <typename Action>
  testing::AssertionResult DoAndWaitForIntentPickerIconUpdate(Action action) {
    base::test::TestFuture<void> intent_picker_done;
    auto* tab_helper = IntentPickerTabHelper::FromWebContents(GetWebContents());
    tab_helper->SetIconUpdateCallbackForTesting(
        intent_picker_done.GetCallback());
    // On Mac, updating the icon requires asynchronous work that is done on the
    // threadpool (see `WebAppsIntentPickerDelegate::FindAllAppsForUrl()` for
    // more information). Flushing the thread pool thus helps prevent flakiness
    // in tests.
#if BUILDFLAG(IS_MAC)
    base::ThreadPoolInstance::Get()->FlushForTesting();
#endif  // BUILDFLAG(IS_MAC)
    action();
    if (HasFailure()) {
      return testing::AssertionFailure();
    }
    if (intent_picker_done.Wait()) {
      return testing::AssertionSuccess();
    }
    return testing::AssertionFailure() << "Intent picker never resolved";
  }

  content::WebContents* OpenNewTab(const GURL& url,
                                   const std::string& rel = "") {
    chrome::NewTab(browser());
    content::WebContents* web_contents =
        browser()->tab_strip_model()->GetActiveWebContents();

    EXPECT_TRUE(DoAndWaitForIntentPickerIconUpdate(
        [this] { NavigateToLaunchingPage(browser()); }));
    EXPECT_TRUE(DoAndWaitForIntentPickerIconUpdate([this, url] {
      ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
    }));

    return web_contents;
  }

  // Inserts an iframe in the main frame of |web_contents|.
  bool InsertIFrame(content::WebContents* web_contents) {
    return content::ExecJs(web_contents,
                           "let iframe = document.createElement('iframe');"
                           "iframe.id = 'iframe';"
                           "document.body.appendChild(iframe);");
  }


  content::WebContents* GetWebContents() {
    return browser()->tab_strip_model()->GetActiveWebContents();
  }

  IntentPickerBubbleView* intent_picker_bubble() {
    return IntentPickerBubbleView::intent_picker_bubble();
  }
  // Returns a bool indicating whether the ongoing page action framework
  // migration is enabled. This function provides a default implementation that
  // can be overridden in tests to control the enabled state of the page action
  // view.
  virtual bool IsMigrationEnabled() const { return false; }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Tests to do with the behavior of the intent picker icon in the omnibox. Does
// not test the behavior of the intent picker bubble itself.
// Note that behavior specific to the chip version of the icon is tested
// separately in intent_chip_button_browsertest.cc.
class IntentPickerIconBrowserTest
    : public IntentPickerBrowserTest,
      public ::testing::WithParamInterface<
          std::tuple<std::string, LinkCapturingFeatureVersion, bool>>,
      public IntentChipButtonTestBase {
 public:
  // TODO(crbug.com/40097608): Stop disabling Paint Holding.
  IntentPickerIconBrowserTest() {
    std::vector<base::test::FeatureRefAndParams> features_to_enable =
        apps::test::GetFeaturesToEnableLinkCapturingUX(LinkCapturingVersion());

    features_to_enable.push_back({blink::features::kPaintHolding, {}});
    if (IsMigrationEnabled()) {
      features_to_enable.push_back(
          {::features::kPageActionsMigration,
           {{::features::kPageActionsMigrationIntentPicker.name, "true"}}});
    }

    feature_list_.InitWithFeaturesAndParameters(features_to_enable, {});
  }

  bool IsMigrationEnabled() const override {
    return std::get<bool>(GetParam());
  }

  LinkCapturingFeatureVersion LinkCapturingVersion() {
    return std::get<LinkCapturingFeatureVersion>(GetParam());
  }

  std::string rel() { return std::get<std::string>(GetParam()); }

  bool IsDefaultOnEnabled() {
#if BUILDFLAG(IS_CHROMEOS)
    return false;
#else
    return LinkCapturingVersion() == LinkCapturingFeatureVersion::kV2DefaultOn;
#endif  // BUILDFLAG(IS_CHROMEOS)
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

// Tests that clicking a link from a tabbed browser to outside the scope of an
// installed app does not show the intent picker.
IN_PROC_BROWSER_TEST_P(IntentPickerIconBrowserTest,
                       NavigationToOutOfScopeLinkDoesNotShowIntentPicker) {
  InstallTestWebApp();

  const GURL out_of_scope_url =
      https_server().GetURL(GetAppUrlHost(), GetOutOfScopeUrlPath());
  NavigateToLaunchingPage(browser());
  ASSERT_TRUE(ExpectLinkClickNotCapturedIntoAppBrowser(
      browser(), out_of_scope_url, rel()));

  EXPECT_FALSE(GetIntentChip(browser())->GetVisible());

  EXPECT_EQ(nullptr, intent_picker_bubble());
}

// Tests that clicking a link from a tabbed browser to within the scope of an
// installed app shows the intent picker icon in Omnibox.
IN_PROC_BROWSER_TEST_P(IntentPickerIconBrowserTest,
                       NavigationToInScopeLinkShowsIntentPicker) {
  if (IsDefaultOnEnabled()) {
    GTEST_SKIP() << "Default On will launch app by default";
  }
  InstallTestWebApp();

  const GURL in_scope_url =
      https_server().GetURL(GetAppUrlHost(), GetInScopeUrlPath());
  auto* tab_helper = IntentPickerTabHelper::FromWebContents(GetWebContents());
  NavigateToLaunchingPage(browser());

  base::RunLoop run_loop;
  tab_helper->SetIconUpdateCallbackForTesting(run_loop.QuitClosure());
  ASSERT_TRUE(
      ExpectLinkClickNotCapturedIntoAppBrowser(browser(), in_scope_url, rel()));
  run_loop.Run();

  EXPECT_TRUE(
      WaitForPageActionButtonVisible(kActionShowIntentPicker, browser()));
  EXPECT_TRUE(GetIntentChip(browser())->GetVisible());
}

// Tests that the intent icon updates its visibility when switching between
// tabs.
IN_PROC_BROWSER_TEST_P(IntentPickerIconBrowserTest,
                       IconVisibilityAfterTabSwitching) {
  InstallTestWebApp();

  const GURL in_scope_url =
      https_server().GetURL(GetAppUrlHost(), GetInScopeUrlPath());
  const GURL out_of_scope_url =
      https_server().GetURL(GetAppUrlHost(), GetOutOfScopeUrlPath());

  // OpenNewTab opens a new tab and focus on the new tab.
  OpenNewTab(in_scope_url, /*rel=*/rel());
  EXPECT_TRUE(
      WaitForPageActionButtonVisible(kActionShowIntentPicker, browser()));
  EXPECT_TRUE(GetIntentChip(browser())->GetVisible());
  OpenNewTab(out_of_scope_url, /*rel=*/rel());
  EXPECT_FALSE(GetIntentChip(browser())->GetVisible());

  chrome::SelectPreviousTab(browser());
  EXPECT_TRUE(
      WaitForPageActionButtonVisible(kActionShowIntentPicker, browser()));
  EXPECT_TRUE(GetIntentChip(browser())->GetVisible());
  chrome::SelectNextTab(browser());
  EXPECT_FALSE(GetIntentChip(browser())->GetVisible());
}

// Tests that the navigation in iframe doesn't affect intent picker icon
IN_PROC_BROWSER_TEST_P(IntentPickerIconBrowserTest,
                       IframeNavigationDoesNotAffectIntentPicker) {
  InstallTestWebApp();

  const GURL in_scope_url =
      https_server().GetURL(GetAppUrlHost(), GetInScopeUrlPath());
  const GURL out_of_scope_url =
      https_server().GetURL(GetAppUrlHost(), GetOutOfScopeUrlPath());

  views::Button* intent_picker_icon = GetIntentChip(browser());

  content::WebContents* initial_tab = OpenNewTab(out_of_scope_url);
  ASSERT_TRUE(InsertIFrame(initial_tab));

  EXPECT_TRUE(
      content::NavigateIframeToURL(initial_tab, "iframe", in_scope_url));
  EXPECT_FALSE(intent_picker_icon->GetVisible());

  content::WebContents* new_tab = OpenNewTab(in_scope_url);
  ASSERT_TRUE(InsertIFrame(new_tab));

  EXPECT_TRUE(
      content::NavigateIframeToURL(initial_tab, "iframe", out_of_scope_url));
  EXPECT_TRUE(
      WaitForPageActionButtonVisible(kActionShowIntentPicker, browser()));
  EXPECT_TRUE(intent_picker_icon->GetVisible());
}

// Tests that the intent picker icon is not visible if the navigation redirects
// to a URL that doesn't have an installed PWA.
IN_PROC_BROWSER_TEST_P(IntentPickerIconBrowserTest,
                       DoesNotShowIntentPickerWhenRedirectedOutOfScope) {
  InstallTestWebApp(GetOtherAppUrlHost(), /*app_scope=*/"/");

  const GURL out_of_scope_url =
      https_server().GetURL(GetAppUrlHost(), GetOutOfScopeUrlPath());
  const GURL in_scope_url = https_server().GetURL(GetOtherAppUrlHost(), "/");
  const GURL redirect_url = https_server().GetURL(
      GetOtherAppUrlHost(), CreateServerRedirect(out_of_scope_url));

  views::Button* intent_picker_icon = GetIntentChip(browser());

  OpenNewTab(in_scope_url);
  EXPECT_TRUE(
      WaitForPageActionButtonVisible(kActionShowIntentPicker, browser()));
  EXPECT_TRUE(intent_picker_icon->GetVisible());
  ASSERT_TRUE(DoAndWaitForIntentPickerIconUpdate(
      [this, redirect_url, out_of_scope_url] {
        ClickLinkAndWaitForURL(GetWebContents(), redirect_url, out_of_scope_url,
                               LinkTarget::SELF, rel());
      }));
  EXPECT_FALSE(intent_picker_icon->GetVisible());
}

// Test that navigating to service pages (chrome://) will hide the intent picker
// icon.
IN_PROC_BROWSER_TEST_P(IntentPickerIconBrowserTest,
                       DoNotShowIconAndBubbleOnServicePages) {
  InstallTestWebApp();

  const GURL in_scope_url =
      https_server().GetURL(GetAppUrlHost(), GetInScopeUrlPath());
  GURL chrome_pages_url("chrome://version");
  std::string app_name = "test_name";

  views::Button* intent_picker_view = GetIntentChip(browser());

  OpenNewTab(in_scope_url);
  ASSERT_TRUE(intent_picker_view);
  EXPECT_TRUE(
      WaitForPageActionButtonVisible(kActionShowIntentPicker, browser()));
  EXPECT_TRUE(intent_picker_view->GetVisible());

  // Now switch to chrome://version.
  ASSERT_TRUE(DoAndWaitForIntentPickerIconUpdate([this, &chrome_pages_url]() {
    NavigateParams params(browser(), chrome_pages_url,
                          ui::PageTransition::PAGE_TRANSITION_TYPED);
    // Navigates and waits for loading to finish.
    ui_test_utils::NavigateToURL(&params);
  }));

  // Make sure that the intent picker icon is no longer visible.
  EXPECT_FALSE(intent_picker_view->GetVisible());
}

// Test that error pages do not show the intent picker icon.
IN_PROC_BROWSER_TEST_P(IntentPickerIconBrowserTest, DoNotShowIconOnErrorPages) {
  InstallTestWebApp();
  InstallTestWebApp("www.google.com", "/");

  const GURL in_scope_url =
      https_server().GetURL(GetAppUrlHost(), GetInScopeUrlPath());

  views::Button* intent_picker_view = GetIntentChip(browser());
  ASSERT_TRUE(intent_picker_view);

  // Go to the test app and wait for the intent picker icon to load.
  OpenNewTab(in_scope_url);
  EXPECT_TRUE(
      WaitForPageActionButtonVisible(kActionShowIntentPicker, browser()));
  EXPECT_TRUE(intent_picker_view->GetVisible());

  // Now switch to www.google.com, which gives a network error in the test
  // environment.
  ASSERT_TRUE(DoAndWaitForIntentPickerIconUpdate([this]() {
    NavigateParams params(browser(), GURL("https://www.google.com"),
                          ui::PageTransition::PAGE_TRANSITION_TYPED);
    // Navigates and waits for loading to finish.
    ui_test_utils::NavigateToURL(&params);
  }));

  // Make sure that the intent picker icon is not shown on the error page, even
  // though there's a PWA available for www.google.com.
  EXPECT_FALSE(intent_picker_view->GetVisible());
}

// Test that loading a page with pushState() call that changes URL updates the
// intent picker view.
IN_PROC_BROWSER_TEST_P(IntentPickerIconBrowserTest, PushStateURLChangeTest) {
  // Note: The test page is served from embedded_test_server() as https_server()
  // always returns empty responses.
  ASSERT_TRUE(embedded_test_server()->Start());
  ASSERT_TRUE(https_server().Start());

  const GURL test_url =
      embedded_test_server()->GetURL("/intent_picker/push_state_test.html");
  web_app::test::InstallDummyWebApp(profile(), "Test app", test_url);

  views::Button* intent_picker_view = GetIntentChip(browser());

  OpenNewTab(test_url);
  EXPECT_TRUE(
      WaitForPageActionButtonVisible(kActionShowIntentPicker, browser()));
  EXPECT_TRUE(intent_picker_view->GetVisible());

  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(DoAndWaitForIntentPickerIconUpdate([web_contents] {
    ASSERT_TRUE(content::ExecJs(
        web_contents,
        "document.getElementById('push_to_new_url_button').click();"));
  }));

  EXPECT_FALSE(intent_picker_view->GetVisible());
}

INSTANTIATE_TEST_SUITE_P(
    All,
    IntentPickerIconBrowserTest,
    testing::Combine(testing::Values("", "noopener", "noreferrer", "nofollow"),
#if BUILDFLAG(IS_CHROMEOS)
                     testing::Values(LinkCapturingFeatureVersion::kV1DefaultOff,
                                     LinkCapturingFeatureVersion::kV2DefaultOff)
#else
                     testing::Values(LinkCapturingFeatureVersion::kV2DefaultOn,
                                     LinkCapturingFeatureVersion::kV2DefaultOff)
#endif  // BUILDFLAG(IS_CHROMEOS)
                         ,
                     testing::Bool()),
    GetLinkCapturingTestName);

class IntentPickerIconBrowserBubbleTest
    : public IntentPickerBrowserTest,
      public ::testing::WithParamInterface<
          std::tuple<std::string, LinkCapturingFeatureVersion, bool>>,
      public IntentChipButtonTestBase {
 public:
  // TODO(crbug.com/40097608): Stop disabling Paint Holding.
  IntentPickerIconBrowserBubbleTest() {
    std::vector<base::test::FeatureRefAndParams> features_to_enable =
        apps::test::GetFeaturesToEnableLinkCapturingUX(LinkCapturingVersion());

    if (IsMigrationEnabled()) {
      features_to_enable.push_back(
          {::features::kPageActionsMigration,
           {{::features::kPageActionsMigrationIntentPicker.name, "true"}}});
    }

    feature_list_.InitWithFeaturesAndParameters(
        features_to_enable, {blink::features::kPaintHolding});
  }

  bool IsMigrationEnabled() const override {
    return std::get<bool>(GetParam());
  }

  LinkCapturingFeatureVersion LinkCapturingVersion() const {
    return std::get<LinkCapturingFeatureVersion>(GetParam());
  }
  bool LinkCapturingEnabledByDefault() const {
#if BUILDFLAG(IS_CHROMEOS)
    return false;
#else
    return LinkCapturingVersion() == LinkCapturingFeatureVersion::kV2DefaultOn;
#endif  // BUILDFLAG(IS_CHROMEOS)
  }

  size_t GetItemContainerSize(IntentPickerBubbleView* bubble) {
    return bubble->GetViewByID(IntentPickerBubbleView::ViewId::kItemContainer)
        ->children()
        .size();
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

#if BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_P(IntentPickerIconBrowserBubbleTest,
                       IntentChipOpensBubble) {
  InstallTestWebApp();
  const GURL in_scope_url =
      https_server().GetURL(GetAppUrlHost(), GetInScopeUrlPath());

  OpenNewTab(in_scope_url);
  ASSERT_TRUE(web_app::ClickIntentPickerAndWaitForBubble(browser()));

  EXPECT_EQ(1U, GetItemContainerSize(intent_picker_bubble()));
  auto& app_info = intent_picker_bubble()->app_info_for_testing();
  ASSERT_EQ(1U, app_info.size());
  EXPECT_EQ(test_web_app_id(), app_info[0].launch_name);
  EXPECT_EQ(GetAppName(), app_info[0].display_name);
}

// Test that the "Remember this choice" checkbox works.
IN_PROC_BROWSER_TEST_P(IntentPickerIconBrowserBubbleTest, RememberOpenWebApp) {
  base::HistogramTester histogram_tester;

  InstallTestWebApp();
  const GURL in_scope_url =
      https_server().GetURL(GetAppUrlHost(), GetInScopeUrlPath());

  OpenNewTab(in_scope_url);
  ASSERT_TRUE(web_app::ClickIntentPickerAndWaitForBubble(browser()));

  // Check "Remember my choice" and accept the bubble.
  views::Checkbox* remember_selection_checkbox =
      views::AsViewClass<views::Checkbox>(intent_picker_bubble()->GetViewByID(
          IntentPickerBubbleView::ViewId::kRememberCheckbox));
  ASSERT_TRUE(remember_selection_checkbox);
  ASSERT_TRUE(remember_selection_checkbox->GetEnabled());
  remember_selection_checkbox->SetChecked(true);

  apps::PreferredAppsListHandle& preferred_apps =
      apps::AppServiceProxyFactory::GetForProfile(profile())
          ->PreferredAppsList();
  apps_util::PreferredAppUpdateWaiter preference_update_waiter(
      preferred_apps, test_web_app_id());

  ui_test_utils::BrowserChangeObserver added_observer(
      nullptr, ui_test_utils::BrowserChangeObserver::ChangeType::kAdded);
  intent_picker_bubble()->AcceptDialog();

  // Accepting the bubble should open the app.
  Browser* app_browser = added_observer.Wait();
  ASSERT_TRUE(web_app::AppBrowserController::IsForWebApp(app_browser,
                                                         test_web_app_id()));

  // The link capturing preference should be updated.
  preference_update_waiter.Wait();
  ASSERT_TRUE(
      preferred_apps.IsPreferredAppForSupportedLinks(test_web_app_id()));

  // Check that we recorded that settings were changed.
  histogram_tester.ExpectBucketCount(
      "ChromeOS.Intents.LinkCapturingEvent2.WebApp",
      apps::IntentHandlingMetrics::LinkCapturingEvent::kSettingsChanged, 1);
  histogram_tester.ExpectBucketCount(
      "ChromeOS.Intents.LinkCapturingEvent2.ArcApp",
      apps::IntentHandlingMetrics::LinkCapturingEvent::kSettingsChanged, 0);
  histogram_tester.ExpectBucketCount(
      "ChromeOS.Intents.LinkCapturingEvent2",
      apps::IntentHandlingMetrics::LinkCapturingEvent::kSettingsChanged, 1);
}

#else
IN_PROC_BROWSER_TEST_P(IntentPickerIconBrowserBubbleTest,
                       DISABLED_IntentChipLaunchesAppDirectly) {
  InstallTestWebApp();
  const GURL in_scope_url =
      https_server().GetURL(GetAppUrlHost(), GetInScopeUrlPath());

  views::Button* intent_picker_icon = GetIntentChip(browser());

  OpenNewTab(in_scope_url);
  EXPECT_TRUE(intent_picker_icon->GetVisible());

  views::test::ButtonTestApi test_api(intent_picker_icon);
  test_api.NotifyClick(ui::MouseEvent(
      ui::EventType::kMousePressed, gfx::Point(), gfx::Point(),
      base::TimeTicks(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
  Browser* app_browser = ui_test_utils::WaitForBrowserToOpen();
  EXPECT_FALSE(intent_picker_bubble());
  EXPECT_TRUE(app_browser);
  ASSERT_TRUE(web_app::AppBrowserController::IsForWebApp(app_browser,
                                                         test_web_app_id()));
}
#endif  // BUILDFLAG(IS_CHROMEOS)

INSTANTIATE_TEST_SUITE_P(
    All,
    IntentPickerIconBrowserBubbleTest,
    testing::Combine(testing::Values("", "noopener", "noreferrer", "nofollow"),
#if BUILDFLAG(IS_CHROMEOS)
                     testing::Values(LinkCapturingFeatureVersion::kV1DefaultOff,
                                     LinkCapturingFeatureVersion::kV2DefaultOff)
#else
                     testing::Values(LinkCapturingFeatureVersion::kV2DefaultOn,
                                     LinkCapturingFeatureVersion::kV2DefaultOff)
#endif  // BUILDFLAG(IS_CHROMEOS)
                         ,
                     testing::Bool()),
    GetLinkCapturingTestName);

// This test only works when link capturing is set to default off for desktop
// platforms, as prerendering navigations are aborted during link captured app
// launches. See LinkCapturingNavigationThrottle::MaybeCreate for more
// information.
// TODO(b/297256243): Investigate prerendering integration with link capturing.
class IntentPickerIconPrerenderingBrowserTest
    : public IntentPickerIconBrowserTest {
 public:
  IntentPickerIconPrerenderingBrowserTest()
      : prerender_helper_(base::BindRepeating(
            &IntentPickerIconPrerenderingBrowserTest::GetWebContents,
            base::Unretained(this))) {}
  ~IntentPickerIconPrerenderingBrowserTest() override = default;
  IntentPickerIconPrerenderingBrowserTest(
      const IntentPickerIconPrerenderingBrowserTest&) = delete;

  IntentPickerIconPrerenderingBrowserTest& operator=(
      const IntentPickerIconPrerenderingBrowserTest&) = delete;

  void SetUp() override {
    prerender_helper_.RegisterServerRequestMonitor(embedded_test_server());
    IntentPickerIconBrowserTest::SetUp();
  }

  bool IsMigrationEnabled() const override {
    return std::get<bool>(GetParam());
  }

  void SetUpOnMainThread() override {
    host_resolver()->AddRule("*", "127.0.0.1");
    ASSERT_TRUE(embedded_test_server()->Start());
    IntentPickerIconBrowserTest::SetUpOnMainThread();
  }

  content::test::PrerenderTestHelper& prerender_test_helper() {
    return prerender_helper_;
  }

 private:
  content::test::PrerenderTestHelper prerender_helper_;
};

IN_PROC_BROWSER_TEST_P(IntentPickerIconPrerenderingBrowserTest,
                       PrerenderingShouldNotShowIntentPicker) {
  InstallTestWebApp();

  const GURL initial_url =
      https_server().GetURL(GetAppUrlHost(), "/empty.html");
  OpenNewTab(initial_url);

  views::Button* intent_picker_icon = GetIntentChip(browser());
  EXPECT_FALSE(intent_picker_icon->GetVisible());

  // Load a prerender page and prerendering should not try to show the
  // intent picker.
  const GURL prerender_url = https_server().GetURL(
      GetAppUrlHost(), std::string(GetAppScopePath()) + "index1.html");
  content::FrameTreeNodeId host_id =
      prerender_test_helper().AddPrerender(prerender_url);
  content::test::PrerenderHostObserver host_observer(*GetWebContents(),
                                                     host_id);
  EXPECT_FALSE(host_observer.was_activated());
  EXPECT_FALSE(intent_picker_icon->GetVisible());

  // Activate the prerender page.
  ASSERT_TRUE(DoAndWaitForIntentPickerIconUpdate([this, prerender_url] {
    prerender_test_helper().NavigatePrimaryPage(prerender_url);
  }));
  EXPECT_TRUE(host_observer.was_activated());

  // After activation, IntentPickerTabHelper should show the
  // intent picker.
  EXPECT_TRUE(
      WaitForPageActionButtonVisible(kActionShowIntentPicker, browser()));
  EXPECT_TRUE(intent_picker_icon->GetVisible());
}

INSTANTIATE_TEST_SUITE_P(
    All,
    IntentPickerIconPrerenderingBrowserTest,
    testing::Combine(testing::Values("", "noopener", "noreferrer", "nofollow"),
#if BUILDFLAG(IS_CHROMEOS)
                     testing::Values(LinkCapturingFeatureVersion::kV1DefaultOff,
                                     LinkCapturingFeatureVersion::kV2DefaultOff)
#else
                     testing::Values(LinkCapturingFeatureVersion::kV2DefaultOff)
#endif  // BUILDFLAG(IS_CHROMEOS)
                         ,
                     testing::Bool()),
    GetLinkCapturingTestName);

class IntentPickerIconFencedFrameBrowserTest
    : public IntentPickerIconBrowserTest {
 public:
  IntentPickerIconFencedFrameBrowserTest() = default;
  ~IntentPickerIconFencedFrameBrowserTest() override = default;
  IntentPickerIconFencedFrameBrowserTest(
      const IntentPickerIconFencedFrameBrowserTest&) = delete;

  IntentPickerIconFencedFrameBrowserTest& operator=(
      const IntentPickerIconFencedFrameBrowserTest&) = delete;

  content::test::FencedFrameTestHelper& fenced_frame_test_helper() {
    return fenced_frame_helper_;
  }

  bool IsMigrationEnabled() const override {
    return std::get<bool>(GetParam());
  }

 private:
  content::test::FencedFrameTestHelper fenced_frame_helper_;
};

IN_PROC_BROWSER_TEST_P(IntentPickerIconFencedFrameBrowserTest,
                       ShouldShowIntentPickerInFencedFrame) {
  InstallTestWebApp();

  views::Button* intent_picker_icon = GetIntentChip(browser());

  const GURL initial_url =
      https_server().GetURL(GetAppUrlHost(), "/empty.html");
  OpenNewTab(initial_url);
  EXPECT_FALSE(intent_picker_icon->GetVisible());

  const GURL fenced_frame_url = https_server().GetURL(
      GetAppUrlHost(), std::string(GetAppScopePath()) + "index1.html");
  // Create a fenced frame.
  ASSERT_TRUE(fenced_frame_test_helper().CreateFencedFrame(
      GetWebContents()->GetPrimaryMainFrame(), fenced_frame_url));

  EXPECT_FALSE(intent_picker_icon->GetVisible());
}

INSTANTIATE_TEST_SUITE_P(
    All,
    IntentPickerIconFencedFrameBrowserTest,
    testing::Combine(testing::Values("", "noopener", "noreferrer", "nofollow"),
#if BUILDFLAG(IS_CHROMEOS)
                     testing::Values(LinkCapturingFeatureVersion::kV1DefaultOff,
                                     LinkCapturingFeatureVersion::kV2DefaultOff)
#else
                     testing::Values(LinkCapturingFeatureVersion::kV2DefaultOn,
                                     LinkCapturingFeatureVersion::kV2DefaultOff)
#endif  // BUILDFLAG(IS_CHROMEOS)
                         ,
                     testing::Bool()),
    GetLinkCapturingTestName);