1 // Copyright (c) 2012 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.
5 #include <Carbon/Carbon.h>
6 #import <Cocoa/Cocoa.h>
8 #include "base/command_line.h"
9 #include "base/debug/debugger.h"
10 #include "base/mac/scoped_nsautorelease_pool.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "chrome/app/chrome_command_ids.h" // IDC_*
14 #include "chrome/browser/chrome_notification_types.h"
15 #import "chrome/browser/ui/cocoa/browser_window_utils.h"
16 #import "chrome/browser/ui/cocoa/cocoa_profile_test.h"
17 #import "chrome/browser/ui/cocoa/panels/panel_cocoa.h"
18 #import "chrome/browser/ui/cocoa/panels/panel_titlebar_view_cocoa.h"
19 #import "chrome/browser/ui/cocoa/panels/panel_window_controller_cocoa.h"
20 #include "chrome/browser/ui/cocoa/run_loop_testing.h"
21 #include "chrome/browser/ui/panels/panel.h"
22 #include "chrome/browser/ui/panels/panel_manager.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "content/public/test/test_utils.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "testing/gtest_mac.h"
29 class PanelAnimatedBoundsObserver :
30 public content::WindowedNotificationObserver {
32 PanelAnimatedBoundsObserver(Panel* panel)
33 : content::WindowedNotificationObserver(
34 chrome::NOTIFICATION_PANEL_BOUNDS_ANIMATIONS_FINISHED,
35 content::Source<Panel>(panel)) { }
36 ~PanelAnimatedBoundsObserver() override {}
40 class PanelCocoaTest : public CocoaProfileTest {
42 void SetUp() override { CocoaProfileTest::SetUp(); }
44 Panel* CreateTestPanel(const std::string& panel_name) {
45 // Opening panels on a Mac causes NSWindowController of the Panel window
46 // to be autoreleased. We need a pool drained after it's done so the test
47 // can close correctly.
48 base::mac::ScopedNSAutoreleasePool autorelease_pool;
50 PanelManager* manager = PanelManager::GetInstance();
51 int panels_count = manager->num_panels();
53 Panel* panel = manager->CreatePanel(panel_name, profile(),
55 PanelManager::CREATE_AS_DOCKED);
56 EXPECT_EQ(panels_count + 1, manager->num_panels());
59 EXPECT_TRUE(panel->native_panel()); // Native panel is created right away.
60 PanelCocoa* native_window =
61 static_cast<PanelCocoa*>(panel->native_panel());
62 EXPECT_EQ(panel, native_window->panel_); // Back pointer initialized.
64 PanelAnimatedBoundsObserver bounds_observer(panel);
66 // Window should not load before Show().
67 // Note: Loading the wnidow causes Cocoa to autorelease a few objects.
68 // This is the reason we do this within the scope of the
69 // ScopedNSAutoreleasePool.
70 EXPECT_FALSE([native_window->controller_ isWindowLoaded]);
72 EXPECT_TRUE([native_window->controller_ isWindowLoaded]);
73 EXPECT_TRUE([native_window->controller_ window]);
75 // Wait until bounds animate to their specified values.
76 bounds_observer.Wait();
81 void VerifyTitlebarLocation(NSView* contentView, NSView* titlebar) {
82 NSRect content_frame = [contentView frame];
83 NSRect titlebar_frame = [titlebar frame];
84 // Since contentView and titlebar are both children of window's root view,
85 // we can compare their frames since they are in the same coordinate system.
86 EXPECT_EQ(NSMinX(content_frame), NSMinX(titlebar_frame));
87 EXPECT_EQ(NSWidth(content_frame), NSWidth(titlebar_frame));
88 EXPECT_EQ(NSHeight([[titlebar superview] bounds]), NSMaxY(titlebar_frame));
91 void ClosePanelAndWait(Panel* panel) {
93 // Closing a panel may involve several async tasks. Need to use
94 // message pump and wait for the notification.
95 PanelManager* manager = PanelManager::GetInstance();
96 int panel_count = manager->num_panels();
97 content::WindowedNotificationObserver signal(
98 chrome::NOTIFICATION_PANEL_CLOSED,
99 content::Source<Panel>(panel));
102 // Now we have one less panel.
103 EXPECT_EQ(panel_count - 1, manager->num_panels());
106 NSMenuItem* CreateMenuItem(NSMenu* menu, int command_id) {
108 [menu addItemWithTitle:@""
109 action:@selector(commandDispatch:)
111 [item setTag:command_id];
116 TEST_F(PanelCocoaTest, CreateClose) {
117 PanelManager* manager = PanelManager::GetInstance();
118 EXPECT_EQ(0, manager->num_panels()); // No panels initially.
120 Panel* panel = CreateTestPanel("Test Panel");
123 gfx::Rect bounds = panel->GetBounds();
124 EXPECT_TRUE(bounds.width() > 0);
125 EXPECT_TRUE(bounds.height() > 0);
127 PanelCocoa* native_window = static_cast<PanelCocoa*>(panel->native_panel());
128 ASSERT_TRUE(native_window);
129 // NSWindows created by NSWindowControllers don't have this bit even if
130 // their NIB has it. The controller's lifetime is the window's lifetime.
131 EXPECT_EQ(NO, [[native_window->controller_ window] isReleasedWhenClosed]);
133 ClosePanelAndWait(panel);
134 EXPECT_EQ(0, manager->num_panels());
137 TEST_F(PanelCocoaTest, AssignedBounds) {
138 Panel* panel1 = CreateTestPanel("Test Panel 1");
139 Panel* panel2 = CreateTestPanel("Test Panel 2");
140 Panel* panel3 = CreateTestPanel("Test Panel 3");
142 gfx::Rect bounds1 = panel1->GetBounds();
143 gfx::Rect bounds2 = panel2->GetBounds();
144 gfx::Rect bounds3 = panel3->GetBounds();
146 // This checks panelManager calculating and assigning bounds right.
147 // Panels should stack on the bottom right to left.
148 EXPECT_LT(bounds3.x() + bounds3.width(), bounds2.x());
149 EXPECT_LT(bounds2.x() + bounds2.width(), bounds1.x());
150 EXPECT_EQ(bounds1.y(), bounds2.y());
151 EXPECT_EQ(bounds2.y(), bounds3.y());
153 // After panel2 is closed, panel3 should take its place.
154 ClosePanelAndWait(panel2);
155 bounds3 = panel3->GetBounds();
156 EXPECT_EQ(bounds2, bounds3);
158 // After panel1 is closed, panel3 should take its place.
159 ClosePanelAndWait(panel1);
160 EXPECT_EQ(bounds1, panel3->GetBounds());
162 ClosePanelAndWait(panel3);
165 // Same test as AssignedBounds, but checks actual bounds on native OS windows.
166 TEST_F(PanelCocoaTest, NativeBounds) {
167 Panel* panel1 = CreateTestPanel("Test Panel 1");
168 Panel* panel2 = CreateTestPanel("Test Panel 2");
169 Panel* panel3 = CreateTestPanel("Test Panel 3");
171 PanelCocoa* native_window1 = static_cast<PanelCocoa*>(panel1->native_panel());
172 PanelCocoa* native_window2 = static_cast<PanelCocoa*>(panel2->native_panel());
173 PanelCocoa* native_window3 = static_cast<PanelCocoa*>(panel3->native_panel());
175 NSRect bounds1 = [[native_window1->controller_ window] frame];
176 NSRect bounds2 = [[native_window2->controller_ window] frame];
177 NSRect bounds3 = [[native_window3->controller_ window] frame];
179 EXPECT_LT(bounds3.origin.x + bounds3.size.width, bounds2.origin.x);
180 EXPECT_LT(bounds2.origin.x + bounds2.size.width, bounds1.origin.x);
181 EXPECT_EQ(bounds1.origin.y, bounds2.origin.y);
182 EXPECT_EQ(bounds2.origin.y, bounds3.origin.y);
185 // After panel2 is closed, panel3 should take its place.
186 PanelAnimatedBoundsObserver bounds_observer(panel3);
187 ClosePanelAndWait(panel2);
188 bounds_observer.Wait();
189 bounds3 = [[native_window3->controller_ window] frame];
190 EXPECT_EQ(bounds2.origin.x, bounds3.origin.x);
191 EXPECT_EQ(bounds2.origin.y, bounds3.origin.y);
192 EXPECT_EQ(bounds2.size.width, bounds3.size.width);
193 EXPECT_EQ(bounds2.size.height, bounds3.size.height);
197 // After panel1 is closed, panel3 should take its place.
198 PanelAnimatedBoundsObserver bounds_observer(panel3);
199 ClosePanelAndWait(panel1);
200 bounds_observer.Wait();
201 bounds3 = [[native_window3->controller_ window] frame];
202 EXPECT_EQ(bounds1.origin.x, bounds3.origin.x);
203 EXPECT_EQ(bounds1.origin.y, bounds3.origin.y);
204 EXPECT_EQ(bounds1.size.width, bounds3.size.width);
205 EXPECT_EQ(bounds1.size.height, bounds3.size.height);
208 ClosePanelAndWait(panel3);
211 // Verify the titlebar is being created.
212 TEST_F(PanelCocoaTest, TitlebarViewCreate) {
213 Panel* panel = CreateTestPanel("Test Panel");
215 PanelCocoa* native_window = static_cast<PanelCocoa*>(panel->native_panel());
217 PanelTitlebarViewCocoa* titlebar = [native_window->controller_ titlebarView];
218 EXPECT_TRUE(titlebar);
219 EXPECT_EQ(native_window->controller_, [titlebar controller]);
221 ClosePanelAndWait(panel);
224 // Verify the sizing of titlebar - should be affixed on top of regular titlebar.
225 TEST_F(PanelCocoaTest, TitlebarViewSizing) {
226 Panel* panel = CreateTestPanel("Test Panel");
228 PanelCocoa* native_window = static_cast<PanelCocoa*>(panel->native_panel());
229 PanelTitlebarViewCocoa* titlebar = [native_window->controller_ titlebarView];
231 NSView* contentView = [[native_window->controller_ window] contentView];
232 VerifyTitlebarLocation(contentView, titlebar);
234 // In local coordinate system, width of titlebar should match width of
235 // content view of the window. They both use the same scale factor.
236 EXPECT_EQ(NSWidth([contentView bounds]), NSWidth([titlebar bounds]));
238 NSRect oldTitleFrame = [[titlebar title] frame];
239 NSRect oldIconFrame = [[titlebar icon] frame];
241 // Now resize the Panel, see that titlebar follows.
242 const int kDelta = 153; // random number
243 gfx::Rect bounds = panel->GetBounds();
244 // Grow panel in a way so that its titlebar moves and grows.
245 bounds.set_x(bounds.x() - kDelta);
246 bounds.set_y(bounds.y() - kDelta);
247 bounds.set_width(bounds.width() + kDelta);
248 bounds.set_height(bounds.height() + kDelta);
250 PanelAnimatedBoundsObserver bounds_observer(panel);
251 native_window->SetPanelBounds(bounds);
252 bounds_observer.Wait();
254 // Verify the panel resized.
255 NSRect window_frame = [[native_window->controller_ window] frame];
256 EXPECT_EQ(NSWidth(window_frame), bounds.width());
257 EXPECT_EQ(NSHeight(window_frame), bounds.height());
259 // Verify the titlebar is still on top of regular titlebar.
260 VerifyTitlebarLocation(contentView, titlebar);
262 // Verify that the title/icon frames were updated.
263 NSRect newTitleFrame = [[titlebar title] frame];
264 NSRect newIconFrame = [[titlebar icon] frame];
266 EXPECT_EQ(newTitleFrame.origin.x - newIconFrame.origin.x,
267 oldTitleFrame.origin.x - oldIconFrame.origin.x);
268 // Icon and Text should remain at the same left-aligned position.
269 EXPECT_EQ(newTitleFrame.origin.x, oldTitleFrame.origin.x);
270 EXPECT_EQ(newIconFrame.origin.x, oldIconFrame.origin.x);
272 ClosePanelAndWait(panel);
275 // Verify closing behavior of titlebar close button.
276 TEST_F(PanelCocoaTest, TitlebarViewClose) {
277 Panel* panel = CreateTestPanel("Test Panel");
278 PanelCocoa* native_window = static_cast<PanelCocoa*>(panel->native_panel());
280 PanelTitlebarViewCocoa* titlebar = [native_window->controller_ titlebarView];
281 EXPECT_TRUE(titlebar);
283 PanelManager* manager = PanelManager::GetInstance();
284 EXPECT_EQ(1, manager->num_panels());
285 // Simulate clicking Close Button and wait until the Panel closes.
286 content::WindowedNotificationObserver signal(
287 chrome::NOTIFICATION_PANEL_CLOSED,
288 content::Source<Panel>(panel));
289 [titlebar simulateCloseButtonClick];
291 EXPECT_EQ(0, manager->num_panels());
294 // Verify some menu items being properly enabled/disabled for panels.
295 TEST_F(PanelCocoaTest, MenuItems) {
296 Panel* panel = CreateTestPanel("Test Panel");
298 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]);
299 NSMenuItem* close_tab_menu_item = CreateMenuItem(menu, IDC_CLOSE_TAB);
300 NSMenuItem* new_tab_menu_item = CreateMenuItem(menu, IDC_NEW_TAB);
301 NSMenuItem* new_tab_window_item = CreateMenuItem(menu, IDC_NEW_WINDOW);
302 NSMenuItem* new_tab_incognito_window_item =
303 CreateMenuItem(menu, IDC_NEW_INCOGNITO_WINDOW);
304 NSMenuItem* close_window_menu_item = CreateMenuItem(menu, IDC_CLOSE_WINDOW);
305 NSMenuItem* find_menu_item = CreateMenuItem(menu, IDC_FIND);
306 NSMenuItem* find_previous_menu_item = CreateMenuItem(menu, IDC_FIND_PREVIOUS);
307 NSMenuItem* find_next_menu_item = CreateMenuItem(menu, IDC_FIND_NEXT);
308 NSMenuItem* fullscreen_menu_item = CreateMenuItem(menu, IDC_FULLSCREEN);
309 NSMenuItem* presentation_menu_item =
310 CreateMenuItem(menu, IDC_PRESENTATION_MODE);
311 NSMenuItem* sync_menu_item = CreateMenuItem(menu, IDC_SHOW_SYNC_SETUP);
312 NSMenuItem* dev_tools_item = CreateMenuItem(menu, IDC_DEV_TOOLS);
313 NSMenuItem* dev_tools_console_item =
314 CreateMenuItem(menu, IDC_DEV_TOOLS_CONSOLE);
316 PanelCocoa* native_window = static_cast<PanelCocoa*>(panel->native_panel());
317 PanelWindowControllerCocoa* panel_controller = native_window->controller_;
318 for (NSMenuItem *item in [menu itemArray])
319 [item setTarget:panel_controller];
321 [menu update]; // Trigger validation of menu items.
322 EXPECT_FALSE([close_tab_menu_item isEnabled]);
323 EXPECT_TRUE([close_window_menu_item isEnabled]);
324 // No find support. Panels don't have a find bar.
325 EXPECT_FALSE([find_menu_item isEnabled]);
326 EXPECT_FALSE([find_previous_menu_item isEnabled]);
327 EXPECT_FALSE([find_next_menu_item isEnabled]);
328 EXPECT_FALSE([fullscreen_menu_item isEnabled]);
329 EXPECT_FALSE([presentation_menu_item isEnabled]);
330 EXPECT_FALSE([sync_menu_item isEnabled]);
331 // These are not enabled by Panel, so they are expected to be disabled for
332 // this unit_test. In real Chrome app, they are enabled by Chrome NSApp
333 // controller. PanelCocoaBrowsertest.MenuItems verifies that.
334 EXPECT_FALSE([new_tab_menu_item isEnabled]);
335 EXPECT_FALSE([new_tab_window_item isEnabled]);
336 EXPECT_FALSE([new_tab_incognito_window_item isEnabled]);
338 EXPECT_TRUE([dev_tools_item isEnabled]);
339 EXPECT_TRUE([dev_tools_console_item isEnabled]);
341 // Verify that commandDispatch on an invalid menu item does not crash.
342 [NSApp sendAction:[sync_menu_item action]
343 to:[sync_menu_item target]
344 from:sync_menu_item];
346 ClosePanelAndWait(panel);
349 TEST_F(PanelCocoaTest, KeyEvent) {
350 Panel* panel = CreateTestPanel("Test Panel");
351 NSEvent* event = [NSEvent keyEventWithType:NSKeyDown
353 modifierFlags:NSControlKeyMask
358 charactersIgnoringModifiers:@""
361 PanelCocoa* native_window = static_cast<PanelCocoa*>(panel->native_panel());
362 [BrowserWindowUtils handleKeyboardEvent:event
363 inWindow:[native_window->controller_ window]];
364 ClosePanelAndWait(panel);
367 TEST_F(PanelCocoaTest, SetTitle) {
368 NSString *appName = @"Test Panel";
369 Panel* panel = CreateTestPanel(base::SysNSStringToUTF8(appName));
372 PanelCocoa* native_window = static_cast<PanelCocoa*>(panel->native_panel());
373 ASSERT_TRUE(native_window);
374 NSString* previousTitle = [[native_window->controller_ window] title];
375 EXPECT_NSNE(appName, previousTitle);
376 [native_window->controller_ updateTitleBar];
377 chrome::testing::NSRunLoopRunAllPending();
378 NSString* currentTitle = [[native_window->controller_ window] title];
379 EXPECT_NSEQ(appName, currentTitle);
380 EXPECT_NSNE(currentTitle, previousTitle);
381 ClosePanelAndWait(panel);
384 TEST_F(PanelCocoaTest, ActivatePanel) {
385 Panel* panel = CreateTestPanel("Test Panel");
386 Panel* panel2 = CreateTestPanel("Test Panel 2");
390 PanelCocoa* native_window = static_cast<PanelCocoa*>(panel->native_panel());
391 ASSERT_TRUE(native_window);
392 PanelCocoa* native_window2 = static_cast<PanelCocoa*>(panel2->native_panel());
393 ASSERT_TRUE(native_window2);
395 // No one has a good answer why but apparently windows can't take keyboard
396 // focus outside of interactive UI tests. BrowserWindowController uses the
397 // same way of testing this.
398 native_window->ActivatePanel();
399 chrome::testing::NSRunLoopRunAllPending();
400 NSWindow* frontmostWindow = [[NSApp orderedWindows] objectAtIndex:0];
401 EXPECT_NSEQ(frontmostWindow, [native_window->controller_ window]);
403 native_window2->ActivatePanel();
404 chrome::testing::NSRunLoopRunAllPending();
405 frontmostWindow = [[NSApp orderedWindows] objectAtIndex:0];
406 EXPECT_NSEQ(frontmostWindow, [native_window2->controller_ window]);
408 ClosePanelAndWait(panel);
409 ClosePanelAndWait(panel2);