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 #include "apps/app_shim/app_shim_host_manager_mac.h"
9 #include "apps/app_shim/app_shim_messages.h"
10 #include "apps/app_shim/test/app_shim_host_manager_test_api_mac.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/path_service.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/mac/app_mode_common.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "content/public/test/test_utils.h"
21 #include "ipc/ipc_channel_proxy.h"
22 #include "ipc/ipc_listener.h"
23 #include "ipc/ipc_message.h"
27 const char kTestAppMode[] = "test_app";
29 // A test version of the AppShimController IPC client in chrome_main_app_mode.
30 class TestShimClient : public IPC::Listener {
33 virtual ~TestShimClient();
36 void Send(const T& message) {
37 channel_->Send(message);
41 // IPC::Listener overrides:
42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
43 virtual void OnChannelError() OVERRIDE;
45 base::Thread io_thread_;
46 scoped_ptr<IPC::ChannelProxy> channel_;
48 DISALLOW_COPY_AND_ASSIGN(TestShimClient);
51 TestShimClient::TestShimClient() : io_thread_("TestShimClientIO") {
52 base::Thread::Options io_thread_options;
53 io_thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
54 io_thread_.StartWithOptions(io_thread_options);
56 base::FilePath user_data_dir;
57 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
58 base::FilePath symlink_path =
59 user_data_dir.Append(app_mode::kAppShimSocketSymlinkName);
61 base::FilePath socket_path;
62 CHECK(base::ReadSymbolicLink(symlink_path, &socket_path));
63 app_mode::VerifySocketPermissions(socket_path);
65 IPC::ChannelHandle handle(socket_path.value());
66 channel_.reset(new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT,
67 this, io_thread_.message_loop_proxy().get()));
70 TestShimClient::~TestShimClient() {}
72 bool TestShimClient::OnMessageReceived(const IPC::Message& message) {
76 void TestShimClient::OnChannelError() {
77 // Client should not get any channel errors for the current set of tests.
78 PLOG(FATAL) << "ChannelError";
81 // Browser Test for AppShimHostManager to test IPC interactions across the
82 // UNIX domain socket.
83 class AppShimHostManagerBrowserTest : public InProcessBrowserTest,
84 public apps::AppShimHandler {
86 AppShimHostManagerBrowserTest();
87 virtual ~AppShimHostManagerBrowserTest();
90 // Wait for OnShimLaunch, then send a quit, and wait for the response. Used to
91 // test launch behavior.
92 void RunAndExitGracefully();
94 // InProcessBrowserTest overrides:
95 virtual void SetUpOnMainThread() OVERRIDE;
96 virtual void TearDownOnMainThread() OVERRIDE;
98 // AppShimHandler overrides:
99 virtual void OnShimLaunch(apps::AppShimHandler::Host* host,
100 apps::AppShimLaunchType launch_type,
101 const std::vector<base::FilePath>& files) OVERRIDE;
102 virtual void OnShimClose(apps::AppShimHandler::Host* host) OVERRIDE {}
103 virtual void OnShimFocus(apps::AppShimHandler::Host* host,
104 apps::AppShimFocusType focus_type,
105 const std::vector<base::FilePath>& files) OVERRIDE {}
106 virtual void OnShimSetHidden(apps::AppShimHandler::Host* host,
107 bool hidden) OVERRIDE {}
108 virtual void OnShimQuit(apps::AppShimHandler::Host* host) OVERRIDE;
110 scoped_ptr<TestShimClient> test_client_;
111 std::vector<base::FilePath> last_launch_files_;
112 apps::AppShimLaunchType last_launch_type_;
115 scoped_refptr<content::MessageLoopRunner> runner_;
120 DISALLOW_COPY_AND_ASSIGN(AppShimHostManagerBrowserTest);
123 AppShimHostManagerBrowserTest::AppShimHostManagerBrowserTest()
124 : last_launch_type_(apps::APP_SHIM_LAUNCH_NUM_TYPES),
129 AppShimHostManagerBrowserTest::~AppShimHostManagerBrowserTest() {
132 void AppShimHostManagerBrowserTest::RunAndExitGracefully() {
133 runner_ = new content::MessageLoopRunner();
134 EXPECT_EQ(0, launch_count_);
135 runner_->Run(); // Will stop in OnShimLaunch().
136 EXPECT_EQ(1, launch_count_);
138 runner_ = new content::MessageLoopRunner();
139 test_client_->Send(new AppShimHostMsg_QuitApp);
140 EXPECT_EQ(0, quit_count_);
141 runner_->Run(); // Will stop in OnShimQuit().
142 EXPECT_EQ(1, quit_count_);
144 test_client_.reset();
147 void AppShimHostManagerBrowserTest::SetUpOnMainThread() {
148 // Can't do this in the constructor, it needs a BrowserProcess.
149 apps::AppShimHandler::RegisterHandler(kTestAppMode, this);
152 void AppShimHostManagerBrowserTest::TearDownOnMainThread() {
153 apps::AppShimHandler::RemoveHandler(kTestAppMode);
156 void AppShimHostManagerBrowserTest::OnShimLaunch(
157 apps::AppShimHandler::Host* host,
158 apps::AppShimLaunchType launch_type,
159 const std::vector<base::FilePath>& files) {
160 host->OnAppLaunchComplete(apps::APP_SHIM_LAUNCH_SUCCESS);
162 last_launch_type_ = launch_type;
163 last_launch_files_ = files;
167 void AppShimHostManagerBrowserTest::OnShimQuit(
168 apps::AppShimHandler::Host* host) {
173 // Test regular launch, which would ask Chrome to launch the app.
174 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchNormal) {
175 test_client_.reset(new TestShimClient());
176 test_client_->Send(new AppShimHostMsg_LaunchApp(
177 browser()->profile()->GetPath(),
179 apps::APP_SHIM_LAUNCH_NORMAL,
180 std::vector<base::FilePath>()));
182 RunAndExitGracefully();
183 EXPECT_EQ(apps::APP_SHIM_LAUNCH_NORMAL, last_launch_type_);
184 EXPECT_TRUE(last_launch_files_.empty());
187 // Test register-only launch, used when Chrome has already launched the app.
188 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchRegisterOnly) {
189 test_client_.reset(new TestShimClient());
190 test_client_->Send(new AppShimHostMsg_LaunchApp(
191 browser()->profile()->GetPath(),
193 apps::APP_SHIM_LAUNCH_REGISTER_ONLY,
194 std::vector<base::FilePath>()));
196 RunAndExitGracefully();
197 EXPECT_EQ(apps::APP_SHIM_LAUNCH_REGISTER_ONLY, last_launch_type_);
198 EXPECT_TRUE(last_launch_files_.empty());
201 // Ensure the domain socket can be created in a fresh user data dir.
202 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest,
204 test::AppShimHostManagerTestApi test_api(
205 g_browser_process->platform_part()->app_shim_host_manager());
206 EXPECT_TRUE(test_api.factory());
209 // Ensure the domain socket can be re-created after a prior browser process has
211 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest,
213 test::AppShimHostManagerTestApi test_api(
214 g_browser_process->platform_part()->app_shim_host_manager());
215 EXPECT_TRUE(test_api.factory());
218 // Tests for the files created by AppShimHostManager.
219 class AppShimHostManagerBrowserTestSocketFiles
220 : public AppShimHostManagerBrowserTest {
222 AppShimHostManagerBrowserTestSocketFiles() {}
225 base::FilePath directory_in_tmp_;
226 base::FilePath symlink_path_;
229 virtual bool SetUpUserDataDirectory() OVERRIDE;
230 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE;
232 DISALLOW_COPY_AND_ASSIGN(AppShimHostManagerBrowserTestSocketFiles);
235 bool AppShimHostManagerBrowserTestSocketFiles::SetUpUserDataDirectory() {
236 // Create an existing symlink. It should be replaced by AppShimHostManager.
237 base::FilePath user_data_dir;
238 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
239 symlink_path_ = user_data_dir.Append(app_mode::kAppShimSocketSymlinkName);
240 base::FilePath temp_dir;
241 PathService::Get(base::DIR_TEMP, &temp_dir);
242 EXPECT_TRUE(base::CreateSymbolicLink(temp_dir.Append("chrome-XXXXXX"),
244 return AppShimHostManagerBrowserTest::SetUpUserDataDirectory();
247 void AppShimHostManagerBrowserTestSocketFiles::
248 TearDownInProcessBrowserTestFixture() {
249 // Check that created files have been deleted.
250 EXPECT_FALSE(base::PathExists(directory_in_tmp_));
251 EXPECT_FALSE(base::PathExists(symlink_path_));
254 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTestSocketFiles,
255 ReplacesSymlinkAndCleansUpFiles) {
256 // Get the directory created by AppShimHostManager.
257 test::AppShimHostManagerTestApi test_api(
258 g_browser_process->platform_part()->app_shim_host_manager());
259 directory_in_tmp_ = test_api.directory_in_tmp();
261 // Check that socket files have been created.
262 EXPECT_TRUE(base::PathExists(directory_in_tmp_));
263 EXPECT_TRUE(base::PathExists(symlink_path_));
265 // Check that the symlink has been replaced.
266 base::FilePath socket_path;
267 ASSERT_TRUE(base::ReadSymbolicLink(symlink_path_, &socket_path));
268 EXPECT_EQ(app_mode::kAppShimSocketShortName, socket_path.BaseName().value());