Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1657)

Side by Side Diff: chrome/browser/printing/cloud_print/test/cloud_print_proxy_process_browsertest.cc

Issue 9131016: Cloud Print Policy - service side enforcement, Mac browser tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More tweaks Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/chrome_tests.gypi » ('j') | chrome/common/service_process_util_mac.mm » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 // Create a service process that uses a Mock to respond to the browser in order 5 // Create a service process that uses a Mock to respond to the browser in order
6 // to test launching the browser using the cloud print policy check command 6 // to test launching the browser using the cloud print policy check command
7 // line switch. 7 // line switch.
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/process_util.h" 12 #include "base/process_util.h"
13 #include "base/rand_util.h" 13 #include "base/rand_util.h"
14 #include "base/test/multiprocess_test.h" 14 #include "base/test/multiprocess_test.h"
15 #include "base/test/test_timeouts.h" 15 #include "base/test/test_timeouts.h"
16 #include "base/time.h" 16 #include "base/time.h"
17 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" 17 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
18 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory. h" 18 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory. h"
19 #include "chrome/browser/profiles/profile_keyed_service.h" 19 #include "chrome/browser/profiles/profile_keyed_service.h"
20 #include "chrome/browser/service/service_process_control.h" 20 #include "chrome/browser/service/service_process_control.h"
21 #include "chrome/browser/ui/browser_init.h" 21 #include "chrome/browser/ui/browser_init.h"
22 #include "chrome/common/chrome_notification_types.h"
23 #include "chrome/common/chrome_switches.h" 22 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
25 #include "chrome/common/service_messages.h" 24 #include "chrome/common/service_messages.h"
26 #include "chrome/common/service_process_util.h" 25 #include "chrome/common/service_process_util.h"
27 #include "chrome/service/service_ipc_server.h" 26 #include "chrome/service/service_ipc_server.h"
28 #include "chrome/service/service_process.h" 27 #include "chrome/service/service_process.h"
29 #include "chrome/test/base/test_launcher_utils.h" 28 #include "chrome/test/base/test_launcher_utils.h"
30 #include "chrome/test/base/testing_browser_process.h" 29 #include "chrome/test/base/testing_browser_process.h"
31 #include "chrome/test/base/testing_pref_service.h" 30 #include "chrome/test/base/testing_pref_service.h"
32 #include "chrome/test/base/testing_profile.h" 31 #include "chrome/test/base/testing_profile.h"
33 #include "chrome/test/base/testing_profile_manager.h" 32 #include "chrome/test/base/testing_profile_manager.h"
34 #include "chrome/test/base/ui_test_utils.h" 33 #include "chrome/test/base/ui_test_utils.h"
35 #include "content/public/browser/notification_service.h" 34 #include "content/public/browser/notification_service.h"
36 #include "content/test/test_browser_thread.h" 35 #include "content/test/test_browser_thread.h"
37 #include "ipc/ipc_descriptors.h" 36 #include "ipc/ipc_descriptors.h"
38 #include "ipc/ipc_switches.h" 37 #include "ipc/ipc_switches.h"
39 #include "testing/gmock/include/gmock/gmock.h" 38 #include "testing/gmock/include/gmock/gmock.h"
40 #include "testing/gtest/include/gtest/gtest.h" 39 #include "testing/gtest/include/gtest/gtest.h"
41 #include "testing/multiprocess_func_list.h" 40 #include "testing/multiprocess_func_list.h"
42 41
42 #if defined(OS_MACOSX)
43 #include "chrome/common/mac/mock_launchd.h"
44 #endif
43 #if defined(OS_POSIX) 45 #if defined(OS_POSIX)
44 #include "base/global_descriptors_posix.h" 46 #include "base/global_descriptors_posix.h"
45 #endif 47 #endif
46 48
47 using ::testing::AnyNumber; 49 using ::testing::AnyNumber;
48 using ::testing::Assign; 50 using ::testing::Assign;
49 using ::testing::AtLeast; 51 using ::testing::AtLeast;
50 using ::testing::DoAll; 52 using ::testing::DoAll;
51 using ::testing::Invoke; 53 using ::testing::Invoke;
52 using ::testing::Mock; 54 using ::testing::Mock;
53 using ::testing::Property; 55 using ::testing::Property;
54 using ::testing::Return; 56 using ::testing::Return;
55 using ::testing::WithoutArgs; 57 using ::testing::WithoutArgs;
56 using ::testing::_; 58 using ::testing::_;
57 59
58 namespace { 60 namespace {
59 61
62 #if defined(OS_MACOSX)
63 const char kTestExecutablePath[] = "test-executable-path";
64 #endif
65
60 bool g_good_shutdown = false; 66 bool g_good_shutdown = false;
61 67
62 void ShutdownTask() { 68 void ShutdownTask() {
63 g_good_shutdown = true; 69 g_good_shutdown = true;
64 g_service_process->Shutdown(); 70 g_service_process->Shutdown();
65 } 71 }
66 72
67 class TestStartupClientChannelListener : public IPC::Channel::Listener { 73 class TestStartupClientChannelListener : public IPC::Channel::Listener {
68 public: 74 public:
69 virtual bool OnMessageReceived(const IPC::Message& message) { return false; } 75 virtual bool OnMessageReceived(const IPC::Message& message) { return false; }
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 info_.enabled = false; 181 info_.enabled = false;
176 info_.email = std::string(); 182 info_.email = std::string();
177 EXPECT_TRUE(Send(new ServiceHostMsg_CloudPrintProxy_Info(info_))); 183 EXPECT_TRUE(Send(new ServiceHostMsg_CloudPrintProxy_Info(info_)));
178 } 184 }
179 return true; 185 return true;
180 } 186 }
181 187
182 typedef base::Callback<void(MockServiceIPCServer* server)> 188 typedef base::Callback<void(MockServiceIPCServer* server)>
183 SetExpectationsCallback; 189 SetExpectationsCallback;
184 190
191 // The return value from this routine is used as the exit code for the mock
192 // service process. Any non-zero return value will be printed out and can help
193 // determine the failure.
185 int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) { 194 int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) {
186 MessageLoopForUI main_message_loop; 195 MessageLoopForUI main_message_loop;
187 main_message_loop.set_thread_name("Main Thread"); 196 main_message_loop.set_thread_name("Main Thread");
188 197
198 #if defined(OS_MACOSX)
199 CommandLine* cl = CommandLine::ForCurrentProcess();
200 if (!cl->HasSwitch(kTestExecutablePath))
201 return 1;
202 FilePath executable_path = cl->GetSwitchValuePath(kTestExecutablePath);
203 EXPECT_FALSE(executable_path.empty());
204 MockLaunchd mock_launchd(executable_path, &main_message_loop, true, true);
205 Launchd::ScopedInstance use_mock(&mock_launchd);
206 #endif
207
189 ServiceProcessState* state(new ServiceProcessState); 208 ServiceProcessState* state(new ServiceProcessState);
190 EXPECT_TRUE(state->Initialize()); 209 bool service_process_state_initialized = state->Initialize();
210 EXPECT_TRUE(service_process_state_initialized);
211 if (!service_process_state_initialized)
212 return 2;
Albert Bodenhamer 2012/01/23 18:05:41 NIT: I'd really prefer something like kNotInitiali
Scott Byer 2012/01/23 19:03:46 Done.
191 213
192 TestServiceProcess service_process; 214 TestServiceProcess service_process;
193 EXPECT_EQ(&service_process, g_service_process); 215 EXPECT_EQ(&service_process, g_service_process);
194 216
195 // Takes ownership of the pointer, but we can use it since we have the same 217 // Takes ownership of the pointer, but we can use it since we have the same
196 // lifetime. 218 // lifetime.
197 EXPECT_TRUE(service_process.Initialize(&main_message_loop, state)); 219 EXPECT_TRUE(service_process.Initialize(&main_message_loop, state));
198 220
199 MockServiceIPCServer server(state->GetServiceProcessChannel()); 221 MockServiceIPCServer server(state->GetServiceProcessChannel());
200 222
201 // Here is where the expectations/mock responses need to be set up. 223 // Here is where the expectations/mock responses need to be set up.
202 set_expectations.Run(&server); 224 set_expectations.Run(&server);
203 225
204 EXPECT_TRUE(server.Init()); 226 EXPECT_TRUE(server.Init());
205 EXPECT_TRUE(state->SignalReady(service_process.IOMessageLoopProxy(), 227 EXPECT_TRUE(state->SignalReady(service_process.IOMessageLoopProxy(),
206 base::Bind(&ShutdownTask))); 228 base::Bind(&ShutdownTask)));
229 #if defined(OS_MACOSX)
230 mock_launchd.SignalReady();
231 #endif
207 232
208 // Connect up the parent/child IPC channel to signal that the test can 233 // Connect up the parent/child IPC channel to signal that the test can
209 // continue. 234 // continue.
210 TestStartupClientChannelListener listener; 235 TestStartupClientChannelListener listener;
211 EXPECT_TRUE(CommandLine::ForCurrentProcess()->HasSwitch( 236 EXPECT_TRUE(CommandLine::ForCurrentProcess()->HasSwitch(
212 switches::kProcessChannelID)); 237 switches::kProcessChannelID));
213 std::string startup_channel_name = 238 std::string startup_channel_name =
214 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 239 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
215 switches::kProcessChannelID); 240 switches::kProcessChannelID);
216 scoped_ptr<IPC::ChannelProxy> startup_channel; 241 scoped_ptr<IPC::ChannelProxy> startup_channel;
217 startup_channel.reset( 242 startup_channel.reset(
218 new IPC::ChannelProxy(startup_channel_name, 243 new IPC::ChannelProxy(startup_channel_name,
219 IPC::Channel::MODE_CLIENT, 244 IPC::Channel::MODE_CLIENT,
220 &listener, 245 &listener,
221 service_process.IOMessageLoopProxy())); 246 service_process.IOMessageLoopProxy()));
222 247
223 main_message_loop.Run(); 248 main_message_loop.Run();
224 if (!Mock::VerifyAndClearExpectations(&server)) 249 if (!Mock::VerifyAndClearExpectations(&server))
225 return 1; 250 return 3;
226 if (!g_good_shutdown) 251 if (!g_good_shutdown)
227 return 2; 252 return 4;
228 return 0; 253 return 0;
229 } 254 }
230 255
231 void SetServiceEnabledExpectations(MockServiceIPCServer* server) { 256 void SetServiceEnabledExpectations(MockServiceIPCServer* server) {
232 server->SetServiceEnabledExpectations(); 257 server->SetServiceEnabledExpectations();
233 } 258 }
234 259
235 MULTIPROCESS_TEST_MAIN(CloudPrintMockService_StartEnabledWaitForQuit) { 260 MULTIPROCESS_TEST_MAIN(CloudPrintMockService_StartEnabledWaitForQuit) {
236 return CloudPrintMockService_Main( 261 return CloudPrintMockService_Main(
237 base::Bind(&SetServiceEnabledExpectations)); 262 base::Bind(&SetServiceEnabledExpectations));
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 } 303 }
279 304
280 protected: 305 protected:
281 MessageLoopForUI message_loop_; 306 MessageLoopForUI message_loop_;
282 content::TestBrowserThread ui_thread_; 307 content::TestBrowserThread ui_thread_;
283 base::Thread io_thread_; 308 base::Thread io_thread_;
284 309
285 std::string startup_channel_id_; 310 std::string startup_channel_id_;
286 scoped_ptr<IPC::ChannelProxy> startup_channel_; 311 scoped_ptr<IPC::ChannelProxy> startup_channel_;
287 312
313 #if defined(OS_MACOSX)
314 ScopedTempDir temp_dir_;
315 FilePath executable_path_, bundle_path_;
316 scoped_ptr<MockLaunchd> mock_launchd_;
317 scoped_ptr<Launchd::ScopedInstance> scoped_launchd_instance_;
318 #endif
319
288 private: 320 private:
289 class WindowedChannelConnectionObserver { 321 class WindowedChannelConnectionObserver {
290 public: 322 public:
291 WindowedChannelConnectionObserver() 323 WindowedChannelConnectionObserver()
292 : seen_(false), 324 : seen_(false),
293 running_(false) { } 325 running_(false) { }
294 326
295 void Wait() { 327 void Wait() {
296 if (seen_) 328 if (seen_)
297 return; 329 return;
298 running_ = true; 330 running_ = true;
299 ui_test_utils::RunMessageLoop(); 331 ui_test_utils::RunMessageLoop();
300 } 332 }
301 333
302 void Notify() { 334 void Notify() {
303 seen_ = true; 335 seen_ = true;
304 if (running_) 336 if (running_)
305 MessageLoopForUI::current()->Quit(); 337 MessageLoopForUI::current()->Quit();
306 } 338 }
307 339
308 private: 340 private:
309 bool seen_; 341 bool seen_;
310 bool running_; 342 bool running_;
311 }; 343 };
312 344
313 WindowedChannelConnectionObserver observer_; 345 WindowedChannelConnectionObserver observer_;
314 }; 346 };
315 347
316
317 CloudPrintProxyPolicyStartupTest::CloudPrintProxyPolicyStartupTest() 348 CloudPrintProxyPolicyStartupTest::CloudPrintProxyPolicyStartupTest()
318 : ui_thread_(content::BrowserThread::UI, &message_loop_), 349 : ui_thread_(content::BrowserThread::UI, &message_loop_),
319 io_thread_("CloudPrintProxyPolicyTestThread") { 350 io_thread_("CloudPrintProxyPolicyTestThread") {
320 } 351 }
321 352
322 CloudPrintProxyPolicyStartupTest::~CloudPrintProxyPolicyStartupTest() { 353 CloudPrintProxyPolicyStartupTest::~CloudPrintProxyPolicyStartupTest() {
323 } 354 }
324 355
325 void CloudPrintProxyPolicyStartupTest::SetUp() { 356 void CloudPrintProxyPolicyStartupTest::SetUp() {
326 base::Thread::Options options(MessageLoop::TYPE_IO, 0); 357 base::Thread::Options options(MessageLoop::TYPE_IO, 0);
327 ASSERT_TRUE(io_thread_.StartWithOptions(options)); 358 ASSERT_TRUE(io_thread_.StartWithOptions(options));
359
360 #if defined(OS_MACOSX)
361 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
362 EXPECT_TRUE(MockLaunchd::MakeABundle(temp_dir_.path(),
363 "CloudPrintProxyTest",
364 &bundle_path_,
365 &executable_path_));
366 mock_launchd_.reset(new MockLaunchd(executable_path_, &message_loop_,
367 true, false));
368 scoped_launchd_instance_.reset(
369 new Launchd::ScopedInstance(mock_launchd_.get()));
370 #endif
328 } 371 }
329 372
330 base::ProcessHandle CloudPrintProxyPolicyStartupTest::Launch( 373 base::ProcessHandle CloudPrintProxyPolicyStartupTest::Launch(
331 const std::string& name) { 374 const std::string& name) {
332 EXPECT_FALSE(CheckServiceProcessReady()); 375 EXPECT_FALSE(CheckServiceProcessReady());
333 376
334 startup_channel_id_ = 377 startup_channel_id_ =
335 base::StringPrintf("%d.%p.%d", 378 base::StringPrintf("%d.%p.%d",
336 base::GetCurrentProcId(), this, 379 base::GetCurrentProcId(), this,
337 base::RandInt(0, std::numeric_limits<int>::max())); 380 base::RandInt(0, std::numeric_limits<int>::max()));
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 423
381 void CloudPrintProxyPolicyStartupTest::OnChannelConnected(int32 peer_pid) { 424 void CloudPrintProxyPolicyStartupTest::OnChannelConnected(int32 peer_pid) {
382 observer_.Notify(); 425 observer_.Notify();
383 } 426 }
384 427
385 CommandLine CloudPrintProxyPolicyStartupTest::MakeCmdLine( 428 CommandLine CloudPrintProxyPolicyStartupTest::MakeCmdLine(
386 const std::string& procname, 429 const std::string& procname,
387 bool debug_on_start) { 430 bool debug_on_start) {
388 CommandLine cl = MultiProcessTest::MakeCmdLine(procname, debug_on_start); 431 CommandLine cl = MultiProcessTest::MakeCmdLine(procname, debug_on_start);
389 cl.AppendSwitchASCII(switches::kProcessChannelID, startup_channel_id_); 432 cl.AppendSwitchASCII(switches::kProcessChannelID, startup_channel_id_);
433 #if defined(OS_MACOSX)
434 cl.AppendSwitchASCII(kTestExecutablePath, executable_path_.value());
435 #endif
390 return cl; 436 return cl;
391 } 437 }
392 438
393 TEST_F(CloudPrintProxyPolicyStartupTest, StartAndShutdown) { 439 TEST_F(CloudPrintProxyPolicyStartupTest, StartAndShutdown) {
394 base::ProcessHandle handle = 440 base::ProcessHandle handle =
395 Launch("CloudPrintMockService_StartEnabledWaitForQuit"); 441 Launch("CloudPrintMockService_StartEnabledWaitForQuit");
396 WaitForConnect(); 442 WaitForConnect();
397 ShutdownAndWaitForExitWithTimeout(handle); 443 ShutdownAndWaitForExitWithTimeout(handle);
398 } 444 }
399 445
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 // No expectations on run_loop being true here; that would be a race 525 // No expectations on run_loop being true here; that would be a race
480 // condition. 526 // condition.
481 if (run_loop) 527 if (run_loop)
482 MessageLoop::current()->Run(); 528 MessageLoop::current()->Run();
483 529
484 EXPECT_EQ("", prefs->GetString(prefs::kCloudPrintEmail)); 530 EXPECT_EQ("", prefs->GetString(prefs::kCloudPrintEmail));
485 531
486 ShutdownAndWaitForExitWithTimeout(handle); 532 ShutdownAndWaitForExitWithTimeout(handle);
487 profile_manager.DeleteTestingProfile("StartBrowserWithPolicy"); 533 profile_manager.DeleteTestingProfile("StartBrowserWithPolicy");
488 } 534 }
OLDNEW
« no previous file with comments | « no previous file | chrome/chrome_tests.gypi » ('j') | chrome/common/service_process_util_mac.mm » ('J')

Powered by Google App Engine
This is Rietveld 408576698