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) {} | |
Peter Kasting
2016/09/29 07:23:34
Nit: The options bitfield feels a little clever; w
tmartino
2016/09/30 20:55:01
Actually, I had it that way originally, back in pa
| |
26 | |
27 bool AddOnboardingTabs(StartupTabs* tabs) const override { | |
28 if (options_ & kOnboardingTabs) { | |
Peter Kasting
2016/09/29 07:23:34
Nit: All these methods can drop the {} if you reve
tmartino
2016/09/30 20:55:02
Ack. When it's not too much of a hassle to structu
Peter Kasting
2016/09/30 21:50:03
Definitely avoid negative conditionals if there's
tmartino
2016/10/03 23:26:06
No longer applies anyway :)
| |
29 tabs->push_back(StartupTab(GURL("https://onboarding"), false)); | |
Peter Kasting
2016/09/29 07:23:34
Nit: Comment in startup_browser_creator_impl.cc ab
| |
30 return true; | |
31 } | |
32 return false; | |
33 } | |
34 | |
35 bool AddDistributionFirstRunTabs(StartupBrowserCreator* browser_creator, | |
36 StartupTabs* tabs) const override { | |
37 if (options_ & kDistributionFirstRunTabs) { | |
38 tabs->push_back(StartupTab(GURL("https://distribution"), false)); | |
39 return true; | |
40 } | |
41 return false; | |
42 } | |
43 | |
44 bool AddResetTriggerTabs(Profile* profile, StartupTabs* tabs) const override { | |
45 if (options_ & kResetTriggerTabs) { | |
46 tabs->push_back(StartupTab(GURL("https://reset-trigger"), false)); | |
47 return true; | |
48 } | |
49 return false; | |
50 } | |
51 | |
52 bool AddPinnedTabs(StartupTabs* tabs) const override { | |
53 if (options_ & kPinnedTabs) { | |
54 tabs->push_back(StartupTab(GURL("https://pinned"), true)); | |
55 return true; | |
56 } | |
57 return false; | |
58 } | |
59 | |
60 bool AddPreferencesTabs(StartupTabs* tabs) const override { | |
61 if (options_ & kPreferencesTabs) { | |
62 tabs->push_back(StartupTab(GURL("https://prefs"), false)); | |
63 return true; | |
64 } | |
65 return false; | |
66 } | |
67 | |
68 private: | |
69 const uint32_t options_; | |
70 }; | |
71 | |
72 StartupTabs GetSentinelCommandLineTabs() { | |
73 StartupTabs cmd_line_tabs; | |
74 cmd_line_tabs.push_back(StartupTab(GURL("https://cmd-line"), false)); | |
Peter Kasting
2016/09/29 07:23:34
Nit: In this case it seems like you could just do
| |
75 return cmd_line_tabs; | |
76 } | |
77 | |
78 } // namespace | |
79 | |
80 // "Standard" case: Tabs specified in onboarding, reset trigger, pinned tabs, or | |
81 // preferences shouldn't interfere with each other. Nothing specified on the | |
82 // command line. Reset trigger always appears first. | |
83 TEST(StartupBrowserCreatorImplTest, DetermineStartupTabs) { | |
84 FakeStartupTabProvider provider(kOnboardingTabs | kResetTriggerTabs | | |
85 kPinnedTabs | kPreferencesTabs); | |
86 StartupBrowserCreatorImpl impl( | |
87 base::FilePath(), base::CommandLine(base::CommandLine::NO_PROGRAM), | |
88 chrome::startup::IS_FIRST_RUN); | |
89 StartupTabs no_tabs; | |
Peter Kasting
2016/09/29 07:23:34
Nit: Personally, rather than declare this, I'd cal
| |
90 | |
91 StartupTabs output = | |
92 impl.DetermineStartupTabs(provider, no_tabs, false, false); | |
93 ASSERT_EQ(4U, output.size()); | |
94 EXPECT_EQ("reset-trigger", output[0].url.host()); | |
95 EXPECT_EQ("onboarding", output[1].url.host()); | |
96 EXPECT_EQ("prefs", output[2].url.host()); | |
97 EXPECT_EQ("pinned", output[3].url.host()); | |
98 } | |
99 | |
100 // All content is blocked in Incognito mode, or when recovering from a crash. | |
101 // Only the New Tab Page should appear in either case. | |
102 TEST(StartupBrowserCreatorImplTest, DetermineStartupTabs_IncognitoOrCrash) { | |
103 FakeStartupTabProvider provider(kOnboardingTabs | kDistributionFirstRunTabs | | |
104 kResetTriggerTabs | kPinnedTabs | | |
105 kPreferencesTabs); | |
106 StartupBrowserCreatorImpl impl( | |
107 base::FilePath(), base::CommandLine(base::CommandLine::NO_PROGRAM), | |
108 chrome::startup::IS_FIRST_RUN); | |
109 StartupTabs no_tabs; | |
110 | |
111 // Incognito | |
Peter Kasting
2016/09/29 07:23:34
Nit: Trailing period (several places)
tmartino
2016/09/30 20:55:01
Fragment, so went with colon instead.
| |
112 StartupTabs output = | |
113 impl.DetermineStartupTabs(provider, no_tabs, true, false); | |
114 ASSERT_EQ(1U, output.size()); | |
115 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL).host(), output[0].url.host()); | |
Peter Kasting
2016/09/29 07:23:34
Nit: Maybe omit .host() calls? (3 different places
| |
116 | |
117 // Crash Recovery | |
118 output = impl.DetermineStartupTabs(provider, no_tabs, false, true); | |
119 ASSERT_EQ(1U, output.size()); | |
120 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL).host(), output[0].url.host()); | |
121 } | |
122 | |
123 // If Master Preferences specifies content, this should block all other | |
124 // policies. The only exception is command line URLs, tested below. | |
125 TEST(StartupBrowserCreatorImplTest, DetermineStartupTabs_MasterPrefs) { | |
126 FakeStartupTabProvider provider(kOnboardingTabs | kDistributionFirstRunTabs | | |
127 kResetTriggerTabs | kPinnedTabs | | |
128 kPreferencesTabs); | |
129 StartupBrowserCreatorImpl impl( | |
130 base::FilePath(), base::CommandLine(base::CommandLine::NO_PROGRAM), | |
131 chrome::startup::IS_FIRST_RUN); | |
132 StartupTabs no_tabs; | |
133 | |
134 StartupTabs output = | |
135 impl.DetermineStartupTabs(provider, no_tabs, false, false); | |
136 ASSERT_EQ(1U, output.size()); | |
137 EXPECT_EQ("distribution", output[0].url.host()); | |
138 } | |
139 | |
140 // URLs specified on the command line should always appear, and should block | |
141 // all other tabs except the Reset Trigger tab. | |
142 TEST(StartupBrowserCreatorImplTest, DetermineStartupTabs_CommandLine) { | |
143 FakeStartupTabProvider provider(kOnboardingTabs | kDistributionFirstRunTabs | | |
144 kResetTriggerTabs | kPinnedTabs | | |
145 kPreferencesTabs); | |
146 StartupBrowserCreatorImpl impl( | |
147 base::FilePath(), base::CommandLine(base::CommandLine::NO_PROGRAM), | |
148 chrome::startup::IS_FIRST_RUN); | |
149 | |
150 StartupTabs cmd_line_tabs = GetSentinelCommandLineTabs(); | |
151 | |
152 StartupTabs output = | |
153 impl.DetermineStartupTabs(provider, cmd_line_tabs, false, false); | |
154 ASSERT_EQ(2U, output.size()); | |
155 EXPECT_EQ("reset-trigger", output[0].url.host()); | |
156 EXPECT_EQ("cmd-line", output[1].url.host()); | |
157 | |
158 // Also test that both incognito and crash recovery don't interfere with | |
159 // command line tabs. | |
160 | |
161 // Incognito | |
162 output = impl.DetermineStartupTabs(provider, cmd_line_tabs, true, false); | |
163 ASSERT_EQ(1U, output.size()); | |
164 EXPECT_EQ("cmd-line", output[0].url.host()); | |
165 | |
166 // Crash Recovery | |
167 output = impl.DetermineStartupTabs(provider, cmd_line_tabs, false, true); | |
168 ASSERT_EQ(1U, output.size()); | |
169 EXPECT_EQ("cmd-line", output[0].url.host()); | |
170 } | |
171 | |
172 // New Tab Page should appear alongside pinned tabs and the reset trigger, but | |
173 // should be superseded by onboarding tabs and by tabs specified in preferences. | |
174 TEST(StartupBrowserCreatorImplTest, DetermineStartupTabs_NewTabPage) { | |
175 FakeStartupTabProvider provider_allows_ntp(kPinnedTabs | kResetTriggerTabs); | |
176 StartupBrowserCreatorImpl impl( | |
177 base::FilePath(), base::CommandLine(base::CommandLine::NO_PROGRAM), | |
178 chrome::startup::IS_FIRST_RUN); | |
179 StartupTabs no_tabs; | |
180 | |
181 StartupTabs output = | |
182 impl.DetermineStartupTabs(provider_allows_ntp, no_tabs, false, false); | |
183 ASSERT_EQ(3U, output.size()); | |
184 EXPECT_EQ("reset-trigger", output[0].url.host()); | |
185 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL).host(), output[1].url.host()); | |
186 EXPECT_EQ("pinned", output[2].url.host()); | |
187 } | |
OLD | NEW |