OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/ui/startup/startup_tab_provider.h" |
| 9 #include "chrome/common/url_constants.cc" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Bits for FakeStartupTabProvider options. |
| 15 constexpr uint32_t kOnboardingTabs = 0x01; |
| 16 constexpr uint32_t kDistributionFirstRunTabs = 0x02; |
| 17 constexpr uint32_t kResetTriggerTabs = 0x04; |
| 18 constexpr uint32_t kPinnedTabs = 0x08; |
| 19 constexpr uint32_t kPreferencesTabs = 0x10; |
| 20 |
| 21 class FakeStartupTabProvider : public StartupTabProvider { |
| 22 public: |
| 23 // For each option passed, the corresponding adder below will add a sentinel |
| 24 // tab and return true. For options not passed, the adder will return false. |
| 25 explicit FakeStartupTabProvider(uint32_t options) : options_(options) {} |
| 26 |
| 27 StartupTabs GetOnboardingTabs() const override { |
| 28 StartupTabs tabs; |
| 29 if (options_ & kOnboardingTabs) |
| 30 tabs.emplace_back(GURL("https://onboarding"), false); |
| 31 return tabs; |
| 32 } |
| 33 |
| 34 StartupTabs GetDistributionFirstRunTabs( |
| 35 StartupBrowserCreator* browser_creator) const override { |
| 36 StartupTabs tabs; |
| 37 if (options_ & kDistributionFirstRunTabs) |
| 38 tabs.emplace_back(GURL("https://distribution"), false); |
| 39 return tabs; |
| 40 } |
| 41 |
| 42 StartupTabs GetResetTriggerTabs(Profile* profile) const override { |
| 43 StartupTabs tabs; |
| 44 if (options_ & kResetTriggerTabs) |
| 45 tabs.emplace_back(GURL("https://reset-trigger"), false); |
| 46 return tabs; |
| 47 } |
| 48 |
| 49 StartupTabs GetPinnedTabs() const override { |
| 50 StartupTabs tabs; |
| 51 if (options_ & kPinnedTabs) |
| 52 tabs.emplace_back(GURL("https://pinned"), true); |
| 53 return tabs; |
| 54 } |
| 55 |
| 56 StartupTabs GetPreferencesTabs() const override { |
| 57 StartupTabs tabs; |
| 58 if (options_ & kPreferencesTabs) |
| 59 tabs.emplace_back(GURL("https://prefs"), false); |
| 60 return tabs; |
| 61 } |
| 62 |
| 63 private: |
| 64 const uint32_t options_; |
| 65 }; |
| 66 |
| 67 } // namespace |
| 68 |
| 69 // "Standard" case: Tabs specified in onboarding, reset trigger, pinned tabs, or |
| 70 // preferences shouldn't interfere with each other. Nothing specified on the |
| 71 // command line. Reset trigger always appears first. |
| 72 TEST(StartupBrowserCreatorImplTest, DetermineStartupTabs) { |
| 73 FakeStartupTabProvider provider(kOnboardingTabs | kResetTriggerTabs | |
| 74 kPinnedTabs | kPreferencesTabs); |
| 75 StartupBrowserCreatorImpl impl( |
| 76 base::FilePath(), base::CommandLine(base::CommandLine::NO_PROGRAM), |
| 77 chrome::startup::IS_FIRST_RUN); |
| 78 |
| 79 StartupTabs output = |
| 80 impl.DetermineStartupTabs(provider, StartupTabs(), false, false); |
| 81 ASSERT_EQ(4U, output.size()); |
| 82 EXPECT_EQ("reset-trigger", output[0].url.host()); |
| 83 EXPECT_EQ("onboarding", output[1].url.host()); |
| 84 EXPECT_EQ("prefs", output[2].url.host()); |
| 85 EXPECT_EQ("pinned", output[3].url.host()); |
| 86 } |
| 87 |
| 88 // All content is blocked in Incognito mode, or when recovering from a crash. |
| 89 // Only the New Tab Page should appear in either case. |
| 90 TEST(StartupBrowserCreatorImplTest, DetermineStartupTabs_IncognitoOrCrash) { |
| 91 FakeStartupTabProvider provider(kOnboardingTabs | kDistributionFirstRunTabs | |
| 92 kResetTriggerTabs | kPinnedTabs | |
| 93 kPreferencesTabs); |
| 94 StartupBrowserCreatorImpl impl( |
| 95 base::FilePath(), base::CommandLine(base::CommandLine::NO_PROGRAM), |
| 96 chrome::startup::IS_FIRST_RUN); |
| 97 |
| 98 // Incognito case: |
| 99 StartupTabs output = |
| 100 impl.DetermineStartupTabs(provider, StartupTabs(), true, false); |
| 101 ASSERT_EQ(1U, output.size()); |
| 102 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), output[0].url); |
| 103 |
| 104 // Crash Recovery case: |
| 105 output = impl.DetermineStartupTabs(provider, StartupTabs(), false, true); |
| 106 ASSERT_EQ(1U, output.size()); |
| 107 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), output[0].url); |
| 108 } |
| 109 |
| 110 // If Master Preferences specifies content, this should block all other |
| 111 // policies. The only exception is command line URLs, tested below. |
| 112 TEST(StartupBrowserCreatorImplTest, DetermineStartupTabs_MasterPrefs) { |
| 113 FakeStartupTabProvider provider(kOnboardingTabs | kDistributionFirstRunTabs | |
| 114 kResetTriggerTabs | kPinnedTabs | |
| 115 kPreferencesTabs); |
| 116 StartupBrowserCreatorImpl impl( |
| 117 base::FilePath(), base::CommandLine(base::CommandLine::NO_PROGRAM), |
| 118 chrome::startup::IS_FIRST_RUN); |
| 119 |
| 120 StartupTabs output = |
| 121 impl.DetermineStartupTabs(provider, StartupTabs(), false, false); |
| 122 ASSERT_EQ(1U, output.size()); |
| 123 EXPECT_EQ("distribution", output[0].url.host()); |
| 124 } |
| 125 |
| 126 // URLs specified on the command line should always appear, and should block |
| 127 // all other tabs except the Reset Trigger tab. |
| 128 TEST(StartupBrowserCreatorImplTest, DetermineStartupTabs_CommandLine) { |
| 129 FakeStartupTabProvider provider(kOnboardingTabs | kDistributionFirstRunTabs | |
| 130 kResetTriggerTabs | kPinnedTabs | |
| 131 kPreferencesTabs); |
| 132 StartupBrowserCreatorImpl impl( |
| 133 base::FilePath(), base::CommandLine(base::CommandLine::NO_PROGRAM), |
| 134 chrome::startup::IS_FIRST_RUN); |
| 135 |
| 136 StartupTabs cmd_line_tabs = {StartupTab(GURL("https://cmd-line"), false)}; |
| 137 |
| 138 StartupTabs output = |
| 139 impl.DetermineStartupTabs(provider, cmd_line_tabs, false, false); |
| 140 ASSERT_EQ(2U, output.size()); |
| 141 EXPECT_EQ("reset-trigger", output[0].url.host()); |
| 142 EXPECT_EQ("cmd-line", output[1].url.host()); |
| 143 |
| 144 // Also test that both incognito and crash recovery don't interfere with |
| 145 // command line tabs. |
| 146 |
| 147 // Incognito |
| 148 output = impl.DetermineStartupTabs(provider, cmd_line_tabs, true, false); |
| 149 ASSERT_EQ(1U, output.size()); |
| 150 EXPECT_EQ("cmd-line", output[0].url.host()); |
| 151 |
| 152 // Crash Recovery |
| 153 output = impl.DetermineStartupTabs(provider, cmd_line_tabs, false, true); |
| 154 ASSERT_EQ(1U, output.size()); |
| 155 EXPECT_EQ("cmd-line", output[0].url.host()); |
| 156 } |
| 157 |
| 158 // New Tab Page should appear alongside pinned tabs and the reset trigger, but |
| 159 // should be superseded by onboarding tabs and by tabs specified in preferences. |
| 160 TEST(StartupBrowserCreatorImplTest, DetermineStartupTabs_NewTabPage) { |
| 161 FakeStartupTabProvider provider_allows_ntp(kPinnedTabs | kResetTriggerTabs); |
| 162 StartupBrowserCreatorImpl impl( |
| 163 base::FilePath(), base::CommandLine(base::CommandLine::NO_PROGRAM), |
| 164 chrome::startup::IS_FIRST_RUN); |
| 165 |
| 166 StartupTabs output = impl.DetermineStartupTabs(provider_allows_ntp, |
| 167 StartupTabs(), false, false); |
| 168 ASSERT_EQ(3U, output.size()); |
| 169 EXPECT_EQ("reset-trigger", output[0].url.host()); |
| 170 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), output[1].url); |
| 171 EXPECT_EQ("pinned", output[2].url.host()); |
| 172 } |
OLD | NEW |