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