1 /*
2  * Copyright (c) 2021 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 #include <gtest/gtest.h>
16 #include <vsync_controller.h>
17 
18 using namespace testing;
19 using namespace testing::ext;
20 
21 namespace OHOS::Rosen {
22 class VSyncControllerTest : public testing::Test {
23 public:
24     static void SetUpTestCase();
25     static void TearDownTestCase();
26     static inline sptr<VSyncController> vsyncController_ = nullptr;
27     static inline sptr<VSyncGenerator> vsyncGenerator_ = nullptr;
28     static constexpr const int32_t WAIT_SYSTEM_ABILITY_REPORT_DATA_SECONDS = 5;
29 };
30 
31 class VSyncControllerCallback : public VSyncController::Callback {
32 public:
33     void OnVSyncEvent(int64_t now, int64_t period, uint32_t refreshRate, VSyncMode vsyncMode) override;
34     void OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t, uint32_t>> &refreshRates) override;
35 };
36 
SetUpTestCase()37 void VSyncControllerTest::SetUpTestCase()
38 {
39     vsyncGenerator_ = CreateVSyncGenerator();
40     vsyncController_ = new VSyncController(vsyncGenerator_, 0);
41 }
42 
TearDownTestCase()43 void VSyncControllerTest::TearDownTestCase()
44 {
45     sleep(WAIT_SYSTEM_ABILITY_REPORT_DATA_SECONDS);
46     vsyncGenerator_ = nullptr;
47     DestroyVSyncGenerator();
48     vsyncController_ = nullptr;
49 }
50 
OnVSyncEvent(int64_t now,int64_t period,uint32_t refreshRate,VSyncMode vsyncMode)51 void VSyncControllerCallback::OnVSyncEvent(int64_t now, int64_t period, uint32_t refreshRate, VSyncMode vsyncMode) {}
52 
OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t,uint32_t>> & refreshRates)53 void VSyncControllerCallback::OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t, uint32_t>> &refreshRates)
54 {
55 }
56 
57 /*
58 * Function: SetEnable
59 * Type: Function
60 * Rank: Important(2)
61 * EnvConditions: N/A
62 * CaseDescription: 1. call SetEnable
63  */
64 HWTEST_F(VSyncControllerTest, SetEnable001, Function | MediumTest | Level2)
65 {
66     bool isGeneratorEnable = false;
67     ASSERT_EQ(VSyncControllerTest::vsyncController_->SetEnable(true, isGeneratorEnable), VSYNC_ERROR_OK);
68 }
69 
70 /*
71 * Function: SetCallback001
72 * Type: Function
73 * Rank: Important(2)
74 * EnvConditions: N/A
75 * CaseDescription: 1. generate a VSyncControllerCallback obj
76 *                  2. call SetCallback
77  */
78 HWTEST_F(VSyncControllerTest, SetCallback001, Function | MediumTest | Level2)
79 {
80     VSyncControllerCallback* callback = new VSyncControllerCallback;
81     ASSERT_EQ(VSyncControllerTest::vsyncController_->SetCallback(callback), VSYNC_ERROR_OK);
82     delete callback;
83 }
84 
85 /*
86 * Function: SetCallback002
87 * Type: Function
88 * Rank: Important(2)
89 * EnvConditions: N/A
90 * CaseDescription: 1. call SetCallback with nullptr
91  */
92 HWTEST_F(VSyncControllerTest, SetCallback002, Function | MediumTest | Level2)
93 {
94     ASSERT_EQ(VSyncControllerTest::vsyncController_->SetCallback(nullptr), VSYNC_ERROR_NULLPTR);
95 }
96 
97 /*
98 * Function: SetPhaseOffset001
99 * Type: Function
100 * Rank: Important(2)
101 * EnvConditions: N/A
102 * CaseDescription: 1. call SetPhaseOffset
103  */
104 HWTEST_F(VSyncControllerTest, SetPhaseOffset001, Function | MediumTest | Level2)
105 {
106     ASSERT_EQ(VSyncControllerTest::vsyncController_->SetPhaseOffset(2), VSYNC_ERROR_OK);
107 }
108 
109 /*
110 * Function: SetEnable002
111 * Type: Function
112 * Rank: Important(2)
113 * EnvConditions: N/A
114 * CaseDescription: 1. call SetEnable
115  */
116 HWTEST_F(VSyncControllerTest, SetEnable002, Function | MediumTest | Level2)
117 {
118     bool isGeneratorEnable = false;
119     ASSERT_EQ(VSyncControllerTest::vsyncController_->SetEnable(false, isGeneratorEnable), VSYNC_ERROR_OK);
120 }
121 
122 /*
123 * Function: SetPhaseOffset002
124 * Type: Function
125 * Rank: Important(2)
126 * EnvConditions: N/A
127 * CaseDescription: 1. call SetPhaseOffset
128  */
129 HWTEST_F(VSyncControllerTest, SetPhaseOffset002, Function | MediumTest | Level2)
130 {
131     ASSERT_EQ(VSyncControllerTest::vsyncController_->SetPhaseOffset(2), VSYNC_ERROR_INVALID_OPERATING);
132 }
133 }
134