• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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  */
53 HWTEST_F(DomPopupTest, DomPopupCreatorTest001, TestSize.Level1)
54 {
55     /**
56      * @tc.steps: step1. construct the json string of popup component with tag only.
57      */
58     const std::string jsonPopupStr = ""
59                                     "{                                                      "
60                                     "  \"tag\": \"popup\"                                   "
61                                     "}";
62     /**
63      * @tc.steps: step2. Verify whether the DomPopup.
64      * @tc.expected: step3. DomPopup is not null.
65      */
66     auto popup = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonPopupStr);
67     EXPECT_TRUE(popup);
68 }
69 
70 /**
71  * @tc.name: DomPopupTest002
72  * @tc.desc: Verify that DomPopup can be set attributes and styles.
73  * @tc.type: FUNC
74  */
75 HWTEST_F(DomPopupTest, DomPopupTest002, TestSize.Level1)
76 {
77     /**
78      * @tc.steps: step1. construct the json string of DomPopup with attributes and styles.
79      */
80     const std::string jsonPopupStr = ""
81                                     "{                                                         "
82                                     "  \"tag\": \"popup\",                                     "
83                                     "  \"attr\": [                                             "
84                                     "              {"
85                                     "                \"placement\" : \"left\"                  "
86                                     "              }],                                         "
87                                     "  \"style\":  [{ "
88                                     "                \"maskColor\" : \"#0x0000ff\"             "
89                                     "              }]"
90                                     "}";
91 
92     /**
93      * @tc.steps: step2. call jsonPopupStr interface, create DomPopup and set its style.
94      */
95     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonPopupStr);
96     auto popupChild = AceType::DynamicCast<PopupComponent>(domNodeRoot->GetSpecializedComponent());
97     ACE_DCHECK(popupChild);
98 
99     /**
100      * @tc.steps: step3. Check all the attributes and styles matched.
101      * @tc.expected: step3. All the attributes and styles are matched.
102      */
103     EXPECT_TRUE(popupChild);
104     EXPECT_EQ(popupChild->GetPopupParam()->GetPlacement(), PLACEMENT_VALUE);
105     EXPECT_EQ(popupChild->GetPopupParam()->GetMaskColor(), Color::FromString(MASK_COLOR_DEFAULT));
106 }
107 
108 /**
109  * @tc.name: DomPopupTest003
110  * @tc.desc: Test add event to popup component successfully.
111  * @tc.type: FUNC
112  */
113 HWTEST_F(DomPopupTest, DomPopupTest003, TestSize.Level1)
114 {
115     /**
116      * @tc.steps: step1. Construct string with right fields, then create popup node with it.
117      * @tc.expected: step1. Popup node and is created successfully.
118      */
119     const std::string jsonPopupStr = ""
120                                         "{                                      "
121                                         "  \"tag\": \"popup\",                  "
122                                         "  \"event\": [ \"visibilitychange\" ]  "
123                                         "}";
124 
125     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonPopupStr);
126     auto popupChild = AceType::DynamicCast<PopupComponent>(domNodeRoot->GetSpecializedComponent());
127     ACE_DCHECK(popupChild);
128 
129     /**
130      * @tc.steps: step2. Check eventId of created popup component.
131      * @tc.expected: step2. The eventId value of popup component is as expected.
132      */
133     EXPECT_TRUE(popupChild);
134     EXPECT_EQ(popupChild->GetPopupParam()->GetOnVisibilityChange(), std::to_string(domNodeRoot->GetNodeId()));
135 }
136 
137 } // namespace OHOS::Ace::Framework
138