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 "VirtualMessageImpl.h"
19
20namespace {
21    bool g_executeSuccess = false;
22
23    int32_t SuccessCallback(const void *data)
24    {
25        g_executeSuccess = true;
26        return 0;
27    }
28
29    int32_t FailCallback(const void *data, uint16_t dataLength, uint16_t errorCode)
30    {
31        return 0;
32    }
33
34    TEST(VirtualMessageImplTest, RegistBundleTest)
35    {
36        const char* key = "test";
37        VirtualMessageImpl::GetInstance().RegistBundle(key, SuccessCallback, FailCallback);
38        EXPECT_TRUE(VirtualMessageImpl::GetInstance().callBacks.find(key) !=
39            VirtualMessageImpl::GetInstance().callBacks.end());
40    }
41
42    TEST(VirtualMessageImplTest, UnregistBundleTest)
43    {
44        const char* key = "test";
45        VirtualMessageImpl::GetInstance().UnregistBundle(key);
46        EXPECT_TRUE(VirtualMessageImpl::GetInstance().callBacks.find(key) ==
47            VirtualMessageImpl::GetInstance().callBacks.end());
48    }
49
50    TEST(VirtualMessageImplTest, StringToCharVectorTest)
51    {
52        std::string test = "123";
53        const char* testArr = test.c_str();
54        std::vector<char> vec = VirtualMessageImpl::GetInstance().StringToCharVector("123");
55        for (int i = 0; i < test.size(); i++) {
56            EXPECT_EQ(vec[i], testArr[i]);
57        }
58    }
59
60    TEST(VirtualMessageImplTest, SendVirtualMessageTest)
61    {
62        MessageInfo info;
63        info.deviceID = "1234";
64        info.bundleName = "index";
65        info.abilityName = "entry";
66        info.message = "test";
67        const char* key = "test";
68        VirtualMessageImpl::GetInstance().RegistBundle(key, SuccessCallback, FailCallback);
69        VirtualMessageImpl::GetInstance().SendVirtualMessage(info);
70        EXPECT_TRUE(g_executeSuccess);
71        VirtualMessageImpl::GetInstance().UnregistBundle(key);
72    }
73}