• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #define private public
19 #define protected public
20 #include "base/log/dump_recorder.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS::Ace {
26 namespace {} // namespace
27 
28 class DumpRecorderTest : public testing::Test {
29 public:
ParseJson(const std::string & jsonStr)30     static std::unique_ptr<JsonValue> ParseJson(const std::string& jsonStr)
31     {
32         return JsonUtil::ParseJsonString(jsonStr);
33     }
34 };
35 
36 /**
37  * @tc.name: CompareDumpParamTest001
38  * @tc.desc: test CompareDumpParam when both empty objects
39  * @tc.type: FUNC
40  */
41 HWTEST_F(DumpRecorderTest, CompareDumpParamTest001, TestSize.Level1)
42 {
43     auto curParams = ParseJson("{}");
44     auto prevParams = ParseJson("{}");
45     DumpRecorder recorder;
46     EXPECT_TRUE(recorder.CompareDumpParam(curParams, prevParams));
47 }
48 
49 /**
50  * @tc.name: CompareDumpParamTest002
51  * @tc.desc: test CompareDumpParam when skip keys only different values returnsTrue
52  * @tc.type: FUNC
53  */
54 HWTEST_F(DumpRecorderTest, CompareDumpParamTest002, TestSize.Level1)
55 {
56     auto curParams = ParseJson(R"({"time": "now", "children": [1]})");
57     auto prevParams = ParseJson(R"({"time": "then", "children": [2]})");
58     DumpRecorder recorder;
59     EXPECT_TRUE(recorder.CompareDumpParam(curParams, prevParams));
60 }
61 
62 /**
63  * @tc.name: CompareDumpParamTest003
64  * @tc.desc: test CompareDumpParam when none skip keys match
65  * @tc.type: FUNC
66  */
67 HWTEST_F(DumpRecorderTest, CompareDumpParamTest003, TestSize.Level1)
68 {
69     auto curParams = ParseJson(R"({"name": "Alice", "age": 30})");
70     auto prevParams = ParseJson(R"({"name": "Alice", "age": 30})");
71     DumpRecorder recorder;
72     EXPECT_TRUE(recorder.CompareDumpParam(curParams, prevParams));
73 }
74 
75 /**
76  * @tc.name: CompareDumpParamTest004
77  * @tc.desc: test CompareDumpParam when missing key in prev
78  * @tc.type: FUNC
79  */
80 HWTEST_F(DumpRecorderTest, CompareDumpParamTest004, TestSize.Level1)
81 {
82     auto curParams = ParseJson(R"({"id": 123})");
83     auto prevParams = ParseJson("{}");
84     DumpRecorder recorder;
85     EXPECT_FALSE(recorder.CompareDumpParam(curParams, prevParams));
86 }
87 
88 /**
89  * @tc.name: CompareDumpParamTest005
90  * @tc.desc: test CompareDumpParam when value mismatch
91  * @tc.type: FUNC
92  */
93 HWTEST_F(DumpRecorderTest, CompareDumpParamTest005, TestSize.Level1)
94 {
95     auto curParams = ParseJson(R"({"status": true})");
96     auto prevParams = ParseJson(R"({"status": false})");
97     DumpRecorder recorder;
98     EXPECT_FALSE(recorder.CompareDumpParam(curParams, prevParams));
99 }
100 
101 /**
102  * @tc.name: CompareDumpParamTest006
103  * @tc.desc: test CompareDumpParam when key empty
104  * @tc.type: FUNC
105  */
106 HWTEST_F(DumpRecorderTest, CompareDumpParamTest006, TestSize.Level1)
107 {
108     auto curParams = ParseJson(R"({"": "invalid"})");
109     auto prevParams = ParseJson(R"({"": "invalid"})");
110     DumpRecorder recorder;
111     EXPECT_FALSE(recorder.CompareDumpParam(curParams, prevParams));
112 }
113 
114 /**
115  * @tc.name: CompareDumpParamTest007
116  * @tc.desc: test CompareDumpParam when mixed keys with none skip mismatch
117  * @tc.type: FUNC
118  */
119 HWTEST_F(DumpRecorderTest, CompareDumpParamTest007, TestSize.Level1)
120 {
121     auto curParams = ParseJson(R"({"time": "now", "mode": "A"})");
122     auto prevParams = ParseJson(R"({"time": "then", "mode": "B"})");
123     DumpRecorder recorder;
124     EXPECT_FALSE(recorder.CompareDumpParam(curParams, prevParams));
125 }
126 
127 /**
128  * @tc.name: CompareDumpParamTest008
129  * @tc.desc: test CompareDumpParam when extra keys in prev ignores extra
130  * @tc.type: FUNC
131  */
132 HWTEST_F(DumpRecorderTest, CompareDumpParamTest008, TestSize.Level1)
133 {
134     auto curParams = ParseJson(R"({"size": 10})");
135     auto prevParams = ParseJson(R"({"size": 10, "color": "red"})");
136     DumpRecorder recorder;
137     EXPECT_TRUE(recorder.CompareDumpParam(curParams, prevParams));
138 }
139 
140 /**
141  * @tc.name: CompareDumpParamTest009
142  * @tc.desc: test CompareDumpParam when different value types to string mismatch
143  * @tc.type: FUNC
144  */
145 HWTEST_F(DumpRecorderTest, CompareDumpParamTest009, TestSize.Level1)
146 {
147     auto curParams = ParseJson(R"({"count": 5})");
148     auto prevParams = ParseJson(R"({"count": "5"})");
149     DumpRecorder recorder;
150     EXPECT_FALSE(recorder.CompareDumpParam(curParams, prevParams));
151 }
152 
153 /**
154  * @tc.name: CompareDumpParamTest010
155  * @tc.desc: test CompareDumpParam when null prev param value
156  * @tc.type: FUNC
157  */
158 HWTEST_F(DumpRecorderTest, CompareDumpParamTest010, TestSize.Level1)
159 {
160     auto curParams = ParseJson(R"({"valid": true})");
161     auto prevParams = ParseJson("{}");
162     DumpRecorder recorder;
163     EXPECT_FALSE(recorder.CompareDumpParam(curParams, prevParams));
164 }
165 
166 /**
167  * @tc.name: CompareDumpParamTest011
168  * @tc.desc: test CompareDumpParam when same unicode characters
169  * @tc.type: FUNC
170  */
171 HWTEST_F(DumpRecorderTest, CompareDumpParamTest011, TestSize.Level1)
172 {
173     auto curParams = ParseJson(R"({"text": "中文 Español Français ��"})");
174     auto prevParams = ParseJson(R"({"text": "中文 Español Français ��"})");
175     DumpRecorder recorder;
176     EXPECT_TRUE(recorder.CompareDumpParam(curParams, prevParams));
177 }
178 
179 /**
180  * @tc.name: CompareDumpParamTest012
181  * @tc.desc: test CompareDumpParam when different unicode characters
182  * @tc.type: FUNC
183  */
184 HWTEST_F(DumpRecorderTest, CompareDumpParamTest012, TestSize.Level1)
185 {
186     auto curParams = ParseJson(R"({"text": "中文 Español Français ��     "})");
187     auto prevParams = ParseJson(R"({"text": "中文 Español Français ��"})");
188     DumpRecorder recorder;
189     EXPECT_FALSE(recorder.CompareDumpParam(curParams, prevParams));
190 }
191 
192 /**
193  * @tc.name: CompareDumpParamTest013
194  * @tc.desc: test CompareDumpParam when special characters
195  * @tc.type: FUNC
196  */
197 HWTEST_F(DumpRecorderTest, CompareDumpParamTest013, TestSize.Level1)
198 {
199     auto curParams = ParseJson(R"({"key!@#$%^&*": "value"})");
200     auto prevParams = ParseJson(R"({"key!@#$%^&*": "value"})");
201     DumpRecorder recorder;
202     EXPECT_TRUE(recorder.CompareDumpParam(curParams, prevParams));
203 }
204 
205 /**
206  * @tc.name: CompareDumpParamTest014
207  * @tc.desc: test CompareDumpParam when same complex nested structures
208  * @tc.type: FUNC
209  */
210 HWTEST_F(DumpRecorderTest, CompareDumpParamTest014, TestSize.Level1)
211 {
212     /**
213      * @tc.steps: step1. Create cur JsonValue with complex nested structures
214      */
215     auto curParams = ParseJson(R"({
216         "user": {
217             "id": 123,
218             "name": "Alice",
219             "preferences": {
220                 "theme": "dark",
221                 "notifications": true
222             },
223             "children": ["Bob", "Charlie"]
224         },
225         "timestamp": 1620000000
226     })");
227 
228     /**
229      * @tc.steps: step2. Create prev JsonValue with same complex nested structures
230      */
231     auto prevParams = ParseJson(R"({
232         "user": {
233             "id": 123,
234             "name": "Alice",
235             "preferences": {
236                 "theme": "dark",
237                 "notifications": true
238             },
239             "children": ["Bob", "Charlie"]
240         },
241         "timestamp": 1620000000
242     })");
243 
244     /**
245      * @tc.steps: step3. Test CompareDumpParam with cur JsonValue and prev JsonValue.
246      */
247     DumpRecorder recorder;
248     EXPECT_TRUE(recorder.CompareDumpParam(curParams, prevParams));
249 }
250 
251 /**
252  * @tc.name: CompareDumpParamTest015
253  * @tc.desc: test CompareDumpParam when different complex nested structures
254  * @tc.type: FUNC
255  */
256 HWTEST_F(DumpRecorderTest, CompareDumpParamTest015, TestSize.Level1)
257 {
258     /**
259      * @tc.steps: step1. Create cur JsonValue with complex nested structures
260      */
261     auto curParams = ParseJson(R"({
262         "config": {
263             "version": 1,
264             "settings": {
265                 "timeout": 30,
266                 "retries": 3
267             }
268         }
269     })");
270 
271     /**
272      * @tc.steps: step2. Create prev JsonValue with retries different
273      */
274     auto prevParams = ParseJson(R"({
275         "config": {
276             "version": 1,
277             "settings": {
278                 "timeout": 30,
279                 "retries": 5
280             }
281         }
282     })");
283 
284     /**
285      * @tc.steps: step3. Test CompareDumpParam with cur JsonValue and prev JsonValue.
286      */
287     DumpRecorder recorder;
288     EXPECT_FALSE(recorder.CompareDumpParam(curParams, prevParams));
289 }
290 
291 /**
292  * @tc.name: CompareDumpParamTest016
293  * @tc.desc: test CompareDumpParam when different key order but same values
294  * @tc.type: FUNC
295  */
296 HWTEST_F(DumpRecorderTest, CompareDumpParamTest016, TestSize.Level1)
297 {
298     auto curParams = ParseJson(R"({"a": 1, "b": 2, "c": 3})");
299     auto prevParams = ParseJson(R"({"c": 3, "b": 2, "a": 1})");
300     DumpRecorder recorder;
301     EXPECT_TRUE(recorder.CompareDumpParam(curParams, prevParams));
302 }
303 } // namespace OHOS::Ace