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