Chromium Code Reviews| Index: chrome/common/multi_process_notification_unittest.cc |
| diff --git a/chrome/common/multi_process_notification_unittest.cc b/chrome/common/multi_process_notification_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..253206b557d7c26e08a67884578a7e497f5fe433 |
| --- /dev/null |
| +++ b/chrome/common/multi_process_notification_unittest.cc |
| @@ -0,0 +1,110 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#if defined(OS_MACOSX) |
|
Mark Mentovai
2011/01/04 18:19:33
This is ineffective because you don’t have build/b
dmac
2011/01/06 06:06:03
Done.
|
| +// TODO(dmaclach): Remove defined(OS_MACOSX) once |
| +// MultiProcessNotification is implemented on Win/Linux. |
| + |
| +#include "chrome/common/multi_process_notification.h" |
| + |
| +#include "base/basictypes.h" |
| +#include "base/environment.h" |
| +#include "base/logging.h" |
| +#include "base/message_loop.h" |
| +#include "base/test/multiprocess_test.h" |
| +#include "base/test/test_timeouts.h" |
| +#include "base/time.h" |
| +#include "testing/multiprocess_func_list.h" |
| + |
| +class MultiProcessNotificationTest : public base::MultiProcessTest { |
| + public: |
| + static const char kStartedNotificationName[]; |
|
Mark Mentovai
2011/01/04 18:19:33
These can be file-static or in an anonymous namesp
dmac
2011/01/06 06:06:03
Done.
|
| + static const char kQuitNotificationName[]; |
| + |
| + class QuitTask : public Task { |
| + public: |
| + virtual void Run() { |
| + MessageLoopForIO::current()->Quit(); |
| + } |
| + }; |
| + |
| + virtual void SetUp() { |
| + io_loop.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
| + } |
| + |
| + virtual void TearDown() { |
| + io_loop.reset(NULL); |
| + } |
| + |
|
Mark Mentovai
2011/01/04 18:19:33
Extra blank line.
dmac
2011/01/06 06:06:03
Done.
|
| + |
| + private: |
| + scoped_ptr<MessageLoop> io_loop; |
|
Paweł Hajdan Jr.
2011/01/04 15:54:09
Why not just stack-allocate the loop? You can RunA
Mark Mentovai
2011/01/04 18:19:33
This should be io_loop_.
dmac
2011/01/06 06:06:03
Done.
dmac
2011/01/06 06:06:03
Done.
|
| +}; |
| + |
| +static void SpinRunLoop(int milliseconds) { |
| + MessageLoopForIO *loop = MessageLoopForIO::current(); |
| + // Post a quit task so that this loop eventually ends and we don't hang |
| + // in the case of a bad test. Usually, the run loop will quit sooner than |
| + // that because all tests use a MultiProcessNotificationTestQuit which quits |
| + // the current run loop when it gets a notification. |
| + loop->PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), milliseconds); |
| + loop->Run(); |
| +} |
| + |
| +const char MultiProcessNotificationTest::kStartedNotificationName[] |
| + = "MULTI_PROCESS_TEST_STARTED_NOTIFICATION"; |
| + |
| +const char MultiProcessNotificationTest::kQuitNotificationName[] |
| + = "MULTI_PROCESS_TEST_QUIT_NOTIFICATION"; |
| + |
| +TEST_F(MultiProcessNotificationTest, BasicCreationTest) { |
| + multi_process_notification::Listener listener("BasicCreationTest", NULL); |
| + ASSERT_TRUE(listener.Start()); |
| +} |
| + |
| +TEST_F(MultiProcessNotificationTest, PostLocalNotification) { |
| + std::string local_notification("QuitLocalNotification"); |
| + multi_process_notification::PerformTaskOnNotification quitter( |
| + new QuitTask()); |
|
Paweł Hajdan Jr.
2011/01/04 15:54:09
Is there any reason for not using MessageLoop::Qui
dmac
2011/01/06 06:06:03
Done.
|
| + multi_process_notification::Listener listener(local_notification, &quitter); |
| + |
| + ASSERT_TRUE(listener.Start()); |
| + ASSERT_TRUE(multi_process_notification::Post(local_notification)); |
| + SpinRunLoop(TestTimeouts::action_max_timeout_ms()); |
| + ASSERT_TRUE(quitter.notification_called()); |
| +} |
| + |
| +TEST_F(MultiProcessNotificationTest, PostGlobalNotification) { |
| + multi_process_notification::PerformTaskOnNotification process_started( |
| + new QuitTask()); |
| + multi_process_notification::Listener listener(kStartedNotificationName, |
| + &process_started); |
| + ASSERT_TRUE(listener.Start()); |
| + base::ProcessHandle handle = SpawnChild("MultiProcessNotificationMain", |
| + false); |
| + ASSERT_TRUE(handle); |
| + SpinRunLoop(TestTimeouts::action_max_timeout_ms()); |
| + ASSERT_TRUE(process_started.notification_called()); |
| + ASSERT_TRUE(multi_process_notification::Post(kQuitNotificationName)); |
| + int exit_code = 0; |
| + EXPECT_TRUE(base::WaitForExitCodeWithTimeout( |
| + handle, &exit_code, TestTimeouts::action_max_timeout_ms())); |
| +} |
| + |
| +MULTIPROCESS_TEST_MAIN(MultiProcessNotificationMain) { |
| + MessageLoop io_loop(MessageLoop::TYPE_IO); |
| + multi_process_notification::PerformTaskOnNotification quitter( |
| + new MultiProcessNotificationTest::QuitTask()); |
| + multi_process_notification::Listener listener( |
| + MultiProcessNotificationTest::kQuitNotificationName, &quitter); |
| + EXPECT_TRUE(listener.Start()); |
| + EXPECT_TRUE(multi_process_notification::Post( |
| + MultiProcessNotificationTest::kStartedNotificationName)); |
| + SpinRunLoop(TestTimeouts::action_max_timeout_ms()); |
| + EXPECT_TRUE(quitter.notification_called()); |
| + return 0; |
| +} |
| + |
| +#endif // defined(OS_MACOSX) |
| + |
|
Mark Mentovai
2011/01/04 18:19:33
Blank line at EOF.
dmac
2011/01/06 06:06:03
Done.
|