• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <optional>
17 #include <string>
18 #include <vector>
19 
20 #include "gtest/gtest.h"
21 
22 #define private public
23 #define protected public
24 #include "core/components_ng/animation/callback_thread_wrapper.h"
25 #include "test/mock/base/mock_task_executor.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 
30 namespace OHOS::Ace::NG {
31 namespace {
32 RefPtr<TaskExecutor> taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>();
33 } // namespace
34 
35 class CallbackThreadWrapperTest : public testing::Test {
36 public:
SetUpTestSuite()37     static void SetUpTestSuite() {}
TearDownTestSuite()38     static void TearDownTestSuite() {}
39 };
40 
41 /**
42  * @tc.name: CallbackThreadWrapperTest001
43  * @tc.desc: Test once is true
44  * @tc.type: FUNC
45  */
46 HWTEST_F(CallbackThreadWrapperTest, CallbackThreadWrapperTest001, TestSize.Level1)
47 {
48     /**
49      * @tc.steps: step1. Create CallbackThreadWrapper whose once param is true.
50      */
51     int32_t cnt = 0;
__anon175ff9420202() 52     std::function func = [&cnt]() {
53         cnt++;
54     };
55     CallbackThreadWrapper wrapper1(taskExecutor_, func, true);
56 
57     /**
58      * @tc.steps: step2. Execute () function for the first time.
59      * @tc.expected: The wrapped function will be executed.
60      */
61     wrapper1();
62     ASSERT_EQ(cnt, 1);
63 
64     /**
65      * @tc.steps: step3. Execute () function for the second time.
66      * @tc.expected: The wrapped function will not be executed, so the number keeps same.
67      */
68     wrapper1();
69     ASSERT_EQ(cnt, 1);
70 }
71 
72 /**
73  * @tc.name: CallbackThreadWrapperTest002
74  * @tc.desc: Test once is false
75  * @tc.type: FUNC
76  */
77 HWTEST_F(CallbackThreadWrapperTest, CallbackThreadWrapperTest002, TestSize.Level1)
78 {
79     /**
80      * @tc.steps: step1. Create CallbackThreadWrapper whose once param is false.
81      */
82     int32_t cnt = 0;
__anon175ff9420302() 83     std::function func = [&cnt]() {
84         cnt++;
85     };
86     CallbackThreadWrapper wrapper1(taskExecutor_, func, false);
87 
88     /**
89      * @tc.steps: step2. Execute () function for the first time.
90      * @tc.expected: The wrapped function will be executed.
91      */
92     wrapper1();
93     ASSERT_EQ(cnt, 1);
94 
95     /**
96      * @tc.steps: step3. Execute () function for four times again.
97      * @tc.expected: The wrapped function will be executed for four times.
98      */
99     constexpr int32_t repeatTimes = 4;
100     for (int32_t i = 0; i != repeatTimes; ++i) {
101         wrapper1();
102     }
103     ASSERT_EQ(cnt, 1 + repeatTimes);
104 }
105 
106 /**
107  * @tc.name: CallbackThreadWrapperTest003
108  * @tc.desc: Test the struct destruction
109  * @tc.type: FUNC
110  */
111 HWTEST_F(CallbackThreadWrapperTest, CallbackThreadWrapperTest003, TestSize.Level1)
112 {
113     int32_t cnt = 0;
__anon175ff9420402() 114     std::function func = [&cnt]() {
115         cnt++;
116     };
117     /**
118      * @tc.steps: step1. Create CallbackThreadWrapper and destruct it.
119      */
120     {
121         CallbackThreadWrapper wrapper1(taskExecutor_, func, false);
122         CallbackThreadWrapper wrapper2(taskExecutor_, func, true);
123     }
124     /**
125      * @tc.steps: step2. Test the result.
126      * @tc.expected: The wrapped function will not be executed when it destructs.
127      */
128     ASSERT_EQ(cnt, 0);
129 }
130 } // namespace OHOS::Ace::NG
131