OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.h" | 5 #import "chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #import "base/mac/foundation_util.h" | 10 #import "base/mac/foundation_util.h" |
11 #import "base/mac/scoped_nsobject.h" | 11 #import "base/mac/scoped_nsobject.h" |
12 #import "base/mac/scoped_objc_class_swizzler.h" | 12 #import "base/mac/scoped_objc_class_swizzler.h" |
13 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
14 #include "chrome/app/chrome_command_ids.h" | 14 #include "chrome/app/chrome_command_ids.h" |
15 #include "chrome/browser/apps/app_browsertest_util.h" | 15 #include "chrome/browser/apps/app_browsertest_util.h" |
16 #include "chrome/browser/apps/app_shim/extension_app_shim_handler_mac.h" | 16 #include "chrome/browser/apps/app_shim/extension_app_shim_handler_mac.h" |
17 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
18 #include "chrome/browser/extensions/launch_util.h" | 18 #include "chrome/browser/extensions/launch_util.h" |
19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/ui/browser_iterator.h" | 20 #include "chrome/browser/ui/browser_iterator.h" |
21 #include "chrome/browser/ui/browser_window.h" | 21 #include "chrome/browser/ui/browser_window.h" |
| 22 #import "chrome/browser/ui/test/scoped_fake_nswindow_main_status.h" |
22 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
23 #include "extensions/browser/app_window/app_window_registry.h" | 24 #include "extensions/browser/app_window/app_window_registry.h" |
24 #include "extensions/browser/app_window/native_app_window.h" | 25 #include "extensions/browser/app_window/native_app_window.h" |
25 #include "extensions/browser/uninstall_reason.h" | 26 #include "extensions/browser/uninstall_reason.h" |
26 #include "extensions/common/extension.h" | 27 #include "extensions/common/extension.h" |
27 #include "extensions/test/extension_test_message_listener.h" | 28 #include "extensions/test/extension_test_message_listener.h" |
28 | 29 |
29 // Donates a testing implementation of [NSWindow isMainWindow]. | |
30 @interface IsMainWindowDonorForWindow : NSObject | |
31 @end | |
32 | |
33 namespace { | 30 namespace { |
34 | 31 |
35 // Simulates a particular NSWindow to report YES for [NSWindow isMainWindow]. | |
36 // This allows test coverage of code relying on window focus changes without | |
37 // resorting to an interactive_ui_test. | |
38 class ScopedFakeWindowMainStatus { | |
39 public: | |
40 ScopedFakeWindowMainStatus(NSWindow* window) | |
41 : swizzler_([NSWindow class], | |
42 [IsMainWindowDonorForWindow class], | |
43 @selector(isMainWindow)) { | |
44 DCHECK(!window_); | |
45 window_ = window; | |
46 } | |
47 | |
48 ~ScopedFakeWindowMainStatus() { window_ = nil; } | |
49 | |
50 static NSWindow* GetMainWindow() { return window_; } | |
51 | |
52 private: | |
53 static NSWindow* window_; | |
54 base::mac::ScopedObjCClassSwizzler swizzler_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(ScopedFakeWindowMainStatus); | |
57 }; | |
58 | |
59 NSWindow* ScopedFakeWindowMainStatus::window_ = nil; | |
60 | |
61 class AppShimMenuControllerBrowserTest | 32 class AppShimMenuControllerBrowserTest |
62 : public extensions::PlatformAppBrowserTest { | 33 : public extensions::PlatformAppBrowserTest { |
63 protected: | 34 protected: |
64 // The apps that can be installed and launched by SetUpApps(). | 35 // The apps that can be installed and launched by SetUpApps(). |
65 enum AvailableApps { PACKAGED_1 = 0x1, PACKAGED_2 = 0x2, HOSTED = 0x4 }; | 36 enum AvailableApps { PACKAGED_1 = 0x1, PACKAGED_2 = 0x2, HOSTED = 0x4 }; |
66 | 37 |
67 AppShimMenuControllerBrowserTest() | 38 AppShimMenuControllerBrowserTest() |
68 : app_1_(nullptr), | 39 : app_1_(nullptr), |
69 app_2_(nullptr), | 40 app_2_(nullptr), |
70 hosted_app_(nullptr), | 41 hosted_app_(nullptr), |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 object:app_2_app_window->GetNativeWindow()]; | 172 object:app_2_app_window->GetNativeWindow()]; |
202 CheckNoAppMenus(); | 173 CheckNoAppMenus(); |
203 } | 174 } |
204 | 175 |
205 // Test that closing windows without main status do not update the menu. | 176 // Test that closing windows without main status do not update the menu. |
206 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, | 177 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, |
207 ClosingBackgroundWindowLeavesMenuBar) { | 178 ClosingBackgroundWindowLeavesMenuBar) { |
208 // Start with app1 active. | 179 // Start with app1 active. |
209 SetUpApps(PACKAGED_1); | 180 SetUpApps(PACKAGED_1); |
210 extensions::AppWindow* app_1_app_window = FirstWindowForApp(app_1_); | 181 extensions::AppWindow* app_1_app_window = FirstWindowForApp(app_1_); |
211 ScopedFakeWindowMainStatus app_1_is_main(app_1_app_window->GetNativeWindow()); | 182 ScopedFakeNSWindowMainStatus app_1_is_main( |
| 183 app_1_app_window->GetNativeWindow()); |
212 | 184 |
213 [[NSNotificationCenter defaultCenter] | 185 [[NSNotificationCenter defaultCenter] |
214 postNotificationName:NSWindowDidBecomeMainNotification | 186 postNotificationName:NSWindowDidBecomeMainNotification |
215 object:app_1_app_window->GetNativeWindow()]; | 187 object:app_1_app_window->GetNativeWindow()]; |
216 CheckHasAppMenus(app_1_); | 188 CheckHasAppMenus(app_1_); |
217 | 189 |
218 // Closing a background window without focusing it should not change menus. | 190 // Closing a background window without focusing it should not change menus. |
219 BrowserWindow* chrome_window = chrome::BrowserIterator()->window(); | 191 BrowserWindow* chrome_window = chrome::BrowserIterator()->window(); |
220 chrome_window->Close(); | 192 chrome_window->Close(); |
221 [[NSNotificationCenter defaultCenter] | 193 [[NSNotificationCenter defaultCenter] |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 // This essentially tests that a NSWindowWillCloseNotification gets fired when | 240 // This essentially tests that a NSWindowWillCloseNotification gets fired when |
269 // an app is uninstalled. We need to close the other windows first since the | 241 // an app is uninstalled. We need to close the other windows first since the |
270 // menu only changes on a NSWindowWillCloseNotification if there are no other | 242 // menu only changes on a NSWindowWillCloseNotification if there are no other |
271 // windows. | 243 // windows. |
272 FirstWindowForApp(app_2_)->GetBaseWindow()->Close(); | 244 FirstWindowForApp(app_2_)->GetBaseWindow()->Close(); |
273 chrome::BrowserIterator()->window()->Close(); | 245 chrome::BrowserIterator()->window()->Close(); |
274 NSWindow* app_1_window = FirstWindowForApp(app_1_)->GetNativeWindow(); | 246 NSWindow* app_1_window = FirstWindowForApp(app_1_)->GetNativeWindow(); |
275 [[NSNotificationCenter defaultCenter] | 247 [[NSNotificationCenter defaultCenter] |
276 postNotificationName:NSWindowDidBecomeMainNotification | 248 postNotificationName:NSWindowDidBecomeMainNotification |
277 object:app_1_window]; | 249 object:app_1_window]; |
278 ScopedFakeWindowMainStatus app_1_is_main(app_1_window); | 250 ScopedFakeNSWindowMainStatus app_1_is_main(app_1_window); |
279 | 251 |
280 CheckHasAppMenus(app_1_); | 252 CheckHasAppMenus(app_1_); |
281 ExtensionService::UninstallExtensionHelper( | 253 ExtensionService::UninstallExtensionHelper( |
282 extension_service(), | 254 extension_service(), |
283 app_1_->id(), | 255 app_1_->id(), |
284 extensions::UNINSTALL_REASON_FOR_TESTING); | 256 extensions::UNINSTALL_REASON_FOR_TESTING); |
285 CheckNoAppMenus(); | 257 CheckNoAppMenus(); |
286 } | 258 } |
287 | 259 |
288 } // namespace | 260 } // namespace |
289 | |
290 @implementation IsMainWindowDonorForWindow | |
291 - (BOOL)isMainWindow { | |
292 NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self); | |
293 return selfAsWindow == ScopedFakeWindowMainStatus::GetMainWindow(); | |
294 } | |
295 @end | |
OLD | NEW |