1/*
2 * Copyright (c) 2023 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#define protected public
19#include "ace_forward_compatibility.h"
20#include "interfaces/inner_api/ace/ui_event.h"
21#include "interfaces/inner_api/ace/ui_event_func.h"
22#include "interfaces/inner_api/ace/ui_event_observer.h"
23#include "test/mock/core/common/mock_container.h"
24#include "test/mock/core/common/mock_theme_manager.h"
25#include "test/mock/core/pipeline/mock_pipeline_context.h"
26
27#include "core/components/popup/popup_theme.h"
28
29using namespace testing;
30using namespace testing::ext;
31
32namespace OHOS::Ace {
33class UiEventTest : public testing::Test {
34public:
35    static void SetUpTestSuite()
36    {
37        NG::MockPipelineContext::SetUp();
38        MockContainer::SetUp();
39        MockContainer::Current()->pipelineContext_ = PipelineBase::GetCurrentContext();
40        auto themeManager = AceType::MakeRefPtr<MockThemeManager>();
41        PipelineBase::GetCurrentContext()->SetThemeManager(themeManager);
42        PipelineBase::GetCurrentContext()->SetEventManager(AceType::MakeRefPtr<EventManager>());
43        EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<PopupTheme>()));
44    }
45    static void TearDownTestSuite()
46    {
47        MockContainer::Current()->pipelineContext_ = nullptr;
48        NG::MockPipelineContext::TearDown();
49    }
50    void SetUp() {}
51    void TearDown() {}
52};
53
54/**
55 * @tc.name: UiEventTest001
56 * @tc.desc: test UIEvent GetNodeProperty
57 * @tc.type: FUNC
58 */
59HWTEST_F(UiEventTest, UiEventTest001, TestSize.Level1)
60{
61    /**
62     * @tc.steps1: initialize parameters.
63     */
64    std::string pageUrl = "Pages/Index";
65    auto nodeProperties = std::unordered_map<std::string, std::string>();
66
67    /**
68     * @tc.steps2: Call the function GetNodeProperty.
69     * @tc.expected: The function is run ok and IsAvailable() is true.
70     */
71    UIEvent::GetNodeProperty(pageUrl, nodeProperties);
72
73    bool result = UIEventFunc::Get().IsAvailable();
74    EXPECT_FALSE(result);
75}
76
77/**
78 * @tc.name: UiEventTest002
79 * @tc.desc: test UIEvent GetSimplifiedInspectorTree
80 * @tc.type: FUNC
81 */
82HWTEST_F(UiEventTest, UiEventTest002, TestSize.Level1)
83{
84    /**
85     * @tc.steps1: initialize parameters.
86     */
87    std::string tree = "treeTest";
88
89    /**
90     * @tc.steps2: Call the function GetSimplifiedInspectorTree.
91     * @tc.expected: The function is run ok and IsAvailable() is true.
92     */
93    UIEvent::GetSimplifiedInspectorTree(tree);
94    bool result = UIEventFunc::Get().IsAvailable();
95    EXPECT_FALSE(result);
96}
97
98/**
99 * @tc.name: UiEventTest003
100 * @tc.desc: test UIEventFunc RegisterUIEventObserver/UnregisterUIEventObserver
101 * @tc.type: FUNC
102 */
103HWTEST_F(UiEventTest, UiEventTest003, TestSize.Level1)
104{
105    /**
106     * @tc.steps1: initialize parameters Call the function RegisterUIEventObserver.
107     */
108    std::string config = "configTest";
109    const std::shared_ptr<UIEventObserver> observer = nullptr;
110    UIEventFunc::RegisterUIEventObserver(config, observer);
111    bool result = UIEventFunc::Get().IsAvailable();
112    EXPECT_FALSE(result);
113
114    /**
115     * @tc.steps2: Call the function UnregisterUIEventObserver.
116     * @tc.expected: The function is run ok and IsAvailable() is true.
117     */
118    UIEventFunc::UnregisterUIEventObserver(observer);
119    bool result2 = UIEventFunc::Get().IsAvailable();
120    EXPECT_FALSE(result2);
121}
122} // namespace OHOS::Ace