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.
5 #import <Cocoa/Cocoa.h>
7 #include "apps/shell_window_registry.h"
8 #include "base/command_line.h"
9 #include "base/mac/scoped_nsobject.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #import "chrome/browser/app_controller_mac.h"
12 #include "chrome/browser/apps/app_browsertest_util.h"
13 #include "chrome/browser/extensions/extension_test_message_listener.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_list.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/host_desktop.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #import "chrome/common/chrome_switches.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/ui_test_utils.h"
22 #include "content/public/browser/web_contents.h"
23 #include "extensions/common/extension.h"
27 class AppControllerPlatformAppBrowserTest
28 : public extensions::PlatformAppBrowserTest {
30 AppControllerPlatformAppBrowserTest()
31 : active_browser_list_(BrowserList::GetInstance(
32 chrome::GetActiveDesktop())) {
35 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
36 PlatformAppBrowserTest::SetUpCommandLine(command_line);
37 command_line->AppendSwitchASCII(switches::kAppId,
41 const BrowserList* active_browser_list_;
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) {
48 base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
49 NSUInteger old_window_count = [[NSApp windows] count];
50 EXPECT_EQ(1u, active_browser_list_->size());
51 [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:YES];
52 // We do not EXPECT_TRUE the result here because the method
53 // deminiaturizes windows manually rather than return YES and have
56 EXPECT_EQ(old_window_count, [[NSApp windows] count]);
57 EXPECT_EQ(1u, active_browser_list_->size());
60 IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest,
61 ActivationFocusesBrowserWindow) {
62 base::scoped_nsobject<AppController> app_controller(
63 [[AppController alloc] init]);
65 ExtensionTestMessageListener listener("Launched", false);
66 const extensions::Extension* app =
67 InstallAndLaunchPlatformApp("minimal");
68 ASSERT_TRUE(listener.WaitUntilSatisfied());
70 NSWindow* app_window = apps::ShellWindowRegistry::Get(profile())->
71 GetShellWindowsForApp(app->id()).front()->GetNativeWindow();
72 NSWindow* browser_window = browser()->window()->GetNativeWindow();
74 EXPECT_LE([[NSApp orderedWindows] indexOfObject:app_window],
75 [[NSApp orderedWindows] indexOfObject:browser_window]);
76 [app_controller applicationShouldHandleReopen:NSApp
77 hasVisibleWindows:YES];
78 EXPECT_LE([[NSApp orderedWindows] indexOfObject:browser_window],
79 [[NSApp orderedWindows] indexOfObject:app_window]);
82 class AppControllerWebAppBrowserTest : public InProcessBrowserTest {
84 AppControllerWebAppBrowserTest()
85 : active_browser_list_(BrowserList::GetInstance(
86 chrome::GetActiveDesktop())) {
89 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
90 command_line->AppendSwitchASCII(switches::kApp, GetAppURL());
93 std::string GetAppURL() const {
94 return "http://example.com/";
97 const BrowserList* active_browser_list_;
100 // Test that in web app mode a reopen event opens the app URL.
101 IN_PROC_BROWSER_TEST_F(AppControllerWebAppBrowserTest,
102 WebAppReopenWithNoWindows) {
103 base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
104 EXPECT_EQ(1u, active_browser_list_->size());
105 BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
107 EXPECT_FALSE(result);
108 EXPECT_EQ(2u, active_browser_list_->size());
110 Browser* browser = active_browser_list_->get(0);
112 browser->tab_strip_model()->GetActiveWebContents()->GetURL();
113 EXPECT_EQ(GetAppURL(), current_url.spec());