| 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" | |
| 23 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 24 #include "extensions/browser/app_window/app_window_registry.h" | 23 #include "extensions/browser/app_window/app_window_registry.h" |
| 25 #include "extensions/browser/app_window/native_app_window.h" | 24 #include "extensions/browser/app_window/native_app_window.h" |
| 26 #include "extensions/browser/uninstall_reason.h" | 25 #include "extensions/browser/uninstall_reason.h" |
| 27 #include "extensions/common/extension.h" | 26 #include "extensions/common/extension.h" |
| 28 #include "extensions/test/extension_test_message_listener.h" | 27 #include "extensions/test/extension_test_message_listener.h" |
| 28 #import "ui/base/test/scoped_fake_nswindow_focus.h" |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 class AppShimMenuControllerBrowserTest | 32 class AppShimMenuControllerBrowserTest |
| 33 : public extensions::PlatformAppBrowserTest { | 33 : public extensions::PlatformAppBrowserTest { |
| 34 protected: | 34 protected: |
| 35 // The apps that can be installed and launched by SetUpApps(). | 35 // The apps that can be installed and launched by SetUpApps(). |
| 36 enum AvailableApps { PACKAGED_1 = 0x1, PACKAGED_2 = 0x2, HOSTED = 0x4 }; | 36 enum AvailableApps { PACKAGED_1 = 0x1, PACKAGED_2 = 0x2, HOSTED = 0x4 }; |
| 37 | 37 |
| 38 AppShimMenuControllerBrowserTest() | 38 AppShimMenuControllerBrowserTest() |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 extensions::LAUNCH_TYPE_WINDOW); | 70 extensions::LAUNCH_TYPE_WINDOW); |
| 71 LaunchHostedApp(hosted_app_); | 71 LaunchHostedApp(hosted_app_); |
| 72 } | 72 } |
| 73 | 73 |
| 74 initial_menu_item_count_ = [[[NSApp mainMenu] itemArray] count]; | 74 initial_menu_item_count_ = [[[NSApp mainMenu] itemArray] count]; |
| 75 } | 75 } |
| 76 | 76 |
| 77 void CheckHasAppMenus(const extensions::Extension* app) const { | 77 void CheckHasAppMenus(const extensions::Extension* app) const { |
| 78 const int kExtraTopLevelItems = 4; | 78 const int kExtraTopLevelItems = 4; |
| 79 NSArray* item_array = [[NSApp mainMenu] itemArray]; | 79 NSArray* item_array = [[NSApp mainMenu] itemArray]; |
| 80 EXPECT_EQ(initial_menu_item_count_ + kExtraTopLevelItems, | 80 ASSERT_EQ(initial_menu_item_count_ + kExtraTopLevelItems, |
| 81 [item_array count]); | 81 [item_array count]); |
| 82 for (NSUInteger i = 0; i < initial_menu_item_count_; ++i) | 82 for (NSUInteger i = 0; i < initial_menu_item_count_; ++i) |
| 83 EXPECT_TRUE([[item_array objectAtIndex:i] isHidden]); | 83 EXPECT_TRUE([[item_array objectAtIndex:i] isHidden]); |
| 84 NSMenuItem* app_menu = [item_array objectAtIndex:initial_menu_item_count_]; | 84 NSMenuItem* app_menu = [item_array objectAtIndex:initial_menu_item_count_]; |
| 85 EXPECT_EQ(app->id(), base::SysNSStringToUTF8([app_menu title])); | 85 EXPECT_EQ(app->id(), base::SysNSStringToUTF8([app_menu title])); |
| 86 EXPECT_EQ(app->name(), | 86 EXPECT_EQ(app->name(), |
| 87 base::SysNSStringToUTF8([[app_menu submenu] title])); | 87 base::SysNSStringToUTF8([[app_menu submenu] title])); |
| 88 for (NSUInteger i = initial_menu_item_count_; | 88 for (NSUInteger i = initial_menu_item_count_; |
| 89 i < initial_menu_item_count_ + kExtraTopLevelItems; | 89 i < initial_menu_item_count_ + kExtraTopLevelItems; |
| 90 ++i) { | 90 ++i) { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 } | 179 } |
| 180 | 180 |
| 181 // Test that closing windows without main status do not update the menu. | 181 // Test that closing windows without main status do not update the menu. |
| 182 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, | 182 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, |
| 183 ClosingBackgroundWindowLeavesMenuBar) { | 183 ClosingBackgroundWindowLeavesMenuBar) { |
| 184 // Start with app1 active. | 184 // Start with app1 active. |
| 185 SetUpApps(PACKAGED_1); | 185 SetUpApps(PACKAGED_1); |
| 186 extensions::AppWindow* app_1_app_window = FirstWindowForApp(app_1_); | 186 extensions::AppWindow* app_1_app_window = FirstWindowForApp(app_1_); |
| 187 | 187 |
| 188 { | 188 { |
| 189 ScopedFakeNSWindowMainStatus app_1_is_main( | 189 ui::test::ScopedFakeNSWindowFocus fake_focus; |
| 190 app_1_app_window->GetNativeWindow()); | 190 [app_1_app_window->GetNativeWindow() makeMainWindow]; |
| 191 [[NSNotificationCenter defaultCenter] | |
| 192 postNotificationName:NSWindowDidBecomeMainNotification | |
| 193 object:app_1_app_window->GetNativeWindow()]; | |
| 194 CheckHasAppMenus(app_1_); | 191 CheckHasAppMenus(app_1_); |
| 195 | 192 |
| 196 // Closing a background window without focusing it should not change menus. | 193 // Closing a background window without focusing it should not change menus. |
| 197 BrowserWindow* chrome_window = chrome::BrowserIterator()->window(); | 194 BrowserWindow* chrome_window = chrome::BrowserIterator()->window(); |
| 198 chrome_window->Close(); | 195 chrome_window->Close(); |
| 199 [[NSNotificationCenter defaultCenter] | 196 [[NSNotificationCenter defaultCenter] |
| 200 postNotificationName:NSWindowWillCloseNotification | 197 postNotificationName:NSWindowWillCloseNotification |
| 201 object:chrome_window->GetNativeWindow()]; | 198 object:chrome_window->GetNativeWindow()]; |
| 202 CheckHasAppMenus(app_1_); | 199 CheckHasAppMenus(app_1_); |
| 200 |
| 201 // |fake_focus| going out of scope sends NSWindowWillResignMainNotification. |
| 203 } | 202 } |
| 204 [[NSNotificationCenter defaultCenter] | |
| 205 postNotificationName:NSWindowDidResignMainNotification | |
| 206 object:app_1_app_window->GetNativeWindow()]; | |
| 207 app_1_app_window->GetBaseWindow()->Close(); | 203 app_1_app_window->GetBaseWindow()->Close(); |
| 208 CheckNoAppMenus(); | 204 CheckNoAppMenus(); |
| 209 } | 205 } |
| 210 | 206 |
| 211 // Test to check that hosted apps have "Find" and "Paste and Match Style" menu | 207 // Test to check that hosted apps have "Find" and "Paste and Match Style" menu |
| 212 // items under the "Edit" menu. | 208 // items under the "Edit" menu. |
| 213 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, | 209 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, |
| 214 HostedAppHasAdditionalEditMenuItems) { | 210 HostedAppHasAdditionalEditMenuItems) { |
| 215 SetUpApps(HOSTED | PACKAGED_1); | 211 SetUpApps(HOSTED | PACKAGED_1); |
| 216 | 212 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 243 } | 239 } |
| 244 | 240 |
| 245 // Test that uninstalling an app restores the main menu. | 241 // Test that uninstalling an app restores the main menu. |
| 246 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, | 242 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, |
| 247 ExtensionUninstallUpdatesMenuBar) { | 243 ExtensionUninstallUpdatesMenuBar) { |
| 248 SetUpApps(PACKAGED_1 | PACKAGED_2); | 244 SetUpApps(PACKAGED_1 | PACKAGED_2); |
| 249 | 245 |
| 250 FirstWindowForApp(app_2_)->GetBaseWindow()->Close(); | 246 FirstWindowForApp(app_2_)->GetBaseWindow()->Close(); |
| 251 chrome::BrowserIterator()->window()->Close(); | 247 chrome::BrowserIterator()->window()->Close(); |
| 252 NSWindow* app_1_window = FirstWindowForApp(app_1_)->GetNativeWindow(); | 248 NSWindow* app_1_window = FirstWindowForApp(app_1_)->GetNativeWindow(); |
| 253 [[NSNotificationCenter defaultCenter] | 249 |
| 254 postNotificationName:NSWindowDidBecomeMainNotification | 250 ui::test::ScopedFakeNSWindowFocus fake_focus; |
| 255 object:app_1_window]; | 251 [app_1_window makeMainWindow]; |
| 256 ScopedFakeNSWindowMainStatus app_1_is_main(app_1_window); | |
| 257 | 252 |
| 258 CheckHasAppMenus(app_1_); | 253 CheckHasAppMenus(app_1_); |
| 259 ExtensionService::UninstallExtensionHelper( | 254 ExtensionService::UninstallExtensionHelper( |
| 260 extension_service(), | 255 extension_service(), |
| 261 app_1_->id(), | 256 app_1_->id(), |
| 262 extensions::UNINSTALL_REASON_FOR_TESTING); | 257 extensions::UNINSTALL_REASON_FOR_TESTING); |
| 263 | 258 |
| 264 // OSX will send NSWindowWillResignMainNotification when a main window is | 259 // OSX will send NSWindowWillResignMainNotification when a main window is |
| 265 // closed. | 260 // closed. |
| 266 [[NSNotificationCenter defaultCenter] | 261 [[NSNotificationCenter defaultCenter] |
| 267 postNotificationName:NSWindowDidResignMainNotification | 262 postNotificationName:NSWindowDidResignMainNotification |
| 268 object:app_1_window]; | 263 object:app_1_window]; |
| 269 CheckNoAppMenus(); | 264 CheckNoAppMenus(); |
| 270 } | 265 } |
| 271 | 266 |
| 272 } // namespace | 267 } // namespace |
| OLD | NEW |