1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include <test_header.h>
18
19 #include "hgm_task_handle_thread.h"
20 #include <thread>
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 class HgmTaskHandleThreadTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 void SetUp();
32 void TearDown();
33 };
34
SetUpTestCase()35 void HgmTaskHandleThreadTest::SetUpTestCase() {}
TearDownTestCase()36 void HgmTaskHandleThreadTest::TearDownTestCase() {}
SetUp()37 void HgmTaskHandleThreadTest::SetUp() {}
TearDown()38 void HgmTaskHandleThreadTest::TearDown() {}
39
40
41 /*
42 * @tc.name: Instance
43 * @tc.desc: Test Instance
44 * @tc.type: FUNC
45 * @tc.require:
46 */
47 HWTEST_F(HgmTaskHandleThreadTest, Instance, TestSize.Level1)
48 {
49 HgmTaskHandleThread& instance1 = HgmTaskHandleThread::Instance();
50 HgmTaskHandleThread& instance2 = HgmTaskHandleThread::Instance();
51 EXPECT_EQ(&instance1, &instance2);
52 }
53
54 /*
55 * @tc.name: PostTask001
56 * @tc.desc: Test PostTask
57 * @tc.type: FUNC
58 * @tc.require:
59 */
60 HWTEST_F(HgmTaskHandleThreadTest, PostTask001, TestSize.Level1)
61 {
__anonbdb6cdb80102() 62 std::function<void()> func = []() -> void {};
63 HgmTaskHandleThread::Instance().PostTask(func);
64 int64_t delayTime = 1;
65 HgmTaskHandleThread::Instance().PostTask(func, delayTime);
66 }
67
68 /*
69 * @tc.name: PostTask002
70 * @tc.desc: Test PostTask
71 * @tc.type: FUNC
72 * @tc.require:
73 */
74 HWTEST_F(HgmTaskHandleThreadTest, PostTask002, TestSize.Level1)
75 {
76 HgmTaskHandleThread& instance = HgmTaskHandleThread::Instance();
77 int count = 0;
__anonbdb6cdb80202()78 instance.PostTask([&count](){ count++; }, 0);
79 std::this_thread::sleep_for(std::chrono::milliseconds(100));
80 EXPECT_EQ(count, 1);
81 }
82
83 /*
84 * @tc.name: PostTask003
85 * @tc.desc: Test PostTask
86 * @tc.type: FUNC
87 * @tc.require:
88 */
89 HWTEST_F(HgmTaskHandleThreadTest, PostTask003, TestSize.Level1)
90 {
91 HgmTaskHandleThread& instance = HgmTaskHandleThread::Instance();
92 int count = 0;
93 auto start_time = std::chrono::high_resolution_clock::now();
__anonbdb6cdb80302()94 instance.PostTask([&count](){ count++; }, 1000);
95 std::this_thread::sleep_for(std::chrono::milliseconds(500));
96 EXPECT_EQ(count, 0);
97 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
98 auto end_time = std::chrono::high_resolution_clock::now();
99 EXPECT_EQ(count, 1);
100 EXPECT_GE(std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count(), 1000);
101 }
102
103 /*
104 * @tc.name: DetectMultiThreadingCalls
105 * @tc.desc: Test DetectMultiThreadingCalls
106 * @tc.type: FUNC
107 * @tc.require:IB3MVN
108 */
109 HWTEST_F(HgmTaskHandleThreadTest, DetectMultiThreadingCalls, TestSize.Level1)
110 {
111 HgmTaskHandleThread& instance = HgmTaskHandleThread::Instance();
112 instance.DetectMultiThreadingCalls();
113 EXPECT_NE(instance.curThreadId_, -1);
114 int32_t curThreadId = instance.curThreadId_;
115 instance.DetectMultiThreadingCalls();
116 EXPECT_EQ(curThreadId, instance.curThreadId_);
__anonbdb6cdb80402() 117 std::thread([&instance]() { instance.DetectMultiThreadingCalls(); }).join();
118 EXPECT_NE(curThreadId, instance.curThreadId_);
119 }
120 } // namespace Rosen
121 } // namespace OHOS
122