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