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/divider/divider_component.h"
19 #include "core/components/divider/divider_theme.h"
20 #include "frameworks/bridge/test/unittest/jsfrontend/dom_node_factory.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS::Ace::Framework {
26 namespace {
27
28 const Dimension DEFAULT_STROKE_WIDTH = Dimension(1.0);
29 const Dimension STROKE_WIDTH = Dimension(1.0);
30 const Color DIVIDER_COLOR = Color(0xffff0000);
31
32 const std::string DIVIDER_TEST_001 = ""
33 "{ "
34 " \"tag\": \"divider\" "
35 "}";
36 const std::string DIVIDER_TEST_002 = ""
37 "{ "
38 " \"tag\": \"divider\", "
39 " \"attr\": [{ "
40 " \"vertical\": \"true\" "
41 " }], "
42 " \"style\": [{ "
43 " \"strokeWidth\": \"1px\" "
44 " }, "
45 " { "
46 " \"lineCap\": \"round\" "
47 " }, "
48 " { "
49 " \"color\": \"#ff0000\" "
50 " }] "
51 "}";
52
53 } // namespace
54
55 class DomDividerTest : public testing::Test {
56 public:
57 static void SetUpTestCase();
58 static void TearDownTestCase();
59 void SetUp() override;
60 void TearDown() override;
61 };
62
SetUpTestCase()63 void DomDividerTest::SetUpTestCase() {}
TearDownTestCase()64 void DomDividerTest::TearDownTestCase() {}
SetUp()65 void DomDividerTest::SetUp() {}
TearDown()66 void DomDividerTest::TearDown() {}
67
68 /**
69 * @tc.name: DomDividerCreatorTest001
70 * @tc.desc: Test divider node are created as expected.
71 * @tc.type: FUNC
72 * @tc.require: AR000DQ1Q5 AR000DQ1Q6
73 * @tc.author: jiangyingjie
74 */
75 HWTEST_F(DomDividerTest, DomDividerCreatorTest001, TestSize.Level1)
76 {
77 /**
78 * @tc.steps: step1. Construct string with right fields, then create divider node with it.
79 * @tc.expected: step1. divider node are created successfully.
80 */
81 auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(DIVIDER_TEST_001);
82 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot);
83 auto divider = AceType::DynamicCast<DividerComponent>(boxChild->GetChild());
84 ASSERT_TRUE(divider);
85
86 /**
87 * @tc.steps: step2. Check styles and attributes of created divider node.
88 * @tc.expected: step2. The styles and attributes are as expected.
89 */
90 auto themeManager = AceType::MakeRefPtr<ThemeManager>();
91 auto theme = themeManager->GetTheme<DividerTheme>();
92 ASSERT_TRUE(theme);
93 EXPECT_EQ(divider->GetStrokeWidth(), DEFAULT_STROKE_WIDTH);
94 ASSERT_FALSE(divider->IsVertical());
95 EXPECT_EQ(divider->GetDividerColor(), theme->GetColor());
96 EXPECT_EQ(divider->GetLineCap(), LineCap::BUTT);
97 }
98
99 /**
100 * @tc.name: DomDividerCreatorTest002
101 * @tc.desc: Test divider node are created as expected.
102 * @tc.type: FUNC
103 * @tc.require: AR000DQ1Q4 AR000DQ1Q6
104 * @tc.author: jiangyingjie
105 */
106 HWTEST_F(DomDividerTest, DomDividerCreatorTest002, TestSize.Level1)
107 {
108 /**
109 * @tc.steps: step1. Construct string with right fields, then create divider node with it.
110 * @tc.expected: step1. divider node are created successfully.
111 */
112 auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(DIVIDER_TEST_002);
113 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot);
114 auto divider = AceType::DynamicCast<DividerComponent>(boxChild->GetChild());
115 ASSERT_TRUE(divider);
116
117 /**
118 * @tc.steps: step2. Check styles and attributes of created divider node.
119 * @tc.expected: step2. The styles and attributes are as expected.
120 */
121 EXPECT_EQ(divider->GetStrokeWidth(), STROKE_WIDTH);
122 ASSERT_TRUE(divider->IsVertical());
123 EXPECT_EQ(divider->GetDividerColor(), DIVIDER_COLOR);
124 EXPECT_EQ(divider->GetLineCap(), LineCap::ROUND);
125 }
126
127 } // namespace OHOS::Ace::Framework
128