123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci#include "gtest/gtest.h" 1723b3eb3cSopenharmony_ci 1823b3eb3cSopenharmony_ci#define private public 1923b3eb3cSopenharmony_ci#define protected public 2023b3eb3cSopenharmony_ci#include "base/json/uobject.h" 2123b3eb3cSopenharmony_ci 2223b3eb3cSopenharmony_ciusing namespace testing; 2323b3eb3cSopenharmony_ciusing namespace testing::ext; 2423b3eb3cSopenharmony_ci 2523b3eb3cSopenharmony_cinamespace OHOS::Ace { 2623b3eb3cSopenharmony_cinamespace {} // namespace 2723b3eb3cSopenharmony_ci 2823b3eb3cSopenharmony_ciclass UObjectTest : public testing::Test {}; 2923b3eb3cSopenharmony_ci 3023b3eb3cSopenharmony_ci/** 3123b3eb3cSopenharmony_ci * @tc.name: UObjectTest001 3223b3eb3cSopenharmony_ci * @tc.desc: AddItemToObject() 3323b3eb3cSopenharmony_ci * @tc.type: FUNC 3423b3eb3cSopenharmony_ci */ 3523b3eb3cSopenharmony_ciHWTEST_F(UObjectTest, UObjectTest001, TestSize.Level1) 3623b3eb3cSopenharmony_ci{ 3723b3eb3cSopenharmony_ci /** 3823b3eb3cSopenharmony_ci * @tc.steps: step1. Create a UOObject. 3923b3eb3cSopenharmony_ci */ 4023b3eb3cSopenharmony_ci UObject uObject; 4123b3eb3cSopenharmony_ci 4223b3eb3cSopenharmony_ci /** 4323b3eb3cSopenharmony_ci * @tc.steps: step2. Save value through AdditemToObject. 4423b3eb3cSopenharmony_ci * @tc.expected: step2. Asserting values through the get method. 4523b3eb3cSopenharmony_ci */ 4623b3eb3cSopenharmony_ci char* value = nullptr; 4723b3eb3cSopenharmony_ci uObject.AddItemToObject("char", value); 4823b3eb3cSopenharmony_ci EXPECT_EQ(uObject.stringItems_.size(), 0); 4923b3eb3cSopenharmony_ci std::string stringValue = "test"; 5023b3eb3cSopenharmony_ci uObject.AddItemToObject("char", const_cast<char*>(stringValue.c_str())); 5123b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetString("char"), "test"); 5223b3eb3cSopenharmony_ci 5323b3eb3cSopenharmony_ci stringValue = "test1"; 5423b3eb3cSopenharmony_ci uObject.AddItemToObject("string", stringValue); 5523b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetString("invalidKey"), ""); 5623b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetString("string"), "test1"); 5723b3eb3cSopenharmony_ci 5823b3eb3cSopenharmony_ci double doubleValue = 1.0; 5923b3eb3cSopenharmony_ci uObject.AddItemToObject("double", doubleValue); 6023b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetDouble("invalidKey"), 0); 6123b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetDouble("double"), 1.0); 6223b3eb3cSopenharmony_ci 6323b3eb3cSopenharmony_ci size_t sizeValue = 10; 6423b3eb3cSopenharmony_ci uObject.AddItemToObject("size_t", sizeValue); 6523b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetSizeT("invalidKey"), 0); 6623b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetSizeT("size_t"), 10); 6723b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetSizeT("double"), 1); 6823b3eb3cSopenharmony_ci 6923b3eb3cSopenharmony_ci int32_t int32Value = 5; 7023b3eb3cSopenharmony_ci uObject.AddItemToObject("int32_t", int32Value); 7123b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetInt32("invalidKey"), 0); 7223b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetInt32("int32_t"), 5); 7323b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetInt32("double"), 1); 7423b3eb3cSopenharmony_ci 7523b3eb3cSopenharmony_ci int64_t int64Value = 1; 7623b3eb3cSopenharmony_ci uObject.AddItemToObject("int64_t", int64Value); 7723b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetInt64("invalidKey"), 0); 7823b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetInt64("int64_t"), 1); 7923b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetInt64("double"), 1); 8023b3eb3cSopenharmony_ci 8123b3eb3cSopenharmony_ci bool boolValue = false; 8223b3eb3cSopenharmony_ci uObject.AddItemToObject("bool", boolValue); 8323b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetBool("invalidKey"), false); 8423b3eb3cSopenharmony_ci EXPECT_EQ(uObject.GetBool("bool"), false); 8523b3eb3cSopenharmony_ci 8623b3eb3cSopenharmony_ci std::shared_ptr<UObject> sharedUObject = std::make_shared<UObject>(); 8723b3eb3cSopenharmony_ci uObject.AddItemToObject("shared_ptr", sharedUObject); 8823b3eb3cSopenharmony_ci EXPECT_TRUE(uObject.GetObject("invalidKey")); 8923b3eb3cSopenharmony_ci EXPECT_TRUE(uObject.GetObject("shared_ptr")); 9023b3eb3cSopenharmony_ci 9123b3eb3cSopenharmony_ci /** 9223b3eb3cSopenharmony_ci * @tc.steps: step3. Contains() test. 9323b3eb3cSopenharmony_ci * @tc.expected: step3. Asserting return bool. 9423b3eb3cSopenharmony_ci */ 9523b3eb3cSopenharmony_ci EXPECT_FALSE(uObject.Contains("invalidKey")); 9623b3eb3cSopenharmony_ci EXPECT_TRUE(uObject.Contains("string")); 9723b3eb3cSopenharmony_ci EXPECT_TRUE(uObject.Contains("double")); 9823b3eb3cSopenharmony_ci EXPECT_TRUE(uObject.Contains("size_t")); 9923b3eb3cSopenharmony_ci EXPECT_TRUE(uObject.Contains("int32_t")); 10023b3eb3cSopenharmony_ci EXPECT_TRUE(uObject.Contains("int64_t")); 10123b3eb3cSopenharmony_ci EXPECT_TRUE(uObject.Contains("bool")); 10223b3eb3cSopenharmony_ci EXPECT_TRUE(uObject.Contains("shared_ptr")); 10323b3eb3cSopenharmony_ci} 10423b3eb3cSopenharmony_ci 10523b3eb3cSopenharmony_ci/** 10623b3eb3cSopenharmony_ci * @tc.name: UObjectTest002 10723b3eb3cSopenharmony_ci * @tc.desc: Serialize() 10823b3eb3cSopenharmony_ci * @tc.type: FUNC 10923b3eb3cSopenharmony_ci */ 11023b3eb3cSopenharmony_ciHWTEST_F(UObjectTest, UObjectTest002, TestSize.Level1) 11123b3eb3cSopenharmony_ci{ 11223b3eb3cSopenharmony_ci /** 11323b3eb3cSopenharmony_ci * @tc.steps: step1. Create a UOObject. 11423b3eb3cSopenharmony_ci */ 11523b3eb3cSopenharmony_ci UObject uObject; 11623b3eb3cSopenharmony_ci std::string stringValue = "test"; 11723b3eb3cSopenharmony_ci uObject.AddItemToObject("char", const_cast<char*>(stringValue.c_str())); 11823b3eb3cSopenharmony_ci stringValue = "test1"; 11923b3eb3cSopenharmony_ci uObject.AddItemToObject("string", stringValue); 12023b3eb3cSopenharmony_ci double doubleValue = 1.0; 12123b3eb3cSopenharmony_ci uObject.AddItemToObject("double", doubleValue); 12223b3eb3cSopenharmony_ci size_t sizeValue = 10; 12323b3eb3cSopenharmony_ci uObject.AddItemToObject("size_t", sizeValue); 12423b3eb3cSopenharmony_ci int32_t int32Value = 5; 12523b3eb3cSopenharmony_ci uObject.AddItemToObject("int32_t", int32Value); 12623b3eb3cSopenharmony_ci int64_t int64Value = 1; 12723b3eb3cSopenharmony_ci uObject.AddItemToObject("int64_t", int64Value); 12823b3eb3cSopenharmony_ci bool boolValue = false; 12923b3eb3cSopenharmony_ci uObject.AddItemToObject("bool", boolValue); 13023b3eb3cSopenharmony_ci std::shared_ptr<UObject> sharedUObject = std::make_shared<UObject>(); 13123b3eb3cSopenharmony_ci uObject.AddItemToObject("shared_ptr", sharedUObject); 13223b3eb3cSopenharmony_ci 13323b3eb3cSopenharmony_ci /** 13423b3eb3cSopenharmony_ci * @tc.steps: step2. Serialize() 13523b3eb3cSopenharmony_ci * @tc.expected: step2. Asserting values through the get method. 13623b3eb3cSopenharmony_ci */ 13723b3eb3cSopenharmony_ci uObject.Hash(); 13823b3eb3cSopenharmony_ci 13923b3eb3cSopenharmony_ci char* buffer = nullptr; 14023b3eb3cSopenharmony_ci uObject.Serialize(buffer, 0); 14123b3eb3cSopenharmony_ci EXPECT_EQ(uObject.offset_, 0); 14223b3eb3cSopenharmony_ci buffer = new char[136]; 14323b3eb3cSopenharmony_ci uObject.Serialize(buffer, 136); 14423b3eb3cSopenharmony_ci EXPECT_EQ(uObject.offset_, 136); 14523b3eb3cSopenharmony_ci 14623b3eb3cSopenharmony_ci /** 14723b3eb3cSopenharmony_ci * @tc.steps: step3. Deserialize() test. 14823b3eb3cSopenharmony_ci * @tc.expected: step3. Asserting offset_. 14923b3eb3cSopenharmony_ci */ 15023b3eb3cSopenharmony_ci uObject.Deserialize(buffer, 136); 15123b3eb3cSopenharmony_ci EXPECT_EQ(uObject.offset_, 136); 15223b3eb3cSopenharmony_ci 15323b3eb3cSopenharmony_ci /** 15423b3eb3cSopenharmony_ci * @tc.steps: step4. delete buffer. 15523b3eb3cSopenharmony_ci */ 15623b3eb3cSopenharmony_ci delete[] buffer; 15723b3eb3cSopenharmony_ci buffer = nullptr; 15823b3eb3cSopenharmony_ci} 15923b3eb3cSopenharmony_ci 16023b3eb3cSopenharmony_ci/** 16123b3eb3cSopenharmony_ci * @tc.name: UObjectTest003 16223b3eb3cSopenharmony_ci * @tc.desc: Serialize() 16323b3eb3cSopenharmony_ci * @tc.type: FUNC 16423b3eb3cSopenharmony_ci */ 16523b3eb3cSopenharmony_ciHWTEST_F(UObjectTest, UObjectTest003, TestSize.Level1) 16623b3eb3cSopenharmony_ci{ 16723b3eb3cSopenharmony_ci UObject uObject; 16823b3eb3cSopenharmony_ci std::string value = ""; 16923b3eb3cSopenharmony_ci uObject.WriteString(value); 17023b3eb3cSopenharmony_ci const char* buffer = nullptr; 17123b3eb3cSopenharmony_ci int32_t bufferLen = 100; 17223b3eb3cSopenharmony_ci uObject.Deserialize(buffer, bufferLen); 17323b3eb3cSopenharmony_ci EXPECT_EQ(uObject.offset_, 0); 17423b3eb3cSopenharmony_ci} 17523b3eb3cSopenharmony_ci} // namespace OHOS::Ace