hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
hta@webrtc.org | 4030013 | 2012-05-23 15:49:48 +0000 | [diff] [blame] | 10 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/interface/thread_wrapper.h" |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 12 | |
| 13 | #include "gtest/gtest.h" |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 14 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 18 | // Function that does nothing, and reports success. |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 19 | bool NullRunFunction(void* obj) { |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 20 | return true; |
| 21 | } |
| 22 | |
andrew@webrtc.org | 23ec30b | 2012-11-15 05:33:25 +0000 | [diff] [blame] | 23 | TEST(ThreadTest, StartStop) { |
henrike@webrtc.org | a3e6bec | 2013-01-18 16:39:21 +0000 | [diff] [blame^] | 24 | ThreadWrapper* thread = ThreadWrapper::CreateThread(&NullRunFunction, NULL); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 25 | unsigned int id = 42; |
| 26 | ASSERT_TRUE(thread->Start(id)); |
| 27 | EXPECT_TRUE(thread->Stop()); |
hta@webrtc.org | 6ed617b | 2012-05-22 16:55:16 +0000 | [diff] [blame] | 28 | delete thread; |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | // Function that sets a boolean. |
| 32 | bool SetFlagRunFunction(void* obj) { |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 33 | bool* obj_as_bool = static_cast<bool*>(obj); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 34 | *obj_as_bool = true; |
| 35 | return true; |
| 36 | } |
| 37 | |
andrew@webrtc.org | 23ec30b | 2012-11-15 05:33:25 +0000 | [diff] [blame] | 38 | TEST(ThreadTest, RunFunctionIsCalled) { |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 39 | bool flag = false; |
| 40 | ThreadWrapper* thread = ThreadWrapper::CreateThread(&SetFlagRunFunction, |
| 41 | &flag); |
| 42 | unsigned int id = 42; |
| 43 | ASSERT_TRUE(thread->Start(id)); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 44 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 45 | // At this point, the flag may be either true or false. |
| 46 | EXPECT_TRUE(thread->Stop()); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 47 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 48 | // We expect the thread to have run at least once. |
| 49 | EXPECT_TRUE(flag); |
hta@webrtc.org | 6ed617b | 2012-05-22 16:55:16 +0000 | [diff] [blame] | 50 | delete thread; |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | } // namespace webrtc |