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
16 #include <fstream>
17 #include <gtest/gtest.h>
18 #include <iostream>
19 #include "view_data.h"
20 #include "session_info.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace AbilityBase {
27 namespace {
28 static const std::string TJSON =
29 "{\"abilityName\":\"\",\"bundleName\":\"\",\"isOtherAccount\":false,\"isUserSelected\":false,"
30 "\"moduleName\":\"\",\"nodes\":[],\"pageRect\":\"{\\\"height\\\":0.0,\\\"left\\\":0.0,\\\"top\\\":0.0,"
31 "\\\"width\\\":0.0}\",\"pageUrl\":\"\"}";
32
33 static const std::string TJSON_WITHOUT_BUNDLE_NAME =
34 "{\"moduleName\":\"\",\"abilityName\":\"\",\"pageUrl\":\"\",\"nodes\":[],"
35 "\"isUserSelected\":false,\"isOtherAccount\":false,"
36 "\"pageRect\":\"{\\\"left\\\":0,\\\"top\\\":0,\\\"width\\\":0,\\\"height\\\":0}\"}";
37
38 static const std::string TJSON_BUNDLE_NAME_NOT_STRING =
39 "{\"bundleName\":0,\"moduleName\":\"\",\"abilityName\":\"\",\"pageUrl\":\"\",\"nodes\":[],"
40 "\"isUserSelected\":false,\"isOtherAccount\":false,"
41 "\"pageRect\":\"{\\\"left\\\":0,\\\"top\\\":0,\\\"width\\\":0,\\\"height\\\":0}\"}";
42
43 static const std::string TJSON_WITHOUT_MODULE_NAME =
44 "{\"bundleName\":\"\",\"abilityName\":\"\",\"pageUrl\":\"\",\"nodes\":[],"
45 "\"isUserSelected\":false,\"isOtherAccount\":false,"
46 "\"pageRect\":\"{\\\"left\\\":0,\\\"top\\\":0,\\\"width\\\":0,\\\"height\\\":0}\"}";
47
48 static const std::string TJSON_MODULE_NAME_NOT_STRING =
49 "{\"bundleName\":\"\",\"moduleName\":0,\"abilityName\":\"\",\"pageUrl\":\"\",\"nodes\":[],"
50 "\"isUserSelected\":false,\"isOtherAccount\":false,"
51 "\"pageRect\":\"{\\\"left\\\":0,\\\"top\\\":0,\\\"width\\\":0,\\\"height\\\":0}\"}";
52
53 static const std::string TJSON_WITHOUT_ABILITY_NAME =
54 "{\"bundleName\":\"\",\"moduleName\":\"\",\"pageUrl\":\"\",\"nodes\":[],"
55 "\"isUserSelected\":false,\"isOtherAccount\":false,"
56 "\"pageRect\":\"{\\\"left\\\":0,\\\"top\\\":0,\\\"width\\\":0,\\\"height\\\":0}\"}";
57
58 static const std::string TJSON_ABILITY_NAME_NOT_STRING =
59 "{\"bundleName\":\"\",\"moduleName\":\"\",\"abilityName\":0,\"pageUrl\":\"\",\"nodes\":[],"
60 "\"isUserSelected\":false,\"isOtherAccount\":false,"
61 "\"pageRect\":\"{\\\"left\\\":0,\\\"top\\\":0,\\\"width\\\":0,\\\"height\\\":0}\"}";
62
63 static const std::string TJSON_WITHOUT_PAGE_URL =
64 "{\"bundleName\":\"\",\"moduleName\":\"\",\"abilityName\":\"\",\"nodes\":[],"
65 "\"isUserSelected\":false,\"isOtherAccount\":false,"
66 "\"pageRect\":\"{\\\"left\\\":0,\\\"top\\\":0,\\\"width\\\":0,\\\"height\\\":0}\"}";
67
68 static const std::string TJSON_PAGE_URL_NOT_STRING =
69 "{\"bundleName\":\"\",\"moduleName\":\"\",\"abilityName\":\"\",\"pageUrl\":0,\"nodes\":[],"
70 "\"isUserSelected\":false,\"isOtherAccount\":false,"
71 "\"pageRect\":\"{\\\"left\\\":0,\\\"top\\\":0,\\\"width\\\":0,\\\"height\\\":0}\"}";
72
73 static const std::string TJSON_WITHOUT_USER_SELECTED =
74 "{\"bundleName\":\"\",\"moduleName\":\"\",\"abilityName\":\"\",\"nodes\":[],"
75 "\"isOtherAccount\":false,"
76 "\"pageRect\":\"{\\\"left\\\":0,\\\"top\\\":0,\\\"width\\\":0,\\\"height\\\":0}\"}";
77
78 static const std::string TJSON_USER_SELECTED_NOT_BOOL =
79 "{\"bundleName\":\"\",\"moduleName\":\"\",\"abilityName\":\"\",\"pageUrl\":0,\"nodes\":[],"
80 "\"isUserSelected\":\"\",\"isOtherAccount\":false,"
81 "\"pageRect\":\"{\\\"left\\\":0,\\\"top\\\":0,\\\"width\\\":0,\\\"height\\\":0}\"}";
82
83 static const std::string TJSON_WITHOUT_OTHER_ACCOUNT =
84 "{\"bundleName\":\"\",\"moduleName\":\"\",\"abilityName\":\"\",\"pageUrl\":\"\",\"nodes\":[],"
85 "\"isUserSelected\":false,"
86 "\"pageRect\":\"{\\\"left\\\":0,\\\"top\\\":0,\\\"width\\\":0,\\\"height\\\":0}\"}";
87
88 static const std::string TJSON_OTHER_ACCOUNT_NOT_BOOL =
89 "{\"bundleName\":\"\",\"moduleName\":\"\",\"abilityName\":\"\",\"pageUrl\":\"\",\"nodes\":[],"
90 "\"isUserSelected\":false,\"isOtherAccount\":false,"
91 "\"pageRect\":\"{\\\"left\\\":0,\\\"top\\\":0,\\\"width\\\":0,\\\"height\\\":0}\"}";
92
93 static const std::string TJSON_WITHOUT_PAGE_RECT =
94 "{\"bundleName\":\"\",\"moduleName\":\"\",\"abilityName\":\"\",\"pageUrl\":\"\",\"nodes\":[],"
95 "\"isUserSelected\":false,\"isOtherAccount\":false";
96
97 static const std::string TJSON_PAGE_RECT_NOT_STRING =
98 "{\"bundleName\":\"\",\"moduleName\":\"\",\"abilityName\":\"\",\"pageUrl\":\"\",\"nodes\":[],"
99 "\"isUserSelected\":false,\"isOtherAccount\":false,"
100 "\"pageRect\":0";
101 } // namespace
102 class ViewDataTest : public testing::Test {
103 public:
104 static void SetUpTestCase();
105 static void TearDownTestCase();
106 void SetUp();
107 void TearDown();
108 };
109
SetUpTestCase()110 void ViewDataTest::SetUpTestCase()
111 {}
112
TearDownTestCase()113 void ViewDataTest::TearDownTestCase()
114 {}
115
SetUp()116 void ViewDataTest::SetUp()
117 {}
118
TearDown()119 void ViewDataTest::TearDown()
120 {}
121
122 /**
123 * @tc.name: FromJsonString_100
124 * @tc.desc: ViewData test for FromJsonString.
125 * @tc.type: FUNC
126 */
127 HWTEST_F(ViewDataTest, FromJsonString_100, TestSize.Level1)
128 {
129 std::string jsonStr = "jsonStr";
130 std::shared_ptr<ViewData> viewdata = std::make_shared<ViewData>();
131 EXPECT_TRUE(viewdata != nullptr);
132 viewdata->FromJsonString(jsonStr);
133 viewdata->FromJsonString(TJSON);
134 std::string ret = viewdata->ToJsonString();
135 EXPECT_NE(ret, jsonStr);
136
137 viewdata->FromJsonString(TJSON_WITHOUT_BUNDLE_NAME);
138 ret = viewdata->ToJsonString();
139 EXPECT_NE(ret, TJSON_WITHOUT_BUNDLE_NAME);
140
141 viewdata->FromJsonString(TJSON_BUNDLE_NAME_NOT_STRING);
142 ret = viewdata->ToJsonString();
143 EXPECT_NE(ret, TJSON_BUNDLE_NAME_NOT_STRING);
144
145 viewdata->FromJsonString(TJSON_WITHOUT_MODULE_NAME);
146 ret = viewdata->ToJsonString();
147 EXPECT_NE(ret, TJSON_WITHOUT_MODULE_NAME);
148
149 viewdata->FromJsonString(TJSON_MODULE_NAME_NOT_STRING);
150 ret = viewdata->ToJsonString();
151 EXPECT_NE(ret, TJSON_MODULE_NAME_NOT_STRING);
152
153 viewdata->FromJsonString(TJSON_WITHOUT_ABILITY_NAME);
154 ret = viewdata->ToJsonString();
155 EXPECT_NE(ret, TJSON_WITHOUT_ABILITY_NAME);
156
157 viewdata->FromJsonString(TJSON_ABILITY_NAME_NOT_STRING);
158 ret = viewdata->ToJsonString();
159 EXPECT_NE(ret, TJSON_ABILITY_NAME_NOT_STRING);
160 }
161
162 /**
163 * @tc.name: FromJsonString_200
164 * @tc.desc: ViewData test for FromJsonString.
165 * @tc.type: FUNC
166 */
167 HWTEST_F(ViewDataTest, FromJsonString_200, TestSize.Level1)
168 {
169 std::string jsonStr = "jsonStr";
170 std::shared_ptr<ViewData> viewdata = std::make_shared<ViewData>();
171 EXPECT_TRUE(viewdata != nullptr);
172
173 viewdata->FromJsonString(TJSON_WITHOUT_PAGE_URL);
174 std::string ret = viewdata->ToJsonString();
175 EXPECT_NE(ret, TJSON_WITHOUT_PAGE_URL);
176
177 viewdata->FromJsonString(TJSON_PAGE_URL_NOT_STRING);
178 ret = viewdata->ToJsonString();
179 EXPECT_NE(ret, TJSON_PAGE_URL_NOT_STRING);
180
181 viewdata->FromJsonString(TJSON_WITHOUT_USER_SELECTED);
182 ret = viewdata->ToJsonString();
183 EXPECT_NE(ret, TJSON_WITHOUT_USER_SELECTED);
184
185 viewdata->FromJsonString(TJSON_USER_SELECTED_NOT_BOOL);
186 ret = viewdata->ToJsonString();
187 EXPECT_NE(ret, TJSON_USER_SELECTED_NOT_BOOL);
188
189 viewdata->FromJsonString(TJSON_WITHOUT_OTHER_ACCOUNT);
190 ret = viewdata->ToJsonString();
191 EXPECT_NE(ret, TJSON_WITHOUT_OTHER_ACCOUNT);
192
193 viewdata->FromJsonString(TJSON_OTHER_ACCOUNT_NOT_BOOL);
194 ret = viewdata->ToJsonString();
195 EXPECT_NE(ret, TJSON_OTHER_ACCOUNT_NOT_BOOL);
196
197 viewdata->FromJsonString(TJSON_WITHOUT_PAGE_RECT);
198 ret = viewdata->ToJsonString();
199 EXPECT_NE(ret, TJSON_WITHOUT_PAGE_RECT);
200
201 viewdata->FromJsonString(TJSON_PAGE_RECT_NOT_STRING);
202 ret = viewdata->ToJsonString();
203 EXPECT_NE(ret, TJSON_PAGE_RECT_NOT_STRING);
204 }
205
206 /**
207 * @tc.name: Unmarshalling_100
208 * @tc.desc: SessionInfo test for Unmarshalling.
209 * @tc.type: FUNC
210 */
211 HWTEST_F(ViewDataTest, Unmarshalling_100, TestSize.Level1)
212 {
213 std::shared_ptr<AAFwk::SessionInfo> sessioninfo = std::make_shared<AAFwk::SessionInfo>();
214 EXPECT_TRUE(sessioninfo != nullptr);
215 Parcel parcel;
216 auto ret = sessioninfo->Unmarshalling(parcel);
217 EXPECT_NE(ret, nullptr);
218 }
219 } // namespace AbilityBase
220 } // namespace OHOS
221