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
16 #include <gtest/gtest.h>
17 #include <sys/mman.h>
18
19 #include "ohos_adapter_helper.h"
20
21 #define private public
22 #include "flowbuffer_adapter_impl.h"
23 #include <chrono>
24 #include <thread>
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace NWeb {
31 namespace {
32 const int64_t PERFORMANCE_PERIOD_MS = 300;
33 }
34
35 class FlowbufferAdapterImplTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp();
40 void TearDown();
41 };
42
SetUpTestCase()43 void FlowbufferAdapterImplTest::SetUpTestCase() {}
44
TearDownTestCase()45 void FlowbufferAdapterImplTest::TearDownTestCase() {}
46
SetUp()47 void FlowbufferAdapterImplTest::SetUp() {}
48
TearDown()49 void FlowbufferAdapterImplTest::TearDown() {}
50
51 /**
52 * @tc.name: FlowbufferAdapterImplTest_001.
53 * @tc.desc: test FlowbufferAdapterImpl StartPerformanceBoost.
54 * @tc.type: FUNC.
55 * @tc.require:
56 */
57 HWTEST_F(FlowbufferAdapterImplTest, FlowbufferAdapterImplTest_001, TestSize.Level1)
58 {
59 auto flowbufferAdapter = OhosAdapterHelper::GetInstance().CreateFlowbufferAdapter();
60 EXPECT_TRUE(flowbufferAdapter != nullptr);
61 flowbufferAdapter->StartPerformanceBoost();
62 }
63
64 /**
65 * @tc.name: FlowbufferAdapterImplTest_002.
66 * @tc.desc: test FlowbufferAdapterImpl CreateAshmem.
67 * @tc.type: FUNC.
68 * @tc.require:
69 */
70 HWTEST_F(FlowbufferAdapterImplTest, FlowbufferAdapterImplTest_002, TestSize.Level1)
71 {
72 auto flowbufferAdapter = OhosAdapterHelper::GetInstance().CreateFlowbufferAdapter();
73 EXPECT_TRUE(flowbufferAdapter != nullptr);
74 int fd;
75 size_t scriptLength = 10;
76 auto ashmem = flowbufferAdapter->CreateAshmem(scriptLength, PROT_READ | PROT_WRITE, fd);
77 EXPECT_TRUE(ashmem != nullptr);
78 ashmem = flowbufferAdapter->CreateAshmem(-1, PROT_READ | PROT_WRITE, fd);
79 EXPECT_EQ(ashmem, nullptr);
80 }
81
82 /**
83 * @tc.name: FlowbufferAdapterImplTest_002.
84 * @tc.desc: test FlowbufferAdapterImpl CreateAshmemWithFd.
85 * @tc.type: FUNC.
86 * @tc.require:
87 */
88 HWTEST_F(FlowbufferAdapterImplTest, FlowbufferAdapterImplTest_003, TestSize.Level1)
89 {
90 auto flowbufferAdapter = OhosAdapterHelper::GetInstance().CreateFlowbufferAdapter();
91 EXPECT_TRUE(flowbufferAdapter != nullptr);
92 int fd;
93 size_t scriptLength = 10;
94 flowbufferAdapter->CreateAshmem(scriptLength, PROT_READ | PROT_WRITE, fd);
95 auto ashmem = flowbufferAdapter->CreateAshmemWithFd(fd, scriptLength, PROT_READ);
96 EXPECT_TRUE(ashmem != nullptr);
97 ashmem = flowbufferAdapter->CreateAshmemWithFd(fd, INT_MAX, PROT_READ);
98 EXPECT_EQ(ashmem, nullptr);
99 ashmem = flowbufferAdapter->CreateAshmemWithFd(-1, scriptLength, PROT_READ);
100 EXPECT_EQ(ashmem, nullptr);
101 }
102
103 /**
104 * @tc.name: FlowbufferAdapterImplTest_004.
105 * @tc.desc: test FlowbufferAdapterImpl NeedReportScene.
106 * @tc.type: FUNC.
107 * @tc.require:
108 */
109 HWTEST_F(FlowbufferAdapterImplTest, FlowbufferAdapterImplTest_004, TestSize.Level1)
110 {
111 auto flowbufferAdapter = std::make_shared<FlowbufferAdapterImpl>();
112 bool needReportScene = flowbufferAdapter->NeedReportScene();
113 EXPECT_FALSE(needReportScene);
114 std::this_thread::sleep_for(std::chrono::milliseconds(PERFORMANCE_PERIOD_MS));
115 needReportScene = flowbufferAdapter->NeedReportScene();
116 EXPECT_TRUE(needReportScene);
117 }
118
119 /**
120 * @tc.name: FlowbufferAdapterImplTest_005.
121 * @tc.desc: test FlowbufferAdapterImpl StartPerformanceBoost.
122 * @tc.type: FUNC.
123 * @tc.require:
124 */
125 HWTEST_F(FlowbufferAdapterImplTest, FlowbufferAdapterImplTest_005, TestSize.Level1)
126 {
127 auto flowbufferAdapter = std::make_shared<FlowbufferAdapterImpl>();
128 bool needReportScene = flowbufferAdapter->NeedReportScene();
129 flowbufferAdapter->StartPerformanceBoost();
130 std::this_thread::sleep_for(std::chrono::milliseconds(PERFORMANCE_PERIOD_MS));
131 flowbufferAdapter->StartPerformanceBoost();
132 EXPECT_FALSE(needReportScene);
133 }
134
135 /**
136 * @tc.name: FlowbufferAdapterImplTest_006.
137 * @tc.desc: test FlowbufferAdapterImpl CreateAshmem.
138 * @tc.type: FUNC.
139 * @tc.require:
140 */
141 HWTEST_F(FlowbufferAdapterImplTest, FlowbufferAdapterImplTest_006, TestSize.Level1)
142 {
143 int fd = -1;
144 size_t scriptLength = 10;
145 auto flowbufferAdapter = std::make_shared<FlowbufferAdapterImpl>();
146 flowbufferAdapter->CreateAshmem(scriptLength, PROT_READ | PROT_WRITE, fd);
147 fd = 1;
148 flowbufferAdapter->CreateAshmem(scriptLength, PROT_READ | PROT_WRITE, fd);
149 scriptLength = 1024;
150 auto ashmem = flowbufferAdapter->CreateAshmem(scriptLength, PROT_READ | PROT_WRITE, fd);
151 EXPECT_TRUE(ashmem != nullptr);
152 }
153
154 /**
155 * @tc.name: FlowbufferAdapterImplTest_007.
156 * @tc.desc: test FlowbufferAdapterImpl CreateAshmemWithFd.
157 * @tc.type: FUNC.
158 * @tc.require:
159 */
160 HWTEST_F(FlowbufferAdapterImplTest, FlowbufferAdapterImplTest_007, TestSize.Level1)
161 {
162 int fd = -1;
163 size_t scriptLength = 10;
164 auto flowbufferAdapter = std::make_shared<FlowbufferAdapterImpl>();
165 flowbufferAdapter->CreateAshmemWithFd(fd, scriptLength, PROT_READ);
166 fd = 1;
167 flowbufferAdapter->CreateAshmemWithFd(fd, scriptLength, PROT_READ);
168 scriptLength = 1024;
169 auto ashmem = flowbufferAdapter->CreateAshmemWithFd(fd, scriptLength, PROT_READ);
170 EXPECT_TRUE(ashmem == nullptr);
171 }
172 } // namespace NWeb
173 } // namespace OHOS
174