OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "apps/app_shim/app_shim_host_manager_mac.h" | 5 #include "apps/app_shim/app_shim_host_manager_mac.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
9 #include "apps/app_shim/app_shim_messages.h" | 9 #include "apps/app_shim/app_shim_messages.h" |
10 #include "apps/app_shim/test/app_shim_host_manager_test_api_mac.h" | 10 #include "apps/app_shim/test/app_shim_host_manager_test_api_mac.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "ipc/ipc_listener.h" | 22 #include "ipc/ipc_listener.h" |
23 #include "ipc/ipc_message.h" | 23 #include "ipc/ipc_message.h" |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 const char kTestAppMode[] = "test_app"; | 27 const char kTestAppMode[] = "test_app"; |
28 | 28 |
29 // A test version of the AppShimController IPC client in chrome_main_app_mode. | 29 // A test version of the AppShimController IPC client in chrome_main_app_mode. |
30 class TestShimClient : public IPC::Listener { | 30 class TestShimClient : public IPC::Listener { |
31 public: | 31 public: |
32 TestShimClient(const base::FilePath& socket_path); | 32 TestShimClient(); |
33 virtual ~TestShimClient(); | 33 virtual ~TestShimClient(); |
34 | 34 |
35 template <class T> | 35 template <class T> |
36 void Send(const T& message) { | 36 void Send(const T& message) { |
37 channel_->Send(message); | 37 channel_->Send(message); |
38 } | 38 } |
39 | 39 |
40 private: | 40 private: |
41 // IPC::Listener overrides: | 41 // IPC::Listener overrides: |
42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
43 virtual void OnChannelError() OVERRIDE; | 43 virtual void OnChannelError() OVERRIDE; |
44 | 44 |
45 base::Thread io_thread_; | 45 base::Thread io_thread_; |
46 scoped_ptr<IPC::ChannelProxy> channel_; | 46 scoped_ptr<IPC::ChannelProxy> channel_; |
47 | 47 |
48 DISALLOW_COPY_AND_ASSIGN(TestShimClient); | 48 DISALLOW_COPY_AND_ASSIGN(TestShimClient); |
49 }; | 49 }; |
50 | 50 |
51 TestShimClient::TestShimClient(const base::FilePath& socket_path) | 51 TestShimClient::TestShimClient() |
52 : io_thread_("TestShimClientIO") { | 52 : io_thread_("TestShimClientIO") { |
53 base::Thread::Options io_thread_options; | 53 base::Thread::Options io_thread_options; |
54 io_thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 54 io_thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
55 io_thread_.StartWithOptions(io_thread_options); | 55 io_thread_.StartWithOptions(io_thread_options); |
56 | 56 |
| 57 base::FilePath user_data_dir; |
| 58 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)); |
| 59 base::FilePath socket_path = app_mode::GetShortSocketPath(user_data_dir); |
| 60 |
57 IPC::ChannelHandle handle(socket_path.value()); | 61 IPC::ChannelHandle handle(socket_path.value()); |
58 channel_.reset(new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, | 62 channel_.reset(new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, |
59 this, io_thread_.message_loop_proxy().get())); | 63 this, io_thread_.message_loop_proxy().get())); |
60 } | 64 } |
61 | 65 |
62 TestShimClient::~TestShimClient() {} | 66 TestShimClient::~TestShimClient() {} |
63 | 67 |
64 bool TestShimClient::OnMessageReceived(const IPC::Message& message) { | 68 bool TestShimClient::OnMessageReceived(const IPC::Message& message) { |
65 return true; | 69 return true; |
66 } | 70 } |
67 | 71 |
68 void TestShimClient::OnChannelError() { | 72 void TestShimClient::OnChannelError() { |
69 // Client should not get any channel errors for the current set of tests. | 73 // Client should not get any channel errors for the current set of tests. |
70 PLOG(FATAL) << "ChannelError"; | 74 PLOG(FATAL) << "ChannelError"; |
71 } | 75 } |
72 | 76 |
73 // Browser Test for AppShimHostManager to test IPC interactions across the | 77 // Browser Test for AppShimHostManager to test IPC interactions across the |
74 // UNIX domain socket. | 78 // UNIX domain socket. |
75 class AppShimHostManagerBrowserTest : public InProcessBrowserTest, | 79 class AppShimHostManagerBrowserTest : public InProcessBrowserTest, |
76 public apps::AppShimHandler { | 80 public apps::AppShimHandler { |
77 public: | 81 public: |
78 AppShimHostManagerBrowserTest(); | 82 AppShimHostManagerBrowserTest(); |
79 virtual ~AppShimHostManagerBrowserTest(); | 83 virtual ~AppShimHostManagerBrowserTest(); |
80 | 84 |
81 protected: | 85 protected: |
82 // Wait for OnShimLaunch, then send a quit, and wait for the response. Used to | 86 // Wait for OnShimLaunch, then send a quit, and wait for the response. Used to |
83 // test launch behavior. | 87 // test launch behavior. |
84 void RunAndExitGracefully(); | 88 void RunAndExitGracefully(); |
85 | 89 |
86 // InProcessBrowserTest overrides: | |
87 virtual bool SetUpUserDataDirectory() OVERRIDE; | |
88 | |
89 // AppShimHandler overrides: | 90 // AppShimHandler overrides: |
90 virtual void OnShimLaunch(apps::AppShimHandler::Host* host, | 91 virtual void OnShimLaunch(apps::AppShimHandler::Host* host, |
91 apps::AppShimLaunchType launch_type, | 92 apps::AppShimLaunchType launch_type, |
92 const std::vector<base::FilePath>& files) OVERRIDE; | 93 const std::vector<base::FilePath>& files) OVERRIDE; |
93 virtual void OnShimClose(apps::AppShimHandler::Host* host) OVERRIDE {} | 94 virtual void OnShimClose(apps::AppShimHandler::Host* host) OVERRIDE {} |
94 virtual void OnShimFocus(apps::AppShimHandler::Host* host, | 95 virtual void OnShimFocus(apps::AppShimHandler::Host* host, |
95 apps::AppShimFocusType focus_type, | 96 apps::AppShimFocusType focus_type, |
96 const std::vector<base::FilePath>& files) OVERRIDE {} | 97 const std::vector<base::FilePath>& files) OVERRIDE {} |
97 virtual void OnShimSetHidden(apps::AppShimHandler::Host* host, | 98 virtual void OnShimSetHidden(apps::AppShimHandler::Host* host, |
98 bool hidden) OVERRIDE {} | 99 bool hidden) OVERRIDE {} |
99 virtual void OnShimQuit(apps::AppShimHandler::Host* host) OVERRIDE; | 100 virtual void OnShimQuit(apps::AppShimHandler::Host* host) OVERRIDE; |
100 | 101 |
101 scoped_ptr<TestShimClient> test_client_; | 102 scoped_ptr<TestShimClient> test_client_; |
102 base::FilePath short_socket_path_; | |
103 std::vector<base::FilePath> last_launch_files_; | 103 std::vector<base::FilePath> last_launch_files_; |
104 apps::AppShimLaunchType last_launch_type_; | 104 apps::AppShimLaunchType last_launch_type_; |
105 | 105 |
106 private: | 106 private: |
107 scoped_refptr<content::MessageLoopRunner> runner_; | 107 scoped_refptr<content::MessageLoopRunner> runner_; |
108 base::ScopedTempDir short_temp_dir_; | 108 base::ScopedTempDir short_temp_dir_; |
109 | 109 |
110 int launch_count_; | 110 int launch_count_; |
111 int quit_count_; | 111 int quit_count_; |
112 | 112 |
(...skipping 19 matching lines...) Expand all Loading... |
132 | 132 |
133 runner_ = new content::MessageLoopRunner(); | 133 runner_ = new content::MessageLoopRunner(); |
134 test_client_->Send(new AppShimHostMsg_QuitApp); | 134 test_client_->Send(new AppShimHostMsg_QuitApp); |
135 EXPECT_EQ(0, quit_count_); | 135 EXPECT_EQ(0, quit_count_); |
136 runner_->Run(); // Will stop in OnShimQuit(). | 136 runner_->Run(); // Will stop in OnShimQuit(). |
137 EXPECT_EQ(1, quit_count_); | 137 EXPECT_EQ(1, quit_count_); |
138 | 138 |
139 test_client_.reset(); | 139 test_client_.reset(); |
140 } | 140 } |
141 | 141 |
142 bool AppShimHostManagerBrowserTest::SetUpUserDataDirectory() { | |
143 // Create a symlink at /tmp/scoped_dir_XXXXXX/udd that points to the real user | |
144 // data dir, and use this as the domain socket path. This is required because | |
145 // there is a path length limit for named sockets that is exceeded in | |
146 // multi-process test spawning. | |
147 base::FilePath real_user_data_dir; | |
148 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &real_user_data_dir)); | |
149 EXPECT_TRUE( | |
150 short_temp_dir_.CreateUniqueTempDirUnderPath(base::FilePath("/tmp"))); | |
151 base::FilePath shortened_user_data_dir = short_temp_dir_.path().Append("udd"); | |
152 EXPECT_EQ(0, ::symlink(real_user_data_dir.AsUTF8Unsafe().c_str(), | |
153 shortened_user_data_dir.AsUTF8Unsafe().c_str())); | |
154 | |
155 test::AppShimHostManagerTestApi::OverrideUserDataDir(shortened_user_data_dir); | |
156 short_socket_path_ = | |
157 shortened_user_data_dir.Append(app_mode::kAppShimSocketName); | |
158 | |
159 return InProcessBrowserTest::SetUpUserDataDirectory(); | |
160 } | |
161 | |
162 void AppShimHostManagerBrowserTest::OnShimLaunch( | 142 void AppShimHostManagerBrowserTest::OnShimLaunch( |
163 apps::AppShimHandler::Host* host, | 143 apps::AppShimHandler::Host* host, |
164 apps::AppShimLaunchType launch_type, | 144 apps::AppShimLaunchType launch_type, |
165 const std::vector<base::FilePath>& files) { | 145 const std::vector<base::FilePath>& files) { |
166 host->OnAppLaunchComplete(apps::APP_SHIM_LAUNCH_SUCCESS); | 146 host->OnAppLaunchComplete(apps::APP_SHIM_LAUNCH_SUCCESS); |
167 ++launch_count_; | 147 ++launch_count_; |
168 last_launch_type_ = launch_type; | 148 last_launch_type_ = launch_type; |
169 last_launch_files_ = files; | 149 last_launch_files_ = files; |
170 runner_->Quit(); | 150 runner_->Quit(); |
171 } | 151 } |
172 | 152 |
173 void AppShimHostManagerBrowserTest::OnShimQuit( | 153 void AppShimHostManagerBrowserTest::OnShimQuit( |
174 apps::AppShimHandler::Host* host) { | 154 apps::AppShimHandler::Host* host) { |
175 ++quit_count_; | 155 ++quit_count_; |
176 runner_->Quit(); | 156 runner_->Quit(); |
177 } | 157 } |
178 | 158 |
179 // Test regular launch, which would ask Chrome to launch the app. | 159 // Test regular launch, which would ask Chrome to launch the app. |
180 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchNormal) { | 160 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchNormal) { |
181 test_client_.reset(new TestShimClient(short_socket_path_)); | 161 test_client_.reset(new TestShimClient()); |
182 test_client_->Send(new AppShimHostMsg_LaunchApp( | 162 test_client_->Send(new AppShimHostMsg_LaunchApp( |
183 browser()->profile()->GetPath(), | 163 browser()->profile()->GetPath(), |
184 kTestAppMode, | 164 kTestAppMode, |
185 apps::APP_SHIM_LAUNCH_NORMAL, | 165 apps::APP_SHIM_LAUNCH_NORMAL, |
186 std::vector<base::FilePath>())); | 166 std::vector<base::FilePath>())); |
187 | 167 |
188 RunAndExitGracefully(); | 168 RunAndExitGracefully(); |
189 EXPECT_EQ(apps::APP_SHIM_LAUNCH_NORMAL, last_launch_type_); | 169 EXPECT_EQ(apps::APP_SHIM_LAUNCH_NORMAL, last_launch_type_); |
190 EXPECT_TRUE(last_launch_files_.empty()); | 170 EXPECT_TRUE(last_launch_files_.empty()); |
191 } | 171 } |
192 | 172 |
193 // Test register-only launch, used when Chrome has already launched the app. | 173 // Test register-only launch, used when Chrome has already launched the app. |
194 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchRegisterOnly) { | 174 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchRegisterOnly) { |
195 test_client_.reset(new TestShimClient(short_socket_path_)); | 175 test_client_.reset(new TestShimClient()); |
196 test_client_->Send(new AppShimHostMsg_LaunchApp( | 176 test_client_->Send(new AppShimHostMsg_LaunchApp( |
197 browser()->profile()->GetPath(), | 177 browser()->profile()->GetPath(), |
198 kTestAppMode, | 178 kTestAppMode, |
199 apps::APP_SHIM_LAUNCH_REGISTER_ONLY, | 179 apps::APP_SHIM_LAUNCH_REGISTER_ONLY, |
200 std::vector<base::FilePath>())); | 180 std::vector<base::FilePath>())); |
201 | 181 |
202 RunAndExitGracefully(); | 182 RunAndExitGracefully(); |
203 EXPECT_EQ(apps::APP_SHIM_LAUNCH_REGISTER_ONLY, last_launch_type_); | 183 EXPECT_EQ(apps::APP_SHIM_LAUNCH_REGISTER_ONLY, last_launch_type_); |
204 EXPECT_TRUE(last_launch_files_.empty()); | 184 EXPECT_TRUE(last_launch_files_.empty()); |
205 } | 185 } |
(...skipping 29 matching lines...) Expand all Loading... |
235 DISALLOW_COPY_AND_ASSIGN(AppShimHostManagerBrowserTestFailsCreate); | 215 DISALLOW_COPY_AND_ASSIGN(AppShimHostManagerBrowserTestFailsCreate); |
236 }; | 216 }; |
237 | 217 |
238 bool AppShimHostManagerBrowserTestFailsCreate::SetUpUserDataDirectory() { | 218 bool AppShimHostManagerBrowserTestFailsCreate::SetUpUserDataDirectory() { |
239 base::FilePath user_data_dir; | 219 base::FilePath user_data_dir; |
240 // Start in the "real" user data dir for this test. This is a meta-test for | 220 // Start in the "real" user data dir for this test. This is a meta-test for |
241 // the symlinking steps used in the superclass. That is, by putting the | 221 // the symlinking steps used in the superclass. That is, by putting the |
242 // clobber in the actual user data dir, the test will fail if the symlink | 222 // clobber in the actual user data dir, the test will fail if the symlink |
243 // does not actually point to the user data dir, since it won't be clobbered. | 223 // does not actually point to the user data dir, since it won't be clobbered. |
244 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)); | 224 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)); |
245 base::FilePath socket_path = | 225 base::FilePath socket_path = app_mode::GetShortSocketPath(user_data_dir); |
246 user_data_dir.Append(app_mode::kAppShimSocketName); | |
247 // Create a "barrier" to forming the UNIX domain socket. This is just a | 226 // Create a "barrier" to forming the UNIX domain socket. This is just a |
248 // pre-existing directory which can not be unlink()ed, in order to place a | 227 // pre-existing directory which can not be unlink()ed, in order to place a |
249 // named socked there instead. | 228 // named socket there instead. |
250 EXPECT_TRUE(barrier_dir_.Set(socket_path)); | 229 EXPECT_TRUE(barrier_dir_.Set(socket_path)); |
251 return AppShimHostManagerBrowserTest::SetUpUserDataDirectory(); | 230 return AppShimHostManagerBrowserTest::SetUpUserDataDirectory(); |
252 } | 231 } |
253 | 232 |
254 // Test error handling. This is essentially testing for lifetime correctness | 233 // Test error handling. This is essentially testing for lifetime correctness |
255 // during startup for unexpected failures. | 234 // during startup for unexpected failures. |
256 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTestFailsCreate, | 235 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTestFailsCreate, |
257 SocketFailure) { | 236 SocketFailure) { |
258 test::AppShimHostManagerTestApi test_api( | 237 test::AppShimHostManagerTestApi test_api( |
259 g_browser_process->platform_part()->app_shim_host_manager()); | 238 g_browser_process->platform_part()->app_shim_host_manager()); |
260 EXPECT_FALSE(test_api.factory()); | 239 EXPECT_FALSE(test_api.factory()); |
261 } | 240 } |
262 | 241 |
263 } // namespace | 242 } // namespace |
OLD | NEW |