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

Side by Side Diff: chrome/common/multi_process_notification_unittest.cc

Issue 5970015: Add multi-process notification class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 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
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4
5 #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.
6 // TODO(dmaclach): Remove defined(OS_MACOSX) once
7 // MultiProcessNotification is implemented on Win/Linux.
8
9 #include "chrome/common/multi_process_notification.h"
10
11 #include "base/basictypes.h"
12 #include "base/environment.h"
13 #include "base/logging.h"
14 #include "base/message_loop.h"
15 #include "base/test/multiprocess_test.h"
16 #include "base/test/test_timeouts.h"
17 #include "base/time.h"
18 #include "testing/multiprocess_func_list.h"
19
20 class MultiProcessNotificationTest : public base::MultiProcessTest {
21 public:
22 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.
23 static const char kQuitNotificationName[];
24
25 class QuitTask : public Task {
26 public:
27 virtual void Run() {
28 MessageLoopForIO::current()->Quit();
29 }
30 };
31
32 virtual void SetUp() {
33 io_loop.reset(new MessageLoop(MessageLoop::TYPE_IO));
34 }
35
36 virtual void TearDown() {
37 io_loop.reset(NULL);
38 }
39
Mark Mentovai 2011/01/04 18:19:33 Extra blank line.
dmac 2011/01/06 06:06:03 Done.
40
41 private:
42 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.
43 };
44
45 static void SpinRunLoop(int milliseconds) {
46 MessageLoopForIO *loop = MessageLoopForIO::current();
47 // Post a quit task so that this loop eventually ends and we don't hang
48 // in the case of a bad test. Usually, the run loop will quit sooner than
49 // that because all tests use a MultiProcessNotificationTestQuit which quits
50 // the current run loop when it gets a notification.
51 loop->PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), milliseconds);
52 loop->Run();
53 }
54
55 const char MultiProcessNotificationTest::kStartedNotificationName[]
56 = "MULTI_PROCESS_TEST_STARTED_NOTIFICATION";
57
58 const char MultiProcessNotificationTest::kQuitNotificationName[]
59 = "MULTI_PROCESS_TEST_QUIT_NOTIFICATION";
60
61 TEST_F(MultiProcessNotificationTest, BasicCreationTest) {
62 multi_process_notification::Listener listener("BasicCreationTest", NULL);
63 ASSERT_TRUE(listener.Start());
64 }
65
66 TEST_F(MultiProcessNotificationTest, PostLocalNotification) {
67 std::string local_notification("QuitLocalNotification");
68 multi_process_notification::PerformTaskOnNotification quitter(
69 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.
70 multi_process_notification::Listener listener(local_notification, &quitter);
71
72 ASSERT_TRUE(listener.Start());
73 ASSERT_TRUE(multi_process_notification::Post(local_notification));
74 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
75 ASSERT_TRUE(quitter.notification_called());
76 }
77
78 TEST_F(MultiProcessNotificationTest, PostGlobalNotification) {
79 multi_process_notification::PerformTaskOnNotification process_started(
80 new QuitTask());
81 multi_process_notification::Listener listener(kStartedNotificationName,
82 &process_started);
83 ASSERT_TRUE(listener.Start());
84 base::ProcessHandle handle = SpawnChild("MultiProcessNotificationMain",
85 false);
86 ASSERT_TRUE(handle);
87 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
88 ASSERT_TRUE(process_started.notification_called());
89 ASSERT_TRUE(multi_process_notification::Post(kQuitNotificationName));
90 int exit_code = 0;
91 EXPECT_TRUE(base::WaitForExitCodeWithTimeout(
92 handle, &exit_code, TestTimeouts::action_max_timeout_ms()));
93 }
94
95 MULTIPROCESS_TEST_MAIN(MultiProcessNotificationMain) {
96 MessageLoop io_loop(MessageLoop::TYPE_IO);
97 multi_process_notification::PerformTaskOnNotification quitter(
98 new MultiProcessNotificationTest::QuitTask());
99 multi_process_notification::Listener listener(
100 MultiProcessNotificationTest::kQuitNotificationName, &quitter);
101 EXPECT_TRUE(listener.Start());
102 EXPECT_TRUE(multi_process_notification::Post(
103 MultiProcessNotificationTest::kStartedNotificationName));
104 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
105 EXPECT_TRUE(quitter.notification_called());
106 return 0;
107 }
108
109 #endif // defined(OS_MACOSX)
110
Mark Mentovai 2011/01/04 18:19:33 Blank line at EOF.
dmac 2011/01/06 06:06:03 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698