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 #include "base/mac/scoped_nsobject.h" | 10 #import "base/mac/foundation_util.h" |
| 11 #import "base/mac/scoped_nsobject.h" |
| 12 #import "base/mac/scoped_objc_class_swizzler.h" |
11 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
12 #include "chrome/app/chrome_command_ids.h" | 14 #include "chrome/app/chrome_command_ids.h" |
13 #include "chrome/browser/apps/app_browsertest_util.h" | 15 #include "chrome/browser/apps/app_browsertest_util.h" |
14 #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" |
15 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
16 #include "chrome/browser/extensions/launch_util.h" | 18 #include "chrome/browser/extensions/launch_util.h" |
17 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/ui/browser_iterator.h" | 20 #include "chrome/browser/ui/browser_iterator.h" |
19 #include "chrome/browser/ui/browser_window.h" | 21 #include "chrome/browser/ui/browser_window.h" |
20 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
21 #include "extensions/browser/app_window/app_window_registry.h" | 23 #include "extensions/browser/app_window/app_window_registry.h" |
22 #include "extensions/browser/app_window/native_app_window.h" | 24 #include "extensions/browser/app_window/native_app_window.h" |
23 #include "extensions/browser/uninstall_reason.h" | 25 #include "extensions/browser/uninstall_reason.h" |
24 #include "extensions/common/extension.h" | 26 #include "extensions/common/extension.h" |
25 #include "extensions/test/extension_test_message_listener.h" | 27 #include "extensions/test/extension_test_message_listener.h" |
26 | 28 |
| 29 // Donates a testing implementation of [NSWindow isMainWindow]. |
| 30 @interface IsMainWindowDonorForWindow : NSObject |
| 31 @end |
| 32 |
27 namespace { | 33 namespace { |
28 | 34 |
| 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 |
29 class AppShimMenuControllerBrowserTest | 61 class AppShimMenuControllerBrowserTest |
30 : public extensions::PlatformAppBrowserTest { | 62 : public extensions::PlatformAppBrowserTest { |
31 protected: | 63 protected: |
32 // The apps that can be installed and launched by SetUpApps(). | 64 // The apps that can be installed and launched by SetUpApps(). |
33 enum AvailableApps { PACKAGED_1 = 0x1, PACKAGED_2 = 0x2, HOSTED = 0x4 }; | 65 enum AvailableApps { PACKAGED_1 = 0x1, PACKAGED_2 = 0x2, HOSTED = 0x4 }; |
34 | 66 |
35 AppShimMenuControllerBrowserTest() | 67 AppShimMenuControllerBrowserTest() |
36 : app_1_(nullptr), | 68 : app_1_(nullptr), |
37 app_2_(nullptr), | 69 app_2_(nullptr), |
38 hosted_app_(nullptr), | 70 hosted_app_(nullptr), |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 // When an app window is closed and there are no other app windows, the menu | 195 // When an app window is closed and there are no other app windows, the menu |
164 // items for the app should be removed. | 196 // items for the app should be removed. |
165 app_1_app_window->GetBaseWindow()->Close(); | 197 app_1_app_window->GetBaseWindow()->Close(); |
166 chrome_window->Close(); | 198 chrome_window->Close(); |
167 [[NSNotificationCenter defaultCenter] | 199 [[NSNotificationCenter defaultCenter] |
168 postNotificationName:NSWindowWillCloseNotification | 200 postNotificationName:NSWindowWillCloseNotification |
169 object:app_2_app_window->GetNativeWindow()]; | 201 object:app_2_app_window->GetNativeWindow()]; |
170 CheckNoAppMenus(); | 202 CheckNoAppMenus(); |
171 } | 203 } |
172 | 204 |
| 205 // Test that closing windows without main status do not update the menu. |
| 206 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, |
| 207 ClosingBackgroundWindowLeavesMenuBar) { |
| 208 // Start with app1 active. |
| 209 SetUpApps(PACKAGED_1); |
| 210 extensions::AppWindow* app_1_app_window = FirstWindowForApp(app_1_); |
| 211 ScopedFakeWindowMainStatus app_1_is_main(app_1_app_window->GetNativeWindow()); |
| 212 |
| 213 [[NSNotificationCenter defaultCenter] |
| 214 postNotificationName:NSWindowDidBecomeMainNotification |
| 215 object:app_1_app_window->GetNativeWindow()]; |
| 216 CheckHasAppMenus(app_1_); |
| 217 |
| 218 // Closing a background window without focusing it should not change menus. |
| 219 BrowserWindow* chrome_window = chrome::BrowserIterator()->window(); |
| 220 chrome_window->Close(); |
| 221 [[NSNotificationCenter defaultCenter] |
| 222 postNotificationName:NSWindowWillCloseNotification |
| 223 object:chrome_window->GetNativeWindow()]; |
| 224 CheckHasAppMenus(app_1_); |
| 225 |
| 226 app_1_app_window->GetBaseWindow()->Close(); |
| 227 CheckNoAppMenus(); |
| 228 } |
| 229 |
173 // Test to check that hosted apps have "Find" and "Paste and Match Style" menu | 230 // Test to check that hosted apps have "Find" and "Paste and Match Style" menu |
174 // items under the "Edit" menu. | 231 // items under the "Edit" menu. |
175 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, | 232 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, |
176 HostedAppHasAdditionalEditMenuItems) { | 233 HostedAppHasAdditionalEditMenuItems) { |
177 SetUpApps(HOSTED | PACKAGED_1); | 234 SetUpApps(HOSTED | PACKAGED_1); |
178 | 235 |
179 // Find the first hosted app window. | 236 // Find the first hosted app window. |
180 Browser* hosted_app_browser = nullptr; | 237 Browser* hosted_app_browser = nullptr; |
181 BrowserList* browsers = | 238 BrowserList* browsers = |
182 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE); | 239 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE); |
(...skipping 24 matching lines...) Expand all Loading... |
207 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, | 264 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, |
208 ExtensionUninstallUpdatesMenuBar) { | 265 ExtensionUninstallUpdatesMenuBar) { |
209 SetUpApps(PACKAGED_1 | PACKAGED_2); | 266 SetUpApps(PACKAGED_1 | PACKAGED_2); |
210 | 267 |
211 // This essentially tests that a NSWindowWillCloseNotification gets fired when | 268 // This essentially tests that a NSWindowWillCloseNotification gets fired when |
212 // an app is uninstalled. We need to close the other windows first since the | 269 // an app is uninstalled. We need to close the other windows first since the |
213 // menu only changes on a NSWindowWillCloseNotification if there are no other | 270 // menu only changes on a NSWindowWillCloseNotification if there are no other |
214 // windows. | 271 // windows. |
215 FirstWindowForApp(app_2_)->GetBaseWindow()->Close(); | 272 FirstWindowForApp(app_2_)->GetBaseWindow()->Close(); |
216 chrome::BrowserIterator()->window()->Close(); | 273 chrome::BrowserIterator()->window()->Close(); |
| 274 NSWindow* app_1_window = FirstWindowForApp(app_1_)->GetNativeWindow(); |
217 [[NSNotificationCenter defaultCenter] | 275 [[NSNotificationCenter defaultCenter] |
218 postNotificationName:NSWindowDidBecomeMainNotification | 276 postNotificationName:NSWindowDidBecomeMainNotification |
219 object:FirstWindowForApp(app_1_)->GetNativeWindow()]; | 277 object:app_1_window]; |
| 278 ScopedFakeWindowMainStatus app_1_is_main(app_1_window); |
220 | 279 |
221 CheckHasAppMenus(app_1_); | 280 CheckHasAppMenus(app_1_); |
222 ExtensionService::UninstallExtensionHelper( | 281 ExtensionService::UninstallExtensionHelper( |
223 extension_service(), | 282 extension_service(), |
224 app_1_->id(), | 283 app_1_->id(), |
225 extensions::UNINSTALL_REASON_FOR_TESTING); | 284 extensions::UNINSTALL_REASON_FOR_TESTING); |
226 CheckNoAppMenus(); | 285 CheckNoAppMenus(); |
227 } | 286 } |
228 | 287 |
229 } // namespace | 288 } // 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 |