1 /*
2 * Copyright (c) 2022 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 "nweb_value.h"
18
19 using namespace testing;
20 using namespace testing::ext;
21 using namespace OHOS;
22
23 namespace OHOS::NWeb {
24 class NWebValueTest : 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 NWebValueTest::SetUpTestCase(void)
33 {}
34
TearDownTestCase(void)35 void NWebValueTest::TearDownTestCase(void)
36 {}
37
SetUp(void)38 void NWebValueTest::SetUp(void)
39 {}
40
TearDown(void)41 void NWebValueTest::TearDown(void)
42 {}
43
44 /**
45 * @tc.name: NWebValue_BOOLEAN_001
46 * @tc.desc: Test set and get boolean/type.
47 * @tc.type: FUNC
48 * @tc.require:
49 */
50 HWTEST_F(NWebValueTest, NWebValue_BOOLEAN_001, TestSize.Level1)
51 {
52 std::shared_ptr<NWebValue> nwebValue = std::make_shared<NWebValue>(NWebValue::Type::NONE);
53 NWebValue::Type type = nwebValue->GetType();
54 EXPECT_EQ(NWebValue::Type::NONE, type);
55
56 nwebValue->SetType(NWebValue::Type::BOOLEAN);
57 nwebValue->SetBoolean(true);
58
59 bool value = nwebValue->GetBoolean();
60 type = nwebValue->GetType();
61 EXPECT_EQ(NWebValue::Type::BOOLEAN, type);
62 EXPECT_TRUE(value);
63
64 nwebValue->SetBoolean(false);
65 value = nwebValue->GetBoolean();
66 EXPECT_FALSE(value);
67 }
68
69 /**
70 * @tc.name: NWebValue_String_001
71 * @tc.desc: Test set and get string.
72 * @tc.type: FUNC
73 * @tc.require:
74 */
75 HWTEST_F(NWebValueTest, NWebValue_String_002, TestSize.Level1)
76 {
77 const std::string testData = "Hello,String";
78 std::shared_ptr<NWebValue> nwebValue = std::make_shared<NWebValue>(NWebValue::Type::NONE);
79 nwebValue->SetType(NWebValue::Type::STRING);
80 nwebValue->SetString(testData);
81
82 std::string actual = nwebValue->GetString();
83 EXPECT_STREQ(testData.c_str(), actual.c_str());
84 }
85
86 /**
87 * @tc.name: NWebValue_INT_003
88 * @tc.desc: Test set and get integer.
89 * @tc.type: FUNC
90 * @tc.require:
91 */
92 HWTEST_F(NWebValueTest, NWebValue_INT_003, TestSize.Level1)
93 {
94 int testData = 12345;
95 std::shared_ptr<NWebValue> nwebValue = std::make_shared<NWebValue>(NWebValue::Type::NONE);
96 nwebValue->SetType(NWebValue::Type::INTEGER);
97 nwebValue->SetInt(testData);
98
99 int actual = nwebValue->GetInt();
100 EXPECT_EQ(testData, actual);
101 }
102
103 /**
104 * @tc.name: NWebValue_JsonString_004
105 * @tc.desc: Test set and get json string.
106 * @tc.type: FUNC
107 * @tc.require:
108 */
109 HWTEST_F(NWebValueTest, NWebValue_JsonString_004, TestSize.Level1)
110 {
111 std::shared_ptr<NWebValue> nwebValue = std::make_shared<NWebValue>(NWebValue::Type::NONE);
112 nwebValue->SetType(NWebValue::Type::STRING);
113
114 const std::string testData = "{\"name\":\"zhangsan\",\"city\":\"hangzhou\"}";
115 nwebValue->SetJsonString(testData);
116
117 std::string actual = nwebValue->GetJsonString();
118 EXPECT_STREQ(testData.c_str(), actual.c_str());
119 }
120
121 /**
122 * @tc.name: NWebValue_Double_005
123 * @tc.desc: Test set and get double.
124 * @tc.type: FUNC
125 * @tc.require:
126 */
127 HWTEST_F(NWebValueTest, NWebValue_Double_005, TestSize.Level1)
128 {
129 std::shared_ptr<NWebValue> nwebValue = std::make_shared<NWebValue>(NWebValue::Type::NONE);
130 nwebValue->SetType(NWebValue::Type::DOUBLE);
131
132 double testData = 1.234567;
133 nwebValue->SetDouble(testData);
134
135 double actual = nwebValue->GetDouble();
136 EXPECT_DOUBLE_EQ(testData, actual);
137 }
138 } // namespace OHOS::NWeb
139