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