1 /*
2 * Copyright (c) 2022 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 <thread>
18
19 #include "perform_reporter.h"
20 #include "window_manager_hilog.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace {
28 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "PerformReporterTest"};
29 }
30 class PerformReporterTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 virtual void SetUp() override;
35 virtual void TearDown() override;
36 void SimuReportProcess(PerformReporter& pr, const std::vector<uint32_t>& durations);
37 bool PerformDataCmp(const PerformReporter& pr, const uint32_t totalCount, const std::vector<uint32_t>& splitCount);
38 };
39
SetUpTestCase()40 void PerformReporterTest::SetUpTestCase()
41 {
42 }
43
TearDownTestCase()44 void PerformReporterTest::TearDownTestCase()
45 {
46 }
47
SetUp()48 void PerformReporterTest::SetUp()
49 {
50 }
51
TearDown()52 void PerformReporterTest::TearDown()
53 {
54 }
55
SimuReportProcess(PerformReporter & pr,const std::vector<uint32_t> & durations)56 void PerformReporterTest::SimuReportProcess(PerformReporter& pr, const std::vector<uint32_t>& durations)
57 {
58 for (auto duration : durations) {
59 pr.start();
60 std::this_thread::sleep_for(std::chrono::milliseconds(duration));
61 pr.end();
62 }
63 }
64
PerformDataCmp(const PerformReporter & pr,const uint32_t totalCount,const std::vector<uint32_t> & splitCount)65 bool PerformReporterTest::PerformDataCmp(const PerformReporter& pr,
66 const uint32_t totalCount, const std::vector<uint32_t>& splitCount)
67 {
68 if (pr.totalCount_ != totalCount) {
69 WLOGFE("pr.totalCount_=%{public}u, expect=%{public}u", pr.totalCount_.load(), totalCount);
70 return false;
71 }
72
73 size_t i = 0;
74 for (auto& iter: pr.timeSplitCount_) {
75 if (iter.second != splitCount[i]) {
76 std::ostringstream oss;
77 oss << "pr.timeSplitCount_[" << iter.first << "]=" << iter.second << ", but expect=" << splitCount[i];
78 WLOGFI("%{public}s", oss.str().c_str());
79 return false;
80 }
81 i++;
82 }
83
84 return true;
85 }
86
87 namespace {
88 /**
89 * @tc.name: StartEnd
90 * @tc.desc: StartEnd test
91 * @tc.type: FUNC
92 */
93 HWTEST_F(PerformReporterTest, StartEnd, Function | SmallTest | Level2)
94 {
95 PerformReporter pr = PerformReporter("TestTag", {100, 200, 300}, 10);
96 SimuReportProcess(pr, {50, 150, 250, 350, 450});
97 ASSERT_EQ(true, PerformDataCmp(pr, 5, {1, 1, 1, 2}));
98 }
99
100 /**
101 * @tc.name: StartEndClear
102 * @tc.desc: StartEndClear test
103 * @tc.type: FUNC
104 */
105 HWTEST_F(PerformReporterTest, StartEndClear, Function | SmallTest | Level2)
106 {
107 PerformReporter pr = PerformReporter("TestTag", {100, 200, 300}, 3);
108 SimuReportProcess(pr, {50, 150, 250});
109 }
110
111 /**
112 * @tc.name: StartEndInvSeq
113 * @tc.desc: StartEndInvSeq test
114 * @tc.type: FUNC
115 */
116 HWTEST_F(PerformReporterTest, StartEndInvSeq, Function | SmallTest | Level2)
117 {
118 PerformReporter pr = PerformReporter("TestTag", {100, 200, 300}, 4);
119 SimuReportProcess(pr, {250, 150, 50});
120 }
121
122 /**
123 * @tc.name: PrivateClear
124 * @tc.desc: PrivateClear test
125 * @tc.type: FUNC
126 */
127 HWTEST_F(PerformReporterTest, PrivateClear, Function | SmallTest | Level2)
128 {
129 PerformReporter pr = PerformReporter("TestTag", {100, 200, 300}, 10);
130 SimuReportProcess(pr, {50, 150, 250, 350, 450});
131
132 pr.clear();
133 }
134 }
135 } // namespace Rosen
136 } // namespace OHOS