OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "base/command_line.h" | |
6 #include "base/stl_util.h" | |
7 #include "chrome/browser/extensions/extension_browsertest.h" | |
8 #include "chrome/browser/extensions/extension_util.h" | |
9 #include "chrome/browser/first_run/first_run.h" | |
10 #include "chrome/browser/ui/browser.h" | |
11 #include "chrome/browser/ui/browser_finder.h" | |
12 #include "chrome/browser/ui/browser_iterator.h" | |
13 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | |
14 #import "chrome/browser/ui/cocoa/website_settings/permission_bubble_cocoa.h" | |
15 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h" | |
16 #include "chrome/browser/ui/startup/startup_types.h" | |
17 #include "chrome/browser/ui/website_settings/mock_permission_bubble_request.h" | |
18 #include "chrome/common/chrome_switches.h" | |
19 #include "chrome/grit/generated_resources.h" | |
20 #include "extensions/browser/extension_registry.h" | |
21 #include "net/dns/mock_host_resolver.h" | |
22 #import "ui/base/cocoa/fullscreen_window_manager.h" | |
23 #include "ui/base/l10n/l10n_util.h" | |
24 #include "ui/base/l10n/l10n_util_mac.h" | |
25 | |
26 // To be used when getting the anchor with a location bar. The bubble should be | |
groby-ooo-7-16
2015/04/01 00:19:14
General Q: How much of this testing framework (for
hcarmona
2015/04/02 01:30:14
Most of these classes look like they can be shared
hcarmona
2015/04/04 01:04:22
These classes are now split so only the tests are
| |
27 // in the top-right corner. | |
28 const int kTolerance = 200; | |
29 | |
30 class TestPermissionBubbleCocoa: public PermissionBubbleCocoa { | |
groby-ooo-7-16
2015/04/01 00:19:14
Space in front of ':'.
Does git cl format not cat
hcarmona
2015/04/02 01:30:14
Had not run git cl format. I've fixed the formatti
| |
31 public: | |
32 TestPermissionBubbleCocoa(NSWindow* parent_window): | |
groby-ooo-7-16
2015/04/01 00:19:14
space in front of ':'
hcarmona
2015/04/02 01:30:14
Done.
| |
33 PermissionBubbleCocoa(parent_window), | |
34 location_bar_present_(false) {} | |
35 | |
36 // Returns the base class value for testing |HasLocationBar|. | |
groby-ooo-7-16
2015/04/01 00:19:14
It seems you either want to mock, or you don't, bu
hcarmona
2015/04/02 01:30:14
Done.
| |
37 bool BaseHasLocationBar() { return PermissionBubbleCocoa::HasLocationBar(); } | |
38 | |
39 // Sets the delegate value for testing |GetAnchorPoint|. | |
40 void set_location_bar_present(bool value) { location_bar_present_ = value; } | |
41 | |
42 protected: | |
43 bool location_bar_present_; | |
44 | |
45 // Override to mock location bar in tests. | |
46 bool HasLocationBar() override { return location_bar_present_; } | |
groby-ooo-7-16
2015/04/01 00:19:14
private:
DISALLOW_COPY_AND_ASSIGN...
hcarmona
2015/04/02 01:30:14
Done.
| |
47 }; | |
48 | |
49 class TestPermissionBubbleViewDelegate: public PermissionBubbleView::Delegate { | |
50 void ToggleAccept(int, bool) override {}; | |
51 void Accept() override {}; | |
52 void Deny() override {}; | |
53 void Closing() override {}; | |
54 void SetView(PermissionBubbleView* view) override {}; | |
55 }; | |
56 | |
57 // Inherit from ExtensionBrowserTest instead of InProcessBrowserTest because | |
58 // testing app mode requires extensions. | |
59 class PermissionBubbleCocoaBrowsertest : public ExtensionBrowserTest { | |
groby-ooo-7-16
2015/04/01 00:19:14
Huh. I had no idea ExtensionBrowserTest stuff live
hcarmona
2015/04/02 01:30:14
I'm a bit confused about what I should do here...
groby-ooo-7-16
2015/04/02 22:21:12
Nothing. I would've just expected ExtensionBrowser
| |
60 public: | |
61 PermissionBubbleCocoaBrowsertest(): | |
62 ExtensionBrowserTest(), | |
63 controller_(nullptr) {} | |
64 | |
65 void SetUpOnMainThread() override { | |
66 ExtensionBrowserTest::SetUpOnMainThread(); | |
67 | |
68 // Add a single permission request | |
69 MockPermissionBubbleRequest* request = new MockPermissionBubbleRequest( | |
70 "Request 1", | |
71 l10n_util::GetStringUTF8(IDS_PERMISSION_ALLOW), | |
72 l10n_util::GetStringUTF8(IDS_PERMISSION_DENY)); | |
73 requests_.push_back(request); | |
74 } | |
75 | |
76 void TearDownOnMainThread() override { | |
77 if (browser()->tab_strip_model()) | |
78 browser()->tab_strip_model()->CloseSelectedTabs(); | |
79 | |
80 if (controller_) | |
81 [controller_ close]; | |
82 | |
83 STLDeleteElements(&requests_); | |
84 ExtensionBrowserTest::TearDownOnMainThread(); | |
85 } | |
86 | |
87 void ShowPermissionBubble(Browser* cur_browser) { | |
88 controller_ = [[BrowserWindowController alloc] initWithBrowser:cur_browser | |
89 takeOwnership:NO]; | |
90 [controller_ showWindow:nil]; | |
91 | |
92 permission_bubble_.reset( | |
93 new TestPermissionBubbleCocoa([controller_ window])); | |
94 permission_delegate_.reset(new TestPermissionBubbleViewDelegate()); | |
95 permission_bubble_->SetDelegate(permission_delegate_.get()); | |
96 | |
97 permission_bubble_->Show(requests_, accept_states_); | |
98 } | |
99 | |
100 protected: | |
101 BrowserWindowController* controller_; | |
102 scoped_ptr<TestPermissionBubbleCocoa> permission_bubble_; | |
103 scoped_ptr<TestPermissionBubbleViewDelegate> permission_delegate_; | |
104 std::vector<PermissionBubbleRequest*> requests_; | |
105 std::vector<bool> accept_states_; | |
106 }; | |
107 | |
108 // Normal Window Test ////////////////////////////////////////////////////////// | |
109 | |
110 IN_PROC_BROWSER_TEST_F(PermissionBubbleCocoaBrowsertest, NormalHasLocationBar) { | |
groby-ooo-7-16
2015/04/01 00:19:14
"Normal" is an unclear naming choice. "HasLocation
hcarmona
2015/04/02 01:30:14
Done.
| |
111 ShowPermissionBubble(browser()); | |
112 EXPECT_TRUE(permission_bubble_->BaseHasLocationBar()); | |
113 } | |
114 | |
115 // Fullscreen Test ///////////////////////////////////////////////////////////// | |
116 | |
117 IN_PROC_BROWSER_TEST_F(PermissionBubbleCocoaBrowsertest, | |
118 FullscreenHasLocationBar) { | |
119 ShowPermissionBubble(browser()); | |
120 | |
121 base::scoped_nsobject<FullscreenWindowManager> manager( | |
122 [[FullscreenWindowManager alloc] initWithWindow:[controller_ window] | |
123 desiredScreen:[NSScreen mainScreen]]); | |
124 EXPECT_TRUE(permission_bubble_->BaseHasLocationBar()); | |
125 [manager enterFullscreenMode]; | |
126 EXPECT_TRUE(permission_bubble_->BaseHasLocationBar()); | |
127 [manager exitFullscreenMode]; | |
128 EXPECT_TRUE(permission_bubble_->BaseHasLocationBar()); | |
129 } | |
130 | |
131 // App Test //////////////////////////////////////////////////////////////////// | |
132 | |
133 class PermissionBubbleCocoaAppTest : public PermissionBubbleCocoaBrowsertest { | |
134 public: | |
135 PermissionBubbleCocoaAppTest(): | |
136 PermissionBubbleCocoaBrowsertest() {} | |
137 | |
138 // Returns the app extension aptly named "App Test". | |
139 const extensions::Extension* GetExtension() { | |
groby-ooo-7-16
2015/04/01 00:19:14
GetTestExtension? Also, are we guaranteed to have
hcarmona
2015/04/02 01:30:14
Refactored so this method is no longer needed. Als
| |
140 extensions::ExtensionRegistry* registry = | |
141 extensions::ExtensionRegistry::Get(browser()->profile()); | |
142 for (const scoped_refptr<const extensions::Extension>& extension : | |
143 registry->enabled_extensions()) { | |
144 if (extension->name() == "App Test") | |
145 return extension.get(); | |
146 } | |
147 NOTREACHED(); | |
148 return NULL; | |
149 } | |
150 }; | |
151 | |
152 IN_PROC_BROWSER_TEST_F(PermissionBubbleCocoaAppTest, AppHasNoLocationBar) { | |
153 ASSERT_TRUE(test_server()->Start()); | |
154 | |
155 // There should be one tab to start with. | |
156 ASSERT_EQ(1, browser()->tab_strip_model()->count()); | |
157 | |
158 // Load an app. | |
159 host_resolver()->AddRule("www.example.com", "127.0.0.1"); | |
160 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app/"))); | |
161 const extensions::Extension* extension_app = GetExtension(); | |
162 | |
163 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); | |
164 command_line.AppendSwitchASCII(switches::kAppId, extension_app->id()); | |
165 | |
166 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ? | |
167 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN; | |
168 StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run); | |
169 | |
170 bool new_bookmark_apps_enabled = extensions::util::IsNewBookmarkAppsEnabled(); | |
171 | |
172 // If the new bookmark app flow is enabled, the app should open as a tab. | |
173 // Otherwise the app should open as an app window. | |
174 EXPECT_EQ(!new_bookmark_apps_enabled, | |
175 launch.OpenApplicationWindow(browser()->profile(), NULL)); | |
176 EXPECT_EQ(new_bookmark_apps_enabled, | |
177 launch.OpenApplicationTab(browser()->profile())); | |
178 | |
179 // Check that a the number of browsers and tabs is correct. | |
180 unsigned int expected_browsers = 1; | |
181 int expected_tabs = 1; | |
182 new_bookmark_apps_enabled ? expected_tabs++ : expected_browsers++; | |
183 | |
184 EXPECT_EQ(expected_browsers, | |
185 chrome::GetBrowserCount(browser()->profile(), | |
186 browser()->host_desktop_type())); | |
187 EXPECT_EQ(expected_tabs, browser()->tab_strip_model()->count()); | |
188 | |
189 Browser* cur_browser = browser(); | |
190 if (expected_browsers == 2) { | |
191 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | |
192 if (*it == browser()) | |
193 continue; | |
194 else | |
195 cur_browser = *it; | |
196 } | |
197 | |
198 EXPECT_NE(browser(), cur_browser); | |
199 } | |
200 | |
201 ShowPermissionBubble(cur_browser); | |
groby-ooo-7-16
2015/04/01 00:19:14
Up until here, this seems to be a verbatim copy fr
hcarmona
2015/04/02 01:30:14
No, not everything is necessary. Refactored test t
groby-ooo-7-16
2015/04/02 22:21:12
Follow-up CL is fine, but please file a crbug for
hcarmona
2015/04/13 21:13:24
The test that goes into app mode is now its own cl
groby-ooo-7-16
2015/04/13 21:19:45
Possibly, if others are using it. I wouldn't mind
| |
202 | |
203 // When bookmark apps are enabled, the browser that we have will not be an | |
204 // app: this means that it should have a location bar. | |
205 EXPECT_NE(new_bookmark_apps_enabled, cur_browser->is_app()); | |
groby-ooo-7-16
2015/04/01 00:19:14
So this doesn't quite know if it tests in app mode
hcarmona
2015/04/02 01:30:14
Updated test so it doesn't try to guess if app mod
| |
206 EXPECT_EQ(new_bookmark_apps_enabled, | |
207 permission_bubble_->BaseHasLocationBar()); | |
groby-ooo-7-16
2015/04/01 00:19:14
Wait, the name says "AppHasNoLocationBar", but thi
hcarmona
2015/04/02 01:30:14
Updated test so test matches the name.
| |
208 | |
209 if (cur_browser->tab_strip_model()) | |
210 cur_browser->tab_strip_model()->CloseSelectedTabs(); | |
groby-ooo-7-16
2015/04/01 00:19:14
Why?
hcarmona
2015/04/02 01:30:14
This should be in TearDownOnMainThread: moved ther
groby-ooo-7-16
2015/04/02 22:21:12
It already was there, that's why I asked :)
| |
211 } | |
212 | |
213 // Kiosk Test ////////////////////////////////////////////////////////////////// | |
214 | |
215 class PermissionBubbleCocoaKioskTest : public PermissionBubbleCocoaBrowsertest { | |
216 public: | |
217 void SetUpCommandLine(base::CommandLine* command_line) override { | |
218 PermissionBubbleCocoaBrowsertest::SetUpCommandLine(command_line); | |
219 command_line->AppendSwitch(switches::kKioskMode); | |
220 } | |
221 }; | |
222 | |
223 // http://crbug.com/470724 | |
224 // Kiosk mode on Mac has a location bar but it shouldn't. | |
225 IN_PROC_BROWSER_TEST_F(PermissionBubbleCocoaKioskTest, | |
226 DISABLED_KioskHasNoLocationBar) { | |
227 ShowPermissionBubble(browser()); | |
228 EXPECT_FALSE(permission_bubble_->BaseHasLocationBar()); | |
229 } | |
230 | |
231 // Anchor Position Tests /////////////////////////////////////////////////////// | |
232 | |
233 IN_PROC_BROWSER_TEST_F(PermissionBubbleCocoaBrowsertest, | |
234 AnchorPositionWithLocationBar) { | |
235 ShowPermissionBubble(browser()); | |
236 permission_bubble_->set_location_bar_present(true); | |
237 | |
238 NSPoint anchor = permission_bubble_->GetAnchorPoint(); | |
239 NSWindow* window = [controller_ window]; | |
240 NSRect frame = [window frame]; | |
241 | |
242 // Expected anchor location will be top left when there's a location bar. | |
243 NSPoint expected = NSMakePoint(0, frame.size.height); | |
244 expected = [window convertBaseToScreen:expected]; | |
245 | |
246 // Anchor should be close to the left of the screen. | |
247 EXPECT_GE(expected.x + kTolerance, anchor.x); | |
248 EXPECT_LE(expected.x, anchor.x); | |
249 | |
250 // Anchor should be close to the top of the screen. | |
251 EXPECT_GE(expected.y, anchor.y); | |
252 EXPECT_LE(expected.y - kTolerance, anchor.y); | |
253 } | |
254 | |
255 IN_PROC_BROWSER_TEST_F(PermissionBubbleCocoaBrowsertest, | |
256 AnchorPositionWithoutLocationBar) { | |
257 ShowPermissionBubble(browser()); | |
258 permission_bubble_->set_location_bar_present(false); | |
259 | |
260 NSPoint anchor = permission_bubble_->GetAnchorPoint(); | |
261 NSWindow* window = [controller_ window]; | |
262 NSRect frame = [window frame]; | |
263 | |
264 // Expected anchor location will be top center when there's no location bar. | |
265 NSPoint expected = NSMakePoint(frame.size.width / 2, frame.size.height); | |
266 expected = [window convertBaseToScreen:expected]; | |
267 | |
268 // Anchor should be centered. | |
269 EXPECT_EQ(expected.x, anchor.x); | |
270 EXPECT_EQ(expected.y, anchor.y); | |
271 } | |
OLD | NEW |