Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac_browsertest.mm

Issue 864153003: Added 'Find'/'Paste and Match Style' menu items for hosted apps on Mac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added test Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_(NULL),
31 app_2_(NULL), 34 app_2_(NULL),
35 hosted_app_(NULL),
tapted 2015/01/30 00:52:48 nit: NULL -> nullptr. You can change those nearby
mitchellj 2015/01/30 02:53:38 Done.
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
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 kEditMenuIndex = initial_menu_item_count_ + 2;
tapted 2015/01/30 00:52:49 nit: for function-local consts, we're meant to use
mitchellj 2015/01/30 02:53:39 Done.
90
91 NSMenuItem* editMenu =
tapted 2015/01/30 00:52:48 editMenu -> edit_menu, more below (camelCase only
mitchellj 2015/01/30 02:53:39 Done.
92 [[[NSApp mainMenu] itemArray] objectAtIndex:kEditMenuIndex];
93 NSMenu* editSubmenu = [editMenu submenu];
94 NSMenuItem* pasteAndMatchStyleMenuItem =
95 [editSubmenu itemWithTag:IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE];
96 NSMenuItem* findMenuItem = [editSubmenu itemWithTag:IDC_FIND_MENU];
97 if (app->is_hosted_app()) {
98 EXPECT_FALSE([pasteAndMatchStyleMenuItem isHidden]);
99 EXPECT_FALSE([findMenuItem isHidden]);
100 } else {
101 EXPECT_TRUE([pasteAndMatchStyleMenuItem isHidden]);
102 EXPECT_TRUE([findMenuItem isHidden]);
103 }
104 }
105
77 const extensions::Extension* app_1_; 106 const extensions::Extension* app_1_;
78 const extensions::Extension* app_2_; 107 const extensions::Extension* app_2_;
108 const extensions::Extension* hosted_app_;
79 NSUInteger initial_menu_item_count_; 109 NSUInteger initial_menu_item_count_;
80 110
81 private: 111 private:
82 DISALLOW_COPY_AND_ASSIGN(AppShimMenuControllerBrowserTest); 112 DISALLOW_COPY_AND_ASSIGN(AppShimMenuControllerBrowserTest);
83 }; 113 };
84 114
85 // Test that focusing an app window changes the menu bar. 115 // Test that focusing an app window changes the menu bar.
86 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, 116 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest,
87 PlatformAppFocusUpdatesMenuBar) { 117 PlatformAppFocusUpdatesMenuBar) {
88 SetUpApps(); 118 SetUpApps();
(...skipping 27 matching lines...) Expand all
116 // When an app window is closed and there are no other app windows, the menu 146 // When an app window is closed and there are no other app windows, the menu
117 // items for the app should be removed. 147 // items for the app should be removed.
118 app_1_app_window->GetBaseWindow()->Close(); 148 app_1_app_window->GetBaseWindow()->Close();
119 chrome_window->Close(); 149 chrome_window->Close();
120 [[NSNotificationCenter defaultCenter] 150 [[NSNotificationCenter defaultCenter]
121 postNotificationName:NSWindowWillCloseNotification 151 postNotificationName:NSWindowWillCloseNotification
122 object:app_2_app_window->GetNativeWindow()]; 152 object:app_2_app_window->GetNativeWindow()];
123 CheckNoAppMenus(); 153 CheckNoAppMenus();
124 } 154 }
125 155
156 // Test to check that hosted apps have "Find" and "Paste and Match Style" menu
157 // items under the "Edit" menu.
158 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest,
159 HostedAppHasAdditionalEditMenuItems) {
160 SetUpApps();
161
162 // Find the first hosted app window.
163 Browser* browser;
tapted 2015/01/30 00:52:48 = nullptr; And perhaps a better name, like hosted_
mitchellj 2015/01/30 02:53:38 Done.
164 BrowserList* browsers =
165 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE);
166 for (Browser* b : *browsers) {
tapted 2015/01/30 00:52:48 `b` isn't really a common abbreviation. Maybe for
mitchellj 2015/01/30 02:53:38 Done.
167 const extensions::Extension* extension =
168 apps::ExtensionAppShimHandler::GetAppForBrowser(b);
169 if (extension && extension->is_hosted_app()) {
170 browser = b;
171 break;
172 }
173 }
174 EXPECT_TRUE(browser);
175
176 // Focus the hosted app.
177 [[NSNotificationCenter defaultCenter]
178 postNotificationName:NSWindowDidBecomeMainNotification
179 object:browser->window()->GetNativeWindow()];
180 CheckEditMenu(hosted_app_);
181
182 // Now focus a platform app, the Edit menu should not have the additional
183 // options.
184 extensions::AppWindow* app_1_app_window =
185 extensions::AppWindowRegistry::Get(profile())
186 ->GetAppWindowsForApp(app_1_->id()).front();
tapted 2015/01/30 00:52:48 Calling .front() is a bit spooky... and this is re
mitchellj 2015/01/30 02:53:39 Done.
187 [[NSNotificationCenter defaultCenter]
188 postNotificationName:NSWindowDidBecomeMainNotification
189 object:app_1_app_window->GetNativeWindow()];
190 CheckEditMenu(app_1_);
191 }
192
126 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest, 193 IN_PROC_BROWSER_TEST_F(AppShimMenuControllerBrowserTest,
127 ExtensionUninstallUpdatesMenuBar) { 194 ExtensionUninstallUpdatesMenuBar) {
128 SetUpApps(); 195 SetUpApps();
129 196
130 // This essentially tests that a NSWindowWillCloseNotification gets fired when 197 // This essentially tests that a NSWindowWillCloseNotification gets fired when
131 // an app is uninstalled. We need to close the other windows first since the 198 // 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 199 // menu only changes on a NSWindowWillCloseNotification if there are no other
133 // windows. 200 // windows.
134 extensions::AppWindow* app_2_app_window = 201 extensions::AppWindow* app_2_app_window =
135 extensions::AppWindowRegistry::Get(profile()) 202 extensions::AppWindowRegistry::Get(profile())
(...skipping 11 matching lines...) Expand all
147 214
148 CheckHasAppMenus(app_1_); 215 CheckHasAppMenus(app_1_);
149 ExtensionService::UninstallExtensionHelper( 216 ExtensionService::UninstallExtensionHelper(
150 extension_service(), 217 extension_service(),
151 app_1_->id(), 218 app_1_->id(),
152 extensions::UNINSTALL_REASON_FOR_TESTING); 219 extensions::UNINSTALL_REASON_FOR_TESTING);
153 CheckNoAppMenus(); 220 CheckNoAppMenus();
154 } 221 }
155 222
156 } // namespace 223 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698