1 /* 2 * Copyright (c) 2021 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 #define LOG_TAG "SerializableTest" 16 #include "log_print.h" 17 #include "serializable/serializable.h" 18 #include "gtest/gtest.h" 19 using namespace testing::ext; 20 using namespace OHOS::DistributedData; 21 22 class SerializableTest : public testing::Test { 23 public: 24 struct Normal final : public Serializable { 25 public: 26 std::string name = "Test"; 27 int32_t count = 0; 28 uint32_t status = 1; 29 int64_t value = 2; 30 bool isClear = false; 31 std::vector<std::string> cols{ "123", "345", "789" }; 32 std::vector<std::vector<int32_t>> colRow{ { 123, 345, 789 }, { 123, 345, 789 } }; 33 MarshalSerializableTest::Normal34 bool Marshal(json &node) const override 35 { 36 SetValue(node[GET_NAME(name)], name); 37 SetValue(node[GET_NAME(count)], count); 38 SetValue(node[GET_NAME(status)], status); 39 SetValue(node[GET_NAME(value)], value); 40 SetValue(node[GET_NAME(isClear)], isClear); 41 SetValue(node[GET_NAME(cols)], cols); 42 SetValue(node[GET_NAME(colRow)], colRow); 43 return true; 44 } UnmarshalSerializableTest::Normal45 bool Unmarshal(const json &node) override 46 { 47 GetValue(node, GET_NAME(name), name); 48 GetValue(node, GET_NAME(count), count); 49 GetValue(node, GET_NAME(status), status); 50 GetValue(node, GET_NAME(value), value); 51 GetValue(node, GET_NAME(isClear), isClear); 52 GetValue(node, GET_NAME(cols), cols); 53 GetValue(node, GET_NAME(colRow), colRow); 54 return true; 55 } operator ==SerializableTest::Normal56 bool operator == (const Normal &ref) const 57 { 58 return name == ref.name && count == ref.count && status == ref.status && value == ref.value 59 && isClear == ref.isClear && cols == ref.cols; 60 } 61 }; 62 63 struct NormalEx final : public Serializable { 64 public: 65 std::vector<Normal> normals {Normal(), Normal()}; 66 Normal normal; 67 int32_t count = 123; 68 std::string name = "wdt"; MarshalSerializableTest::NormalEx69 bool Marshal(json &node) const override 70 { 71 SetValue(node[GET_NAME(normals)], normals); 72 SetValue(node[GET_NAME(normal)], normal); 73 SetValue(node[GET_NAME(count)], count); 74 SetValue(node[GET_NAME(name)], name); 75 return true; 76 } UnmarshalSerializableTest::NormalEx77 bool Unmarshal(const json &node) override 78 { 79 GetValue(node, GET_NAME(normals), normals); 80 GetValue(node, GET_NAME(normal), normal); 81 GetValue(node, GET_NAME(count), count); 82 GetValue(node, GET_NAME(name), name); 83 return true; 84 } operator ==SerializableTest::NormalEx85 bool operator==(const NormalEx &normalEx) const 86 { 87 return normals == normalEx.normals && count == normalEx.count && name == normalEx.name; 88 } 89 }; SetUpTestCase(void)90 static void SetUpTestCase(void) 91 { 92 } TearDownTestCase(void)93 static void TearDownTestCase(void) 94 { 95 } SetUp()96 void SetUp() 97 { 98 Test::SetUp(); 99 } TearDown()100 void TearDown() 101 { 102 Test::TearDown(); 103 } 104 }; 105 106 /** 107 * @tc.name: SerializableSuiteGetVal 108 * @tc.desc: Get Value. 109 * @tc.type: FUNC 110 * @tc.require: 111 * @tc.author: Sven Wang 112 */ 113 HWTEST_F(SerializableTest, GetNormalVal, TestSize.Level2) 114 { 115 ZLOGI("SerializableSuite GetVal begin."); 116 Normal normal; 117 normal.name = "normal"; 118 normal.count = -1; 119 normal.status = 12; 120 normal.value = -56; 121 normal.isClear = true; 122 normal.cols = {"adfasdfas"}; 123 auto jstr = to_string(normal.Marshall()); 124 Normal normal1; 125 normal1.Unmarshall(jstr); 126 ASSERT_TRUE(normal == normal1) << normal1.name; 127 } 128 129 /** 130 * @tc.name: Delete Serializable 131 * @tc.desc: can delete child class, but not delete parent class point. 132 * @tc.type: FUNC 133 * @tc.require: 134 * @tc.author: Sven Wang 135 */ 136 HWTEST_F(SerializableTest, DeleteSerializable, TestSize.Level2) 137 { 138 ZLOGI("SerializableSuite GetVal begin."); 139 NormalEx *normalEx = new NormalEx(); 140 delete normalEx; 141 } 142 143 /** 144 * @tc.name: SerializableSuiteGetMutilVal 145 * @tc.desc: mutil value case. 146 * @tc.type: FUNC 147 * @tc.require: 148 * @tc.author: Sven Wang 149 */ 150 HWTEST_F(SerializableTest, GetMutilVal, TestSize.Level2) 151 { 152 ZLOGI("SerializableSuite GetVal begin."); 153 NormalEx normalEx; 154 normalEx.normals = {Normal()}; 155 normalEx.name = "normalEx"; 156 auto jstr = to_string(normalEx.Marshall()); 157 NormalEx normal1; 158 normal1.Unmarshall(jstr); 159 ASSERT_TRUE(normalEx == normal1) << normal1.name; 160 } 161