1 /* 2 * Copyright (c) 2024 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 #define private public 18 #include "SharedData.h" 19 #include "SharedDataManager.h" 20 using namespace std; 21 22 namespace { 23 int g_intValue = 80; 24 int g_intMinValue = 0; 25 int g_intMaxValue = 255; 26 std::string g_strValue = "zh_CN"; 27 TEST(SharedDataTest,ConstructorTest)28 TEST(SharedDataTest, ConstructorTest) 29 { 30 SharedData<uint8_t>(SharedDataType::HEARTBEAT_VALUE, g_intValue, g_intMinValue, g_intMaxValue); 31 EXPECT_EQ(SharedData<uint8_t>::GetData(SharedDataType::HEARTBEAT_VALUE), g_intValue); 32 EXPECT_TRUE(SharedData<uint8_t>::IsValid(SharedDataType::HEARTBEAT_VALUE, g_intMinValue)); 33 EXPECT_TRUE(SharedData<uint8_t>::IsValid(SharedDataType::HEARTBEAT_VALUE, g_intMaxValue)); 34 SharedData<std::string>(SharedDataType::LANGUAGE, g_strValue); 35 EXPECT_EQ(SharedData<std::string>::GetData(SharedDataType::LANGUAGE), g_strValue); 36 SharedData<bool>(SharedDataType::WEARING_STATE, true); 37 EXPECT_EQ(SharedData<bool>::GetData(SharedDataType::WEARING_STATE), true); 38 } 39 TEST(SharedDataTest,SetDataTest)40 TEST(SharedDataTest, SetDataTest) 41 { 42 SharedData<uint8_t>(SharedDataType::HEARTBEAT_VALUE, g_intValue, g_intMinValue, g_intMaxValue); 43 int newValue = 180; 44 SharedData<uint8_t>::SetData(SharedDataType::HEARTBEAT_VALUE, newValue); 45 EXPECT_EQ(SharedData<uint8_t>::GetData(SharedDataType::HEARTBEAT_VALUE), newValue); 46 } 47 TEST(SharedDataTest,AppendNotifyTest)48 TEST(SharedDataTest, AppendNotifyTest) 49 { 50 SharedData<uint8_t>(SharedDataType::HEARTBEAT_VALUE, g_intValue, g_intMinValue, g_intMaxValue); 51 thread::id curThreadId = this_thread::get_id(); 52 uint8_t num = 3; 53 std::function<void(uint8_t)> func = [&num](uint8_t val) { num += val; }; 54 SharedData<uint8_t>::AppendNotify(SharedDataType::HEARTBEAT_VALUE, func, curThreadId); 55 int newValue = 200; 56 int retValue = num + newValue; 57 SharedData<uint8_t>::SetData(SharedDataType::HEARTBEAT_VALUE, newValue); 58 SharedDataManager::CheckTick(); 59 EXPECT_EQ(num, retValue); 60 } 61 }