OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/apps/app_menu_controller_mac.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 |
| 9 #include "apps/shell_window_registry.h" |
| 10 #include "base/command_line.h" |
| 11 #include "base/mac/scoped_nsobject.h" |
| 12 #include "base/strings/sys_string_conversions.h" |
| 13 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 14 #include "chrome/browser/extensions/platform_app_browsertest_util.h" |
| 15 #import "chrome/common/chrome_switches.h" |
| 16 #include "chrome/common/extensions/extension.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 class AppMenuControllerBrowserTest |
| 21 : public extensions::PlatformAppBrowserTest { |
| 22 protected: |
| 23 AppMenuControllerBrowserTest() {} |
| 24 }; |
| 25 |
| 26 // Test that focusing an app window changes the menu bar. |
| 27 IN_PROC_BROWSER_TEST_F(AppMenuControllerBrowserTest, |
| 28 PlatformAppFocusUpdatesMenuBar) { |
| 29 // Start two apps and wait for them to be launched. |
| 30 ExtensionTestMessageListener listener_1("Launched", false); |
| 31 const extensions::Extension* app_1 = |
| 32 InstallAndLaunchPlatformApp("minimal_id"); |
| 33 ASSERT_TRUE(listener_1.WaitUntilSatisfied()); |
| 34 ExtensionTestMessageListener listener_2("Launched", false); |
| 35 const extensions::Extension* app_2 = |
| 36 InstallAndLaunchPlatformApp("minimal"); |
| 37 ASSERT_TRUE(listener_2.WaitUntilSatisfied()); |
| 38 |
| 39 NSMenu* main_menu = [NSApp mainMenu]; |
| 40 NSUInteger initial_menu_item_count = [[main_menu itemArray] count]; |
| 41 |
| 42 // When an app is focused, all Chrome menu items should be hidden, and a menu |
| 43 // item for the app should be added. |
| 44 apps::ShellWindow* app_1_shell_window = |
| 45 apps::ShellWindowRegistry::Get(profile())-> |
| 46 GetShellWindowsForApp(app_1->id()).front(); |
| 47 [[NSNotificationCenter defaultCenter] |
| 48 postNotificationName:NSWindowDidBecomeMainNotification |
| 49 object:app_1_shell_window->GetNativeWindow()]; |
| 50 NSArray* item_array = [main_menu itemArray]; |
| 51 EXPECT_EQ(initial_menu_item_count + 1, [item_array count]); |
| 52 for (NSUInteger i = 0; i < initial_menu_item_count; ++i) |
| 53 EXPECT_TRUE([[item_array objectAtIndex:i] isHidden]); |
| 54 NSMenuItem* last_item = [item_array lastObject]; |
| 55 EXPECT_EQ(app_1->id(), base::SysNSStringToUTF8([last_item title])); |
| 56 EXPECT_EQ(app_1->name(), |
| 57 base::SysNSStringToUTF8([[last_item submenu] title])); |
| 58 EXPECT_FALSE([last_item isHidden]); |
| 59 |
| 60 // When another app is focused, the menu item for the app should change. |
| 61 apps::ShellWindow* app_2_shell_window = |
| 62 apps::ShellWindowRegistry::Get(profile())-> |
| 63 GetShellWindowsForApp(app_2->id()).front(); |
| 64 [[NSNotificationCenter defaultCenter] |
| 65 postNotificationName:NSWindowDidBecomeMainNotification |
| 66 object:app_2_shell_window->GetNativeWindow()]; |
| 67 item_array = [main_menu itemArray]; |
| 68 EXPECT_EQ(initial_menu_item_count + 1, [item_array count]); |
| 69 for (NSUInteger i = 0; i < initial_menu_item_count; ++i) |
| 70 EXPECT_TRUE([[item_array objectAtIndex:i] isHidden]); |
| 71 last_item = [item_array lastObject]; |
| 72 EXPECT_EQ(app_2->id(), base::SysNSStringToUTF8([last_item title])); |
| 73 EXPECT_EQ(app_2->name(), |
| 74 base::SysNSStringToUTF8([[last_item submenu] title])); |
| 75 EXPECT_FALSE([last_item isHidden]); |
| 76 |
| 77 // When the app window loses focus, the menu items for the app should be |
| 78 // removed. |
| 79 [[NSNotificationCenter defaultCenter] |
| 80 postNotificationName:NSWindowDidResignMainNotification |
| 81 object:app_2_shell_window->GetNativeWindow()]; |
| 82 item_array = [main_menu itemArray]; |
| 83 EXPECT_EQ(initial_menu_item_count, [item_array count]); |
| 84 for (NSUInteger i = 0; i < initial_menu_item_count; ++i) |
| 85 EXPECT_FALSE([[item_array objectAtIndex:i] isHidden]); |
| 86 } |
| 87 |
| 88 } // namespace |
OLD | NEW |