• 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 #include "webview_value.h"
18 
19 using namespace testing;
20 using namespace testing::ext;
21 using namespace OHOS;
22 
23 namespace OHOS::NWeb {
24 class WebviewValueTest : public testing::Test {
25 public:
26     static void SetUpTestCase(void);
27     static void TearDownTestCase(void);
28     void SetUp();
29     void TearDown();
30 };
31 
SetUpTestCase(void)32 void WebviewValueTest::SetUpTestCase(void)
33 {}
34 
TearDownTestCase(void)35 void WebviewValueTest::TearDownTestCase(void)
36 {}
37 
SetUp(void)38 void WebviewValueTest::SetUp(void)
39 {}
40 
TearDown(void)41 void WebviewValueTest::TearDown(void)
42 {}
43 
44 /**
45  * @tc.name: WebviewValue_BOOLEAN_001
46  * @tc.desc: Test set and get boolean/type.
47  * @tc.type: FUNC
48  * @tc.require:
49  */
50 HWTEST_F(WebviewValueTest, WebviewValue_BOOLEAN_001, TestSize.Level1)
51 {
52     std::shared_ptr<WebViewValue> webviewValue = std::make_shared<WebViewValue>(NWebRomValue::Type::NONE);
53     NWebRomValue::Type type = webviewValue->GetType();
54     EXPECT_EQ(NWebRomValue::Type::NONE, type);
55     webviewValue->SetType(NWebRomValue::Type::BOOLEAN);
56     webviewValue->SetBool(true);
57     bool value = webviewValue->GetBool();
58     type = webviewValue->GetType();
59     EXPECT_EQ(NWebRomValue::Type::BOOLEAN, type);
60     EXPECT_TRUE(value);
61     webviewValue->SetBool(false);
62     value = webviewValue->GetBool();
63     EXPECT_FALSE(value);
64 
65     webviewValue->SetType(NWebRomValue::Type::BOOLEANARRAY);
66     const std::vector<bool> testArray = {true, false};
67     webviewValue->SetBoolArray(testArray);
68     std::vector<bool> actualArray = webviewValue->GetBoolArray();
69     EXPECT_EQ(testArray, actualArray);
70 }
71 
72 /**
73  * @tc.name: WebviewValue_STRING_002
74  * @tc.desc: Test set and get string.
75  * @tc.type: FUNC
76  * @tc.require:
77  */
78 HWTEST_F(WebviewValueTest, WebviewValue_STRING_002, TestSize.Level1)
79 {
80     std::shared_ptr<WebViewValue> webviewValue = std::make_shared<WebViewValue>(NWebRomValue::Type::NONE);
81     webviewValue->SetType(NWebRomValue::Type::STRING);
82     const std::string testData = "Hello,String";
83     webviewValue->SetString(testData);
84     std::string actual= webviewValue->GetString();
85     EXPECT_STREQ(testData.c_str(), actual.c_str());
86 
87     webviewValue->SetType(NWebRomValue::Type::STRINGARRAY);
88     const std::vector<std::string> testArray = {"str1", "str2", "str3"};
89     webviewValue->SetStringArray(testArray);
90     std::vector<std::string> actualArray = webviewValue->GetStringArray();
91     EXPECT_EQ(testArray, actualArray);
92 }
93 
94 /**
95  * @tc.name: WebviewValue_INT_003
96  * @tc.desc: Test set and get int.
97  * @tc.type: FUNC
98  * @tc.require:
99  */
100 HWTEST_F(WebviewValueTest, WebviewValue_INT_003, TestSize.Level1)
101 {
102     std::shared_ptr<WebViewValue> webviewValue = std::make_shared<WebViewValue>(NWebRomValue::Type::NONE);
103     webviewValue->SetType(NWebRomValue::Type::INTEGER);
104     int testData = 12345;
105     webviewValue->SetInt(testData);
106     int actual = webviewValue->GetInt();
107     EXPECT_EQ(testData, actual);
108 
109     int64_t testDataInt64 = 12345;
110     webviewValue->SetInt64(testDataInt64);
111     int64_t actualInt64 = webviewValue->GetInt64();
112     EXPECT_EQ(testDataInt64, actualInt64);
113 
114     webviewValue->SetType(NWebRomValue::Type::INT64ARRAY);
115     const std::vector<int64_t> testArray = {12345, 54321};
116     webviewValue->SetInt64Array(testArray);
117     std::vector<int64_t> actualArray = webviewValue->GetInt64Array();
118     EXPECT_EQ(testArray, actualArray);
119 }
120 
121 /**
122  * @tc.name: WebviewValue_DOUBLE_004
123  * @tc.desc: Test set and get double.
124  * @tc.type: FUNC
125  * @tc.require:
126  */
127 HWTEST_F(WebviewValueTest, WebviewValue_DOUBLE_004, TestSize.Level1)
128 {
129     std::shared_ptr<WebViewValue> webviewValue = std::make_shared<WebViewValue>(NWebRomValue::Type::NONE);
130     webviewValue->SetType(NWebRomValue::Type::DOUBLE);
131     double testData = 1.234567;
132     webviewValue->SetDouble(testData);
133     double actual = webviewValue->GetDouble();
134     EXPECT_DOUBLE_EQ(testData, actual);
135 
136     webviewValue->SetType(NWebRomValue::Type::DOUBLEARRAY);
137     const std::vector<double> testArray = {1.234567, 7.654321};
138     webviewValue->SetDoubleArray(testArray);
139     std::vector<double> actualArray = webviewValue->GetDoubleArray();
140     EXPECT_EQ(testArray, actualArray);
141 }
142 
143 /**
144  * @tc.name: WebviewValue_BINARY_005
145  * @tc.desc: Test set and get binary.
146  * @tc.type: FUNC
147  * @tc.require:
148  */
149 HWTEST_F(WebviewValueTest, WebviewValue_BINARY_005, TestSize.Level1)
150 {
151     std::shared_ptr<WebViewValue> webviewValue = std::make_shared<WebViewValue>(NWebRomValue::Type::NONE);
152     webviewValue->SetType(NWebRomValue::Type::BINARY);
153     const char* testData = "Hello,Binary";
154     int length = 12;
155     webviewValue->SetBinary(length, testData);
156     int actualLength = -1;
157     const char* actual = webviewValue->GetBinary(actualLength);
158     EXPECT_EQ(length, actualLength);
159     EXPECT_NE(nullptr, actual);
160     if (actual && actualLength > 0) {
161         std::string str1 = std::string(testData, length);
162         std::string str2 = std::string(actual, actualLength);
163         EXPECT_EQ(str1, str2);
164     }
165 
166     std::vector<uint8_t> testArray = {0x01, 0x02, 0x03};
167     webviewValue->SetBinary(testArray);
168     std::vector<uint8_t> actualArray = webviewValue->GetBinary();
169     EXPECT_EQ(testArray, actualArray);
170 }
171 
172 /**
173  * @tc.name: WebviewValue_DICT_006
174  * @tc.desc: Test set and get dict/list.
175  * @tc.type: FUNC
176  * @tc.require:
177  */
178 HWTEST_F(WebviewValueTest, WebviewValue_DICT_006, TestSize.Level1)
179 {
180     std::shared_ptr<WebViewValue> webviewValue = std::make_shared<WebViewValue>(NWebRomValue::Type::NONE);
181     // !child_node
182     const std::string key = "Dict";
183     webviewValue->SaveDictChildValue(key);
184     webviewValue->SaveListChildValue();
185     // child_node
186     std::shared_ptr<NWebRomValue> childNode = webviewValue->NewChildValue();
187     EXPECT_NE(nullptr, childNode);
188     webviewValue->SetType(NWebRomValue::Type::DICTIONARY);
189     webviewValue->SaveDictChildValue(key);
190     std::map<std::string, std::shared_ptr<NWebRomValue>> result = webviewValue->GetDictValue();
191     EXPECT_NE(nullptr, childNode);
192     EXPECT_EQ(childNode, result[key]);
193 
194     std::shared_ptr<NWebRomValue> childNode1 = webviewValue->NewChildValue();
195     EXPECT_NE(nullptr, childNode);
196     webviewValue->SetType(NWebRomValue::Type::LIST);
197     webviewValue->SaveListChildValue();
198     std::vector<std::shared_ptr<NWebRomValue>> result1 = webviewValue->GetListValue();
199     EXPECT_EQ(1, result1.size());
200     if (result1.size() == 1) {
201         EXPECT_EQ(childNode1, result1[0]);
202     }
203 }
204 
205 /**
206  * @tc.name: WebviewValue_ERR_007
207  * @tc.desc: Test Err.
208  * @tc.type: FUNC
209  * @tc.require:
210  */
211 HWTEST_F(WebviewValueTest, WebviewValue_ERR_007, TestSize.Level1)
212 {
213     std::shared_ptr<WebViewValue> webviewValue = std::make_shared<WebViewValue>(NWebRomValue::Type::NONE);
214     webviewValue->SetType(NWebRomValue::Type::ERROR);
215     const std::string name = "Hello,Err";
216     const std::string msg = "ErrMsg";
217     webviewValue->SetErrName(name);
218     webviewValue->SetErrMsg(msg);
219     std::string errName = webviewValue->GetErrName();
220     std::string errMsg = webviewValue->GetErrMsg();
221     EXPECT_EQ(name, errName);
222     EXPECT_EQ(msg, errMsg);
223 }
224 }