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