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 }
79
80 /**
81 * @tc.name: FlowbufferAdapterImplTest_002.
82 * @tc.desc: test FlowbufferAdapterImpl CreateAshmemWithFd.
83 * @tc.type: FUNC.
84 * @tc.require:
85 */
86 HWTEST_F(FlowbufferAdapterImplTest, FlowbufferAdapterImplTest_003, TestSize.Level1)
87 {
88 auto flowbufferAdapter = OhosAdapterHelper::GetInstance().CreateFlowbufferAdapter();
89 EXPECT_TRUE(flowbufferAdapter != nullptr);
90 int fd;
91 size_t scriptLength = 10;
92 flowbufferAdapter->CreateAshmem(scriptLength, PROT_READ | PROT_WRITE, fd);
93 auto ashmem = flowbufferAdapter->CreateAshmemWithFd(fd, scriptLength, PROT_READ);
94 EXPECT_TRUE(ashmem != nullptr);
95 }
96
97 /**
98 * @tc.name: FlowbufferAdapterImplTest_004.
99 * @tc.desc: test FlowbufferAdapterImpl NeedReportScene.
100 * @tc.type: FUNC.
101 * @tc.require:
102 */
103 HWTEST_F(FlowbufferAdapterImplTest, FlowbufferAdapterImplTest_004, TestSize.Level1)
104 {
105 auto flowbufferAdapter = std::make_shared<FlowbufferAdapterImpl>();
106 bool needReportScene = flowbufferAdapter->NeedReportScene();
107 EXPECT_FALSE(needReportScene);
108 std::this_thread::sleep_for(std::chrono::milliseconds(PERFORMANCE_PERIOD_MS));
109 needReportScene = flowbufferAdapter->NeedReportScene();
110 EXPECT_TRUE(needReportScene);
111 }
112
113 /**
114 * @tc.name: FlowbufferAdapterImplTest_005.
115 * @tc.desc: test FlowbufferAdapterImpl StartPerformanceBoost.
116 * @tc.type: FUNC.
117 * @tc.require:
118 */
119 HWTEST_F(FlowbufferAdapterImplTest, FlowbufferAdapterImplTest_005, TestSize.Level1)
120 {
121 auto flowbufferAdapter = std::make_shared<FlowbufferAdapterImpl>();
122 bool needReportScene = flowbufferAdapter->NeedReportScene();
123 flowbufferAdapter->StartPerformanceBoost();
124 std::this_thread::sleep_for(std::chrono::milliseconds(PERFORMANCE_PERIOD_MS));
125 flowbufferAdapter->StartPerformanceBoost();
126 EXPECT_FALSE(needReportScene);
127 }
128
129 /**
130 * @tc.name: FlowbufferAdapterImplTest_006.
131 * @tc.desc: test FlowbufferAdapterImpl CreateAshmem.
132 * @tc.type: FUNC.
133 * @tc.require:
134 */
135 HWTEST_F(FlowbufferAdapterImplTest, FlowbufferAdapterImplTest_006, TestSize.Level1)
136 {
137 int fd = -1;
138 size_t scriptLength = 10;
139 auto flowbufferAdapter = std::make_shared<FlowbufferAdapterImpl>();
140 flowbufferAdapter->CreateAshmem(scriptLength, PROT_READ | PROT_WRITE, fd);
141 fd = 1;
142 flowbufferAdapter->CreateAshmem(scriptLength, PROT_READ | PROT_WRITE, fd);
143 scriptLength = 1024;
144 auto ashmem = flowbufferAdapter->CreateAshmem(scriptLength, PROT_READ | PROT_WRITE, fd);
145 EXPECT_TRUE(ashmem != nullptr);
146 }
147
148 /**
149 * @tc.name: FlowbufferAdapterImplTest_007.
150 * @tc.desc: test FlowbufferAdapterImpl CreateAshmemWithFd.
151 * @tc.type: FUNC.
152 * @tc.require:
153 */
154 HWTEST_F(FlowbufferAdapterImplTest, FlowbufferAdapterImplTest_007, TestSize.Level1)
155 {
156 int fd = -1;
157 size_t scriptLength = 10;
158 auto flowbufferAdapter = std::make_shared<FlowbufferAdapterImpl>();
159 flowbufferAdapter->CreateAshmemWithFd(fd, scriptLength, PROT_READ);
160 fd = 1;
161 flowbufferAdapter->CreateAshmemWithFd(fd, scriptLength, PROT_READ);
162 scriptLength = 1024;
163 auto ashmem = flowbufferAdapter->CreateAshmemWithFd(fd, scriptLength, PROT_READ);
164 EXPECT_TRUE(ashmem == nullptr);
165 }
166 } // namespace NWeb
167 } // namespace OHOS
168