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 {
__anon620da0c00102() 62 std::function<void()> func = []() -> void {};
63 HgmTaskHandleThread::Instance().PostTask(func);
64 int64_t delayTime = 1;
65 HgmTaskHandleThread::Instance().PostTask(func, delayTime);
66 EXPECT_TRUE(HgmTaskHandleThread::Instance().handler_);
67 }
68
69 /*
70 * @tc.name: PostTask002
71 * @tc.desc: Test PostTask
72 * @tc.type: FUNC
73 * @tc.require:
74 */
75 HWTEST_F(HgmTaskHandleThreadTest, PostTask002, TestSize.Level1)
76 {
77 HgmTaskHandleThread& instance = HgmTaskHandleThread::Instance();
78 int count = 0;
__anon620da0c00202()79 instance.PostTask([&count](){ count++; }, 0);
80 std::this_thread::sleep_for(std::chrono::milliseconds(100));
81 EXPECT_EQ(count, 1);
82 }
83
84 /*
85 * @tc.name: PostTask003
86 * @tc.desc: Test PostTask
87 * @tc.type: FUNC
88 * @tc.require:
89 */
90 HWTEST_F(HgmTaskHandleThreadTest, PostTask003, TestSize.Level1)
91 {
92 HgmTaskHandleThread& instance = HgmTaskHandleThread::Instance();
93 int count = 0;
94 const int count1 = 1;
95 const int time = 500;
96 const int time1 = 1000;
97 auto start_time = std::chrono::high_resolution_clock::now();
__anon620da0c00302()98 instance.PostTask([&count](){ count++; }, time1);
99 std::this_thread::sleep_for(std::chrono::milliseconds(time));
100 std::this_thread::sleep_for(std::chrono::milliseconds(time1));
101 auto end_time = std::chrono::high_resolution_clock::now();
102 EXPECT_EQ(count, count1);
103 EXPECT_GE(std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count(), time1);
104 }
105
106 /*
107 * @tc.name: DetectMultiThreadingCalls
108 * @tc.desc: Test DetectMultiThreadingCalls
109 * @tc.type: FUNC
110 * @tc.require:IB3MVN
111 */
112 HWTEST_F(HgmTaskHandleThreadTest, DetectMultiThreadingCalls, TestSize.Level1)
113 {
114 HgmTaskHandleThread& instance = HgmTaskHandleThread::Instance();
115 instance.DetectMultiThreadingCalls();
116 int32_t curThreadId = instance.curThreadId_;
117 instance.DetectMultiThreadingCalls();
118 EXPECT_EQ(curThreadId, instance.curThreadId_);
__anon620da0c00402() 119 std::thread([&instance]() { instance.DetectMultiThreadingCalls(); }).join();
120 EXPECT_NE(curThreadId, instance.curThreadId_);
121 }
122 } // namespace Rosen
123 } // namespace OHOS
124