OLD | NEW |
---|---|
(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 #include "chrome/common/multi_process_notification.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/environment.h" | |
9 #include "base/logging.h" | |
10 #include "base/message_loop.h" | |
11 #include "base/test/multiprocess_test.h" | |
12 #include "base/test/test_timeouts.h" | |
13 #include "base/time.h" | |
14 #include "testing/multiprocess_func_list.h" | |
15 | |
16 #if defined(OS_MACOSX) | |
17 // TODO(dmaclach): Remove defined(OS_MACOSX) once | |
18 // MultiProcessNotification is implemented on Win/Linux. | |
19 | |
20 namespace { | |
21 | |
22 const char kStartedNotificationName[] = "MultiProcessTestStartedNotification"; | |
23 const char kQuitNotificationName[] = "MultiProcessTestQuitNotification"; | |
24 | |
25 void SpinRunLoop(int milliseconds) { | |
26 MessageLoopForIO *loop = MessageLoopForIO::current(); | |
27 // Post a quit task so that this loop eventually ends and we don't hang | |
Mark Mentovai
2011/01/06 17:59:09
What’s with this indentation? Bring it back to be
dmac
2011/01/06 18:22:37
Stupid Xcode.
| |
28 // in the case of a bad test. Usually, the run loop will quit sooner than | |
29 // that because all tests use a MultiProcessNotificationTestQuit which quits | |
30 // the current run loop when it gets a notification. | |
31 loop->PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), milliseconds); | |
32 loop->Run(); | |
33 } | |
34 | |
35 int MultiProcessNotificationMain(multi_process_notification::Domain domain) { | |
36 MessageLoop io_loop(MessageLoop::TYPE_IO); | |
37 multi_process_notification::PerformTaskOnNotification quitter( | |
38 new MessageLoop::QuitTask()); | |
39 multi_process_notification::Listener listener( | |
40 kQuitNotificationName, domain, &quitter); | |
41 EXPECT_TRUE(listener.Start()); | |
42 EXPECT_TRUE(multi_process_notification::Post(kStartedNotificationName, | |
43 domain)); | |
44 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | |
45 EXPECT_TRUE(quitter.WasNotificationCalled()); | |
46 return 0; | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 class MultiProcessNotificationTest : public base::MultiProcessTest { | |
52 public: | |
53 MultiProcessNotificationTest(); | |
54 | |
55 void PostNotificationTest(multi_process_notification::Domain domain); | |
56 void CrossPostNotificationTest(multi_process_notification::Domain domain); | |
57 | |
58 private: | |
59 MessageLoop io_loop_; | |
60 }; | |
61 | |
62 MultiProcessNotificationTest::MultiProcessNotificationTest() | |
63 : io_loop_(MessageLoop::TYPE_IO) { | |
64 } | |
65 | |
66 void MultiProcessNotificationTest::PostNotificationTest( | |
67 multi_process_notification::Domain domain) { | |
68 multi_process_notification::PerformTaskOnNotification process_started( | |
69 new MessageLoop::QuitTask()); | |
70 multi_process_notification::Listener listener(kStartedNotificationName, | |
71 domain, | |
72 &process_started); | |
73 ASSERT_TRUE(listener.Start()); | |
74 std::string process_name; | |
75 switch (domain) { | |
76 case multi_process_notification::ProfileDomain: | |
77 process_name = "MultiProcessProfileNotificationMain"; | |
78 break; | |
79 | |
80 case multi_process_notification::UserDomain: | |
81 process_name = "MultiProcessUserNotificationMain"; | |
82 break; | |
83 | |
84 case multi_process_notification::SystemDomain: | |
85 process_name = "MultiProcessSystemNotificationMain"; | |
86 break; | |
87 } | |
88 base::ProcessHandle handle = SpawnChild(process_name, false); | |
89 ASSERT_TRUE(handle); | |
90 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | |
91 ASSERT_TRUE(process_started.WasNotificationCalled()); | |
92 ASSERT_TRUE(multi_process_notification::Post(kQuitNotificationName, domain)); | |
93 int exit_code = 0; | |
94 EXPECT_TRUE(base::WaitForExitCodeWithTimeout( | |
95 handle, &exit_code, TestTimeouts::action_max_timeout_ms())); | |
96 } | |
97 | |
98 void MultiProcessNotificationTest::CrossPostNotificationTest( | |
99 multi_process_notification::Domain domain) { | |
100 // Check to make sure notifications sent to user domain aren't picked up | |
101 // by system domain listeners and vice versa. | |
102 std::string local_notification("QuitLocalNotification"); | |
103 std::string final_notification("FinalQuitLocalNotification"); | |
104 multi_process_notification::PerformTaskOnNotification profile_quitter( | |
105 new MessageLoop::QuitTask()); | |
106 multi_process_notification::PerformTaskOnNotification user_quitter( | |
107 new MessageLoop::QuitTask()); | |
108 multi_process_notification::PerformTaskOnNotification system_quitter( | |
109 new MessageLoop::QuitTask()); | |
110 multi_process_notification::PerformTaskOnNotification final_quitter( | |
111 new MessageLoop::QuitTask()); | |
112 multi_process_notification::Listener profile_listener( | |
113 local_notification, multi_process_notification::ProfileDomain, | |
114 &profile_quitter); | |
115 multi_process_notification::Listener user_listener( | |
116 local_notification, multi_process_notification::UserDomain, | |
117 &user_quitter); | |
118 multi_process_notification::Listener system_listener( | |
119 local_notification, multi_process_notification::SystemDomain, | |
120 &system_quitter); | |
121 multi_process_notification::Listener final_listener( | |
122 final_notification, multi_process_notification::UserDomain, | |
123 &final_quitter); | |
124 | |
125 ASSERT_TRUE(profile_listener.Start()); | |
126 ASSERT_TRUE(user_listener.Start()); | |
127 ASSERT_TRUE(system_listener.Start()); | |
128 ASSERT_TRUE(multi_process_notification::Post(local_notification, domain)); | |
129 SpinRunLoop(TestTimeouts::action_timeout_ms()); | |
130 | |
131 // Now send out a final_notification to queue up a notification | |
132 // after the local_notification and make sure that all listeners have had a | |
133 // chance to process local_notification before we check to see if they | |
134 // were called. | |
135 ASSERT_TRUE(final_listener.Start()); | |
136 ASSERT_TRUE(multi_process_notification::Post( | |
137 final_notification, multi_process_notification::UserDomain)); | |
138 SpinRunLoop(TestTimeouts::action_timeout_ms()); | |
139 ASSERT_TRUE(final_quitter.WasNotificationCalled()); | |
140 switch (domain) { | |
141 case multi_process_notification::ProfileDomain: | |
142 ASSERT_TRUE(profile_quitter.WasNotificationCalled()); | |
143 ASSERT_FALSE(user_quitter.WasNotificationCalled()); | |
144 ASSERT_FALSE(system_quitter.WasNotificationCalled()); | |
145 break; | |
146 | |
147 case multi_process_notification::UserDomain: | |
148 ASSERT_FALSE(profile_quitter.WasNotificationCalled()); | |
149 ASSERT_TRUE(user_quitter.WasNotificationCalled()); | |
150 ASSERT_FALSE(system_quitter.WasNotificationCalled()); | |
151 break; | |
152 | |
153 case multi_process_notification::SystemDomain: | |
154 ASSERT_FALSE(profile_quitter.WasNotificationCalled()); | |
155 ASSERT_FALSE(user_quitter.WasNotificationCalled()); | |
156 ASSERT_TRUE(system_quitter.WasNotificationCalled()); | |
157 break; | |
158 } | |
159 } | |
160 | |
161 TEST_F(MultiProcessNotificationTest, BasicCreationTest) { | |
162 multi_process_notification::Listener local_listener( | |
163 "BasicCreationTest", multi_process_notification::UserDomain, NULL); | |
164 ASSERT_TRUE(local_listener.Start()); | |
165 multi_process_notification::Listener system_listener( | |
166 "BasicCreationTest", multi_process_notification::SystemDomain, NULL); | |
167 ASSERT_TRUE(system_listener.Start()); | |
168 } | |
169 | |
170 TEST_F(MultiProcessNotificationTest, PostInProcessNotification) { | |
171 std::string local_notification("QuitLocalNotification"); | |
172 multi_process_notification::PerformTaskOnNotification quitter( | |
173 new MessageLoop::QuitTask()); | |
174 multi_process_notification::Listener listener( | |
175 local_notification, multi_process_notification::UserDomain, &quitter); | |
176 | |
177 ASSERT_TRUE(listener.Start()); | |
178 ASSERT_TRUE(multi_process_notification::Post( | |
179 local_notification, multi_process_notification::UserDomain)); | |
180 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | |
181 ASSERT_TRUE(quitter.WasNotificationCalled()); | |
182 } | |
183 | |
184 TEST_F(MultiProcessNotificationTest, PostProfileNotification) { | |
185 PostNotificationTest(multi_process_notification::ProfileDomain); | |
186 } | |
187 | |
188 TEST_F(MultiProcessNotificationTest, PostUserNotification) { | |
189 PostNotificationTest(multi_process_notification::UserDomain); | |
190 } | |
191 | |
192 TEST_F(MultiProcessNotificationTest, PostSystemNotification) { | |
193 PostNotificationTest(multi_process_notification::SystemDomain); | |
194 } | |
195 | |
196 TEST_F(MultiProcessNotificationTest, ProfileCrossDomainPosting) { | |
197 CrossPostNotificationTest(multi_process_notification::ProfileDomain); | |
198 } | |
199 | |
200 TEST_F(MultiProcessNotificationTest, UserCrossDomainPosting) { | |
201 CrossPostNotificationTest(multi_process_notification::UserDomain); | |
202 } | |
203 | |
204 TEST_F(MultiProcessNotificationTest, SystemCrossDomainPosting) { | |
205 CrossPostNotificationTest(multi_process_notification::SystemDomain); | |
206 } | |
207 | |
208 MULTIPROCESS_TEST_MAIN(MultiProcessProfileNotificationMain) { | |
209 return MultiProcessNotificationMain( | |
210 multi_process_notification::ProfileDomain); | |
211 } | |
212 | |
213 MULTIPROCESS_TEST_MAIN(MultiProcessUserNotificationMain) { | |
214 return MultiProcessNotificationMain(multi_process_notification::UserDomain); | |
215 } | |
216 | |
217 MULTIPROCESS_TEST_MAIN(MultiProcessSystemNotificationMain) { | |
218 return MultiProcessNotificationMain(multi_process_notification::SystemDomain); | |
219 } | |
220 | |
221 #endif // defined(OS_MACOSX) | |
OLD | NEW |