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 | |
27 // in the top-right corner. | |
28 const int kTolerance = 200; | |
groby-ooo-7-16
2015/04/02 22:21:13
I don't think 200 pixels is an acceptable toleranc
hcarmona
2015/04/04 01:04:23
Made tolerance smaller and moved my expected point
| |
29 | |
30 class TestPermissionBubbleCocoa : public PermissionBubbleCocoa { | |
31 public: | |
32 TestPermissionBubbleCocoa(NSWindow* parent_window) | |
33 : PermissionBubbleCocoa(parent_window) {} | |
34 | |
35 // Returns the base class value for testing |HasLocationBar|. | |
36 bool HasLocationBar() override { | |
37 return PermissionBubbleCocoa::HasLocationBar(); | |
38 } | |
39 | |
40 private: | |
41 DISALLOW_COPY_AND_ASSIGN(TestPermissionBubbleCocoa); | |
42 }; | |
43 | |
44 class MockPermissionBubbleCocoa : public PermissionBubbleCocoa { | |
groby-ooo-7-16
2015/04/02 22:21:12
FWIW: You can just add a mock class via gmock.h
c
hcarmona
2015/04/04 01:04:22
Awesome! Done.
| |
45 public: | |
46 MockPermissionBubbleCocoa(NSWindow* parent_window) | |
47 : PermissionBubbleCocoa(parent_window), location_bar_present_(false) {} | |
48 | |
49 // Sets the delegate value for testing |GetAnchorPoint|. | |
50 void set_location_bar_present(bool value) { location_bar_present_ = value; } | |
51 | |
52 protected: | |
53 bool location_bar_present_; | |
54 | |
55 // Override to mock location bar in tests. | |
56 bool HasLocationBar() override { return location_bar_present_; } | |
57 | |
58 private: | |
59 DISALLOW_COPY_AND_ASSIGN(MockPermissionBubbleCocoa); | |
60 }; | |
61 | |
62 class TestPermissionBubbleViewDelegate : public PermissionBubbleView::Delegate { | |
groby-ooo-7-16
2015/04/02 22:21:12
Since this does exactly squat, gmock to the rescue
hcarmona
2015/04/04 01:04:22
Done.
| |
63 public: | |
64 TestPermissionBubbleViewDelegate() : PermissionBubbleView::Delegate() {} | |
65 | |
66 void ToggleAccept(int, bool) override {} | |
67 void Accept() override {} | |
68 void Deny() override {} | |
69 void Closing() override {} | |
70 void SetView(PermissionBubbleView* view) override {} | |
71 | |
72 private: | |
73 DISALLOW_COPY_AND_ASSIGN(TestPermissionBubbleViewDelegate); | |
74 }; | |
75 | |
76 // Inherit from ExtensionBrowserTest instead of InProcessBrowserTest because | |
77 // testing app mode requires extensions. | |
78 class PermissionBubbleCocoaBrowsertest : public ExtensionBrowserTest { | |
79 public: | |
80 PermissionBubbleCocoaBrowsertest() | |
81 : ExtensionBrowserTest(), controller_(nullptr) {} | |
82 | |
83 void SetUpOnMainThread() override { | |
84 ExtensionBrowserTest::SetUpOnMainThread(); | |
85 | |
86 // Add a single permission request | |
87 MockPermissionBubbleRequest* request = new MockPermissionBubbleRequest( | |
88 "Request 1", l10n_util::GetStringUTF8(IDS_PERMISSION_ALLOW), | |
89 l10n_util::GetStringUTF8(IDS_PERMISSION_DENY)); | |
90 requests_.push_back(request); | |
91 } | |
92 | |
93 void TearDownOnMainThread() override { | |
94 if (browser()->tab_strip_model()) | |
95 browser()->tab_strip_model()->CloseSelectedTabs(); | |
96 | |
97 if (controller_) | |
98 [controller_ close]; | |
99 | |
100 STLDeleteElements(&requests_); | |
groby-ooo-7-16
2015/04/02 22:21:12
Use a ScopedVector instead.
hcarmona
2015/04/04 01:04:22
Done.
| |
101 ExtensionBrowserTest::TearDownOnMainThread(); | |
102 } | |
103 | |
104 virtual PermissionBubbleCocoa* CreatePermissionBubble( | |
105 NSWindow* parent_window) { | |
106 return new TestPermissionBubbleCocoa(parent_window); | |
107 } | |
108 | |
109 bool HasLocationBar() { | |
110 return static_cast<TestPermissionBubbleCocoa*>(permission_bubble_.get()) | |
111 ->HasLocationBar(); | |
112 } | |
113 | |
114 void ShowPermissionBubble(Browser* cur_browser) { | |
115 controller_ = [[BrowserWindowController alloc] initWithBrowser:cur_browser | |
groby-ooo-7-16
2015/04/02 22:21:13
Why create one?
This sequence should yield the BW
hcarmona
2015/04/04 01:04:22
Done.
| |
116 takeOwnership:NO]; | |
117 [controller_ showWindow:nil]; | |
118 | |
119 permission_bubble_.reset(CreatePermissionBubble([controller_ window])); | |
120 permission_delegate_.reset(new TestPermissionBubbleViewDelegate()); | |
groby-ooo-7-16
2015/04/02 22:21:12
The mock already takes care of this, but: Why do y
hcarmona
2015/04/04 01:04:22
Done.
| |
121 permission_bubble_->SetDelegate(permission_delegate_.get()); | |
122 | |
123 permission_bubble_->Show(requests_, accept_states_); | |
124 } | |
125 | |
126 protected: | |
127 BrowserWindowController* controller_; | |
128 scoped_ptr<PermissionBubbleCocoa> permission_bubble_; | |
129 scoped_ptr<TestPermissionBubbleViewDelegate> permission_delegate_; | |
130 std::vector<PermissionBubbleRequest*> requests_; | |
131 std::vector<bool> accept_states_; | |
132 }; | |
133 | |
134 // Default Window Test ///////////////////////////////////////////////////////// | |
135 | |
136 IN_PROC_BROWSER_TEST_F(PermissionBubbleCocoaBrowsertest, | |
137 HasLocationBarByDefault) { | |
138 ShowPermissionBubble(browser()); | |
139 EXPECT_TRUE(HasLocationBar()); | |
140 } | |
141 | |
142 // Fullscreen Test ///////////////////////////////////////////////////////////// | |
143 | |
144 IN_PROC_BROWSER_TEST_F(PermissionBubbleCocoaBrowsertest, | |
145 FullscreenHasLocationBar) { | |
146 ShowPermissionBubble(browser()); | |
147 | |
148 base::scoped_nsobject<FullscreenWindowManager> manager( | |
149 [[FullscreenWindowManager alloc] initWithWindow:[controller_ window] | |
150 desiredScreen:[NSScreen mainScreen]]); | |
151 EXPECT_TRUE(HasLocationBar()); | |
152 [manager enterFullscreenMode]; | |
153 EXPECT_TRUE(HasLocationBar()); | |
154 [manager exitFullscreenMode]; | |
155 EXPECT_TRUE(HasLocationBar()); | |
156 } | |
157 | |
158 // App Test //////////////////////////////////////////////////////////////////// | |
159 | |
160 class PermissionBubbleCocoaAppTest : public PermissionBubbleCocoaBrowsertest { | |
161 public: | |
162 PermissionBubbleCocoaAppTest() | |
163 : PermissionBubbleCocoaBrowsertest(), app_browser_(nullptr) {} | |
164 | |
165 void TearDownOnMainThread() override { | |
166 if (app_browser_) | |
167 app_browser_->tab_strip_model()->CloseSelectedTabs(); | |
168 | |
169 PermissionBubbleCocoaBrowsertest::TearDownOnMainThread(); | |
170 } | |
171 | |
172 // Only one app can be opened per test. | |
173 bool OpenExtensionAppWindow(const extensions::Extension* extension) { | |
groby-ooo-7-16
2015/04/02 22:21:13
Question: Can this code be shared in a util place?
hcarmona
2015/04/04 01:04:23
Would it make sense to move it to the ExtensionBro
| |
174 // On MacOS, it's not possible to enter app mode with the new bookmark apps | |
175 // enabled. | |
hcarmona
2015/04/02 01:30:14
I'm currently looking into this. Spoke with benwel
groby-ooo-7-16
2015/04/02 22:21:12
Well, then let's not land this until we know we're
hcarmona
2015/04/04 01:04:22
Issue is not related to this change, created bug 4
| |
176 if (extensions::util::IsNewBookmarkAppsEnabled()) | |
177 return false; | |
178 | |
179 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); | |
180 command_line.AppendSwitchASCII(switches::kAppId, extension->id()); | |
181 | |
182 chrome::startup::IsFirstRun first_run = | |
groby-ooo-7-16
2015/04/02 22:21:13
Why do we care about first_run status? Just launch
hcarmona
2015/04/04 01:04:22
Code no longer needed in this test.
| |
183 first_run::IsChromeFirstRun() ? chrome::startup::IS_FIRST_RUN | |
184 : chrome::startup::IS_NOT_FIRST_RUN; | |
185 StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run); | |
186 | |
187 if (!launch.OpenApplicationWindow(browser()->profile(), NULL)) | |
188 return false; | |
189 | |
190 unsigned int browser_count = chrome::GetBrowserCount( | |
191 browser()->profile(), browser()->host_desktop_type()); | |
192 | |
193 if (browser_count != 2) | |
groby-ooo-7-16
2015/04/02 22:21:12
Please ASSERT here - that way, we know what exactl
hcarmona
2015/04/04 01:04:22
Code changed so we get the browser and don't need
| |
194 return false; | |
195 | |
196 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | |
197 if (*it != browser()) { | |
198 app_browser_ = *it; | |
199 return true; | |
200 } | |
201 } | |
202 | |
203 NOTREACHED(); | |
204 return false; | |
205 } | |
206 | |
207 Browser* app_browser() { return app_browser_; } | |
208 | |
209 private: | |
210 Browser* app_browser_; | |
211 }; | |
212 | |
213 IN_PROC_BROWSER_TEST_F(PermissionBubbleCocoaAppTest, AppHasNoLocationBar) { | |
214 auto extension = LoadExtension(test_data_dir_.AppendASCII("packaged_app/")); | |
215 | |
216 ASSERT_TRUE(extension); | |
217 ASSERT_TRUE(OpenExtensionAppWindow(extension)); | |
218 | |
219 ShowPermissionBubble(app_browser()); | |
220 | |
221 EXPECT_TRUE(app_browser()->is_app()); | |
222 EXPECT_FALSE(HasLocationBar()); | |
223 } | |
224 | |
225 // Kiosk Test ////////////////////////////////////////////////////////////////// | |
226 | |
227 class PermissionBubbleCocoaKioskTest : public PermissionBubbleCocoaBrowsertest { | |
228 public: | |
229 void SetUpCommandLine(base::CommandLine* command_line) override { | |
230 PermissionBubbleCocoaBrowsertest::SetUpCommandLine(command_line); | |
231 command_line->AppendSwitch(switches::kKioskMode); | |
232 } | |
233 }; | |
234 | |
235 // http://crbug.com/470724 | |
236 // Kiosk mode on Mac has a location bar but it shouldn't. | |
237 IN_PROC_BROWSER_TEST_F(PermissionBubbleCocoaKioskTest, | |
238 DISABLED_KioskHasNoLocationBar) { | |
239 ShowPermissionBubble(browser()); | |
240 EXPECT_FALSE(HasLocationBar()); | |
241 } | |
242 | |
243 // Anchor Position Tests /////////////////////////////////////////////////////// | |
244 | |
245 class PermissionBubbleCocoaAnchorBrowsertest | |
246 : public PermissionBubbleCocoaBrowsertest { | |
247 protected: | |
248 PermissionBubbleCocoa* CreatePermissionBubble( | |
249 NSWindow* parent_window) override { | |
250 return new MockPermissionBubbleCocoa(parent_window); | |
251 } | |
252 | |
253 void SetLocationBarPresent(bool present) { | |
254 static_cast<MockPermissionBubbleCocoa*>(permission_bubble_.get()) | |
255 ->set_location_bar_present(present); | |
256 } | |
257 }; | |
258 | |
259 IN_PROC_BROWSER_TEST_F(PermissionBubbleCocoaAnchorBrowsertest, | |
260 AnchorPositionWithLocationBar) { | |
261 ShowPermissionBubble(browser()); | |
262 SetLocationBarPresent(true); | |
263 | |
264 NSPoint anchor = permission_bubble_->GetAnchorPoint(); | |
265 NSWindow* window = [controller_ window]; | |
266 NSRect frame = [window frame]; | |
267 | |
268 // Expected anchor location will be top left when there's a location bar. | |
269 NSPoint expected = NSMakePoint(0, frame.size.height); | |
groby-ooo-7-16
2015/04/02 22:21:13
You want NSMinX(frame), NSMaxY(frame) (it's not gu
hcarmona
2015/04/04 01:04:22
Done.
| |
270 expected = [window convertBaseToScreen:expected]; | |
271 | |
272 // Anchor should be close to the left of the screen. | |
273 EXPECT_GE(expected.x + kTolerance, anchor.x); | |
274 EXPECT_LE(expected.x, anchor.x); | |
275 | |
276 // Anchor should be close to the top of the screen. | |
277 EXPECT_GE(expected.y, anchor.y); | |
278 EXPECT_LE(expected.y - kTolerance, anchor.y); | |
groby-ooo-7-16
2015/04/02 22:21:12
That means exepcted.y should be within kTolerance
hcarmona
2015/04/04 01:04:22
Done.
| |
279 } | |
280 | |
281 IN_PROC_BROWSER_TEST_F(PermissionBubbleCocoaAnchorBrowsertest, | |
282 AnchorPositionWithoutLocationBar) { | |
283 ShowPermissionBubble(browser()); | |
284 SetLocationBarPresent(false); | |
285 | |
286 NSPoint anchor = permission_bubble_->GetAnchorPoint(); | |
287 NSWindow* window = [controller_ window]; | |
288 NSRect frame = [window frame]; | |
289 | |
290 // Expected anchor location will be top center when there's no location bar. | |
291 NSPoint expected = NSMakePoint(frame.size.width / 2, frame.size.height); | |
292 expected = [window convertBaseToScreen:expected]; | |
293 | |
294 // Anchor should be centered. | |
295 EXPECT_EQ(expected.x, anchor.x); | |
groby-ooo-7-16
2015/04/02 22:21:12
EXPECT_TRUE(NSEqualPoints(expected, anchor));
hcarmona
2015/04/04 01:04:23
Done.
| |
296 EXPECT_EQ(expected.y, anchor.y); | |
297 } | |
OLD | NEW |