• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
16 #include <unistd.h>
17 #include <vector>
18 #include "native_vsync.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 
23 namespace OHOS::Rosen {
24 namespace {
25 int g_counter = 0;
26 int g_lastData = 0;
OnVSync(long long time,void * data)27 static void OnVSync(long long time, void *data)
28 {
29     if (!data) {
30         return;
31     }
32     g_lastData = *(int *)data;
33 }
34 }
35 class NativeVSyncRequestFrameTest : public testing::Test {
36 public:
37     void TestMultiTimes(int times);
38 };
39 
TestMultiTimes(int times)40 void NativeVSyncRequestFrameTest::TestMultiTimes(int times)
41 {
42     char name[] = "TestMultiTimes";
43     OH_NativeVSync *native_vsync = OH_NativeVSync_Create(name, sizeof(name));
44     int lastUserData = 0;
45     for (int i = 0; i < times; i++) {
46         int *userData = new int(g_counter++);
47         OH_NativeVSync_FrameCallback callback = OnVSync;
48         OH_NativeVSync_RequestFrame(native_vsync, callback, userData);
49         lastUserData = *userData;
50     }
51     usleep(200000); // 200000us
52     ASSERT_EQ(g_lastData, lastUserData);
53 }
54 
55 /*
56 * Function: OH_NativeVSync_RequestFrame
57 * Type: Function
58 * Rank: Important(2)
59 * EnvConditions: N/A
60 * CaseDescription: OH_NativeVSync_RequestFrame
61  */
62 HWTEST_F(NativeVSyncRequestFrameTest, OH_NativeVSync_RequestFrame_1_time, Function | MediumTest | Level2)
63 {
64     TestMultiTimes(1); // test 1 time
65 }
66 
67 /*
68 * Function: OH_NativeVSync_RequestFrame
69 * Type: Function
70 * Rank: Important(2)
71 * EnvConditions: N/A
72 * CaseDescription: OH_NativeVSync_RequestFrame
73  */
74 HWTEST_F(NativeVSyncRequestFrameTest, OH_NativeVSync_RequestFrame_3_times, Function | MediumTest | Level2)
75 {
76     TestMultiTimes(3); // test 3 times
77 }
78 
79 /*
80 * Function: OH_NativeVSync_RequestFrame
81 * Type: Function
82 * Rank: Important(2)
83 * EnvConditions: N/A
84 * CaseDescription: OH_NativeVSync_RequestFrame
85  */
86 HWTEST_F(NativeVSyncRequestFrameTest, OH_NativeVSync_RequestFrame_10_times, Function | MediumTest | Level2)
87 {
88     TestMultiTimes(10); // test 10 times
89 }
90 }
91