1 /*
2 * Copyright (c) 2021 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
18 #include "core/components/common/layout/constants.h"
19 #include "core/components/popup/popup_component.h"
20 #include "frameworks/bridge/common/dom/dom_document.h"
21 #include "frameworks/bridge/common/dom/dom_popup.h"
22 #include "frameworks/bridge/test/unittest/jsfrontend/dom_node_factory.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS::Ace::Framework {
28 namespace {
29
30 const Placement PLACEMENT_VALUE = Placement::LEFT;
31 const std::string MASK_COLOR_DEFAULT = "#0x0000ff";
32
33 } // namespace
34
35 class DomPopupTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp() override;
40 void TearDown() override;
41 };
42
SetUpTestCase()43 void DomPopupTest::SetUpTestCase() {}
TearDownTestCase()44 void DomPopupTest::TearDownTestCase() {}
SetUp()45 void DomPopupTest::SetUp() {}
TearDown()46 void DomPopupTest::TearDown() {}
47
48 /**
49 * @tc.name: DomPopupCreatorTest001
50 * @tc.desc: Test create popup node successfully and popupcomponent create as desire.
51 * @tc.type: FUNC
52 * @tc.require: AR000DD66M
53 * @tc.author: chenbenzhi
54 */
55 HWTEST_F(DomPopupTest, DomPopupCreatorTest001, TestSize.Level1)
56 {
57 /**
58 * @tc.steps: step1. construct the json string of popup component with tag only.
59 */
60 const std::string jsonPopupStr = ""
61 "{ "
62 " \"tag\": \"popup\" "
63 "}";
64 /**
65 * @tc.steps: step2. Verify whether the DomPopup.
66 * @tc.expected: step3. DomPopup is not null.
67 */
68 auto popup = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonPopupStr);
69 EXPECT_TRUE(popup);
70 }
71
72 /**
73 * @tc.name: DomPopupTest002
74 * @tc.desc: Verify that DomPopup can be set attributes and styles.
75 * @tc.type: FUNC
76 * @tc.require: AR000DD66M
77 * @tc.author: chenbenzhi
78 */
79 HWTEST_F(DomPopupTest, DomPopupTest002, TestSize.Level1)
80 {
81 /**
82 * @tc.steps: step1. construct the json string of DomPopup with attributes and styles.
83 */
84 const std::string jsonPopupStr = ""
85 "{ "
86 " \"tag\": \"popup\", "
87 " \"attr\": [ "
88 " {"
89 " \"placement\" : \"left\" "
90 " }], "
91 " \"style\": [{ "
92 " \"maskColor\" : \"#0x0000ff\" "
93 " }]"
94 "}";
95
96 /**
97 * @tc.steps: step2. call jsonPopupStr interface, create DomPopup and set its style.
98 */
99 auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonPopupStr);
100 auto popupChild = AceType::DynamicCast<PopupComponent>(domNodeRoot->GetSpecializedComponent());
101 ACE_DCHECK(popupChild);
102
103 /**
104 * @tc.steps: step3. Check all the attributes and styles matched.
105 * @tc.expected: step3. All the attributes and styles are matched.
106 */
107 EXPECT_TRUE(popupChild);
108 EXPECT_EQ(popupChild->GetPopupParam()->GetPlacement(), PLACEMENT_VALUE);
109 EXPECT_EQ(popupChild->GetPopupParam()->GetMaskColor(), Color::FromString(MASK_COLOR_DEFAULT));
110 }
111
112 /**
113 * @tc.name: DomPopupTest003
114 * @tc.desc: Test add event to popup component successfully.
115 * @tc.type: FUNC
116 * @tc.require: AR000DD66M
117 * @tc.author: chenbenzhi
118 */
119 HWTEST_F(DomPopupTest, DomPopupTest003, TestSize.Level1)
120 {
121 /**
122 * @tc.steps: step1. Construct string with right fields, then create popup node with it.
123 * @tc.expected: step1. Popup node and is created successfully.
124 */
125 const std::string jsonPopupStr = ""
126 "{ "
127 " \"tag\": \"popup\", "
128 " \"event\": [ \"visibilitychange\" ] "
129 "}";
130
131 auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonPopupStr);
132 auto popupChild = AceType::DynamicCast<PopupComponent>(domNodeRoot->GetSpecializedComponent());
133 ACE_DCHECK(popupChild);
134
135 /**
136 * @tc.steps: step2. Check eventId of created popup component.
137 * @tc.expected: step2. The eventId value of popup component is as expected.
138 */
139 EXPECT_TRUE(popupChild);
140 EXPECT_EQ(popupChild->GetPopupParam()->GetOnVisibilityChange(), std::to_string(domNodeRoot->GetNodeId()));
141 }
142
143 } // namespace OHOS::Ace::Framework
144