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