• 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 <mutex>
17 #include <unistd.h>
18 #include <vector>
19 #include "native_vsync.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 
24 namespace OHOS::Rosen {
25 namespace {
26 std::mutex g_mutex;
27 int g_counter = 0;
28 std::vector<int> datas;
OnVSync(long long time,void * data)29 static void OnVSync(long long time, void *data)
30 {
31     std::unique_lock<std::mutex> locker(g_mutex);
32     if (!data) {
33         return;
34     }
35     vector<int>::iterator it = find(datas.begin(), datas.end(), *(int *)data);
36     if (it != datas.end()) {
37         datas.erase(it);
38     }
39     delete (int *)data;
40 }
41 }
42 class NativeVSyncMultCallbackTest : public testing::Test {
43 public:
44     void TestMultiTimes(int times);
45 };
46 
TestMultiTimes(int times)47 void NativeVSyncMultCallbackTest::TestMultiTimes(int times)
48 {
49     char name[] = "TestMultiTimes";
50     OH_NativeVSync *native_vsync = OH_NativeVSync_Create(name, sizeof(name));
51     for (int i = 0; i < times; i++) {
52         std::unique_lock<std::mutex> locker(g_mutex);
53         int *userData = new int(g_counter++);
54         OH_NativeVSync_FrameCallback callback = OnVSync;
55         int ret = OH_NativeVSync_RequestFrameWithMultiCallback(native_vsync, callback, userData);
56         if (ret == 0) {
57             datas.push_back(*userData);
58         }
59         usleep(1); // 1us
60     }
61     usleep(200000); // 200000us
62     ASSERT_EQ(datas.size(), 0);
63 }
64 
65 /*
66 * Function: OH_NativeVSync_RequestFrameWithMultiCallback
67 * Type: Function
68 * Rank: Important(2)
69 * EnvConditions: N/A
70 * CaseDescription: RequestFrameWithMultiCallback
71  */
72 HWTEST_F(NativeVSyncMultCallbackTest, RequestFrameWithMultiCallback_1_time, Function | MediumTest | Level2)
73 {
74     TestMultiTimes(1); // test 1 time
75 }
76 
77 /*
78 * Function: OH_NativeVSync_RequestFrameWithMultiCallback
79 * Type: Function
80 * Rank: Important(2)
81 * EnvConditions: N/A
82 * CaseDescription: RequestFrameWithMultiCallback
83  */
84 HWTEST_F(NativeVSyncMultCallbackTest, RequestFrameWithMultiCallback_3_times, Function | MediumTest | Level2)
85 {
86     TestMultiTimes(3); // test 3 times
87 }
88 
89 /*
90 * Function: OH_NativeVSync_RequestFrameWithMultiCallback
91 * Type: Function
92 * Rank: Important(2)
93 * EnvConditions: N/A
94 * CaseDescription: RequestFrameWithMultiCallback
95  */
96 HWTEST_F(NativeVSyncMultCallbackTest, RequestFrameWithMultiCallback_10_times, Function | MediumTest | Level2)
97 {
98     TestMultiTimes(10); // test 10 times
99 }
100 
101 /*
102 * Function: OH_NativeVSync_RequestFrameWithMultiCallback
103 * Type: Function
104 * Rank: Important(2)
105 * EnvConditions: N/A
106 * CaseDescription: RequestFrameWithMultiCallback
107  */
108 HWTEST_F(NativeVSyncMultCallbackTest, RequestFrameWithMultiCallback_100_times, Function | MediumTest | Level2)
109 {
110     TestMultiTimes(100); // test 100 times
111 }
112 }
113