1 /* 2 * Copyright (c) 2021-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 18 #define private public 19 #define protected public 20 #include "base/json/uobject.h" 21 22 using namespace testing; 23 using namespace testing::ext; 24 25 namespace OHOS::Ace { 26 namespace {} // namespace 27 28 class UObjectTest : public testing::Test {}; 29 30 /** 31 * @tc.name: UObjectTest001 32 * @tc.desc: AddItemToObject() 33 * @tc.type: FUNC 34 */ 35 HWTEST_F(UObjectTest, UObjectTest001, TestSize.Level1) 36 { 37 /** 38 * @tc.steps: step1. Create a UOObject. 39 */ 40 UObject uObject; 41 42 /** 43 * @tc.steps: step2. Save value through AdditemToObject. 44 * @tc.expected: step2. Asserting values through the get method. 45 */ 46 char* value = nullptr; 47 uObject.AddItemToObject("char", value); 48 EXPECT_EQ(uObject.stringItems_.size(), 0); 49 std::string stringValue = "test"; 50 uObject.AddItemToObject("char", const_cast<char*>(stringValue.c_str())); 51 EXPECT_EQ(uObject.GetString("char"), "test"); 52 53 stringValue = "test1"; 54 uObject.AddItemToObject("string", stringValue); 55 EXPECT_EQ(uObject.GetString("invalidKey"), ""); 56 EXPECT_EQ(uObject.GetString("string"), "test1"); 57 58 double doubleValue = 1.0; 59 uObject.AddItemToObject("double", doubleValue); 60 EXPECT_EQ(uObject.GetDouble("invalidKey"), 0); 61 EXPECT_EQ(uObject.GetDouble("double"), 1.0); 62 63 size_t sizeValue = 10; 64 uObject.AddItemToObject("size_t", sizeValue); 65 EXPECT_EQ(uObject.GetSizeT("invalidKey"), 0); 66 EXPECT_EQ(uObject.GetSizeT("size_t"), 10); 67 EXPECT_EQ(uObject.GetSizeT("double"), 1); 68 69 int32_t int32Value = 5; 70 uObject.AddItemToObject("int32_t", int32Value); 71 EXPECT_EQ(uObject.GetInt32("invalidKey"), 0); 72 EXPECT_EQ(uObject.GetInt32("int32_t"), 5); 73 EXPECT_EQ(uObject.GetInt32("double"), 1); 74 75 int64_t int64Value = 1; 76 uObject.AddItemToObject("int64_t", int64Value); 77 EXPECT_EQ(uObject.GetInt64("invalidKey"), 0); 78 EXPECT_EQ(uObject.GetInt64("int64_t"), 1); 79 EXPECT_EQ(uObject.GetInt64("double"), 1); 80 81 bool boolValue = false; 82 uObject.AddItemToObject("bool", boolValue); 83 EXPECT_EQ(uObject.GetBool("invalidKey"), false); 84 EXPECT_EQ(uObject.GetBool("bool"), false); 85 86 std::shared_ptr<UObject> sharedUObject = std::make_shared<UObject>(); 87 uObject.AddItemToObject("shared_ptr", sharedUObject); 88 EXPECT_TRUE(uObject.GetObject("invalidKey")); 89 EXPECT_TRUE(uObject.GetObject("shared_ptr")); 90 91 /** 92 * @tc.steps: step3. Contains() test. 93 * @tc.expected: step3. Asserting return bool. 94 */ 95 EXPECT_FALSE(uObject.Contains("invalidKey")); 96 EXPECT_TRUE(uObject.Contains("string")); 97 EXPECT_TRUE(uObject.Contains("double")); 98 EXPECT_TRUE(uObject.Contains("size_t")); 99 EXPECT_TRUE(uObject.Contains("int32_t")); 100 EXPECT_TRUE(uObject.Contains("int64_t")); 101 EXPECT_TRUE(uObject.Contains("bool")); 102 EXPECT_TRUE(uObject.Contains("shared_ptr")); 103 } 104 105 /** 106 * @tc.name: UObjectTest003 107 * @tc.desc: Serialize() 108 * @tc.type: FUNC 109 */ 110 HWTEST_F(UObjectTest, UObjectTest003, TestSize.Level1) 111 { 112 UObject uObject; 113 std::string value = ""; 114 uObject.WriteString(value); 115 const char* buffer = nullptr; 116 int32_t bufferLen = 100; 117 uObject.Deserialize(buffer, bufferLen); 118 } 119 } // namespace OHOS::Ace