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 #include <gtest/gtest.h>
16 #include "form_report.h"
17 #include "want.h"
18 #include "form_event_report.h"
19 
20 using namespace testing::ext;
21 using namespace OHOS;
22 using namespace OHOS::AppExecFwk;
23 
24 namespace {
25 const std::string BUNDLE = "test_bundle";
26 const std::string ABILITY = "test_ability";
27 const std::string MODULENAME = "test_modulename";
28 int64_t FORMID = 12345;
29 
30 class FormReportTest : public testing::Test {
31 public:
32     void SetUp() override;
33     void TearDown() override;
34 
35     FormReport& formReport = FormReport::GetInstance();
36     Want want;
37     int64_t testFormId = FORMID;
38 };
39 
SetUp()40 void FormReportTest::SetUp()
41 {
42     OHOS::AppExecFwk::ElementName element;
43     element.SetBundleName(BUNDLE);
44     element.SetAbilityName(ABILITY);
45     element.SetModuleName(MODULENAME);
46     want.SetElement(element);
47     want.SetParam(Constants::PARAM_MODULE_NAME_KEY, MODULENAME);
48     want.SetParam(Constants::PARAM_ABILITY_NAME_KEY, ABILITY);
49 }
50 
TearDown()51 void FormReportTest::TearDown()
52 {
53     formReport.GetStatistic().clear();
54 }
55 
56 /**
57  * @tc.name: FormReport_001
58  * @tc.desc: test SetFormRecordInfo function.
59  * @tc.type: FUNC
60  */
61 HWTEST_F(FormReportTest, FormReport_001, TestSize.Level1)
62 {
63     formReport.SetFormRecordInfo(testFormId, want);
64     auto statisticIt = formReport.GetStatistic().find(testFormId);
65     ASSERT_NE(statisticIt, formReport.GetStatistic().end());
66     FormStatistic &statistic = statisticIt->second;
67     EXPECT_EQ(statistic.bundleName_, BUNDLE);
68     EXPECT_EQ(statistic.moduleName_, MODULENAME);
69     EXPECT_EQ(statistic.abilityName_, ABILITY);
70 }
71 
72 /**
73  * @tc.name: FormReport_002
74  * @tc.desc: test SetStartGetTime function.
75  * @tc.type: FUNC
76  */
77 HWTEST_F(FormReportTest, FormReport_002, TestSize.Level1)
78 {
79     int64_t startTime = 1000;
80     formReport.SetStartGetTime(testFormId, startTime);
81     auto statisticIt = formReport.GetStatistic().find(testFormId);
82     EXPECT_NE(statisticIt, formReport.GetStatistic().end());
83     EXPECT_EQ(statisticIt->second.startGetTime_, startTime);
84 }
85 
86 /**
87  * @tc.name: FormReport_003
88  * @tc.desc: test SetEndGetTime function.
89  * @tc.type: FUNC
90  */
91 HWTEST_F(FormReportTest, FormReport_003, TestSize.Level1)
92 {
93     int64_t endTime = 2000;
94     formReport.SetEndGetTime(testFormId, endTime);
95     auto statisticIt = formReport.GetStatistic().find(testFormId);
96     EXPECT_EQ(statisticIt, formReport.GetStatistic().end());
97 }
98 
99 /**
100  * @tc.name: FormReport_004
101  * @tc.desc: test SetStartGetTime function.
102  * @tc.type: FUNC
103  */
104 HWTEST_F(FormReportTest, FormReport_004, TestSize.Level1)
105 {
106     int64_t endTime = 2000;
107     int64_t startTime = 1000;
108     formReport.SetStartGetTime(testFormId, startTime);
109     formReport.SetEndGetTime(testFormId, endTime);
110     auto statisticIt = formReport.GetStatistic().find(testFormId);
111     EXPECT_NE(statisticIt, formReport.GetStatistic().end());
112     EXPECT_EQ(statisticIt->second.endGetTime_, endTime);
113 }
114 
115 /**
116  * @tc.name: FormReport_005
117  * @tc.desc: test SetStartGetTime function.
118  * @tc.type: FUNC
119  */
120 HWTEST_F(FormReportTest, FormReport_005, TestSize.Level1)
121 {
122     int64_t endTime = 12 * 1000;
123     int64_t startTime = 1* 1000;
124     formReport.SetDurationStartTime(testFormId, startTime);
125     formReport.SetDurationEndTime(testFormId, endTime);
126     auto statisticIt = formReport.GetStatistic().find(testFormId);
127     EXPECT_NE(statisticIt, formReport.GetStatistic().end());
128 }
129 
130 /**
131  * @tc.name: FormReport_006
132  * @tc.desc: test SetDurationStartTime function.
133  * @tc.type: FUNC
134  */
135 HWTEST_F(FormReportTest, FormReport_006, TestSize.Level1)
136 {
137     int64_t startTime = 1* 1000;
138     formReport.SetDurationStartTime(testFormId, startTime);
139     auto statisticIt = formReport.GetStatistic().find(testFormId);
140     EXPECT_NE(statisticIt, formReport.GetStatistic().end());
141     EXPECT_EQ(statisticIt->second.durationStartTime_, startTime);
142 }
143 
144 /**
145  * @tc.name: FormReport_007
146  * @tc.desc: test SetDurationEndTime function.
147  * @tc.type: FUNC
148  */
149 HWTEST_F(FormReportTest, FormReport_007, TestSize.Level1)
150 {
151     int64_t endTime = 12 * 1000;
152     formReport.SetDurationEndTime(testFormId, endTime);
153     auto statisticIt = formReport.GetStatistic().find(testFormId);
154     EXPECT_EQ(statisticIt, formReport.GetStatistic().end());
155 }
156 
157 /**
158  * @tc.name: FormReport_008
159  * @tc.desc: test SetStartAquireTime function.
160  * @tc.type: FUNC
161  */
162 HWTEST_F(FormReportTest, FormReport_008, TestSize.Level1)
163 {
164     int64_t startTime = 1000;
165     formReport.SetStartAquireTime(testFormId, startTime);
166     auto statisticIt = formReport.GetStatistic().find(testFormId);
167     EXPECT_NE(statisticIt, formReport.GetStatistic().end());
168     EXPECT_EQ(statisticIt->second.startAquireTime_, startTime);
169 }
170 
171 /**
172  * @tc.name: FormReport_009
173  * @tc.desc: test SetEndAquireTime function.
174  * @tc.type: FUNC
175  */
176 HWTEST_F(FormReportTest, FormReport_009, TestSize.Level1)
177 {
178     int64_t endTime = 1000;
179     formReport.SetEndAquireTime(testFormId, endTime);
180     auto statisticIt = formReport.GetStatistic().find(testFormId);
181     EXPECT_EQ(statisticIt, formReport.GetStatistic().end());
182 }
183 
184 /**
185  * @tc.name: FormReport_010
186  * @tc.desc: test SetDurationEndTime function.
187  * @tc.type: FUNC
188  */
189 HWTEST_F(FormReportTest, FormReport_010, TestSize.Level1)
190 {
191     int64_t endTime = 10 * 1000;
192     int64_t endTime1 = 0;
193     int64_t startTime = 1000;
194     formReport.SetStartAquireTime(testFormId, startTime);
195     formReport.SetEndAquireTime(testFormId, endTime);
196     auto statisticIt = formReport.GetStatistic().find(testFormId);
197     EXPECT_NE(statisticIt, formReport.GetStatistic().end());
198     formReport.GetEndAquireTime(testFormId, endTime1);
199     EXPECT_EQ(endTime1, endTime);
200 }
201 
202 /**
203  * @tc.name: FormReport_011
204  * @tc.desc: test SetEndAquireTime function.
205  * @tc.type: FUNC
206  */
207 HWTEST_F(FormReportTest, FormReport_011, TestSize.Level1)
208 {
209     int64_t startTime = 1000;
210     formReport.SetStartBindTime(testFormId, startTime);
211     auto statisticIt = formReport.GetStatistic().find(testFormId);
212     EXPECT_NE(statisticIt, formReport.GetStatistic().end());
213     EXPECT_EQ(statisticIt->second.startBindTime_, startTime);
214 }
215 
216 /**
217  * @tc.name: FormReport_012
218  * @tc.desc: test SetEndAquireTime function.
219  * @tc.type: FUNC
220  */
221 HWTEST_F(FormReportTest, FormReport_012, TestSize.Level1)
222 {
223     int64_t endTime = 10 * 1000;
224     formReport.SetEndBindTime(testFormId, endTime);
225     auto statisticIt = formReport.GetStatistic().find(testFormId);
226     EXPECT_EQ(statisticIt, formReport.GetStatistic().end());
227 }
228 
229 /**
230  * @tc.name: FormReport_013
231  * @tc.desc: test SetEndBindTime function.
232  * @tc.type: FUNC
233  */
234 HWTEST_F(FormReportTest, FormReport_013, TestSize.Level1)
235 {
236     int64_t endTime = 10 * 1000;
237     int64_t startTime = 1000;
238     formReport.SetStartBindTime(testFormId, startTime);
239     formReport.SetEndBindTime(testFormId, endTime);
240     auto statisticIt = formReport.GetStatistic().find(testFormId);
241     EXPECT_NE(statisticIt, formReport.GetStatistic().end());
242     EXPECT_EQ(statisticIt->second.endBindTime_, endTime);
243 }
244 
245 /**
246  * @tc.name: FormReport_014
247  * @tc.desc: test HandleAddFormStatistic function.
248  * @tc.type: FUNC
249  */
250 HWTEST_F(FormReportTest, FormReport_014, TestSize.Level1)
251 {
252     formReport.SetFormRecordInfo(testFormId, want);
253     formReport.HandleAddFormStatistic(testFormId);
254     auto statisticIt = formReport.GetStatistic().find(testFormId);
255     EXPECT_NE(statisticIt, formReport.GetStatistic().end());
256 }
257 
258 /**
259  * @tc.name: FormReport_015
260  * @tc.desc: test HandleAddFormStatistic function.
261  * @tc.type: FUNC
262  */
263 HWTEST_F(FormReportTest, FormReport_015, TestSize.Level1)
264 {
265     formReport.SetFormRecordInfo(testFormId, want);
266     formReport.InsertFormId(testFormId);
267     formReport.HandleAddFormStatistic(testFormId);
268     auto statisticIt = formReport.GetStatistic().find(testFormId);
269     EXPECT_NE(statisticIt, formReport.GetStatistic().end());
270 }
271 } // namespace
272