blob: 6b55fb0bf7863c44235f54e9a4180308cf983f12 [file] [log] [blame]
hta@webrtc.orge1919f42012-05-22 15:57:34 +00001/*
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.org40300132012-05-23 15:49:48 +000010
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000011#include "webrtc/system_wrappers/interface/thread_wrapper.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000012
13#include "gtest/gtest.h"
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000014#include "webrtc/system_wrappers/interface/scoped_ptr.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000015
16namespace webrtc {
17
andrew@webrtc.org23ec30b2012-11-15 05:33:25 +000018TEST(ThreadTest, NullFunctionPointer) {
henrike@webrtc.org5ba44112012-10-05 14:36:54 +000019 webrtc::scoped_ptr<ThreadWrapper> thread(
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000020 webrtc::ThreadWrapper::CreateThread());
henrike@webrtc.org5ba44112012-10-05 14:36:54 +000021 unsigned int id = 42;
22 EXPECT_FALSE(thread->Start(id));
23}
24
hta@webrtc.orge1919f42012-05-22 15:57:34 +000025// Function that does nothing, and reports success.
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000026bool NullRunFunction(void* obj) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000027 return true;
28}
29
andrew@webrtc.org23ec30b2012-11-15 05:33:25 +000030TEST(ThreadTest, StartStop) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000031 ThreadWrapper* thread = ThreadWrapper::CreateThread(&NullRunFunction);
32 unsigned int id = 42;
33 ASSERT_TRUE(thread->Start(id));
34 EXPECT_TRUE(thread->Stop());
hta@webrtc.org6ed617b2012-05-22 16:55:16 +000035 delete thread;
hta@webrtc.orge1919f42012-05-22 15:57:34 +000036}
37
38// Function that sets a boolean.
39bool SetFlagRunFunction(void* obj) {
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000040 bool* obj_as_bool = static_cast<bool*>(obj);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000041 *obj_as_bool = true;
42 return true;
43}
44
andrew@webrtc.org23ec30b2012-11-15 05:33:25 +000045TEST(ThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000046 bool flag = false;
47 ThreadWrapper* thread = ThreadWrapper::CreateThread(&SetFlagRunFunction,
48 &flag);
49 unsigned int id = 42;
50 ASSERT_TRUE(thread->Start(id));
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000051
hta@webrtc.orge1919f42012-05-22 15:57:34 +000052 // At this point, the flag may be either true or false.
53 EXPECT_TRUE(thread->Stop());
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000054
hta@webrtc.orge1919f42012-05-22 15:57:34 +000055 // We expect the thread to have run at least once.
56 EXPECT_TRUE(flag);
hta@webrtc.org6ed617b2012-05-22 16:55:16 +000057 delete thread;
hta@webrtc.orge1919f42012-05-22 15:57:34 +000058}
59
60} // namespace webrtc