/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "gtest/gtest.h" #define private public #include "SharedData.h" #include "SharedDataManager.h" namespace { int g_intValue = 80; int g_intMinValue = 0; int g_intMaxValue = 255; std::string g_strValue = "zh_CN"; TEST(SharedDataTest, ConstructorTest) { SharedData(SharedDataType::HEARTBEAT_VALUE, g_intValue, g_intMinValue, g_intMaxValue); EXPECT_EQ(SharedData::GetData(SharedDataType::HEARTBEAT_VALUE), g_intValue); EXPECT_TRUE(SharedData::IsValid(SharedDataType::HEARTBEAT_VALUE, g_intMinValue)); EXPECT_TRUE(SharedData::IsValid(SharedDataType::HEARTBEAT_VALUE, g_intMaxValue)); SharedData(SharedDataType::LANGUAGE, g_strValue); EXPECT_EQ(SharedData::GetData(SharedDataType::LANGUAGE), g_strValue); SharedData(SharedDataType::WEARING_STATE, true); EXPECT_EQ(SharedData::GetData(SharedDataType::WEARING_STATE), true); } TEST(SharedDataTest, SetDataTest) { SharedData(SharedDataType::HEARTBEAT_VALUE, g_intValue, g_intMinValue, g_intMaxValue); int newValue = 180; SharedData::SetData(SharedDataType::HEARTBEAT_VALUE, newValue); EXPECT_EQ(SharedData::GetData(SharedDataType::HEARTBEAT_VALUE), newValue); } TEST(SharedDataTest, AppendNotifyTest) { SharedData(SharedDataType::HEARTBEAT_VALUE, g_intValue, g_intMinValue, g_intMaxValue); std::thread::id curThreadId = std::this_thread::get_id(); uint8_t num = 3; std::function func = [&num](uint8_t val) { num += val; }; SharedData::AppendNotify(SharedDataType::HEARTBEAT_VALUE, func, curThreadId); int newValue = 200; int retValue = num + newValue; SharedData::SetData(SharedDataType::HEARTBEAT_VALUE, newValue); SharedDataManager::CheckTick(); EXPECT_EQ(num, retValue); } }