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 <string>
17#include "gtest/gtest.h"
18#define private public
19#include "EventHandler.h"
20
21namespace {
22    int g_originNum = 0;
23    int g_replacedNum = 3;
24
25    // 测试拷贝构造函数是否被删除
26    TEST(EventHandlerTest, CopyConstructorDeletedTest)
27    {
28        EXPECT_TRUE(std::is_copy_constructible<OHOS::AppExecFwk::EventHandler>::value == false);
29    }
30
31    // 测试赋值运算符是否被删除
32    TEST(EventHandlerTest, AssignmentOperatorDeletedTest)
33    {
34        EXPECT_TRUE(std::is_copy_assignable<OHOS::AppExecFwk::EventHandler>::value == false);
35    }
36
37    TEST(EventHandlerTest, SetMainThreadIdTest)
38    {
39        std::thread::id tid = std::this_thread::get_id();
40        OHOS::AppExecFwk::EventHandler::SetMainThreadId(tid);
41        EXPECT_EQ(OHOS::AppExecFwk::EventRunner::GetMainEventRunner().threadId, tid);
42    }
43
44    TEST(EventHandlerTest, IsCurrentRunnerThreadTest)
45    {
46        std::thread commandThead([]() {
47            EXPECT_FALSE(OHOS::AppExecFwk::EventHandler::IsCurrentRunnerThread());
48        });
49        commandThead.join();
50        EXPECT_TRUE(OHOS::AppExecFwk::EventHandler::IsCurrentRunnerThread());
51    }
52
53    TEST(EventHandlerTest, PostTaskTest)
54    {
55        OHOS::AppExecFwk::EventHandler::PostTask([]() {
56            g_originNum = g_replacedNum;
57        });
58        EXPECT_TRUE(OHOS::AppExecFwk::EventRunner::GetMainEventRunner().queue.size() > 0);
59    }
60
61    TEST(EventHandlerTest, RunTest)
62    {
63        OHOS::AppExecFwk::EventHandler::Run();
64        EXPECT_EQ(g_originNum, g_replacedNum);
65    }
66}