File: web_app_tab_strip_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 (1426 lines) | stat: -rw-r--r-- 59,434 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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
// 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/memory/stack_allocated.h"
#include "base/strings/string_util.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#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/app_service/browser_app_launcher.h"
#include "chrome/browser/apps/link_capturing/link_capturing_feature_test_support.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/tab_ui_helper.h"
#include "chrome/browser/ui/tabs/existing_window_sub_menu_model.h"
#include "chrome/browser/ui/tabs/tab_menu_model.h"
#include "chrome/browser/ui/unload_controller.h"
#include "chrome/browser/ui/views/frame/browser_non_client_frame_view.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/location_bar/custom_tab_bar_view.h"
#include "chrome/browser/ui/views/tabs/tab_icon.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/browser/ui/views/tabs/tab_strip_controller.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/ui/web_applications/web_app_browser_controller.h"
#include "chrome/browser/ui/web_applications/web_app_browsertest_base.h"
#include "chrome/browser/ui/web_applications/web_app_launch_utils.h"
#include "chrome/browser/web_applications/manifest_update_manager.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_params.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_screenshot_fetcher.h"
#include "chrome/common/chrome_features.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/embedder_support/switches.h"
#include "components/page_load_metrics/browser/page_load_metrics_test_waiter.h"
#include "components/webapps/browser/install_result_code.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/test/background_color_change_waiter.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"

using content::OpenURLParams;
using content::Referrer;

namespace {

constexpr SkColor kAppBackgroundColor = SK_ColorBLUE;

// The page shouldn't have a manifest so no updating will occur to override
// our settings.
constexpr char kAppPath[] = "/web_apps/no_manifest.html";

}  // namespace
namespace web_app {

class WebAppTabStripBrowserTest : public WebAppBrowserTestBase,
                                  public testing::WithParamInterface<
                                      apps::test::LinkCapturingFeatureVersion> {
 public:
  WebAppTabStripBrowserTest() = default;
  ~WebAppTabStripBrowserTest() override = default;

  void SetUp() override {
    std::vector<base::test::FeatureRefAndParams> enabled_features =
        apps::test::GetFeaturesToEnableLinkCapturingUX(GetParam());
    enabled_features.emplace_back(blink::features::kDesktopPWAsTabStrip,
                                  base::FieldTrialParams());
    enabled_features.emplace_back(features::kDesktopPWAsTabStripSettings,
                                  base::FieldTrialParams());
    enabled_features.emplace_back(
        blink::features::kDesktopPWAsTabStripCustomizations,
        base::FieldTrialParams());
    features_.InitWithFeaturesAndParameters(enabled_features, {});

    ASSERT_TRUE(embedded_test_server()->Start());

    WebAppBrowserTestBase::SetUp();
  }

  struct App {
    STACK_ALLOCATED();

   public:
    webapps::AppId id;
    Browser* browser;
    BrowserView* browser_view;
    content::WebContents* web_contents;
  };

  webapps::AppId Install() {
    Profile* profile = browser()->profile();
    GURL start_url = embedded_test_server()->GetURL(kAppPath);

    auto web_app_info =
        web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(start_url);
    web_app_info->scope = start_url.GetWithoutFilename();
    web_app_info->title = u"Test app";
    web_app_info->background_color = kAppBackgroundColor;
    web_app_info->user_display_mode = mojom::UserDisplayMode::kStandalone;
    web_app_info->display_override = {blink::mojom::DisplayMode::kTabbed};
    return test::InstallWebApp(profile, std::move(web_app_info));
  }

  App InstallAndLaunch() {
    Profile* profile = browser()->profile();
    webapps::AppId app_id = Install();

    Browser* app_browser = ::web_app::LaunchWebAppBrowser(profile, app_id);
    return App{app_id, app_browser,
               BrowserView::GetBrowserViewForBrowser(app_browser),
               app_browser->tab_strip_model()->GetActiveWebContents()};
  }

  webapps::AppId InstallTestWebApp(GURL start_url, bool await_metric = true) {
    page_load_metrics::PageLoadMetricsTestWaiter metrics_waiter(
        browser()->tab_strip_model()->GetActiveWebContents());
    if (await_metric) {
      metrics_waiter.AddWebFeatureExpectation(
          blink::mojom::WebFeature::kWebAppTabbed);
    }
    webapps::AppId app_id = InstallWebAppFromPage(browser(), start_url);
    if (await_metric) {
      metrics_waiter.Wait();
    }
    return app_id;
  }

  void OpenUrlAndWait(Browser* app_browser, GURL url) {
    TabStripModel* tab_strip = app_browser->tab_strip_model();

    content::WaitForLoadStop(tab_strip->GetActiveWebContents());

    NavigateParams params(app_browser, url,
                          ui::PageTransition::PAGE_TRANSITION_LINK);
    params.disposition = WindowOpenDisposition::CURRENT_TAB;
    ui_test_utils::NavigateToURL(&params);
  }

  SkColor GetTabColor(BrowserView* browser_view) {
    return TabStyle::Get()->GetTabBackgroundColor(
        TabStyle::TabSelectionState::kActive, /*hovered=*/false,
        /*frame_active=*/true, *browser_view->GetColorProvider());
  }

  WebAppRegistrar& registrar() {
    return WebAppProvider::GetForTest(browser()->profile())->registrar_unsafe();
  }

 private:
  base::test::ScopedFeatureList features_;
};

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest,
                       CustomTabBarUpdateOnTabSwitch) {
  App app = InstallAndLaunch();

  CustomTabBarView* custom_tab_bar =
      app.browser_view->toolbar()->custom_tab_bar();
  EXPECT_FALSE(custom_tab_bar->GetVisible());

  // Add second tab.
  chrome::NewTab(app.browser);
  ASSERT_EQ(app.browser->tab_strip_model()->count(), 2);

  // Navigate tab out of scope, custom tab bar should appear.
  GURL in_scope_url = embedded_test_server()->GetURL(kAppPath);
  GURL out_of_scope_url =
      embedded_test_server()->GetURL("/banners/theme-color.html");
  ASSERT_TRUE(content::NavigateToURL(
      app.browser->tab_strip_model()->GetActiveWebContents(),
      out_of_scope_url));
  EXPECT_EQ(
      app.browser->tab_strip_model()->GetActiveWebContents()->GetVisibleURL(),
      out_of_scope_url);
  EXPECT_TRUE(custom_tab_bar->GetVisible());

  // Custom tab bar should go away for in scope tab.
  chrome::SelectNextTab(app.browser);
  EXPECT_EQ(
      app.browser->tab_strip_model()->GetActiveWebContents()->GetVisibleURL(),
      in_scope_url);
  EXPECT_FALSE(custom_tab_bar->GetVisible());

  // Custom tab bar should return for out of scope tab.
  chrome::SelectNextTab(app.browser);
  EXPECT_EQ(
      app.browser->tab_strip_model()->GetActiveWebContents()->GetVisibleURL(),
      out_of_scope_url);
  EXPECT_TRUE(custom_tab_bar->GetVisible());
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, PopOutTabOnInstall) {
  GURL start_url = embedded_test_server()->GetURL("/web_apps/basic.html");

  NavigateViaLinkClickToURLAndWait(browser(), start_url);

  // Install the site with the user display mode set to kTabbed.
  Browser* app_browser;
  webapps::AppId app_id;
  {
    ui_test_utils::BrowserChangeObserver app_browser_observer(
        nullptr, ui_test_utils::BrowserChangeObserver::ChangeType::kAdded);
    base::RunLoop run_loop;
    auto* provider = WebAppProvider::GetForTest(browser()->profile());
    DCHECK(provider);
    test::WaitUntilReady(provider);
    provider->scheduler().FetchManifestAndInstall(
        webapps::WebappInstallSource::MENU_BROWSER_TAB,
        browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),
        base::BindLambdaForTesting(
            [](base::WeakPtr<WebAppScreenshotFetcher>, content::WebContents*,
               std::unique_ptr<web_app::WebAppInstallInfo> web_app_info,
               WebAppInstallationAcceptanceCallback acceptance_callback) {
              web_app_info->user_display_mode = mojom::UserDisplayMode::kTabbed;
              std::move(acceptance_callback)
                  .Run(/*user_accepted=*/true, std::move(web_app_info));
            }),
        base::BindLambdaForTesting(
            [&run_loop, &app_id](const webapps::AppId& installed_app_id,
                                 webapps::InstallResultCode code) {
              DCHECK_EQ(code, webapps::InstallResultCode::kSuccessNewInstall);
              app_id = installed_app_id;
              run_loop.Quit();
            }),
        FallbackBehavior::kAllowFallbackDataAlways);
    run_loop.Run();
    app_browser = app_browser_observer.Wait();
    ASSERT_TRUE(app_browser);
    EXPECT_NE(app_browser, browser());
  }

  // After installing a tabbed display mode app the install page should pop out
  // to a standalone app window.
  EXPECT_TRUE(AppBrowserController::IsForWebApp(app_browser, app_id));
  EXPECT_EQ(
      browser()->tab_strip_model()->GetActiveWebContents()->GetVisibleURL(),
      GURL("chrome://newtab/"));
}

// TODO(crbug.com/40598974) Enabled tab strip for web apps on non-Chrome OS.
#if BUILDFLAG(IS_CHROMEOS)

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest,
                       ActiveTabColorIsBackgroundColor) {
  // Ensure we're not using the system theme on Linux.
  ThemeService* theme_service =
      ThemeServiceFactory::GetForProfile(browser()->profile());
  theme_service->UseDefaultTheme();

  webapps::AppId app_id = Install();

  // Trigger the launch but do not wait for the web contents to load.
  content::WebContents* web_contents =
      apps::AppServiceProxyFactory::GetForProfile(profile())
          ->BrowserAppLauncher()
          ->LaunchAppWithParamsForTesting(apps::AppLaunchParams(
              app_id, apps::LaunchContainer::kLaunchContainerWindow,
              WindowOpenDisposition::CURRENT_TAB,
              apps::LaunchSource::kFromTest));
  ASSERT_TRUE(web_contents);
  Browser* app_browser = chrome::FindBrowserWithTab(web_contents);
  App app{app_id, app_browser,
          BrowserView::GetBrowserViewForBrowser(app_browser), web_contents};

  EXPECT_EQ(registrar().GetAppBackgroundColor(app.id), kAppBackgroundColor);

  // Expect manifest background color prior to page loading.
  {
    ASSERT_FALSE(
        app.web_contents->IsDocumentOnLoadCompletedInPrimaryMainFrame());
    EXPECT_EQ(app.browser->app_controller()->GetBackgroundColor().value(),
              kAppBackgroundColor);
    EXPECT_EQ(GetTabColor(app.browser_view), kAppBackgroundColor);
  }

  // Expect initial page background color to be white.
  {
    content::BackgroundColorChangeWaiter(app.web_contents).Wait();
    EXPECT_EQ(app.browser->app_controller()->GetBackgroundColor().value(),
              SK_ColorWHITE);
    EXPECT_EQ(GetTabColor(app.browser_view), SK_ColorWHITE);
  }

  // Ensure HTML document has loaded before we execute JS in it.
  content::AwaitDocumentOnLoadCompleted(app.web_contents);

  // Set document color to black and read tab background color.
  {
    content::BackgroundColorChangeWaiter waiter(app.web_contents);
    EXPECT_TRUE(content::ExecJs(
        app.web_contents, "document.body.style.backgroundColor = 'black';"));
    waiter.Wait();
    EXPECT_EQ(app.browser->app_controller()->GetBackgroundColor().value(),
              SK_ColorBLACK);
    EXPECT_EQ(GetTabColor(app.browser_view), SK_ColorBLACK);
  }

  // Update document color to cyan and check that the tab color matches.
  {
    content::BackgroundColorChangeWaiter waiter(app.web_contents);
    EXPECT_TRUE(content::ExecJs(
        app.web_contents, "document.body.style.backgroundColor = 'cyan';"));
    waiter.Wait();
    EXPECT_EQ(app.browser->app_controller()->GetBackgroundColor().value(),
              SK_ColorCYAN);
    EXPECT_EQ(GetTabColor(app.browser_view), SK_ColorCYAN);
  }
}

#endif  // BUILDFLAG(IS_CHROMEOS)

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, AutoNewTabUrl) {
  GURL start_url = embedded_test_server()->GetURL(
      "/banners/"
      "manifest_test_page.html?manifest=manifest_tabbed_display_override.json");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = LaunchWebAppBrowser(app_id);

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  chrome::NewTab(app_browser);
  EXPECT_EQ(
      app_browser->tab_strip_model()->GetActiveWebContents()->GetVisibleURL(),
      registrar().GetAppStartUrl(app_id));
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, NewTabUrl) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = LaunchWebAppBrowser(app_id);

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  chrome::NewTab(app_browser);
  EXPECT_EQ(
      app_browser->tab_strip_model()->GetActiveWebContents()->GetVisibleURL(),
      embedded_test_server()->GetURL("/web_apps/favicon_only.html"));
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, NonTabbedWebApp) {
  Profile* profile = browser()->profile();

  auto web_app_info = web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(
      embedded_test_server()->GetURL(kAppPath));
  web_app_info->title = u"Test app";
  webapps::AppId app_id = test::InstallWebApp(profile, std::move(web_app_info));

  Browser* app_browser = web_app::LaunchWebAppBrowser(profile, app_id);

  EXPECT_TRUE(app_browser->app_controller()->ShouldHideNewTabButton());
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, InstallingPinsHomeTab) {
  GURL start_url = embedded_test_server()->GetURL(
      "/web_apps/tab_strip_customizations.html#blah");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  // The URL of the pinned home tab should include the fragment.
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);

  // App should have a new tab button.
  EXPECT_FALSE(app_browser->app_controller()->ShouldHideNewTabButton());
}

// Tests that the monochrome app icon is used on the home tab and it is
// correctly colored.
IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, MonochromeAppIconOnHomeTab) {
  // Ensure we're not using the system theme on Linux.
  ThemeService* theme_service =
      ThemeServiceFactory::GetForProfile(browser()->profile());
  theme_service->UseDefaultTheme();

  GURL start_url = embedded_test_server()->GetURL(
      "/web_apps/get_manifest.html?tab_strip_fixed_home_scope.json");

  base::RunLoop run_loop;
  WebAppProvider::GetForTest(browser()->profile())
      ->icon_manager()
      .SetFaviconMonochromeReadCallbackForTesting(base::BindLambdaForTesting(
          [&](const webapps::AppId& cached_app_id) { run_loop.Quit(); }));

  webapps::AppId app_id =
      InstallWebAppFromPageAndCloseAppBrowser(browser(), start_url);
  run_loop.Run();

  Browser* app_browser = LaunchWebAppBrowser(app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  TabIcon* tab_icon = BrowserView::GetBrowserViewForBrowser(app_browser)
                          ->tabstrip()
                          ->tab_at(0)
                          ->GetTabIconForTesting();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->active_index(), 0);

  const SkBitmap* favicon_bitmap = tab_icon->GetThemedIconForTesting().bitmap();

  EXPECT_EQ(favicon_bitmap->width(), 16);
  EXPECT_EQ(favicon_bitmap->height(), 16);
  EXPECT_EQ(gfx::kGoogleGrey900, favicon_bitmap->getColor(0, 0));

  // Adding a new tab causes the home tab to be inactive and colored black (the
  // app theme_color), so the icon should change color.
  chrome::NewTab(app_browser);

  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 1);

  favicon_bitmap = tab_icon->GetThemedIconForTesting().bitmap();

  EXPECT_EQ(favicon_bitmap->width(), 16);
  EXPECT_EQ(favicon_bitmap->height(), 16);
  EXPECT_EQ(SK_ColorWHITE, favicon_bitmap->getColor(0, 0));
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, InstallFromNonHomeTabUrl) {
  GURL install_url = embedded_test_server()->GetURL(
      "/web_apps/get_manifest.html?tab_strip_customizations.json");
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(install_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect the home tab was opened in addition to the install URL.
  EXPECT_EQ(tab_strip->count(), 2);
  // Home tab - start URL.
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  // Regular tab (active) - install URL.
  EXPECT_FALSE(tab_strip->IsTabPinned(1));
  EXPECT_EQ(tab_strip->active_index(), 1);
  EXPECT_EQ(tab_strip->GetWebContentsAt(1)->GetVisibleURL(), install_url);
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, OpeningPinsHomeTab) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  // Install and close app.
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  CloseAndWait(app_browser);

  // Launch the app to a non home tab URL.
  app_browser = LaunchWebAppToURL(
      browser()->profile(), app_id,
      embedded_test_server()->GetURL("/web_apps/favicon_only.html"));
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect the home tab was opened in addition to the launch URL.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(),
            registrar().GetAppStartUrl(app_id));
  EXPECT_EQ(tab_strip->active_index(), 1);

  // Open app at home tab URL.
  EXPECT_EQ(app_browser,
            LaunchWebAppToURL(browser()->profile(), app_id, start_url));

  // Expect the home tab to be focused.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, ReparentingPinsHomeTab) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  // Install and close app.
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  CloseAndWait(app_browser);

  // Navigate to the app URL in the browser.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(),
      embedded_test_server()->GetURL("/web_apps/favicon_only.html")));
  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();

  // Reparent web contents into app browser.
  app_browser =
      web_app::ReparentWebContentsIntoAppBrowser(web_contents, app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  // Expect the pinned home tab to also be opened.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(),
            registrar().GetAppStartUrl(app_id));
  EXPECT_EQ(tab_strip->active_index(), 1);

  // Navigate to home tab URL in the browser.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL(
                     "/web_apps/tab_strip_customizations.html")));
  web_contents = browser()->tab_strip_model()->GetActiveWebContents();

  // Reparent web contents into app browser.
  EXPECT_EQ(app_browser,
            web_app::ReparentWebContentsIntoAppBrowser(web_contents, app_id));
  tab_strip = app_browser->tab_strip_model();

  // Expect home tab to be focused.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(),
            registrar().GetAppStartUrl(app_id));
  EXPECT_EQ(tab_strip->active_index(), 0);
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, NavigationThrottle) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect app opened with pinned home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);

  // Navigate to a non home tab URL.
  OpenUrlAndWait(app_browser,
                 embedded_test_server()->GetURL("/web_apps/get_manifest.html"));

  // Expect URL to have opened in new tab.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 1);
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->GetWebContentsAt(1)->GetVisibleURL(),
            embedded_test_server()->GetURL("/web_apps/get_manifest.html"));

  // Navigate to home tab URL with hash ref.
  OpenUrlAndWait(app_browser,
                 embedded_test_server()->GetURL(
                     "/web_apps/tab_strip_customizations.html#some_hash"));

  // Expect navigation to happen in home tab.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(),
            embedded_test_server()->GetURL(
                "/web_apps/tab_strip_customizations.html#some_hash"));

  // Navigate to a non home tab URL.
  OpenUrlAndWait(app_browser, embedded_test_server()->GetURL(
                                  "/web_apps/get_manifest.html?blah"));

  // Expect URL to have opened in new tab.
  EXPECT_EQ(tab_strip->count(), 3);
  EXPECT_EQ(tab_strip->active_index(), 1);
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(),
            embedded_test_server()->GetURL(
                "/web_apps/tab_strip_customizations.html#some_hash"));
  EXPECT_EQ(tab_strip->GetWebContentsAt(1)->GetVisibleURL(),
            embedded_test_server()->GetURL("/web_apps/get_manifest.html?blah"));
  EXPECT_EQ(tab_strip->GetWebContentsAt(2)->GetVisibleURL(),
            embedded_test_server()->GetURL("/web_apps/get_manifest.html"));

  // Navigate to home tab URL.
  OpenUrlAndWait(app_browser, start_url);

  // Expect navigation to happen in home tab.
  EXPECT_EQ(tab_strip->count(), 3);
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
}

// TODO(crbug.com/40257354): Enable this test.
IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, DISABLED_TargetBlankLink) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Open a second tab then refocus the home tab.
  chrome::NewTab(app_browser);
  tab_strip->ActivateTabAt(0);

  // Navigate to a home tab URL via a target=_blank link.
  content::TestNavigationObserver nav_observer(
      tab_strip->GetActiveWebContents(), 1);
  ASSERT_TRUE(ExecJs(
      tab_strip->GetActiveWebContents(),
      "document.getElementById('test-link-with-blank-target').click();"));
  nav_observer.Wait();

  // Expect no new tab was opened, and the home tab is focused.
  EXPECT_EQ(tab_strip->count(), 3);
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_EQ(tab_strip->GetActiveWebContents()->GetVisibleURL(),
            embedded_test_server()->GetURL(
                "/web_apps/tab_strip_customizations.html#target_blank"));
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, OpenInChrome) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = LaunchWebAppBrowser(app_id);

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // 'Open in Chrome' menu item should not be enabled for the pinned home tab.
  EXPECT_FALSE(
      app_browser->command_controller()->IsCommandEnabled(IDC_OPEN_IN_CHROME));

  chrome::NewTab(app_browser);
  // 'Open in Chrome' menu item should be enabled for other tabs.
  EXPECT_TRUE(
      app_browser->command_controller()->IsCommandEnabled(IDC_OPEN_IN_CHROME));
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, WebAppMenuModelTabbedApp) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = LaunchWebAppBrowser(app_id);

  WebAppMenuModel model(nullptr, app_browser);
  model.Init();
  // Check menu contains 'New Tab'.
  EXPECT_TRUE(model.GetIndexOfCommandId(IDC_NEW_TAB).has_value());
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, WebAppMenuModelNonTabbedApp) {
  GURL start_url = embedded_test_server()->GetURL("/web_apps/basic.html");
  webapps::AppId app_id = InstallTestWebApp(start_url, /*await_metric=*/false);
  Browser* app_browser = LaunchWebAppBrowser(app_id);

  WebAppMenuModel model(nullptr, app_browser);
  model.Init();
  // Check menu does not contain 'New Tab'.
  EXPECT_FALSE(model.GetIndexOfCommandId(IDC_NEW_TAB).has_value());
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, MoveTabsToNewWindow) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = LaunchWebAppBrowser(app_id);

  chrome::NewTab(app_browser);

  size_t initial_browser_count = BrowserList::GetInstance()->size();

  ui_test_utils::BrowserChangeObserver new_browser_observer(
      nullptr, ui_test_utils::BrowserChangeObserver::ChangeType::kAdded);
  chrome::MoveTabsToNewWindow(app_browser, {1});
  Browser* new_browser = new_browser_observer.Wait();
  ASSERT_TRUE(new_browser);

  EXPECT_EQ(initial_browser_count + 1, BrowserList::GetInstance()->size());

  // Check that the tab made it to a new window.
  EXPECT_NE(app_browser, new_browser);
  EXPECT_TRUE(AppBrowserController::IsForWebApp(new_browser, app_id));
  EXPECT_EQ(app_browser->tab_strip_model()->count(), 1);

  // Check the new browser contains the moved tab and a pinned home tab.
  EXPECT_EQ(new_browser->tab_strip_model()->count(), 2);
  EXPECT_TRUE(new_browser->tab_strip_model()->IsTabPinned(0));
  EXPECT_EQ(
      new_browser->tab_strip_model()->GetWebContentsAt(0)->GetVisibleURL(),
      start_url);
  EXPECT_EQ(new_browser->tab_strip_model()->active_index(), 1);
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, MoveTabsToExistingWindow) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = LaunchWebAppBrowser(app_id);
  chrome::NewTab(app_browser);

  // Open a second app browser window.
  ui_test_utils::BrowserChangeObserver app_browser_observer(
      nullptr, ui_test_utils::BrowserChangeObserver::ChangeType::kAdded);
  chrome::MoveTabsToNewWindow(app_browser, {1});
  Browser* app_browser2 = app_browser_observer.Wait();
  ASSERT_TRUE(app_browser2);

  EXPECT_EQ(app_browser->tab_strip_model()->count(), 1);
  EXPECT_EQ(app_browser2->tab_strip_model()->count(), 2);

  // Test the "open in existing window" menu option.
  TabMenuModel menu(nullptr,
                    app_browser2->GetFeatures().tab_menu_model_delegate(),
                    app_browser2->tab_strip_model(), 1);
  size_t submenu_index =
      menu.GetIndexOfCommandId(TabStripModel::CommandMoveToExistingWindow)
          .value();
  ExistingWindowSubMenuModel* submenu =
      static_cast<ExistingWindowSubMenuModel*>(
          menu.GetSubmenuModelAt(submenu_index));
  EXPECT_EQ(submenu->GetItemCount(), 3u);
  EXPECT_EQ(submenu->GetLabelAt(0),
            l10n_util::GetStringUTF16(IDS_TAB_CXMENU_MOVETOANOTHERNEWWINDOW));
  EXPECT_EQ(submenu->GetTypeAt(1), ui::MenuModel::TYPE_SEPARATOR);
  EXPECT_EQ(submenu->GetLabelAt(2), app_browser->GetWindowTitleForCurrentTab(
                                        /*include_app_name=*/false));

  submenu->ExecuteCommand(submenu->GetCommandIdAt(2), 0);

  EXPECT_EQ(app_browser2->tab_strip_model()->count(), 1);
  EXPECT_EQ(app_browser->tab_strip_model()->count(), 2);
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest,
                       OnlyNavigateHomeTabIfDifferentUrl) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect app opened with pinned home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);

  // Execute some JS to set a variable.
  EXPECT_EQ(nullptr, EvalJs(tab_strip->GetWebContentsAt(0), "var test = 5"));

  // Navigate to a non home tab URL.
  OpenUrlAndWait(app_browser,
                 embedded_test_server()->GetURL("/web_apps/get_manifest.html"));
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 1);

  // Navigate to home tab using the same URL.
  app_browser->OpenURL(OpenURLParams(start_url, content::Referrer(),
                                     WindowOpenDisposition::CURRENT_TAB,
                                     ui::PAGE_TRANSITION_LINK, false),
                       /*navigation_handle_callback=*/{});

  // Expect the JS variable to still be set, meaning the page was not navigated.
  EXPECT_EQ(true, EvalJs(tab_strip->GetWebContentsAt(0), "test == 5"));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);

  // Launch the app to the home tab using the same URL.
  app_browser = LaunchWebAppToURL(browser()->profile(), app_id, start_url);

  // Expect the JS variable to still be set, meaning the page was not navigated.
  EXPECT_EQ(true, EvalJs(tab_strip->GetWebContentsAt(0), "test == 5"));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, NoFavicons) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // No favicons shown for web apps.
  EXPECT_FALSE(tab_strip->delegate()->ShouldDisplayFavicon(
      tab_strip->GetActiveWebContents()));
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest,
                       OnlyThrottlePrimaryMainFrame) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();
  content::WebContents* web_contents = tab_strip->GetActiveWebContents();

  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  GURL iframe_url = embedded_test_server()->GetURL("/iframe_blank.html");
  GURL iframe_nav_url =
      embedded_test_server()->GetURL("/web_apps/get_manifest.html");

  content::TestNavigationObserver nav_observer(web_contents, 1);
  EXPECT_TRUE(
      content::NavigateIframeToURL(web_contents, "test", iframe_nav_url));
  nav_observer.Wait();

  // Expect the navigation happened in the iframe and no new tab was opened.
  EXPECT_EQ(iframe_nav_url, nav_observer.last_navigation_url());
  EXPECT_EQ(tab_strip->count(), 1);
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, DontCreateThrottleForReload) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();
  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Reload.
  content::TestNavigationObserver reload_nav_observer(
      tab_strip->GetActiveWebContents(), 1);
  chrome::Reload(app_browser, WindowOpenDisposition::CURRENT_TAB);
  reload_nav_observer.Wait();

  // Expect the reload did not cause a new tab to open.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, QueryParamsInStartUrl) {
  GURL start_url = embedded_test_server()->GetURL(
      "/web_apps/get_manifest.html?tab_strip_query_params_in_start_url.json");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect only the home tab was opened.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);

  // Navigate to start_url with different query params.
  OpenUrlAndWait(app_browser, embedded_test_server()->GetURL(
                                  "/web_apps/get_manifest.html?query=param"));

  // Expect navigation to happen in a new tab in that window.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->active_index(), 1);
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->GetWebContentsAt(1)->GetVisibleURL(),
            embedded_test_server()->GetURL(
                "/web_apps/get_manifest.html?query=param"));

  // Navigate to start_url.
  GURL start_url_with_fragment = embedded_test_server()->GetURL(
      "/web_apps/"
      "get_manifest.html?tab_strip_query_params_in_start_url.json#param");
  OpenUrlAndWait(app_browser, start_url_with_fragment);

  // Expect that the home tab is focused and the new tab.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(),
            start_url_with_fragment);
  EXPECT_EQ(tab_strip->GetWebContentsAt(1)->GetVisibleURL(),
            embedded_test_server()->GetURL(
                "/web_apps/get_manifest.html?query=param"));
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest,
                       OutOfScopeNavigationFromHomeTab) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect app opened with pinned home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);

  // Navigate to an out of scope URL.
  OpenUrlAndWait(app_browser, GURL("https://www.example.com"));

  // Expect URL to have opened in a new browser tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(
      browser()->tab_strip_model()->GetActiveWebContents()->GetVisibleURL(),
      GURL("https://www.example.com"));
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, TabbedModeMediaCSS) {
  GURL start_url = embedded_test_server()->GetURL(
      "/banners/"
      "manifest_test_page.html?manifest=manifest_tabbed_display_override.json");
  webapps::AppId app_id = InstallTestWebApp(start_url);

  Browser* app_browser = LaunchWebAppBrowser(app_id);
  content::WebContents* web_contents =
      app_browser->tab_strip_model()->GetActiveWebContents();

  std::string match_media_standalone =
      "window.matchMedia('(display-mode: standalone)').matches;";
  std::string match_media_tabbed =
      "window.matchMedia('(display-mode: tabbed)').matches;";
  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));
  ASSERT_FALSE(EvalJs(web_contents, match_media_standalone).ExtractBool());
  ASSERT_TRUE(EvalJs(web_contents, match_media_tabbed).ExtractBool());
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest,
                       CloseTabCommandDisabledForHomeTab) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect app opened with pinned home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);

  chrome::BrowserCommandController* commandController =
      app_browser->command_controller();
  // Close tab command should be enabled since the home tab is the only tab.
  EXPECT_TRUE(commandController->IsCommandEnabled(IDC_CLOSE_TAB));

  // Open a new tab.
  OpenUrlAndWait(app_browser,
                 embedded_test_server()->GetURL("/web_apps/get_manifest.html"));
  EXPECT_EQ(tab_strip->count(), 2);

  // Close tab command should be enabled since this is not the home tab.
  EXPECT_TRUE(commandController->IsCommandEnabled(IDC_CLOSE_TAB));

  tab_strip->ActivateTabAt(0);

  // Close tab command should not be enabled for home tab when there are
  // multiple tabs open.
  EXPECT_FALSE(commandController->IsCommandEnabled(IDC_CLOSE_TAB));
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest,
                       MiddleClickDoesntCloseHomeTab) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip_model = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect app opened with pinned home tab.
  EXPECT_EQ(tab_strip_model->count(), 1);
  EXPECT_TRUE(tab_strip_model->IsTabPinned(0));
  EXPECT_EQ(tab_strip_model->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip_model->active_index(), 0);

  BrowserView* view = BrowserView::GetBrowserViewForBrowser(app_browser);

  // Open another tab.
  OpenUrlAndWait(app_browser,
                 embedded_test_server()->GetURL("/web_apps/get_manifest.html"));
  EXPECT_EQ(tab_strip_model->count(), 2);

  // Home tab should not be closable.
  view->tabstrip()->CloseTab(view->tabstrip()->tab_at(0),
                             CloseTabSource::kFromMouse);
  EXPECT_EQ(tab_strip_model->count(), 2);

  // Non home tab should be closable.
  view->tabstrip()->CloseTab(view->tabstrip()->tab_at(1),
                             CloseTabSource::kFromMouse);
  EXPECT_EQ(tab_strip_model->count(), 1);

  // The home tab is the only tab open so it can be closed.
  view->tabstrip()->CloseTab(view->tabstrip()->tab_at(0),
                             CloseTabSource::kFromMouse);
  EXPECT_EQ(tab_strip_model->count(), 0);
}

// Tests that the home tab is not closable unless it is the only tab left in the
// window. This is to ensure that window.close() does not close the home tab.
IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, HomeTabCantBeClosedUsingJS) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect app opened with pinned home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);

  UnloadController unload_controller(app_browser);

  // Check that the home tab can be closed since it is the only tab in the
  // window.
  EXPECT_TRUE(
      unload_controller.CanCloseContents(tab_strip->GetWebContentsAt(0)));

  // Open another tab.
  OpenUrlAndWait(app_browser,
                 embedded_test_server()->GetURL("/web_apps/get_manifest.html"));
  EXPECT_EQ(tab_strip->count(), 2);

  // Check that the home tab can't be closed.
  EXPECT_FALSE(
      unload_controller.CanCloseContents(tab_strip->GetWebContentsAt(0)));

  // Check that non home tab is closable.
  EXPECT_TRUE(
      unload_controller.CanCloseContents(tab_strip->GetWebContentsAt(1)));
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, HomeTabScopeSegmentWildcard) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect app opened with pinned home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);
  // Navigate to an out of home tab scope URL.
  OpenUrlAndWait(app_browser,
                 embedded_test_server()->GetURL("/web_apps/favicon_only.html"));
  // Expect URL to have opened in a new  tab.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 1);

  // Navigate to an in home tab scope URL.
  OpenUrlAndWait(app_browser, embedded_test_server()->GetURL(
                                  "/web_apps/title_appname_prefix.html"));
  // Expect it was opened in home tab.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_EQ(
      tab_strip->GetActiveWebContents()->GetVisibleURL(),
      embedded_test_server()->GetURL("/web_apps/title_appname_prefix.html"));

  // Navigate to start_url.
  OpenUrlAndWait(app_browser, start_url);
  // Expect it was opened in home tab.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_EQ(tab_strip->GetActiveWebContents()->GetVisibleURL(), start_url);
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, HomeTabScopeFixedString) {
  GURL start_url = embedded_test_server()->GetURL(
      "/web_apps/get_manifest.html?tab_strip_fixed_home_scope.json");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect app opened with pinned home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);

  // Navigate to an in home tab scope URL.
  OpenUrlAndWait(app_browser, embedded_test_server()->GetURL(
                                  "/web_apps/title_appname_prefix.html"));
  // Expect it was opened in home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_EQ(
      tab_strip->GetActiveWebContents()->GetVisibleURL(),
      embedded_test_server()->GetURL("/web_apps/title_appname_prefix.html"));

  // Navigate to an out of home tab scope URL.
  OpenUrlAndWait(app_browser,
                 embedded_test_server()->GetURL("/web_apps/favicon_only.html"));
  // Expect URL to have opened in a new  tab.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 1);

  // Navigate to another in home tab scope URL.
  OpenUrlAndWait(app_browser, embedded_test_server()->GetURL(
                                  "/web_apps/tab_strip_customizations.html"));
  // Expect it was opened in home tab.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_EQ(tab_strip->GetActiveWebContents()->GetVisibleURL(),
            embedded_test_server()->GetURL(
                "/web_apps/tab_strip_customizations.html"));

  // Navigate to start_url.
  OpenUrlAndWait(app_browser, start_url);
  // Expect it was opened in home tab.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_EQ(tab_strip->GetActiveWebContents()->GetVisibleURL(), start_url);
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, HomeTabScopeWildcardString) {
  GURL start_url = embedded_test_server()->GetURL(
      "/web_apps/get_manifest.html?tab_strip_wildcard_home_scope.json");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect app opened with pinned home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);

  // Navigate to an in home tab scope URL.
  OpenUrlAndWait(app_browser, embedded_test_server()->GetURL(
                                  "/web_apps/title_appname_prefix.html"));
  // Expect it was opened in home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_EQ(
      tab_strip->GetActiveWebContents()->GetVisibleURL(),
      embedded_test_server()->GetURL("/web_apps/title_appname_prefix.html"));

  // Navigate to an out of home tab scope URL.
  OpenUrlAndWait(app_browser,
                 embedded_test_server()->GetURL("/banners/theme-color.html"));
  // Expect URL to have opened in a new  tab.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 1);

  // Navigate to another in home tab scope URL.
  OpenUrlAndWait(app_browser, embedded_test_server()->GetURL(
                                  "/web_apps/standalone/basic.html"));
  // Expect it was opened in home tab.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_EQ(tab_strip->GetActiveWebContents()->GetVisibleURL(),
            embedded_test_server()->GetURL("/web_apps/standalone/basic.html"));

  // Navigate to start_url.
  OpenUrlAndWait(app_browser, start_url);
  // Expect it was opened in home tab.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_EQ(tab_strip->active_index(), 0);
  EXPECT_EQ(tab_strip->GetActiveWebContents()->GetVisibleURL(), start_url);
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, CloseAllTabsCommand) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect app opened with pinned home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);

  chrome::NewTab(app_browser);
  chrome::NewTab(app_browser);
  chrome::NewTab(app_browser);

  EXPECT_EQ(tab_strip->count(), 4);

  tab_strip->ExecuteContextMenuCommand(
      2, TabStripModel::ContextMenuCommand::CommandCloseAllTabs);

  // Expect all tabs closed except the home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest,
                       NewTabButtonUrlInHomeTabScope) {
  GURL start_url = embedded_test_server()->GetURL(
      "/web_apps/get_manifest.html?tab_strip_wildcard_home_scope.json");
  webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* app_browser = LaunchWebAppBrowser(app_id);

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  EXPECT_TRUE(app_browser->app_controller()->ShouldHideNewTabButton());

  WebAppMenuModel model(nullptr, app_browser);
  model.Init();
  // Check menu does not contain 'New Tab'.
  EXPECT_FALSE(model.GetIndexOfCommandId(IDC_NEW_TAB).has_value());
}

// Tests that middle clicking links to the home tab, opens the link in the home
// tab.
IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, MiddleClickHomeTabLink) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallWebAppFromPage(browser(), start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect app opened with pinned home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);

  // Middle click home tab link from the home tab.
  app_browser->OpenURL(OpenURLParams(start_url, content::Referrer(),
                                     WindowOpenDisposition::NEW_BACKGROUND_TAB,
                                     ui::PAGE_TRANSITION_LINK, false),
                       /*navigation_handle_callback=*/{});

  // Check we stayed in the home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);

  chrome::NewTab(app_browser);

  // Middle click home tab link from a different tab.
  app_browser->OpenURL(OpenURLParams(start_url, content::Referrer(),
                                     WindowOpenDisposition::NEW_BACKGROUND_TAB,
                                     ui::PAGE_TRANSITION_LINK, false),
                       /*navigation_handle_callback=*/{});

  // Check it was opened in the home tab.
  EXPECT_EQ(tab_strip->count(), 2);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);
}

// Tests the page title, which is used for accessibility.
IN_PROC_BROWSER_TEST_P(WebAppTabStripBrowserTest, PageTitle) {
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  webapps::AppId app_id = InstallWebAppFromPage(browser(), start_url);
  Browser* app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  TabStripModel* tab_strip = app_browser->tab_strip_model();

  EXPECT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));

  // Expect app opened with pinned home tab.
  EXPECT_EQ(tab_strip->count(), 1);
  EXPECT_TRUE(tab_strip->IsTabPinned(0));
  EXPECT_EQ(tab_strip->GetWebContentsAt(0)->GetVisibleURL(), start_url);
  EXPECT_EQ(tab_strip->active_index(), 0);

  BrowserView* browser_view =
      BrowserView::GetBrowserViewForBrowser(app_browser);

  // The tab title starts with the tab name, followed by whether it is pinned
  // but may also have more things after that.
  EXPECT_TRUE(base::StartsWith(browser_view->GetAccessibleTabLabel(0),
                               u"Tab Strip Customizations - Pinned"));

  chrome::NewTab(app_browser);
  content::WaitForLoadStop(tab_strip->GetActiveWebContents());

  EXPECT_TRUE(base::StartsWith(browser_view->GetAccessibleTabLabel(0),
                               u"Tab Strip Customizations - Pinned"));
  EXPECT_TRUE(base::StartsWith(browser_view->GetAccessibleTabLabel(1),
                               u"Favicon only"));
}

#if BUILDFLAG(IS_CHROMEOS)

// Browser tests that verify web-app tab strip behavior when locked (and not
// locked) for OnTask. Only relevant for non-web browser scenarios.
using WebAppTabStripForOnTaskBrowserTest = WebAppTabStripBrowserTest;

IN_PROC_BROWSER_TEST_P(WebAppTabStripForOnTaskBrowserTest,
                       MiddleClickDoesNotCloseTabsWhenLockedForOnTask) {
  // Set up app and lock the app for OnTask.
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  const webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* const app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  app_browser->SetLockedForOnTask(true);

  const TabStripModel* const tab_strip_model = app_browser->tab_strip_model();
  ASSERT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));
  ASSERT_EQ(tab_strip_model->count(), 1);
  ASSERT_TRUE(tab_strip_model->IsTabPinned(0));

  // Open another tab so we can test tab close behavior on both home and
  // non-home tabs.
  OpenUrlAndWait(app_browser,
                 embedded_test_server()->GetURL("/web_apps/get_manifest.html"));
  ASSERT_EQ(tab_strip_model->count(), 2);

  // Verify home tab cannot be closed.
  auto* const tab_strip =
      BrowserView::GetBrowserViewForBrowser(app_browser)->tabstrip();
  tab_strip->CloseTab(tab_strip->tab_at(0), CloseTabSource::kFromMouse);
  ASSERT_EQ(tab_strip_model->count(), 2);

  // Also verify the non-home tab cannot be closed.
  tab_strip->CloseTab(tab_strip->tab_at(1), CloseTabSource::kFromMouse);
  EXPECT_EQ(tab_strip_model->count(), 2);
}

IN_PROC_BROWSER_TEST_P(WebAppTabStripForOnTaskBrowserTest,
                       MiddleClickCanCloseTabsWhenNotLockedForOnTask) {
  // Set up app and do not lock the app for OnTask.
  GURL start_url =
      embedded_test_server()->GetURL("/web_apps/tab_strip_customizations.html");
  const webapps::AppId app_id = InstallTestWebApp(start_url);
  Browser* const app_browser = FindWebAppBrowser(browser()->profile(), app_id);
  app_browser->SetLockedForOnTask(false);

  const TabStripModel* const tab_strip_model = app_browser->tab_strip_model();
  ASSERT_TRUE(registrar().IsTabbedWindowModeEnabled(app_id));
  ASSERT_EQ(tab_strip_model->count(), 1);
  ASSERT_TRUE(tab_strip_model->IsTabPinned(0));

  // Open another tab so we can test tab close behavior on both home and
  // non-home tabs.
  OpenUrlAndWait(app_browser,
                 embedded_test_server()->GetURL("/web_apps/get_manifest.html"));
  ASSERT_EQ(tab_strip_model->count(), 2);

  // Verify home tab cannot be closed (default behavior on tabbed web apps).
  auto* const tab_strip =
      BrowserView::GetBrowserViewForBrowser(app_browser)->tabstrip();
  tab_strip->CloseTab(tab_strip->tab_at(0), CloseTabSource::kFromMouse);
  ASSERT_EQ(tab_strip_model->count(), 2);

  // Verify the non-home tab can be closed.
  tab_strip->CloseTab(tab_strip->tab_at(1), CloseTabSource::kFromMouse);
  ASSERT_EQ(tab_strip_model->count(), 1);

  // The home tab is the only tab open so it can be closed now.
  tab_strip->CloseTab(tab_strip->tab_at(0), CloseTabSource::kFromMouse);
  EXPECT_EQ(tab_strip_model->count(), 0);
}

INSTANTIATE_TEST_SUITE_P(
    All,
    WebAppTabStripForOnTaskBrowserTest,
    testing::Values(apps::test::LinkCapturingFeatureVersion::kV1DefaultOff,
                    apps::test::LinkCapturingFeatureVersion::kV2DefaultOff),
    apps::test::LinkCapturingVersionToString);

#endif  // BUILDFLAG(IS_CHROMEOS)

INSTANTIATE_TEST_SUITE_P(
    All,
    WebAppTabStripBrowserTest,
#if BUILDFLAG(IS_CHROMEOS)
    testing::Values(apps::test::LinkCapturingFeatureVersion::kV1DefaultOff,
                    apps::test::LinkCapturingFeatureVersion::kV2DefaultOff)
#else
    testing::Values(apps::test::LinkCapturingFeatureVersion::kV2DefaultOff,
                    apps::test::LinkCapturingFeatureVersion::kV2DefaultOn)
#endif  // BUILDFLAG(IS_CHROMEOS)
        ,
    apps::test::LinkCapturingVersionToString);

}  // namespace web_app