| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/win/object_watcher.h" | 5 #include "base/win/object_watcher.h" |
| 6 | 6 |
| 7 #include <process.h> | 7 #include <process.h> |
| 8 | 8 |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 --(*counter_); | 30 --(*counter_); |
| 31 } | 31 } |
| 32 private: | 32 private: |
| 33 int* counter_; | 33 int* counter_; |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { | 36 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { |
| 37 MessageLoop message_loop(message_loop_type); | 37 MessageLoop message_loop(message_loop_type); |
| 38 | 38 |
| 39 ObjectWatcher watcher; | 39 ObjectWatcher watcher; |
| 40 EXPECT_EQ(NULL, watcher.GetWatchedObject()); | 40 EXPECT_FALSE(watcher.IsWatching()); |
| 41 | 41 |
| 42 // A manual-reset event that is not yet signaled. | 42 // A manual-reset event that is not yet signaled. |
| 43 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); | 43 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 44 | 44 |
| 45 QuitDelegate delegate; | 45 QuitDelegate delegate; |
| 46 bool ok = watcher.StartWatching(event, &delegate); | 46 bool ok = watcher.StartWatching(event, &delegate); |
| 47 EXPECT_TRUE(ok); | 47 EXPECT_TRUE(ok); |
| 48 EXPECT_TRUE(watcher.IsWatching()); |
| 48 EXPECT_EQ(event, watcher.GetWatchedObject()); | 49 EXPECT_EQ(event, watcher.GetWatchedObject()); |
| 49 | 50 |
| 50 SetEvent(event); | 51 SetEvent(event); |
| 51 | 52 |
| 52 MessageLoop::current()->Run(); | 53 MessageLoop::current()->Run(); |
| 53 | 54 |
| 54 EXPECT_EQ(NULL, watcher.GetWatchedObject()); | 55 EXPECT_FALSE(watcher.IsWatching()); |
| 55 CloseHandle(event); | 56 CloseHandle(event); |
| 56 } | 57 } |
| 57 | 58 |
| 58 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { | 59 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { |
| 59 MessageLoop message_loop(message_loop_type); | 60 MessageLoop message_loop(message_loop_type); |
| 60 | 61 |
| 61 ObjectWatcher watcher; | 62 ObjectWatcher watcher; |
| 62 | 63 |
| 63 // A manual-reset event that is not yet signaled. | 64 // A manual-reset event that is not yet signaled. |
| 64 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); | 65 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 | 109 |
| 109 // A manual-reset event that is signaled before we begin watching. | 110 // A manual-reset event that is signaled before we begin watching. |
| 110 HANDLE event = CreateEvent(NULL, TRUE, TRUE, NULL); | 111 HANDLE event = CreateEvent(NULL, TRUE, TRUE, NULL); |
| 111 | 112 |
| 112 QuitDelegate delegate; | 113 QuitDelegate delegate; |
| 113 bool ok = watcher.StartWatching(event, &delegate); | 114 bool ok = watcher.StartWatching(event, &delegate); |
| 114 EXPECT_TRUE(ok); | 115 EXPECT_TRUE(ok); |
| 115 | 116 |
| 116 MessageLoop::current()->Run(); | 117 MessageLoop::current()->Run(); |
| 117 | 118 |
| 118 EXPECT_EQ(NULL, watcher.GetWatchedObject()); | 119 EXPECT_FALSE(watcher.IsWatching()); |
| 119 CloseHandle(event); | 120 CloseHandle(event); |
| 120 } | 121 } |
| 121 | 122 |
| 122 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { | 123 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { |
| 123 // Simulate a MessageLoop that dies before an ObjectWatcher. This ordinarily | 124 // Simulate a MessageLoop that dies before an ObjectWatcher. This ordinarily |
| 124 // doesn't happen when people use the Thread class, but it can happen when | 125 // doesn't happen when people use the Thread class, but it can happen when |
| 125 // people use the Singleton pattern or atexit. | 126 // people use the Singleton pattern or atexit. |
| 126 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); // not signaled | 127 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); // not signaled |
| 127 { | 128 { |
| 128 ObjectWatcher watcher; | 129 ObjectWatcher watcher; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 } | 166 } |
| 166 | 167 |
| 167 TEST(ObjectWatcherTest, OutlivesMessageLoop) { | 168 TEST(ObjectWatcherTest, OutlivesMessageLoop) { |
| 168 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT); | 169 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT); |
| 169 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO); | 170 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO); |
| 170 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI); | 171 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI); |
| 171 } | 172 } |
| 172 | 173 |
| 173 } // namespace win | 174 } // namespace win |
| 174 } // namespace base | 175 } // namespace base |
| OLD | NEW |