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