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