| 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 21 matching lines...) Expand all Loading... |
| 32 int* counter_; | 32 int* counter_; |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { | 35 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { |
| 36 MessageLoop message_loop(message_loop_type); | 36 MessageLoop message_loop(message_loop_type); |
| 37 | 37 |
| 38 ObjectWatcher watcher; | 38 ObjectWatcher watcher; |
| 39 EXPECT_FALSE(watcher.IsWatching()); | 39 EXPECT_FALSE(watcher.IsWatching()); |
| 40 | 40 |
| 41 // A manual-reset event that is not yet signaled. | 41 // A manual-reset event that is not yet signaled. |
| 42 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); | 42 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); |
| 43 | 43 |
| 44 QuitDelegate delegate; | 44 QuitDelegate delegate; |
| 45 bool ok = watcher.StartWatching(event, &delegate); | 45 bool ok = watcher.StartWatching(event.Get(), &delegate); |
| 46 EXPECT_TRUE(ok); | 46 EXPECT_TRUE(ok); |
| 47 EXPECT_TRUE(watcher.IsWatching()); | 47 EXPECT_TRUE(watcher.IsWatching()); |
| 48 EXPECT_EQ(event, watcher.GetWatchedObject()); | 48 EXPECT_EQ(event.Get(), watcher.GetWatchedObject()); |
| 49 | 49 |
| 50 SetEvent(event); | 50 SetEvent(event.Get()); |
| 51 | 51 |
| 52 MessageLoop::current()->Run(); | 52 MessageLoop::current()->Run(); |
| 53 | 53 |
| 54 EXPECT_FALSE(watcher.IsWatching()); | 54 EXPECT_FALSE(watcher.IsWatching()); |
| 55 CloseHandle(event); | |
| 56 } | 55 } |
| 57 | 56 |
| 58 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { | 57 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { |
| 59 MessageLoop message_loop(message_loop_type); | 58 MessageLoop message_loop(message_loop_type); |
| 60 | 59 |
| 61 ObjectWatcher watcher; | 60 ObjectWatcher watcher; |
| 62 | 61 |
| 63 // A manual-reset event that is not yet signaled. | 62 // A manual-reset event that is not yet signaled. |
| 64 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); | 63 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); |
| 65 | 64 |
| 66 QuitDelegate delegate; | 65 QuitDelegate delegate; |
| 67 bool ok = watcher.StartWatching(event, &delegate); | 66 bool ok = watcher.StartWatching(event.Get(), &delegate); |
| 68 EXPECT_TRUE(ok); | 67 EXPECT_TRUE(ok); |
| 69 | 68 |
| 70 watcher.StopWatching(); | 69 watcher.StopWatching(); |
| 71 | |
| 72 CloseHandle(event); | |
| 73 } | 70 } |
| 74 | 71 |
| 75 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { | 72 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { |
| 76 MessageLoop message_loop(message_loop_type); | 73 MessageLoop message_loop(message_loop_type); |
| 77 | 74 |
| 78 ObjectWatcher watcher; | 75 ObjectWatcher watcher; |
| 79 | 76 |
| 80 int counter = 1; | 77 int counter = 1; |
| 81 DecrementCountDelegate delegate(&counter); | 78 DecrementCountDelegate delegate(&counter); |
| 82 | 79 |
| 83 // A manual-reset event that is not yet signaled. | 80 // A manual-reset event that is not yet signaled. |
| 84 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); | 81 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); |
| 85 | 82 |
| 86 bool ok = watcher.StartWatching(event, &delegate); | 83 bool ok = watcher.StartWatching(event.Get(), &delegate); |
| 87 EXPECT_TRUE(ok); | 84 EXPECT_TRUE(ok); |
| 88 | 85 |
| 89 SetEvent(event); | 86 SetEvent(event.Get()); |
| 90 | 87 |
| 91 // Let the background thread do its business | 88 // Let the background thread do its business |
| 92 Sleep(30); | 89 Sleep(30); |
| 93 | 90 |
| 94 watcher.StopWatching(); | 91 watcher.StopWatching(); |
| 95 | 92 |
| 96 RunLoop().RunUntilIdle(); | 93 RunLoop().RunUntilIdle(); |
| 97 | 94 |
| 98 // Our delegate should not have fired. | 95 // Our delegate should not have fired. |
| 99 EXPECT_EQ(1, counter); | 96 EXPECT_EQ(1, counter); |
| 100 | |
| 101 CloseHandle(event); | |
| 102 } | 97 } |
| 103 | 98 |
| 104 void RunTest_SignalBeforeWatch(MessageLoop::Type message_loop_type) { | 99 void RunTest_SignalBeforeWatch(MessageLoop::Type message_loop_type) { |
| 105 MessageLoop message_loop(message_loop_type); | 100 MessageLoop message_loop(message_loop_type); |
| 106 | 101 |
| 107 ObjectWatcher watcher; | 102 ObjectWatcher watcher; |
| 108 | 103 |
| 109 // A manual-reset event that is signaled before we begin watching. | 104 // A manual-reset event that is signaled before we begin watching. |
| 110 HANDLE event = CreateEvent(NULL, TRUE, TRUE, NULL); | 105 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, TRUE, NULL)); |
| 111 | 106 |
| 112 QuitDelegate delegate; | 107 QuitDelegate delegate; |
| 113 bool ok = watcher.StartWatching(event, &delegate); | 108 bool ok = watcher.StartWatching(event.Get(), &delegate); |
| 114 EXPECT_TRUE(ok); | 109 EXPECT_TRUE(ok); |
| 115 | 110 |
| 116 MessageLoop::current()->Run(); | 111 MessageLoop::current()->Run(); |
| 117 | 112 |
| 118 EXPECT_FALSE(watcher.IsWatching()); | 113 EXPECT_FALSE(watcher.IsWatching()); |
| 119 CloseHandle(event); | |
| 120 } | 114 } |
| 121 | 115 |
| 122 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { | 116 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { |
| 123 // Simulate a MessageLoop that dies before an ObjectWatcher. This ordinarily | 117 // 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 | 118 // doesn't happen when people use the Thread class, but it can happen when |
| 125 // people use the Singleton pattern or atexit. | 119 // people use the Singleton pattern or atexit. |
| 126 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); // not signaled | 120 // Note that |event| is not signaled |
| 121 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); |
| 127 { | 122 { |
| 128 ObjectWatcher watcher; | 123 ObjectWatcher watcher; |
| 129 { | 124 { |
| 130 MessageLoop message_loop(message_loop_type); | 125 MessageLoop message_loop(message_loop_type); |
| 131 | 126 |
| 132 QuitDelegate delegate; | 127 QuitDelegate delegate; |
| 133 watcher.StartWatching(event, &delegate); | 128 watcher.StartWatching(event.Get(), &delegate); |
| 134 } | 129 } |
| 135 } | 130 } |
| 136 CloseHandle(event); | |
| 137 } | 131 } |
| 138 | 132 |
| 139 } // namespace | 133 } // namespace |
| 140 | 134 |
| 141 //----------------------------------------------------------------------------- | 135 //----------------------------------------------------------------------------- |
| 142 | 136 |
| 143 TEST(ObjectWatcherTest, BasicSignal) { | 137 TEST(ObjectWatcherTest, BasicSignal) { |
| 144 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT); | 138 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT); |
| 145 RunTest_BasicSignal(MessageLoop::TYPE_IO); | 139 RunTest_BasicSignal(MessageLoop::TYPE_IO); |
| 146 RunTest_BasicSignal(MessageLoop::TYPE_UI); | 140 RunTest_BasicSignal(MessageLoop::TYPE_UI); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 165 } | 159 } |
| 166 | 160 |
| 167 TEST(ObjectWatcherTest, OutlivesMessageLoop) { | 161 TEST(ObjectWatcherTest, OutlivesMessageLoop) { |
| 168 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT); | 162 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT); |
| 169 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO); | 163 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO); |
| 170 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI); | 164 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI); |
| 171 } | 165 } |
| 172 | 166 |
| 173 } // namespace win | 167 } // namespace win |
| 174 } // namespace base | 168 } // namespace base |
| OLD | NEW |