blob: 567d1833aece974a8b081fc92e5dc3979448286c [file] [log] [blame]
[email protected]fc44f242012-02-14 16:54:391// 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]a8522032013-06-24 22:51:468#include "base/mac/scoped_nsobject.h"
[email protected]ee9ccfd42013-07-23 02:31:479#include "base/strings/sys_string_conversions.h"
[email protected]fc44f242012-02-14 16:54:3910#include "chrome/app/chrome_command_ids.h"
11#import "chrome/browser/app_controller_mac.h"
[email protected]ee9ccfd42013-07-23 02:31:4712#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]52877dbc62012-06-29 22:22:0315#include "chrome/browser/ui/browser.h"
16#include "chrome/browser/ui/browser_list.h"
[email protected]ee9ccfd42013-07-23 02:31:4717#include "chrome/browser/ui/browser_window.h"
[email protected]b4207c42013-02-12 06:44:2018#include "chrome/browser/ui/host_desktop.h"
[email protected]617ee962013-01-29 20:49:1219#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]fc44f242012-02-14 16:54:3920#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
25namespace {
26
[email protected]ee9ccfd42013-07-23 02:31:4727class AppControllerPlatformAppBrowserTest
28 : public extensions::PlatformAppBrowserTest {
[email protected]fc44f242012-02-14 16:54:3929 protected:
[email protected]b4207c42013-02-12 06:44:2030 AppControllerPlatformAppBrowserTest()
[email protected]7d1a810b2013-06-26 19:51:5931 : active_browser_list_(BrowserList::GetInstance(
[email protected]ed2fa722013-06-25 20:37:3432 chrome::GetActiveDesktop())) {
[email protected]b4207c42013-02-12 06:44:2033 }
[email protected]fc44f242012-02-14 16:54:3934
35 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
[email protected]ee9ccfd42013-07-23 02:31:4736 PlatformAppBrowserTest::SetUpCommandLine(command_line);
[email protected]fc44f242012-02-14 16:54:3937 command_line->AppendSwitchASCII(switches::kAppId,
38 "1234");
39 }
[email protected]b4207c42013-02-12 06:44:2040
[email protected]7d1a810b2013-06-26 19:51:5941 const BrowserList* active_browser_list_;
[email protected]fc44f242012-02-14 16:54:3942};
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.
46IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest,
47 PlatformAppReopenWithWindows) {
[email protected]a8522032013-06-24 22:51:4648 base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
[email protected]fc44f242012-02-14 16:54:3949 NSUInteger old_window_count = [[NSApp windows] count];
[email protected]7d1a810b2013-06-26 19:51:5950 EXPECT_EQ(1u, active_browser_list_->size());
[email protected]fc44f242012-02-14 16:54:3951 BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:YES];
52
53 EXPECT_TRUE(result);
54 EXPECT_EQ(old_window_count, [[NSApp windows] count]);
[email protected]7d1a810b2013-06-26 19:51:5955 EXPECT_EQ(1u, active_browser_list_->size());
[email protected]fc44f242012-02-14 16:54:3956}
57
[email protected]ee9ccfd42013-07-23 02:31:4758// Test that focusing an app window changes the menu bar.
59IN_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]fc44f242012-02-14 16:54:39124class AppControllerWebAppBrowserTest : public InProcessBrowserTest {
125 protected:
[email protected]b4207c42013-02-12 06:44:20126 AppControllerWebAppBrowserTest()
[email protected]7d1a810b2013-06-26 19:51:59127 : active_browser_list_(BrowserList::GetInstance(
[email protected]ed2fa722013-06-25 20:37:34128 chrome::GetActiveDesktop())) {
[email protected]b4207c42013-02-12 06:44:20129 }
[email protected]fc44f242012-02-14 16:54:39130
131 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
[email protected]90ca44272012-07-18 18:15:48132 command_line->AppendSwitchASCII(switches::kApp, GetAppURL());
[email protected]fc44f242012-02-14 16:54:39133 }
134
135 std::string GetAppURL() const {
136 return "http://example.com/";
137 }
[email protected]b4207c42013-02-12 06:44:20138
[email protected]7d1a810b2013-06-26 19:51:59139 const BrowserList* active_browser_list_;
[email protected]fc44f242012-02-14 16:54:39140};
141
142// Test that in web app mode a reopen event opens the app URL.
143IN_PROC_BROWSER_TEST_F(AppControllerWebAppBrowserTest,
144 WebAppReopenWithNoWindows) {
[email protected]a8522032013-06-24 22:51:46145 base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
[email protected]7d1a810b2013-06-26 19:51:59146 EXPECT_EQ(1u, active_browser_list_->size());
[email protected]fc44f242012-02-14 16:54:39147 BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
148
149 EXPECT_FALSE(result);
[email protected]7d1a810b2013-06-26 19:51:59150 EXPECT_EQ(2u, active_browser_list_->size());
[email protected]fc44f242012-02-14 16:54:39151
[email protected]7d1a810b2013-06-26 19:51:59152 Browser* browser = active_browser_list_->get(0);
[email protected]617ee962013-01-29 20:49:12153 GURL current_url =
154 browser->tab_strip_model()->GetActiveWebContents()->GetURL();
[email protected]fc44f242012-02-14 16:54:39155 EXPECT_EQ(GetAppURL(), current_url.spec());
156}
157
158} // namespace