1 /*
2 * Copyright (c) 2023-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
18 #include "hiview_adapter.h"
19 #include "pasteboard_behaviour_reporter_impl.h"
20 #include "pasteboard_fault_impl.h"
21 #include "pasteboard_hilog.h"
22 #include "reporter.h"
23 #include "time_consuming_statistic_impl.h"
24
25 namespace OHOS::MiscServices {
26 using namespace testing::ext;
27 class DFXTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30 static void TearDownTestCase(void);
31 void SetUp();
32 void TearDown();
33 };
34
SetUpTestCase(void)35 void DFXTest::SetUpTestCase(void) { }
36
TearDownTestCase(void)37 void DFXTest::TearDownTestCase(void) { }
38
SetUp(void)39 void DFXTest::SetUp(void)
40 {
41 HiViewAdapter::StartTimerThread();
42 }
43
TearDown(void)44 void DFXTest::TearDown(void) { }
45
46 /**
47 * @tc.name: DFXTest001
48 * @tc.desc: test dfx report fault.
49 * @tc.type: FUNC
50 * @tc.require:
51 * @tc.author:
52 */
53 HWTEST_F(DFXTest, DFXTest001, TestSize.Level0)
54 {
55 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
56 PasteboardFaultMsg faultMsg = { .userId = 1, .errorCode = "error" };
57 auto status = Reporter::GetInstance().PasteboardFault().Report(faultMsg);
58 ASSERT_EQ(status, ReportStatus::SUCCESS);
59 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
60 }
61
62 /**
63 * @tc.name: DFXTest002
64 * @tc.desc: test dfx report time consuming statistic with SPS_COPY_STATE.
65 * @tc.type: FUNC
66 * @tc.require:
67 * @tc.author:
68 */
69 HWTEST_F(DFXTest, DFXTest002, TestSize.Level0)
70 {
71 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
72 TimeConsumingStat timeConsumingStat = { .pasteboardState = SPS_COPY_STATE, .dataSize = -1, .timeConsuming = -1 };
73 auto status = Reporter::GetInstance().TimeConsumingStatistic().Report(timeConsumingStat);
74 ASSERT_EQ(status, ReportStatus::SUCCESS);
75
76 timeConsumingStat = { .pasteboardState = SPS_COPY_STATE, .dataSize = DATA_LEVEL_ONE, .timeConsuming = -1 };
77 status = Reporter::GetInstance().TimeConsumingStatistic().Report(timeConsumingStat);
78 ASSERT_EQ(status, ReportStatus::SUCCESS);
79
80 timeConsumingStat = {
81 .pasteboardState = SPS_COPY_STATE, .dataSize = DATA_LEVEL_ONE, .timeConsuming = TCS_TIME_CONSUMING_LEVEL_ONE
82 };
83 status = Reporter::GetInstance().TimeConsumingStatistic().Report(timeConsumingStat);
84 ASSERT_EQ(status, ReportStatus::SUCCESS);
85 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
86 }
87
88 /**
89 * @tc.name: DFXTest003
90 * @tc.desc: test dfx report time consuming statistic with SPS_PASTE_STATE.
91 * @tc.type: FUNC
92 * @tc.require:
93 * @tc.author:
94 */
95 HWTEST_F(DFXTest, DFXTest003, TestSize.Level0)
96 {
97 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
98 TimeConsumingStat timeConsumingStat = { .pasteboardState = SPS_PASTE_STATE, .dataSize = -1, .timeConsuming = -1 };
99 auto status = Reporter::GetInstance().TimeConsumingStatistic().Report(timeConsumingStat);
100 ASSERT_EQ(status, ReportStatus::SUCCESS);
101
102 timeConsumingStat = { .pasteboardState = SPS_PASTE_STATE, .dataSize = DATA_LEVEL_ONE, .timeConsuming = -1 };
103 status = Reporter::GetInstance().TimeConsumingStatistic().Report(timeConsumingStat);
104 ASSERT_EQ(status, ReportStatus::SUCCESS);
105
106 timeConsumingStat = {
107 .pasteboardState = SPS_PASTE_STATE, .dataSize = DATA_LEVEL_ONE, .timeConsuming = TCS_TIME_CONSUMING_LEVEL_ONE
108 };
109 status = Reporter::GetInstance().TimeConsumingStatistic().Report(timeConsumingStat);
110 ASSERT_EQ(status, ReportStatus::SUCCESS);
111 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
112 }
113
114 /**
115 * @tc.name: DFXTest004
116 * @tc.desc: test dfx report time consuming statistic with SPS_REMOTE_PASTE_STATE.
117 * @tc.type: FUNC
118 * @tc.require:
119 * @tc.author:
120 */
121 HWTEST_F(DFXTest, DFXTest004, TestSize.Level0)
122 {
123 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
124 TimeConsumingStat timeConsumingStat = {
125 .pasteboardState = SPS_REMOTE_PASTE_STATE, .dataSize = -1, .timeConsuming = -1
126 };
127 auto status = Reporter::GetInstance().TimeConsumingStatistic().Report(timeConsumingStat);
128 ASSERT_EQ(status, ReportStatus::SUCCESS);
129
130 timeConsumingStat = { .pasteboardState = SPS_REMOTE_PASTE_STATE, .dataSize = DATA_LEVEL_ONE, .timeConsuming = -1 };
131 status = Reporter::GetInstance().TimeConsumingStatistic().Report(timeConsumingStat);
132 ASSERT_EQ(status, ReportStatus::SUCCESS);
133
134 timeConsumingStat = { .pasteboardState = SPS_REMOTE_PASTE_STATE,
135 .dataSize = DATA_LEVEL_ONE,
136 .timeConsuming = TCS_TIME_CONSUMING_LEVEL_ONE };
137 status = Reporter::GetInstance().TimeConsumingStatistic().Report(timeConsumingStat);
138 ASSERT_EQ(status, ReportStatus::SUCCESS);
139 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
140 }
141
142 /**
143 * @tc.name: DFXTest005
144 * @tc.desc: test dfx report time consuming statistic with SPS_INVALID_STATE.
145 * @tc.type: FUNC
146 * @tc.require:
147 * @tc.author:
148 */
149 HWTEST_F(DFXTest, DFXTest005, TestSize.Level0)
150 {
151 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
152 TimeConsumingStat timeConsumingStat = {
153 .pasteboardState = SPS_INVALID_STATE, .dataSize = DATA_LEVEL_ONE, .timeConsuming = TCS_TIME_CONSUMING_LEVEL_ONE
154 };
155 auto status = Reporter::GetInstance().TimeConsumingStatistic().Report(timeConsumingStat);
156 ASSERT_EQ(status, ReportStatus::SUCCESS);
157 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
158 }
159
160 /**
161 * @tc.name: DFXTest006
162 * @tc.desc: test dfx report behaviour.
163 * @tc.type: FUNC
164 * @tc.require:
165 * @tc.author:
166 */
167 HWTEST_F(DFXTest, DFXTest006, TestSize.Level0)
168 {
169 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
170 PasteboardBehaviourMsg behaviourMsg = { .pasteboardState = BPS_COPY_STATE, .bundleName = "com.paste.test" };
171 auto status = Reporter::GetInstance().PasteboardBehaviour().Report(behaviourMsg);
172 ASSERT_EQ(status, ReportStatus::SUCCESS);
173 status = Reporter::GetInstance().PasteboardBehaviour().Report(behaviourMsg);
174 ASSERT_EQ(status, ReportStatus::SUCCESS);
175
176 behaviourMsg = { .pasteboardState = BPS_PASTE_STATE, .bundleName = "com.paste.test" };
177 status = Reporter::GetInstance().PasteboardBehaviour().Report(behaviourMsg);
178 ASSERT_EQ(status, ReportStatus::SUCCESS);
179 status = Reporter::GetInstance().PasteboardBehaviour().Report(behaviourMsg);
180 ASSERT_EQ(status, ReportStatus::SUCCESS);
181
182 behaviourMsg = { .pasteboardState = BPS_REMOTE_PASTE_STATE, .bundleName = "com.paste.test" };
183 status = Reporter::GetInstance().PasteboardBehaviour().Report(behaviourMsg);
184 ASSERT_EQ(status, ReportStatus::SUCCESS);
185 status = Reporter::GetInstance().PasteboardBehaviour().Report(behaviourMsg);
186 ASSERT_EQ(status, ReportStatus::SUCCESS);
187
188 behaviourMsg = { .pasteboardState = BPS_INVALID_STATE, .bundleName = "com.paste.test" };
189 status = Reporter::GetInstance().PasteboardBehaviour().Report(behaviourMsg);
190 ASSERT_EQ(status, ReportStatus::SUCCESS);
191 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
192 }
193 } // namespace OHOS::MiscServices