[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 | |
[email protected] | f6d9b28 | 2013-08-09 11:03:20 | [diff] [blame] | 7 | #include "apps/shell_window_registry.h" |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 8 | #include "base/command_line.h" |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 9 | #include "base/mac/scoped_nsobject.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] | bb4bec0 | 2013-08-15 11:26:58 | [diff] [blame] | 12 | #include "chrome/browser/apps/app_browsertest_util.h" |
[email protected] | ee9ccfd4 | 2013-07-23 02:31:47 | [diff] [blame] | 13 | #include "chrome/browser/extensions/extension_test_message_listener.h" |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser.h" |
| 15 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | ee9ccfd4 | 2013-07-23 02:31:47 | [diff] [blame] | 16 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 17 | #include "chrome/browser/ui/host_desktop.h" |
[email protected] | 617ee96 | 2013-01-29 20:49:12 | [diff] [blame] | 18 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 19 | #import "chrome/common/chrome_switches.h" |
[email protected] | 131cbf23 | 2013-08-09 15:35:28 | [diff] [blame] | 20 | #include "chrome/common/extensions/extension.h" |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 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] | a25920ee | 2013-09-05 19:38:49 | [diff] [blame^] | 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 |
| 54 | // AppKit do it. |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 55 | |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 56 | EXPECT_EQ(old_window_count, [[NSApp windows] count]); |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 57 | EXPECT_EQ(1u, active_browser_list_->size()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 60 | IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest, |
| 61 | ActivationFocusesBrowserWindow) { |
| 62 | base::scoped_nsobject<AppController> app_controller( |
| 63 | [[AppController alloc] init]); |
| 64 | |
| 65 | ExtensionTestMessageListener listener("Launched", false); |
| 66 | const extensions::Extension* app = |
| 67 | InstallAndLaunchPlatformApp("minimal"); |
| 68 | ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 69 | |
[email protected] | f6d9b28 | 2013-08-09 11:03:20 | [diff] [blame] | 70 | NSWindow* app_window = apps::ShellWindowRegistry::Get(profile())-> |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 71 | GetShellWindowsForApp(app->id()).front()->GetNativeWindow(); |
| 72 | NSWindow* browser_window = browser()->window()->GetNativeWindow(); |
| 73 | |
| 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]); |
| 80 | } |
| 81 | |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 82 | class AppControllerWebAppBrowserTest : public InProcessBrowserTest { |
| 83 | protected: |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 84 | AppControllerWebAppBrowserTest() |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 85 | : active_browser_list_(BrowserList::GetInstance( |
[email protected] | ed2fa72 | 2013-06-25 20:37:34 | [diff] [blame] | 86 | chrome::GetActiveDesktop())) { |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 87 | } |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 88 | |
| 89 | virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
[email protected] | 90ca4427 | 2012-07-18 18:15:48 | [diff] [blame] | 90 | command_line->AppendSwitchASCII(switches::kApp, GetAppURL()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | std::string GetAppURL() const { |
| 94 | return "http://example.com/"; |
| 95 | } |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 96 | |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 97 | const BrowserList* active_browser_list_; |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | // Test that in web app mode a reopen event opens the app URL. |
| 101 | IN_PROC_BROWSER_TEST_F(AppControllerWebAppBrowserTest, |
| 102 | WebAppReopenWithNoWindows) { |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 103 | base::scoped_nsobject<AppController> ac([[AppController alloc] init]); |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 104 | EXPECT_EQ(1u, active_browser_list_->size()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 105 | BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO]; |
| 106 | |
| 107 | EXPECT_FALSE(result); |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 108 | EXPECT_EQ(2u, active_browser_list_->size()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 109 | |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 110 | Browser* browser = active_browser_list_->get(0); |
[email protected] | 617ee96 | 2013-01-29 20:49:12 | [diff] [blame] | 111 | GURL current_url = |
| 112 | browser->tab_strip_model()->GetActiveWebContents()->GetURL(); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 113 | EXPECT_EQ(GetAppURL(), current_url.spec()); |
| 114 | } |
| 115 | |
| 116 | } // namespace |