[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 1 | // Copyright (c) 2012 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 <Cocoa/Cocoa.h> |
| 6 | |
| 7 | #include "base/command_line.h" |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 8 | #include "base/mac/scoped_nsobject.h" |
[email protected] | ee9ccfd4 | 2013-07-23 02:31:47 | [diff] [blame^] | 9 | #include "base/strings/sys_string_conversions.h" |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 10 | #include "chrome/app/chrome_command_ids.h" |
| 11 | #import "chrome/browser/app_controller_mac.h" |
[email protected] | ee9ccfd4 | 2013-07-23 02:31:47 | [diff] [blame^] | 12 | #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 13 | #include "chrome/browser/extensions/platform_app_browsertest_util.h" |
| 14 | #include "chrome/browser/extensions/shell_window_registry.h" |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser.h" |
| 16 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | ee9ccfd4 | 2013-07-23 02:31:47 | [diff] [blame^] | 17 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 18 | #include "chrome/browser/ui/host_desktop.h" |
[email protected] | 617ee96 | 2013-01-29 20:49:12 | [diff] [blame] | 19 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 20 | #import "chrome/common/chrome_switches.h" |
| 21 | #include "chrome/test/base/in_process_browser_test.h" |
| 22 | #include "chrome/test/base/ui_test_utils.h" |
| 23 | #include "content/public/browser/web_contents.h" |
| 24 | |
| 25 | namespace { |
| 26 | |
[email protected] | ee9ccfd4 | 2013-07-23 02:31:47 | [diff] [blame^] | 27 | class AppControllerPlatformAppBrowserTest |
| 28 | : public extensions::PlatformAppBrowserTest { |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 29 | protected: |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 30 | AppControllerPlatformAppBrowserTest() |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 31 | : active_browser_list_(BrowserList::GetInstance( |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 32 | chrome::GetActiveDesktop())) { |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 33 | } |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 34 | |
| 35 | virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
[email protected] | ee9ccfd4 | 2013-07-23 02:31:47 | [diff] [blame^] | 36 | PlatformAppBrowserTest::SetUpCommandLine(command_line); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 37 | command_line->AppendSwitchASCII(switches::kAppId, |
| 38 | "1234"); |
| 39 | } |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 40 | |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 41 | const BrowserList* active_browser_list_; |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | // Test that if only a platform app window is open and no browser windows are |
| 45 | // open then a reopen event does nothing. |
| 46 | IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest, |
| 47 | PlatformAppReopenWithWindows) { |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 48 | base::scoped_nsobject<AppController> ac([[AppController alloc] init]); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 49 | NSUInteger old_window_count = [[NSApp windows] count]; |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 50 | EXPECT_EQ(1u, active_browser_list_->size()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 51 | BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:YES]; |
| 52 | |
| 53 | EXPECT_TRUE(result); |
| 54 | EXPECT_EQ(old_window_count, [[NSApp windows] count]); |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 55 | EXPECT_EQ(1u, active_browser_list_->size()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 56 | } |
| 57 | |
[email protected] | ee9ccfd4 | 2013-07-23 02:31:47 | [diff] [blame^] | 58 | // Test that focusing an app window changes the menu bar. |
| 59 | IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest, |
| 60 | PlatformAppFocusUpdatesMenuBar) { |
| 61 | base::scoped_nsobject<AppController> app_controller( |
| 62 | [[AppController alloc] init]); |
| 63 | |
| 64 | // Start two apps and wait for them to be launched. |
| 65 | ExtensionTestMessageListener listener_1("Launched", false); |
| 66 | const extensions::Extension* app_1 = |
| 67 | InstallAndLaunchPlatformApp("minimal_id"); |
| 68 | ASSERT_TRUE(listener_1.WaitUntilSatisfied()); |
| 69 | ExtensionTestMessageListener listener_2("Launched", false); |
| 70 | const extensions::Extension* app_2 = |
| 71 | InstallAndLaunchPlatformApp("minimal"); |
| 72 | ASSERT_TRUE(listener_2.WaitUntilSatisfied()); |
| 73 | |
| 74 | NSMenu* main_menu = [NSApp mainMenu]; |
| 75 | NSUInteger initial_menu_item_count = [[main_menu itemArray] count]; |
| 76 | |
| 77 | // When an app is focused, all Chrome menu items should be hidden, and a menu |
| 78 | // item for the app should be added. |
| 79 | apps::ShellWindow* app_1_shell_window = |
| 80 | extensions::ShellWindowRegistry::Get(profile())-> |
| 81 | GetShellWindowsForApp(app_1->id()).front(); |
| 82 | [[NSNotificationCenter defaultCenter] |
| 83 | postNotificationName:NSWindowDidBecomeMainNotification |
| 84 | object:app_1_shell_window->GetNativeWindow()]; |
| 85 | NSArray* item_array = [main_menu itemArray]; |
| 86 | EXPECT_EQ(initial_menu_item_count + 1, [item_array count]); |
| 87 | for (NSUInteger i = 0; i < initial_menu_item_count; ++i) |
| 88 | EXPECT_TRUE([[item_array objectAtIndex:i] isHidden]); |
| 89 | NSMenuItem* last_item = [item_array lastObject]; |
| 90 | EXPECT_EQ(app_1->id(), base::SysNSStringToUTF8([last_item title])); |
| 91 | EXPECT_EQ(app_1->name(), |
| 92 | base::SysNSStringToUTF8([[last_item submenu] title])); |
| 93 | EXPECT_FALSE([last_item isHidden]); |
| 94 | |
| 95 | // When another app is focused, the menu item for the app should change. |
| 96 | apps::ShellWindow* app_2_shell_window = |
| 97 | extensions::ShellWindowRegistry::Get(profile())-> |
| 98 | GetShellWindowsForApp(app_2->id()).front(); |
| 99 | [[NSNotificationCenter defaultCenter] |
| 100 | postNotificationName:NSWindowDidBecomeMainNotification |
| 101 | object:app_2_shell_window->GetNativeWindow()]; |
| 102 | item_array = [main_menu itemArray]; |
| 103 | EXPECT_EQ(initial_menu_item_count + 1, [item_array count]); |
| 104 | for (NSUInteger i = 0; i < initial_menu_item_count; ++i) |
| 105 | EXPECT_TRUE([[item_array objectAtIndex:i] isHidden]); |
| 106 | last_item = [item_array lastObject]; |
| 107 | EXPECT_EQ(app_2->id(), base::SysNSStringToUTF8([last_item title])); |
| 108 | EXPECT_EQ(app_2->name(), |
| 109 | base::SysNSStringToUTF8([[last_item submenu] title])); |
| 110 | EXPECT_FALSE([last_item isHidden]); |
| 111 | |
| 112 | // When Chrome is focused, the menu item for the app should be removed. |
| 113 | NSWindow* browser_native_window = |
| 114 | active_browser_list_->get(0)->window()->GetNativeWindow(); |
| 115 | [[NSNotificationCenter defaultCenter] |
| 116 | postNotificationName:NSWindowDidBecomeMainNotification |
| 117 | object:browser_native_window]; |
| 118 | item_array = [main_menu itemArray]; |
| 119 | EXPECT_EQ(initial_menu_item_count, [item_array count]); |
| 120 | for (NSUInteger i = 0; i < initial_menu_item_count; ++i) |
| 121 | EXPECT_FALSE([[item_array objectAtIndex:i] isHidden]); |
| 122 | } |
| 123 | |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 124 | class AppControllerWebAppBrowserTest : public InProcessBrowserTest { |
| 125 | protected: |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 126 | AppControllerWebAppBrowserTest() |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 127 | : active_browser_list_(BrowserList::GetInstance( |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 128 | chrome::GetActiveDesktop())) { |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 129 | } |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 130 | |
| 131 | virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
[email protected] | 90ca4427 | 2012-07-18 18:15:48 | [diff] [blame] | 132 | command_line->AppendSwitchASCII(switches::kApp, GetAppURL()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | std::string GetAppURL() const { |
| 136 | return "http://example.com/"; |
| 137 | } |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 138 | |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 139 | const BrowserList* active_browser_list_; |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | // Test that in web app mode a reopen event opens the app URL. |
| 143 | IN_PROC_BROWSER_TEST_F(AppControllerWebAppBrowserTest, |
| 144 | WebAppReopenWithNoWindows) { |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 145 | base::scoped_nsobject<AppController> ac([[AppController alloc] init]); |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 146 | EXPECT_EQ(1u, active_browser_list_->size()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 147 | BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO]; |
| 148 | |
| 149 | EXPECT_FALSE(result); |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 150 | EXPECT_EQ(2u, active_browser_list_->size()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 151 | |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 152 | Browser* browser = active_browser_list_->get(0); |
[email protected] | 617ee96 | 2013-01-29 20:49:12 | [diff] [blame] | 153 | GURL current_url = |
| 154 | browser->tab_strip_model()->GetActiveWebContents()->GetURL(); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 155 | EXPECT_EQ(GetAppURL(), current_url.spec()); |
| 156 | } |
| 157 | |
| 158 | } // namespace |