File: startup_browser_creator_impl_unittest.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 (615 lines) | stat: -rw-r--r-- 27,496 bytes parent folder | download | duplicates (3)
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
// Copyright 2016 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/startup/startup_browser_creator_impl.h"

#include "base/command_line.h"
#include "build/build_config.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/ui/startup/startup_tab_provider.h"
#include "chrome/common/url_constants.h"
#include "components/signin/public/base/signin_switches.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using Creator = StartupBrowserCreatorImpl;

namespace {

// Bits for FakeStartupTabProvider options.
constexpr uint32_t kOnboardingTabs = 1 << 0;
constexpr uint32_t kDistributionFirstRunTabs = 1 << 1;
constexpr uint32_t kResetTriggerTabs = 1 << 2;
constexpr uint32_t kPinnedTabs = 1 << 3;
constexpr uint32_t kPreferencesTabs = 1 << 4;
constexpr uint32_t kNewTabPageTabs = 1 << 5;
constexpr uint32_t kPostCrashTabs = 1 << 6;
constexpr uint32_t kCommandLineTabs = 1 << 7;
#if !BUILDFLAG(IS_ANDROID)
constexpr uint32_t kNewFeaturesTabs = 1 << 8;
constexpr uint32_t kPrivacySandboxTabs = 1 << 9;
#endif  // !BUILDFLAG(IS_ANDROID)

class FakeStartupTabProvider : public StartupTabProvider {
 public:
  // For each option passed, the corresponding adder below will add a sentinel
  // tab and return true. For options not passed, the adder will return false.
  explicit FakeStartupTabProvider(uint32_t options) : options_(options) {}

  StartupTabs GetDistributionFirstRunTabs(
      StartupBrowserCreator* browser_creator) const override {
    StartupTabs tabs;
    if (options_ & kDistributionFirstRunTabs) {
      tabs.emplace_back(GURL("https://distribution"));
    }
    return tabs;
  }

  StartupTabs GetResetTriggerTabs(Profile* profile) const override {
    StartupTabs tabs;
    if (options_ & kResetTriggerTabs) {
      tabs.emplace_back(GURL("https://reset-trigger"));
    }
    return tabs;
  }

  StartupTabs GetPinnedTabs(const base::CommandLine& command_line_,
                            Profile* profile) const override {
    StartupTabs tabs;
    if (options_ & kPinnedTabs) {
      tabs.emplace_back(GURL("https://pinned"), StartupTab::Type::kPinned);
    }
    return tabs;
  }

  StartupTabs GetPreferencesTabs(const base::CommandLine& command_line_,
                                 Profile* profile) const override {
    StartupTabs tabs;
    if (options_ & kPreferencesTabs) {
      tabs.emplace_back(GURL("https://prefs"));
    }
    return tabs;
  }

  StartupTabs GetNewTabPageTabs(const base::CommandLine& command_line_,
                                Profile* profile) const override {
    StartupTabs tabs;
    if (options_ & kNewTabPageTabs) {
      tabs.emplace_back(GURL("https://new-tab"));
    }
    return tabs;
  }

  StartupTabs GetPostCrashTabs(
      bool has_incompatible_applications) const override {
    StartupTabs tabs;
    if (has_incompatible_applications && (options_ & kPostCrashTabs)) {
      tabs.emplace_back(GURL("https://incompatible-applications"));
    }
    return tabs;
  }

  StartupTabs GetCommandLineTabs(const base::CommandLine& command_line,
                                 const base::FilePath& cur_dir,
                                 Profile* profile) const override {
    StartupTabs tabs;
    if (options_ & kCommandLineTabs) {
      tabs.emplace_back(GURL("https://cmd-line"));
    }
    return tabs;
  }

  CommandLineTabsPresent HasCommandLineTabs(
      const base::CommandLine& command_line,
      const base::FilePath& cur_dir) const override {
    return (options_ & kCommandLineTabs) ? CommandLineTabsPresent::kYes
                                         : CommandLineTabsPresent::kNo;
  }

#if !BUILDFLAG(IS_ANDROID)
  StartupTabs GetNewFeaturesTabs(bool whats_new_enabled) const override {
    StartupTabs tabs;
    if (options_ & kNewFeaturesTabs) {
      tabs.emplace_back(GURL("https://whats-new/"));
    }
    return tabs;
  }

  StartupTabs GetPrivacySandboxTabs(
      Profile* profile,
      const StartupTabs& other_startup_tabs) const override {
    StartupTabs tabs;
    if (options_ & kPrivacySandboxTabs) {
      tabs.emplace_back(GURL("https://privacy-sandbox"));
    }
    return tabs;
  }
#endif  // !BUILDFLAG(IS_ANDROID)

 private:
  const uint32_t options_;
};

}  // namespace

// Comparing a `StartupTab` with a string will compare the tab's host with that
// string. This is used to compare lists more easily via
// `testing::ElementsAreArray`.
bool operator==(const StartupTab& actual_tab,
                const std::string& expected_host) {
  return actual_tab.url.host() == expected_host;
}

class StartupBrowserCreatorImplTest : public testing::Test {
 public:
  StartupBrowserCreatorImplTest() = default;
};

// "Standard" case: Tabs specified in onboarding, reset trigger, pinned tabs, or
// preferences shouldn't interfere with each other. Nothing specified on the
// command line. Reset trigger always appears first.
TEST_F(StartupBrowserCreatorImplTest, DetermineStartupTabs) {
  using LaunchResult = Creator::LaunchResult;

  FakeStartupTabProvider provider(kOnboardingTabs | kResetTriggerTabs |
                                  kPinnedTabs | kPreferencesTabs |
                                  kNewTabPageTabs);
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  Creator impl(base::FilePath(), command_line,
               chrome::startup::IsFirstRun::kYes);

  auto output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false, /*is_post_crash_launch=*/false,
      /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/false,
      /*privacy_sandbox_confirmation_required=*/false);
  EXPECT_EQ(LaunchResult::kNormally, output.launch_result);

  std::vector<std::string> expected_tab_hosts;
  expected_tab_hosts.emplace_back("reset-trigger");
  expected_tab_hosts.emplace_back("prefs");
  expected_tab_hosts.emplace_back("pinned");
  EXPECT_THAT(output.tabs, testing::ElementsAreArray(expected_tab_hosts));

  // No extra onboarding content for managed starts.
  output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false, /*is_post_crash_launch=*/false,
      /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/false, /*whats_new_enabled=*/false,
      /*privacy_sandbox_confirmation_required=*/false);
  EXPECT_EQ(LaunchResult::kNormally, output.launch_result);

  ASSERT_EQ(3U, output.tabs.size());
  EXPECT_EQ("reset-trigger", output.tabs[0].url.host());
  EXPECT_EQ("prefs", output.tabs[1].url.host());
  EXPECT_EQ("pinned", output.tabs[2].url.host());

  // No onboarding if not enabled even if promo is allowed.
  output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false, /*is_post_crash_launch=*/false,
      /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/false,
      /*privacy_sandbox_confirmation_required=*/false);
  EXPECT_EQ(LaunchResult::kNormally, output.launch_result);

  ASSERT_EQ(3U, output.tabs.size());
  EXPECT_EQ("reset-trigger", output.tabs[0].url.host());
  EXPECT_EQ("prefs", output.tabs[1].url.host());
  EXPECT_EQ("pinned", output.tabs[2].url.host());
}

// Only the New Tab Page should appear in Incognito mode, skipping all the usual
// tabs.
TEST_F(StartupBrowserCreatorImplTest, DetermineStartupTabs_Incognito) {
  FakeStartupTabProvider provider(kOnboardingTabs | kDistributionFirstRunTabs |
                                  kResetTriggerTabs | kPinnedTabs |
                                  kPreferencesTabs | kNewTabPageTabs |
                                  kNewFeaturesTabs | kPrivacySandboxTabs);
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  Creator impl(base::FilePath(), command_line,
               chrome::startup::IsFirstRun::kYes);

  auto output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/true, /*is_post_crash_launch=*/false,
      /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(Creator::LaunchResult::kNormally, output.launch_result);
  ASSERT_EQ(1U, output.tabs.size());
  // Check for the actual NTP URL, rather than the sentinel returned by the
  // fake, because the Provider is ignored entirely when short-circuited by
  // incognito logic.
  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), output.tabs[0].url);
}

// Also only show the New Tab Page after a crash, except if there is a
// problem application.
TEST_F(StartupBrowserCreatorImplTest, DetermineStartupTabs_Crash) {
  FakeStartupTabProvider provider(
      kOnboardingTabs | kDistributionFirstRunTabs | kResetTriggerTabs |
      kPinnedTabs | kPreferencesTabs | kNewTabPageTabs | kNewFeaturesTabs |
      kPostCrashTabs | kPrivacySandboxTabs);
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  Creator impl(base::FilePath(), command_line,
               chrome::startup::IsFirstRun::kYes);

  // Regular Crash Recovery case:
  auto output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false, /*is_post_crash_launch=*/true,
      /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(Creator::LaunchResult::kNormally, output.launch_result);

  ASSERT_EQ(1U, output.tabs.size());
  // Check for the actual NTP URL, rather than the sentinel returned by the
  // fake, because the Provider is ignored entirely when short-circuited by
  // the post-crash logic.
  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), output.tabs[0].url);

  // Crash Recovery case with problem applications:
  output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false, /*is_post_crash_launch=*/true,
      /*has_incompatible_applications=*/true, /*promotional_tabs_enabled=*/true,
      /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(Creator::LaunchResult::kNormally, output.launch_result);

  ASSERT_EQ(1U, output.tabs.size());
  EXPECT_EQ(GURL("https://incompatible-applications"), output.tabs[0].url);
}

// If initial preferences specify content, this should block all other
// policies. The only exception is command line URLs, tested below.
TEST_F(StartupBrowserCreatorImplTest, DetermineStartupTabs_InitialPrefs) {
  FakeStartupTabProvider provider(kOnboardingTabs | kDistributionFirstRunTabs |
                                  kResetTriggerTabs | kPinnedTabs |
                                  kPreferencesTabs | kNewTabPageTabs |
                                  kNewFeaturesTabs | kPrivacySandboxTabs);
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  Creator impl(base::FilePath(), command_line,
               chrome::startup::IsFirstRun::kYes);

  auto output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false, /*is_post_crash_launch=*/false,
      /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(Creator::LaunchResult::kNormally, output.launch_result);
  ASSERT_EQ(1U, output.tabs.size());
  EXPECT_EQ("distribution", output.tabs[0].url.host());
}

// URLs specified on the command line should always appear, and should block
// all other tabs except the Reset Trigger tab.
TEST_F(StartupBrowserCreatorImplTest, DetermineStartupTabs_CommandLine) {
  using LaunchResult = Creator::LaunchResult;

  FakeStartupTabProvider provider(
      kOnboardingTabs | kDistributionFirstRunTabs | kResetTriggerTabs |
      kPinnedTabs | kPreferencesTabs | kNewTabPageTabs | kNewFeaturesTabs |
      kCommandLineTabs | kPrivacySandboxTabs);
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  Creator impl(base::FilePath(), command_line,
               chrome::startup::IsFirstRun::kYes);

  auto output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false, /*is_post_crash_launch=*/false,
      /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(LaunchResult::kWithGivenUrls, output.launch_result);

  ASSERT_EQ(3U, output.tabs.size());
  EXPECT_EQ("reset-trigger", output.tabs[0].url.host());
  EXPECT_EQ("cmd-line", output.tabs[1].url.host());
  EXPECT_EQ("pinned", output.tabs[2].url.host());

  // Also test that both incognito and crash recovery don't interfere with
  // command line tabs.

  // Incognito
  output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/true, /*is_post_crash_launch=*/false,
      /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(LaunchResult::kWithGivenUrls, output.launch_result);

  ASSERT_EQ(1U, output.tabs.size());
  EXPECT_EQ("cmd-line", output.tabs[0].url.host());

  // Crash Recovery
  output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false, /*is_post_crash_launch=*/true,
      /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(LaunchResult::kWithGivenUrls, output.launch_result);

  ASSERT_EQ(1U, output.tabs.size());
  EXPECT_EQ("cmd-line", output.tabs[0].url.host());

  // Crash Recovery with incompatible applications.
  output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false, /*is_post_crash_launch=*/true,
      /*has_incompatible_applications=*/true, /*promotional_tabs_enabled=*/true,
      /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(LaunchResult::kWithGivenUrls, output.launch_result);
  ASSERT_EQ(1U, output.tabs.size());
  EXPECT_EQ("cmd-line", output.tabs[0].url.host());
}

// New Tab Page should appear alongside pinned tabs and the reset trigger, but
// should be superseded by onboarding tabs and by tabs specified in preferences.
TEST_F(StartupBrowserCreatorImplTest, DetermineStartupTabs_NewTabPage) {
  FakeStartupTabProvider provider_allows_ntp(kPinnedTabs | kResetTriggerTabs |
                                             kNewTabPageTabs);
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  Creator impl(base::FilePath(), command_line,
               chrome::startup::IsFirstRun::kYes);

  auto output = impl.DetermineStartupTabs(
      provider_allows_ntp, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false,
      /*is_post_crash_launch=*/false, /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/false,
      /*privacy_sandbox_confirmation_required=*/false);
  EXPECT_EQ(Creator::LaunchResult::kNormally, output.launch_result);
  ASSERT_EQ(3U, output.tabs.size());
  EXPECT_EQ("reset-trigger", output.tabs[0].url.host());
  EXPECT_EQ("new-tab", output.tabs[1].url.host());
  EXPECT_EQ("pinned", output.tabs[2].url.host());
}

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
// If the user's preferences satisfy the conditions, show the What's New page
// upon startup.
TEST_F(StartupBrowserCreatorImplTest, DetermineStartupTabs_NewFeaturesPage) {
  using LaunchResult = Creator::LaunchResult;

  FakeStartupTabProvider provider(kNewTabPageTabs | kNewFeaturesTabs);
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  Creator impl(base::FilePath(), command_line,
               chrome::startup::IsFirstRun::kNo);

  auto output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false, /*is_post_crash_launch=*/false,
      /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(LaunchResult::kNormally, output.launch_result);
  ASSERT_EQ(2U, output.tabs.size());
  EXPECT_EQ("whats-new", output.tabs[0].url.host());
  EXPECT_EQ("new-tab", output.tabs[1].url.host());

  // New features can appear with prefs/pinned.
  FakeStartupTabProvider provider_with_pinned(
      kPinnedTabs | kPreferencesTabs | kNewTabPageTabs | kNewFeaturesTabs);
  output = impl.DetermineStartupTabs(
      provider_with_pinned, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false,
      /*is_post_crash_launch=*/false, /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(LaunchResult::kNormally, output.launch_result);
  ASSERT_EQ(3U, output.tabs.size());
  EXPECT_EQ("whats-new", output.tabs[0].url.host());
  EXPECT_EQ("prefs", output.tabs[1].url.host());
  EXPECT_EQ("pinned", output.tabs[2].url.host());

  // Onboarding overrides What's New.
  base::CommandLine first_run_command_line(base::CommandLine::NO_PROGRAM);
  Creator first_run_impl(base::FilePath(), first_run_command_line,
                         chrome::startup::IsFirstRun::kYes);
  FakeStartupTabProvider provider_with_onboarding(
      kOnboardingTabs | kNewTabPageTabs | kNewFeaturesTabs);
  output = first_run_impl.DetermineStartupTabs(
      provider_with_onboarding, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false,
      /*is_post_crash_launch=*/false, /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(LaunchResult::kNormally, output.launch_result);

  std::vector<std::string> expected_tab_hosts;
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
  expected_tab_hosts.emplace_back("new-tab");
#else
  // Onboarding is not supported and has no effect.
  expected_tab_hosts.emplace_back("whats-new");
  expected_tab_hosts.emplace_back("new-tab");
#endif
  EXPECT_THAT(output.tabs, testing::ElementsAreArray(expected_tab_hosts));
}
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)

#if !BUILDFLAG(IS_ANDROID)
// If required, a tab for the Privacy Sandbox dialog should be added.
TEST_F(StartupBrowserCreatorImplTest, DetermineStartupTabs_PrivacySandbox) {
  FakeStartupTabProvider provider(kNewTabPageTabs | kPrivacySandboxTabs);
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  Creator impl(base::FilePath(), command_line,
               chrome::startup::IsFirstRun::kNo);

  auto output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false, /*is_post_crash_launch=*/false,
      /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(Creator::LaunchResult::kNormally, output.launch_result);
  ASSERT_EQ(2U, output.tabs.size());
  EXPECT_EQ("new-tab", output.tabs[0].url.host());
  EXPECT_EQ("privacy-sandbox", output.tabs[1].url.host());

  // The tab for the Privacy Sandbox should be added even if promotional tabs
  // are disabled.
  output = impl.DetermineStartupTabs(
      provider, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false, /*is_post_crash_launch=*/false,
      /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/false, /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(Creator::LaunchResult::kNormally, output.launch_result);
  ASSERT_EQ(2U, output.tabs.size());
  EXPECT_EQ("new-tab", output.tabs[0].url.host());
  EXPECT_EQ("privacy-sandbox", output.tabs[1].url.host());

  // A Privacy Sandbox tab should be able to appear alongside other
  // prefs/pinned/feature tabs.
  FakeStartupTabProvider provider_pinned_prefs_features(
      kPinnedTabs | kPreferencesTabs | kNewTabPageTabs | kNewFeaturesTabs |
      kPrivacySandboxTabs);
  output = impl.DetermineStartupTabs(
      provider_pinned_prefs_features, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false, /*is_post_crash_launch=*/false,
      /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(Creator::LaunchResult::kNormally, output.launch_result);
  ASSERT_EQ(4U, output.tabs.size());
  EXPECT_EQ("whats-new", output.tabs[0].url.host());
  EXPECT_EQ("prefs", output.tabs[1].url.host());
  EXPECT_EQ("privacy-sandbox", output.tabs[2].url.host());
  EXPECT_EQ("pinned", output.tabs[3].url.host());

  // Any onboarding tabs should prevent a Privacy Sandbox tab being added.
  FakeStartupTabProvider provider_with_onboarding(
      kOnboardingTabs | kNewTabPageTabs | kPrivacySandboxTabs);
  base::CommandLine first_run_command_line(base::CommandLine::NO_PROGRAM);
  Creator first_run_impl(base::FilePath(), first_run_command_line,
                         chrome::startup::IsFirstRun::kYes);
  output = first_run_impl.DetermineStartupTabs(
      provider_with_onboarding, chrome::startup::IsProcessStartup::kYes,
      /*is_ephemeral_profile=*/false,
      /*is_post_crash_launch=*/false, /*has_incompatible_applications=*/false,
      /*promotional_tabs_enabled=*/true, /*whats_new_enabled=*/true,
      /*privacy_sandbox_confirmation_required=*/true);
  EXPECT_EQ(Creator::LaunchResult::kNormally, output.launch_result);
  std::vector<std::string> expected_tab_hosts = {"new-tab", "privacy-sandbox"};
  EXPECT_THAT(output.tabs, testing::ElementsAreArray(expected_tab_hosts));
}

#endif  // !BUILDFLAG(IS_ANDROID)

TEST_F(StartupBrowserCreatorImplTest, DetermineBrowserOpenBehavior_Startup) {
  SessionStartupPref pref_default(SessionStartupPref::Type::DEFAULT);
  SessionStartupPref pref_last(SessionStartupPref::Type::LAST);
  SessionStartupPref pref_urls(SessionStartupPref::Type::URLS);
  SessionStartupPref pref_last_and_urls(
      SessionStartupPref::Type::LAST_AND_URLS);

  // The most typical case: startup, not recovering from a crash, no switches.
  // Test each pref with and without command-line tabs.
  Creator::BrowserOpenBehavior output = Creator::DetermineBrowserOpenBehavior(
      pref_default, Creator::PROCESS_STARTUP);
  EXPECT_EQ(Creator::BrowserOpenBehavior::NEW, output);

  output = Creator::DetermineBrowserOpenBehavior(
      pref_default, Creator::PROCESS_STARTUP | Creator::HAS_CMD_LINE_TABS);
  EXPECT_EQ(Creator::BrowserOpenBehavior::NEW, output);

  output = Creator::DetermineBrowserOpenBehavior(pref_urls,
                                                 Creator::PROCESS_STARTUP);
  EXPECT_EQ(Creator::BrowserOpenBehavior::NEW, output);

  output = Creator::DetermineBrowserOpenBehavior(
      pref_urls, Creator::PROCESS_STARTUP | Creator::HAS_CMD_LINE_TABS);
  EXPECT_EQ(Creator::BrowserOpenBehavior::NEW, output);

  output = Creator::DetermineBrowserOpenBehavior(pref_last,
                                                 Creator::PROCESS_STARTUP);
  EXPECT_EQ(Creator::BrowserOpenBehavior::SYNCHRONOUS_RESTORE, output);

  output = Creator::DetermineBrowserOpenBehavior(
      pref_last, Creator::PROCESS_STARTUP | Creator::HAS_CMD_LINE_TABS);
  EXPECT_EQ(Creator::BrowserOpenBehavior::SYNCHRONOUS_RESTORE, output);

  output = Creator::DetermineBrowserOpenBehavior(pref_last_and_urls,
                                                 Creator::PROCESS_STARTUP);
  EXPECT_EQ(Creator::BrowserOpenBehavior::SYNCHRONOUS_RESTORE, output);

  output = Creator::DetermineBrowserOpenBehavior(
      pref_last_and_urls,
      Creator::PROCESS_STARTUP | Creator::HAS_CMD_LINE_TABS);
  EXPECT_EQ(Creator::BrowserOpenBehavior::SYNCHRONOUS_RESTORE, output);
}

TEST_F(StartupBrowserCreatorImplTest,
       DetermineBrowserOpenBehavior_CmdLineTabs) {
  SessionStartupPref pref_default(SessionStartupPref::Type::DEFAULT);
  SessionStartupPref pref_last(SessionStartupPref::Type::LAST);
  SessionStartupPref pref_urls(SessionStartupPref::Type::URLS);
  SessionStartupPref pref_last_and_urls(
      SessionStartupPref::Type::LAST_AND_URLS);

  // Command line tabs after startup should prompt use of existing window,
  // regardless of pref.
  Creator::BrowserOpenBehavior output = Creator::DetermineBrowserOpenBehavior(
      pref_default, Creator::HAS_CMD_LINE_TABS);
  EXPECT_EQ(Creator::BrowserOpenBehavior::USE_EXISTING, output);

  output = Creator::DetermineBrowserOpenBehavior(pref_urls,
                                                 Creator::HAS_CMD_LINE_TABS);
  EXPECT_EQ(Creator::BrowserOpenBehavior::USE_EXISTING, output);

  output = Creator::DetermineBrowserOpenBehavior(pref_last,
                                                 Creator::HAS_CMD_LINE_TABS);
  EXPECT_EQ(Creator::BrowserOpenBehavior::USE_EXISTING, output);

  output = Creator::DetermineBrowserOpenBehavior(pref_last_and_urls,
                                                 Creator::HAS_CMD_LINE_TABS);
  EXPECT_EQ(Creator::BrowserOpenBehavior::USE_EXISTING, output);

  // Exception: this can be overridden by passing a switch.
  output = Creator::DetermineBrowserOpenBehavior(
      pref_urls, Creator::HAS_NEW_WINDOW_SWITCH | Creator::HAS_CMD_LINE_TABS);
  EXPECT_EQ(Creator::BrowserOpenBehavior::NEW, output);
}

TEST_F(StartupBrowserCreatorImplTest, DetermineBrowserOpenBehavior_PostCrash) {
  SessionStartupPref pref_last(SessionStartupPref::Type::LAST);

  // Launching after crash should block session restore.
  Creator::BrowserOpenBehavior output = Creator::DetermineBrowserOpenBehavior(
      pref_last, Creator::PROCESS_STARTUP | Creator::IS_POST_CRASH_LAUNCH);
  EXPECT_EQ(Creator::BrowserOpenBehavior::NEW, output);
}

TEST_F(StartupBrowserCreatorImplTest, DetermineBrowserOpenBehavior_NotStartup) {
  SessionStartupPref pref_default(SessionStartupPref::Type::DEFAULT);
  SessionStartupPref pref_last(SessionStartupPref::Type::LAST);
  SessionStartupPref pref_urls(SessionStartupPref::Type::URLS);
  SessionStartupPref pref_last_and_urls(
      SessionStartupPref::Type::LAST_AND_URLS);

  // Launch after startup without command-line tabs should always create a new
  // window.
  Creator::BrowserOpenBehavior output =
      Creator::DetermineBrowserOpenBehavior(pref_default, 0);
  EXPECT_EQ(Creator::BrowserOpenBehavior::NEW, output);

  output = Creator::DetermineBrowserOpenBehavior(pref_last, 0);
  EXPECT_EQ(Creator::BrowserOpenBehavior::NEW, output);

  output = Creator::DetermineBrowserOpenBehavior(pref_urls, 0);
  EXPECT_EQ(Creator::BrowserOpenBehavior::NEW, output);

  output = Creator::DetermineBrowserOpenBehavior(pref_last_and_urls, 0);
  EXPECT_EQ(Creator::BrowserOpenBehavior::NEW, output);
}