OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #import <Cocoa/Cocoa.h> | |
6 | |
7 #include "base/location.h" | |
8 #import "base/mac/scoped_nsobject.h" | |
9 #include "base/run_loop.h" | |
10 #include "base/single_thread_task_runner.h" | |
11 #include "base/test/test_timeouts.h" | |
12 #include "base/threading/thread_task_runner_handle.h" | |
13 #include "chrome/app/chrome_command_ids.h" | |
14 #include "chrome/browser/chrome_notification_types.h" | |
15 #include "chrome/browser/ui/browser_commands.h" | |
16 #include "chrome/browser/ui/browser_finder.h" | |
17 #include "chrome/browser/ui/browser_window.h" | |
18 #include "chrome/browser/ui/panels/base_panel_browser_test.h" | |
19 #include "chrome/browser/ui/panels/panel.h" | |
20 #include "content/public/test/test_utils.h" | |
21 | |
22 // Class that spins a run loop until an NSWindow gains key status. | |
23 @interface CocoaActivationWaiter : NSObject { | |
24 @private | |
25 base::RunLoop* runLoop_; | |
26 BOOL observed_; | |
27 } | |
28 | |
29 - (id)initWithWindow:(NSWindow*)window; | |
30 - (void)windowDidBecomeKey:(NSNotification*)notification; | |
31 - (BOOL)waitUntilActive; | |
32 | |
33 @end | |
34 | |
35 @implementation CocoaActivationWaiter | |
36 | |
37 - (id)initWithWindow:(NSWindow*)window { | |
38 EXPECT_FALSE([window isKeyWindow]); | |
39 if ((self = [super init])) { | |
40 [[NSNotificationCenter defaultCenter] | |
41 addObserver:self | |
42 selector:@selector(windowDidBecomeKey:) | |
43 name:NSWindowDidBecomeKeyNotification | |
44 object:window]; | |
45 } | |
46 return self; | |
47 } | |
48 | |
49 - (void)dealloc { | |
50 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
51 [super dealloc]; | |
52 } | |
53 | |
54 - (void)windowDidBecomeKey:(NSNotification*)notification { | |
55 observed_ = YES; | |
56 if (runLoop_) | |
57 runLoop_->Quit(); | |
58 } | |
59 | |
60 - (BOOL)waitUntilActive { | |
61 if (observed_) | |
62 return YES; | |
63 | |
64 base::RunLoop runLoop; | |
65 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
66 FROM_HERE, runLoop.QuitClosure(), TestTimeouts::action_timeout()); | |
67 runLoop_ = &runLoop; | |
68 runLoop.Run(); | |
69 runLoop_ = nullptr; | |
70 return observed_; | |
71 } | |
72 | |
73 @end | |
74 | |
75 typedef BasePanelBrowserTest PanelCocoaBrowserTest; | |
76 | |
77 IN_PROC_BROWSER_TEST_F(PanelCocoaBrowserTest, MenuItems) { | |
78 Panel* panel = CreatePanel("Panel"); | |
79 | |
80 // Close main tabbed window. | |
81 content::WindowedNotificationObserver signal( | |
82 chrome::NOTIFICATION_BROWSER_CLOSED, | |
83 content::Source<Browser>(browser())); | |
84 chrome::CloseWindow(browser()); | |
85 signal.Wait(); | |
86 | |
87 // There should be no browser windows. | |
88 EXPECT_EQ(0u, chrome::GetTotalBrowserCount()); | |
89 | |
90 // There should be one panel. | |
91 EXPECT_EQ(1, PanelManager::GetInstance()->num_panels()); | |
92 | |
93 NSMenu* mainMenu = [NSApp mainMenu]; | |
94 EXPECT_TRUE(mainMenu); | |
95 | |
96 // Chrome(0) File(1) .... | |
97 // Get File submenu. It doesn't have a command id, fetch it by index. | |
98 NSMenu* fileSubmenu = [[[mainMenu itemArray] objectAtIndex:1] submenu]; | |
99 EXPECT_TRUE(fileSubmenu); | |
100 [fileSubmenu update]; | |
101 | |
102 // Verify the items normally enabled for "all windows closed" case are | |
103 // also enabled when there is a panel but no browser windows on the screen. | |
104 NSMenuItem* item = [fileSubmenu itemWithTag:IDC_NEW_TAB]; | |
105 EXPECT_TRUE(item); | |
106 EXPECT_TRUE([item isEnabled]); | |
107 | |
108 item = [fileSubmenu itemWithTag:IDC_NEW_WINDOW]; | |
109 EXPECT_TRUE(item); | |
110 EXPECT_TRUE([item isEnabled]); | |
111 | |
112 item = [fileSubmenu itemWithTag:IDC_NEW_INCOGNITO_WINDOW]; | |
113 EXPECT_TRUE(item); | |
114 EXPECT_TRUE([item isEnabled]); | |
115 | |
116 NSMenu* historySubmenu = [[mainMenu itemWithTag:IDC_HISTORY_MENU] submenu]; | |
117 EXPECT_TRUE(historySubmenu); | |
118 [historySubmenu update]; | |
119 | |
120 // These should be disabled. | |
121 item = [historySubmenu itemWithTag:IDC_HOME]; | |
122 EXPECT_TRUE(item); | |
123 EXPECT_FALSE([item isEnabled]); | |
124 | |
125 item = [historySubmenu itemWithTag:IDC_BACK]; | |
126 EXPECT_TRUE(item); | |
127 EXPECT_FALSE([item isEnabled]); | |
128 | |
129 item = [historySubmenu itemWithTag:IDC_FORWARD]; | |
130 EXPECT_TRUE(item); | |
131 EXPECT_FALSE([item isEnabled]); | |
132 | |
133 // 'Close Window' should be enabled because the remaining Panel is a Responder | |
134 // which implements performClose:, the 'action' of 'Close Window'. | |
135 for (NSMenuItem *i in [fileSubmenu itemArray]) { | |
136 if ([i action] == @selector(performClose:)) { | |
137 item = i; | |
138 break; | |
139 } | |
140 } | |
141 EXPECT_TRUE(item); | |
142 EXPECT_TRUE([item isEnabled]); | |
143 | |
144 panel->Close(); | |
145 } | |
146 | |
147 // Test that panels do not become active when closing a window, even when a | |
148 // panel is otherwise the topmost window. | |
149 IN_PROC_BROWSER_TEST_F(PanelCocoaBrowserTest, InactivePanelsNotActivated) { | |
150 // Note CreateDockedPanel() sets wait_for_fully_created and SHOW_AS_ACTIVE, | |
151 // so the following spins a run loop until the respective panel is activated. | |
152 Panel* docked_panel_1 = CreateDockedPanel("Panel1", gfx::Rect()); | |
153 EXPECT_TRUE([docked_panel_1->GetNativeWindow() isKeyWindow]); | |
154 | |
155 // Activate the browser. Otherwise closing the second panel will correctly | |
156 // raise the first panel since panels are allowed to become key if they were | |
157 // actually the most recently focused window (as opposed to being merely the | |
158 // topmost window on the z-order stack). | |
159 NSWindow* browser_window = browser()->window()->GetNativeWindow(); | |
160 base::scoped_nsobject<CocoaActivationWaiter> waiter( | |
161 [[CocoaActivationWaiter alloc] initWithWindow:browser_window]); | |
162 browser()->window()->Activate(); | |
163 EXPECT_TRUE([waiter waitUntilActive]); | |
164 | |
165 // Creating a second panel will activate it (and make it topmost). | |
166 Panel* docked_panel_2 = CreateDockedPanel("Panel2", gfx::Rect()); | |
167 EXPECT_TRUE([docked_panel_2->GetNativeWindow() isKeyWindow]); | |
168 | |
169 // Verify the assumptions that the panels are actually topmost. | |
170 EXPECT_EQ(docked_panel_2->GetNativeWindow(), | |
171 [[NSApp orderedWindows] objectAtIndex:0]); | |
172 EXPECT_EQ(docked_panel_1->GetNativeWindow(), | |
173 [[NSApp orderedWindows] objectAtIndex:1]); | |
174 | |
175 // Close the second panel and wait for the browser to become active. | |
176 waiter.reset([[CocoaActivationWaiter alloc] initWithWindow:browser_window]); | |
177 docked_panel_2->Close(); | |
178 | |
179 EXPECT_TRUE([waiter waitUntilActive]); | |
180 EXPECT_TRUE([browser_window isKeyWindow]); | |
181 } | |
OLD | NEW |