| 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 #include "base/mac/scoped_nsobject.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "chrome/app/chrome_command_ids.h" |
| 12 #include "chrome/browser/apps/app_browsertest_util.h" | 13 #include "chrome/browser/apps/app_browsertest_util.h" |
| 14 #include "chrome/browser/apps/app_shim/extension_app_shim_handler_mac.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/extensions/launch_util.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/browser_iterator.h" | 18 #include "chrome/browser/ui/browser_iterator.h" |
| 16 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
| 17 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
| 18 #include "extensions/browser/app_window/app_window_registry.h" | 21 #include "extensions/browser/app_window/app_window_registry.h" |
| 19 #include "extensions/browser/app_window/native_app_window.h" | 22 #include "extensions/browser/app_window/native_app_window.h" |
| 20 #include "extensions/browser/uninstall_reason.h" | 23 #include "extensions/browser/uninstall_reason.h" |
| 21 #include "extensions/common/extension.h" | 24 #include "extensions/common/extension.h" |
| 22 #include "extensions/test/extension_test_message_listener.h" | 25 #include "extensions/test/extension_test_message_listener.h" |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 class AppShimMenuControllerBrowserTest | 29 class AppShimMenuControllerBrowserTest |
| 27 : public extensions::PlatformAppBrowserTest { | 30 : public extensions::PlatformAppBrowserTest { |
| 28 protected: | 31 protected: |
| 32 // The apps that can be installed and launched by SetUpApps(). |
| 33 enum AvailableApps { PACKAGED_1 = 0x1, PACKAGED_2 = 0x2, HOSTED = 0x4 }; |
| 34 |
| 29 AppShimMenuControllerBrowserTest() | 35 AppShimMenuControllerBrowserTest() |
| 30 : app_1_(NULL), | 36 : app_1_(nullptr), |
| 31 app_2_(NULL), | 37 app_2_(nullptr), |
| 38 hosted_app_(nullptr), |
| 32 initial_menu_item_count_(0) {} | 39 initial_menu_item_count_(0) {} |
| 33 | 40 |
| 34 void SetUpCommandLine(base::CommandLine* command_line) override { | 41 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 35 PlatformAppBrowserTest::SetUpCommandLine(command_line); | 42 PlatformAppBrowserTest::SetUpCommandLine(command_line); |
| 36 } | 43 } |
| 37 | 44 |
| 38 // Start two apps and wait for them to be launched. | 45 // Start testing apps and wait for them to launch. |flags| is a bitmask of |
| 39 void SetUpApps() { | 46 // AvailableApps. |
| 40 ExtensionTestMessageListener listener_1("Launched", false); | 47 void SetUpApps(int flags) { |
| 41 app_1_ = InstallAndLaunchPlatformApp("minimal_id"); | 48 |
| 42 ASSERT_TRUE(listener_1.WaitUntilSatisfied()); | 49 if (flags & PACKAGED_1) { |
| 43 ExtensionTestMessageListener listener_2("Launched", false); | 50 ExtensionTestMessageListener listener_1("Launched", false); |
| 44 app_2_ = InstallAndLaunchPlatformApp("minimal"); | 51 app_1_ = InstallAndLaunchPlatformApp("minimal_id"); |
| 45 ASSERT_TRUE(listener_2.WaitUntilSatisfied()); | 52 ASSERT_TRUE(listener_1.WaitUntilSatisfied()); |
| 53 } |
| 54 |
| 55 if (flags & PACKAGED_2) { |
| 56 ExtensionTestMessageListener listener_2("Launched", false); |
| 57 app_2_ = InstallAndLaunchPlatformApp("minimal"); |
| 58 ASSERT_TRUE(listener_2.WaitUntilSatisfied()); |
| 59 } |
| 60 |
| 61 if (flags & HOSTED) { |
| 62 hosted_app_ = InstallHostedApp(); |
| 63 |
| 64 // Explicitly set the launch type to open in a new window. |
| 65 extensions::SetLaunchType(profile(), hosted_app_->id(), |
| 66 extensions::LAUNCH_TYPE_WINDOW); |
| 67 LaunchHostedApp(hosted_app_); |
| 68 } |
| 46 | 69 |
| 47 initial_menu_item_count_ = [[[NSApp mainMenu] itemArray] count]; | 70 initial_menu_item_count_ = [[[NSApp mainMenu] itemArray] count]; |
| 48 } | 71 } |
| 49 | 72 |
| 50 void CheckHasAppMenus(const extensions::Extension* app) const { | 73 void CheckHasAppMenus(const extensions::Extension* app) const { |
| 51 const int kExtraTopLevelItems = 4; | 74 const int kExtraTopLevelItems = 4; |
| 52 NSArray* item_array = [[NSApp mainMenu] itemArray]; | 75 NSArray* item_array = [[NSApp mainMenu] itemArray]; |
| 53 EXPECT_EQ(initial_menu_item_count_ + kExtraTopLevelItems, | 76 EXPECT_EQ(initial_menu_item_count_ + kExtraTopLevelItems, |
| 54 [item_array count]); | 77 [item_array count]); |
| 55 for (NSUInteger i = 0; i < initial_menu_item_count_; ++i) | 78 for (NSUInteger i = 0; i < initial_menu_item_count_; ++i) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 67 } | 90 } |
| 68 } | 91 } |
| 69 | 92 |
| 70 void CheckNoAppMenus() const { | 93 void CheckNoAppMenus() const { |
| 71 NSArray* item_array = [[NSApp mainMenu] itemArray]; | 94 NSArray* item_array = [[NSApp mainMenu] itemArray]; |
| 72 EXPECT_EQ(initial_menu_item_count_, [item_array count]); | 95 EXPECT_EQ(initial_menu_item_count_, [item_array count]); |
| 73 for (NSUInteger i = 0; i < initial_menu_item_count_; ++i) | 96 for (NSUInteger i = 0; i < initial_menu_item_count_; ++i) |
| 74 EXPECT_FALSE([[item_array objectAtIndex:i] isHidden]); | 97 EXPECT_FALSE([[item_array objectAtIndex:i] isHidden]); |
| 75 } | 98 } |
| 76 | 99 |
| 100 void CheckEditMenu(const extensions::Extension* app) const { |
| 101 const int edit_menu_index = initial_menu_item_count_ + 2; |
| 102 |
| 103 NSMenuItem* edit_menu = |
| 104 [[[NSApp mainMenu] itemArray] objectAtIndex:edit_menu_index]; |
| 105 NSMenu* edit_submenu = [edit_menu submenu]; |
| 106 NSMenuItem* paste_match_style_menu_item = |
| 107 [edit_submenu itemWithTag:IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE]; |
| 108 NSMenuItem* find_menu_item = [edit_submenu itemWithTag:IDC_FIND_MENU]; |
| 109 if (app->is_hosted_app()) { |
| 110 EXPECT_FALSE([paste_match_style_menu_item isHidden]); |
| 111 EXPECT_FALSE([find_menu_item isHidden]); |
| 112 } else { |
| 113 EXPECT_TRUE([paste_match_style_menu_item isHidden]); |
| 114 EXPECT_TRUE([find_menu_item isHidden]); |
| 115 } |
| 116 } |
| 117 |
| 118 extensions::AppWindow* FirstWindowForApp(const extensions::Extension* app) { |
| 119 extensions::AppWindowRegistry::AppWindowList window_list = |
| 120 extensions::AppWindowRegistry::Get(profile()) |
| 121 ->GetAppWindowsForApp(app->id()); |
| 122 EXPECT_FALSE(window_list.empty()); |
| 123 return window_list.front(); |
| 124 } |
| 125 |
| 77 const extensions::Extension* app_1_; | 126 const extensions::Extension* app_1_; |
| 78 const extensions::Extension* app_2_; | 127 const extensions::Extension* app_2_; |
| 128 const extensions::Extension* hosted_app_; |
| 79 NSUInteger initial_menu_item_count_; | 129 NSUInteger initial_menu_item_count_; |
| 80 | 130 |
| 81 private: | 131 private: |
| 82 DISALLOW_COPY_AND_ASSIGN(AppShimMenuControllerBrowserTest); | 132 DISALLOW_COPY_AND_ASSIGN(AppShimMenuControllerBrowserTest); |
| 83 }; | 133 }; |
| 84 | 134 |
| 85 // Test that focusing an app window changes the menu bar. | 135 // Test that focusing an app window changes the menu bar. |
| 86 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, | 136 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, |
| 87 PlatformAppFocusUpdatesMenuBar) { | 137 PlatformAppFocusUpdatesMenuBar) { |
| 88 SetUpApps(); | 138 SetUpApps(PACKAGED_1 | PACKAGED_2); |
| 89 // When an app is focused, all Chrome menu items should be hidden, and a menu | 139 // When an app is focused, all Chrome menu items should be hidden, and a menu |
| 90 // item for the app should be added. | 140 // item for the app should be added. |
| 91 extensions::AppWindow* app_1_app_window = | 141 extensions::AppWindow* app_1_app_window = FirstWindowForApp(app_1_); |
| 92 extensions::AppWindowRegistry::Get(profile()) | |
| 93 ->GetAppWindowsForApp(app_1_->id()).front(); | |
| 94 [[NSNotificationCenter defaultCenter] | 142 [[NSNotificationCenter defaultCenter] |
| 95 postNotificationName:NSWindowDidBecomeMainNotification | 143 postNotificationName:NSWindowDidBecomeMainNotification |
| 96 object:app_1_app_window->GetNativeWindow()]; | 144 object:app_1_app_window->GetNativeWindow()]; |
| 97 CheckHasAppMenus(app_1_); | 145 CheckHasAppMenus(app_1_); |
| 98 | 146 |
| 99 // When another app is focused, the menu item for the app should change. | 147 // When another app is focused, the menu item for the app should change. |
| 100 extensions::AppWindow* app_2_app_window = | 148 extensions::AppWindow* app_2_app_window = FirstWindowForApp(app_2_); |
| 101 extensions::AppWindowRegistry::Get(profile()) | |
| 102 ->GetAppWindowsForApp(app_2_->id()).front(); | |
| 103 [[NSNotificationCenter defaultCenter] | 149 [[NSNotificationCenter defaultCenter] |
| 104 postNotificationName:NSWindowDidBecomeMainNotification | 150 postNotificationName:NSWindowDidBecomeMainNotification |
| 105 object:app_2_app_window->GetNativeWindow()]; | 151 object:app_2_app_window->GetNativeWindow()]; |
| 106 CheckHasAppMenus(app_2_); | 152 CheckHasAppMenus(app_2_); |
| 107 | 153 |
| 108 // When a browser window is focused, the menu items for the app should be | 154 // When a browser window is focused, the menu items for the app should be |
| 109 // removed. | 155 // removed. |
| 110 BrowserWindow* chrome_window = chrome::BrowserIterator()->window(); | 156 BrowserWindow* chrome_window = chrome::BrowserIterator()->window(); |
| 111 [[NSNotificationCenter defaultCenter] | 157 [[NSNotificationCenter defaultCenter] |
| 112 postNotificationName:NSWindowDidBecomeMainNotification | 158 postNotificationName:NSWindowDidBecomeMainNotification |
| 113 object:chrome_window->GetNativeWindow()]; | 159 object:chrome_window->GetNativeWindow()]; |
| 114 CheckNoAppMenus(); | 160 CheckNoAppMenus(); |
| 115 | 161 |
| 116 // When an app window is closed and there are no other app windows, the menu | 162 // When an app window is closed and there are no other app windows, the menu |
| 117 // items for the app should be removed. | 163 // items for the app should be removed. |
| 118 app_1_app_window->GetBaseWindow()->Close(); | 164 app_1_app_window->GetBaseWindow()->Close(); |
| 119 chrome_window->Close(); | 165 chrome_window->Close(); |
| 120 [[NSNotificationCenter defaultCenter] | 166 [[NSNotificationCenter defaultCenter] |
| 121 postNotificationName:NSWindowWillCloseNotification | 167 postNotificationName:NSWindowWillCloseNotification |
| 122 object:app_2_app_window->GetNativeWindow()]; | 168 object:app_2_app_window->GetNativeWindow()]; |
| 123 CheckNoAppMenus(); | 169 CheckNoAppMenus(); |
| 124 } | 170 } |
| 125 | 171 |
| 172 // Test to check that hosted apps have "Find" and "Paste and Match Style" menu |
| 173 // items under the "Edit" menu. |
| 174 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, |
| 175 HostedAppHasAdditionalEditMenuItems) { |
| 176 SetUpApps(HOSTED | PACKAGED_1); |
| 177 |
| 178 // Find the first hosted app window. |
| 179 Browser* hosted_app_browser = nullptr; |
| 180 BrowserList* browsers = |
| 181 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE); |
| 182 for (Browser* browser : *browsers) { |
| 183 const extensions::Extension* extension = |
| 184 apps::ExtensionAppShimHandler::GetAppForBrowser(browser); |
| 185 if (extension && extension->is_hosted_app()) { |
| 186 hosted_app_browser = browser; |
| 187 break; |
| 188 } |
| 189 } |
| 190 EXPECT_TRUE(hosted_app_browser); |
| 191 |
| 192 // Focus the hosted app. |
| 193 [[NSNotificationCenter defaultCenter] |
| 194 postNotificationName:NSWindowDidBecomeMainNotification |
| 195 object:hosted_app_browser->window()->GetNativeWindow()]; |
| 196 CheckEditMenu(hosted_app_); |
| 197 |
| 198 // Now focus a platform app, the Edit menu should not have the additional |
| 199 // options. |
| 200 [[NSNotificationCenter defaultCenter] |
| 201 postNotificationName:NSWindowDidBecomeMainNotification |
| 202 object:FirstWindowForApp(app_1_)->GetNativeWindow()]; |
| 203 CheckEditMenu(app_1_); |
| 204 } |
| 205 |
| 126 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, | 206 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, |
| 127 ExtensionUninstallUpdatesMenuBar) { | 207 ExtensionUninstallUpdatesMenuBar) { |
| 128 SetUpApps(); | 208 SetUpApps(PACKAGED_1 | PACKAGED_2); |
| 129 | 209 |
| 130 // This essentially tests that a NSWindowWillCloseNotification gets fired when | 210 // This essentially tests that a NSWindowWillCloseNotification gets fired when |
| 131 // an app is uninstalled. We need to close the other windows first since the | 211 // an app is uninstalled. We need to close the other windows first since the |
| 132 // menu only changes on a NSWindowWillCloseNotification if there are no other | 212 // menu only changes on a NSWindowWillCloseNotification if there are no other |
| 133 // windows. | 213 // windows. |
| 134 extensions::AppWindow* app_2_app_window = | 214 FirstWindowForApp(app_2_)->GetBaseWindow()->Close(); |
| 135 extensions::AppWindowRegistry::Get(profile()) | |
| 136 ->GetAppWindowsForApp(app_2_->id()).front(); | |
| 137 app_2_app_window->GetBaseWindow()->Close(); | |
| 138 | |
| 139 chrome::BrowserIterator()->window()->Close(); | 215 chrome::BrowserIterator()->window()->Close(); |
| 140 | |
| 141 extensions::AppWindow* app_1_app_window = | |
| 142 extensions::AppWindowRegistry::Get(profile()) | |
| 143 ->GetAppWindowsForApp(app_1_->id()).front(); | |
| 144 [[NSNotificationCenter defaultCenter] | 216 [[NSNotificationCenter defaultCenter] |
| 145 postNotificationName:NSWindowDidBecomeMainNotification | 217 postNotificationName:NSWindowDidBecomeMainNotification |
| 146 object:app_1_app_window->GetNativeWindow()]; | 218 object:FirstWindowForApp(app_1_)->GetNativeWindow()]; |
| 147 | 219 |
| 148 CheckHasAppMenus(app_1_); | 220 CheckHasAppMenus(app_1_); |
| 149 ExtensionService::UninstallExtensionHelper( | 221 ExtensionService::UninstallExtensionHelper( |
| 150 extension_service(), | 222 extension_service(), |
| 151 app_1_->id(), | 223 app_1_->id(), |
| 152 extensions::UNINSTALL_REASON_FOR_TESTING); | 224 extensions::UNINSTALL_REASON_FOR_TESTING); |
| 153 CheckNoAppMenus(); | 225 CheckNoAppMenus(); |
| 154 } | 226 } |
| 155 | 227 |
| 156 } // namespace | 228 } // namespace |
| OLD | NEW |