Move renderer ExtensionHelper into //extensions
[chromium-blink-merge.git] / apps / app_shim / app_shim_host_manager_browsertest_mac.mm
blobc97f544d97416344f1f4169dc4ef8f97a00b6c53
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"
7 #include <unistd.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"
25 namespace {
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 {
31  public:
32   TestShimClient();
33   virtual ~TestShimClient();
35   template <class T>
36   void Send(const T& message) {
37     channel_->Send(message);
38   }
40  private:
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) {
73   return true;
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 {
85  public:
86   AppShimHostManagerBrowserTest();
87   virtual ~AppShimHostManagerBrowserTest();
89  protected:
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_;
114 private:
115   scoped_refptr<content::MessageLoopRunner> runner_;
117   int launch_count_;
118   int quit_count_;
120   DISALLOW_COPY_AND_ASSIGN(AppShimHostManagerBrowserTest);
123 AppShimHostManagerBrowserTest::AppShimHostManagerBrowserTest()
124     : last_launch_type_(apps::APP_SHIM_LAUNCH_NUM_TYPES),
125       launch_count_(0),
126       quit_count_(0) {
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);
161   ++launch_count_;
162   last_launch_type_ = launch_type;
163   last_launch_files_ = files;
164   runner_->Quit();
167 void AppShimHostManagerBrowserTest::OnShimQuit(
168     apps::AppShimHandler::Host* host) {
169   ++quit_count_;
170   runner_->Quit();
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(),
178       kTestAppMode,
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(),
192       kTestAppMode,
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,
203                        PRE_ReCreate) {
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
210 // quit.
211 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest,
212                        ReCreate) {
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 {
221  public:
222   AppShimHostManagerBrowserTestSocketFiles() {}
224  protected:
225   base::FilePath directory_in_tmp_;
226   base::FilePath symlink_path_;
228  private:
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"),
243                                        symlink_path_));
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());
271 }  // namespace