1 /*
2 * Copyright (c) 2024 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 <chrono>
16 #include <thread>
17 #include <unistd.h>
18 #include <condition_variable>
19 #include <gtest/gtest.h>
20 #include <iservice_registry.h>
21 #include "vsync_receiver.h"
22 #include "vsync_controller.h"
23 #include "vsync_sampler.h"
24 #include "vsync_generator.h"
25 #include "vsync_distributor.h"
26 #include "accesstoken_kit.h"
27 #ifdef SUPPORT_ACCESS_TOKEN
28 #include "nativetoken_kit.h"
29 #include "token_setproc.h"
30 #endif
31
32 #include <iostream>
33
34 using namespace testing;
35 using namespace testing::ext;
36
37 namespace OHOS::Rosen {
38 namespace {
39 int64_t onVsyncPeriod = 0;
40 int64_t lastTimeStamp = 0;
41 constexpr int32_t SAMPLER_NUMBER = 6;
OnVSync(int64_t time,void * data)42 static void OnVSync(int64_t time, void *data)
43 {
44 onVsyncPeriod = time - lastTimeStamp;
45 lastTimeStamp = time;
46 }
47 }
48 class SetVsyncRateTest : public testing::Test {
49 public:
50 void Process1();
51 void Process2();
52
53 sptr<VSyncSampler> vsyncSampler = nullptr;
54 sptr<VSyncGenerator> vsyncGenerator = nullptr;
55 sptr<VSyncController> vsyncController = nullptr;
56 sptr<VSyncDistributor> vsyncDistributor = nullptr;
57 sptr<VSyncConnection> vsyncConnection = nullptr;
58
59 static inline pid_t pid = 0;
60 static inline int pipeFd[2] = {};
61 static inline int pipe1Fd[2] = {};
62 static inline int32_t systemAbilityID = 345135;
63 };
64
InitNativeTokenInfo()65 static void InitNativeTokenInfo()
66 {
67 #ifdef SUPPORT_ACCESS_TOKEN
68 uint64_t tokenId;
69 const char *perms[2];
70 perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
71 perms[1] = "ohos.permission.CAMERA";
72 NativeTokenInfoParams infoInstance = {
73 .dcapsNum = 0,
74 .permsNum = 2,
75 .aclsNum = 0,
76 .dcaps = NULL,
77 .perms = perms,
78 .acls = NULL,
79 .processName = "dcamera_client_demo",
80 .aplStr = "system_basic",
81 };
82 tokenId = GetAccessTokenId(&infoInstance);
83 SetSelfTokenID(tokenId);
84 int32_t ret = Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
85 ASSERT_EQ(ret, Security::AccessToken::RET_SUCCESS);
86 std::this_thread::sleep_for(std::chrono::milliseconds(50)); // wait 50ms
87 #endif
88 }
89
Process1()90 void SetVsyncRateTest::Process1()
91 {
92 #ifdef SUPPORT_ACCESS_TOKEN
93 InitNativeTokenInfo();
94 vsyncGenerator = CreateVSyncGenerator();
95 vsyncSampler = CreateVSyncSampler();
96 int32_t count = 0;
97 int64_t timestamp = 8333333; // 8333333ns
98 while (count <= SAMPLER_NUMBER) {
99 vsyncSampler->AddSample(timestamp);
100 usleep(1000); // 1000us
101 timestamp += 8333333; // 8333333ns
102 count++;
103 }
104 vsyncController = new VSyncController(vsyncGenerator, 0);
105 vsyncDistributor = new VSyncDistributor(vsyncController, "test");
106 vsyncConnection = new VSyncConnection(vsyncDistributor, "test");
107 vsyncDistributor->AddConnection(vsyncConnection);
108 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
109 sam->AddSystemAbility(systemAbilityID, vsyncConnection);
110 close(pipeFd[1]);
111 close(pipe1Fd[0]);
112 char buf[10] = "start";
113 write(pipe1Fd[1], buf, sizeof(buf));
114 read(pipeFd[0], buf, sizeof(buf));
115 sam->RemoveSystemAbility(systemAbilityID);
116 close(pipeFd[0]);
117 close(pipe1Fd[1]);
118 exit(0);
119 #endif
120 }
121
Process2()122 void SetVsyncRateTest::Process2()
123 {
124 close(pipeFd[0]);
125 close(pipe1Fd[1]);
126 char buf[10];
127 read(pipe1Fd[0], buf, sizeof(buf));
128 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
129 auto robj = sam->GetSystemAbility(systemAbilityID);
130 std::cout << "(robj==nullptr):" << (robj==nullptr) << std::endl;
131 auto conn = iface_cast<IVSyncConnection>(robj);
132 sptr<VSyncReceiver> receiver = new VSyncReceiver(conn);
133 receiver->Init();
134 VSyncReceiver::FrameCallback fcb = {
135 .userData_ = nullptr,
136 .callback_ = OnVSync,
137 };
138 receiver->SetVSyncRate(fcb, 1); // set rate 1
139 usleep(200000); // 200000us
140 EXPECT_GT(onVsyncPeriod, 8000000); // 8000000ns
141 EXPECT_LT(onVsyncPeriod, 9000000); // 9000000ns
142 receiver->SetVSyncRate(fcb, 2); // set rate 2
143 usleep(200000); // 200000us
144 EXPECT_GT(onVsyncPeriod, 16000000); // 16000000ns
145 EXPECT_LT(onVsyncPeriod, 18000000); // 18000000ns
146 receiver->SetVSyncRate(fcb, 3); // set rate 3
147 usleep(200000); // 200000us
148 EXPECT_GT(onVsyncPeriod, 24000000); // 24000000ns
149 EXPECT_LT(onVsyncPeriod, 26000000); // 26000000ns
150 int64_t period;
151 int64_t timeStamp;
152 EXPECT_EQ(receiver->GetVSyncPeriodAndLastTimeStamp(period, timeStamp), VSYNC_ERROR_OK);
153 }
154
155 /*
156 * Function: RequestNextSetVsyncRateTest
157 * Type: Function
158 * Rank: Important(2)
159 * EnvConditions: N/A
160 * CaseDescription: RequestNextSetVsyncRateTest
161 */
162 HWTEST_F(SetVsyncRateTest, RequestNextSetVsyncRateTest, Function | MediumTest | Level2)
163 {
164 if (pipe(pipeFd) < 0) {
165 exit(1);
166 }
167 if (pipe(pipe1Fd) < 0) {
168 exit(0);
169 }
170 pid = fork();
171 if (pid < 0) {
172 exit(1);
173 }
174 if (pid != 0) {
175 Process1();
176 } else {
177 Process2();
178 }
179 }
180 }
181