1 // Copyright 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.
5 // Tests behavior when quitting apps with app shims.
7 #import <Cocoa/Cocoa.h>
10 #include "apps/switches.h"
11 #include "chrome/browser/apps/app_browsertest_util.h"
12 #include "chrome/browser/apps/app_shim/app_shim_host_manager_mac.h"
13 #include "chrome/browser/apps/app_shim/extension_app_shim_handler_mac.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/browser_shutdown.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/test/base/interactive_test_utils.h"
19 #include "content/public/test/test_utils.h"
20 #include "extensions/browser/extension_registry.h"
21 #include "extensions/test/extension_test_message_listener.h"
22 #include "ui/events/test/cocoa_test_event_utils.h"
24 using extensions::PlatformAppBrowserTest;
30 class FakeHost : public apps::AppShimHandler::Host {
32 FakeHost(const base::FilePath& profile_path,
33 const std::string& app_id,
34 ExtensionAppShimHandler* handler)
35 : profile_path_(profile_path),
39 void OnAppLaunchComplete(AppShimLaunchResult result) override {}
40 void OnAppClosed() override { handler_->OnShimClose(this); }
41 void OnAppHide() override {}
42 void OnAppUnhideWithoutActivation() override {}
43 void OnAppRequestUserAttention(AppShimAttentionType type) override {}
44 base::FilePath GetProfilePath() const override { return profile_path_; }
45 std::string GetAppId() const override { return app_id_; }
48 base::FilePath profile_path_;
50 ExtensionAppShimHandler* handler_;
52 DISALLOW_COPY_AND_ASSIGN(FakeHost);
55 // Starts an app without a browser window using --load_and_launch_app and
57 class AppShimQuitTest : public PlatformAppBrowserTest {
62 ASSERT_EQ(0u, [[NSApp windows] count]);
63 ExtensionTestMessageListener launched_listener("Launched", false);
64 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
65 ASSERT_EQ(1u, [[NSApp windows] count]);
67 handler_ = g_browser_process->platform_part()->app_shim_host_manager()->
68 extension_app_shim_handler();
70 // Attach a host for the app.
71 extensions::ExtensionRegistry* registry =
72 extensions::ExtensionRegistry::Get(profile());
74 GetExtensionByPath(registry->enabled_extensions(), app_path_)->id();
75 host_.reset(new FakeHost(profile()->GetPath().BaseName(),
78 handler_->OnShimLaunch(host_.get(),
79 APP_SHIM_LAUNCH_REGISTER_ONLY,
80 std::vector<base::FilePath>());
81 EXPECT_EQ(host_.get(), handler_->FindHost(profile(), extension_id_));
83 // Focus the app window.
84 NSWindow* window = [[NSApp windows] objectAtIndex:0];
85 EXPECT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
86 content::RunAllPendingInMessageLoop();
89 void SetUpCommandLine(base::CommandLine* command_line) override {
90 PlatformAppBrowserTest::SetUpCommandLine(command_line);
91 // Simulate an app shim initiated launch, i.e. launch app but not browser.
92 app_path_ = test_data_dir_
93 .AppendASCII("platform_apps")
94 .AppendASCII("minimal");
95 command_line->AppendSwitchNative(apps::kLoadAndLaunchApp,
97 command_line->AppendSwitch(switches::kSilentLaunch);
100 base::FilePath app_path_;
101 ExtensionAppShimHandler* handler_;
102 std::string extension_id_;
103 scoped_ptr<FakeHost> host_;
105 DISALLOW_COPY_AND_ASSIGN(AppShimQuitTest);
110 // Test that closing an app with Cmd+Q when no browsers have ever been open
111 // terminates Chrome.
112 IN_PROC_BROWSER_TEST_F(AppShimQuitTest, QuitWithKeyEvent) {
115 // Simulate a Cmd+Q event.
116 NSWindow* window = [[NSApp windows] objectAtIndex:0];
117 NSEvent* event = cocoa_test_event_utils::KeyEventWithKeyCode(
118 0, 'q', NSKeyDown, NSCommandKeyMask);
119 [window postEvent:event
122 // This will time out if the event above does not terminate Chrome.
123 content::RunMessageLoop();
125 EXPECT_FALSE(handler_->FindHost(profile(), extension_id_));
126 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());