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/rating/rating_theme.h"
19 #include "core/pipeline/base/constants.h"
20 #include "frameworks/bridge/common/dom/dom_rating.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 std::string FOREGROUND_SRC_VALUE = "/example/foreground";
30 const std::string SECONDARY_SRC_VALUE = "/example/secondary";
31 const std::string BACKGROUND_SRC_VALUE = "/example/background";
32 const double RATING_WIDTH_VALUE = 600.0;
33 const double RATING_HEIGHT_VALUE = 200.0;
34 const double RATING_SCORE_VALUE = 2.5;
35 const double RATING_STEPSIZE_VALUE = 0.5;
36 const int32_t RATING_STAR_NUM_VALUE = 5;
37
38 } // namespace
39
40 class DomRatingTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46 };
47
SetUpTestCase()48 void DomRatingTest::SetUpTestCase() {}
TearDownTestCase()49 void DomRatingTest::TearDownTestCase() {}
SetUp()50 void DomRatingTest::SetUp() {}
TearDown()51 void DomRatingTest::TearDown() {}
52
53 /**
54 * @tc.name: CreateDOMNodeFromDsl001
55 * @tc.desc: Verify that DomRating can create ratingComponent correctly with all attributes set.
56 * @tc.type: FUNC
57 */
58 HWTEST_F(DomRatingTest, CreateDOMNodeFromDsl001, TestSize.Level1)
59 {
60 /**
61 * @tc.steps: step1. the json string of DomRating with all attributes set.
62 */
63 const std::string jsonRatingStr = ""
64 "{ "
65 " \"tag\": \"rating\", "
66 " \"attr\": [{ "
67 " \"numstars\" : \"5\" "
68 " }, "
69 " { "
70 " \"rating\" : \"2.5\" "
71 " }, "
72 " { "
73 " \"stepsize\" : \"0.5\" "
74 " }, "
75 " { "
76 " \"indicator\" : \"true\" "
77 " }], "
78 " \"style\": [{ "
79 " \"starBackground\":\"/example/background\" "
80 " }, "
81 " { "
82 " \"starSecondary\" : \"/example/secondary\" "
83 " }, "
84 " { "
85 " \"starForeground\" : \"/example/foreground\" "
86 " }, "
87 " { "
88 " \"height\" : \"200px\" "
89 " }, "
90 " { "
91 " \"width\" : \"600px\" "
92 " }], "
93 " \"event\": [ \"change\" ] "
94 "}";
95
96 /**
97 * @tc.steps: step2. call JsonUtil interface and create DomRating.
98 */
99 auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonRatingStr);
100 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot);
101 RefPtr<RatingComponent> ratingComponent = AceType::DynamicCast<RatingComponent>(boxChild->GetChild());
102
103 /**
104 * @tc.steps: step3. Verify whether all attributes of ratingComponent are as excepted.
105 * @tc.expected: step3. All attributes of ratingComponent are as excepted.
106 */
107 ASSERT_TRUE(ratingComponent != nullptr);
108 ASSERT_TRUE(ratingComponent->GetStarNum() == RATING_STAR_NUM_VALUE);
109 ASSERT_TRUE(ratingComponent->GetRatingScore() == RATING_SCORE_VALUE);
110 ASSERT_TRUE(ratingComponent->GetStepSize() == RATING_STEPSIZE_VALUE);
111 ASSERT_TRUE(ratingComponent->GetIndicator());
112 ASSERT_TRUE(ratingComponent->GetForegroundSrc() == FOREGROUND_SRC_VALUE);
113 ASSERT_TRUE(ratingComponent->GetSecondarySrc() == SECONDARY_SRC_VALUE);
114 ASSERT_TRUE(ratingComponent->GetBackgroundSrc() == BACKGROUND_SRC_VALUE);
115 ASSERT_TRUE(boxChild->GetWidthDimension().Value() == RATING_WIDTH_VALUE);
116 ASSERT_TRUE(boxChild->GetHeightDimension().Value() == RATING_HEIGHT_VALUE);
117 ASSERT_TRUE(ratingComponent->GetChangeEventId() == std::to_string(domNodeRoot->GetNodeId()));
118 }
119
120 /**
121 * @tc.name: CreateDOMNodeFromDsl002
122 * @tc.desc: Verify that DomRating can create ratingComponent correctly with no attribute set.
123 * @tc.type: FUNC
124 */
125 HWTEST_F(DomRatingTest, CreateDOMNodeFromDsl002, TestSize.Level1)
126 {
127 /**
128 * @tc.steps: step1. the json string of DomRating with no attribute set.
129 */
130 const std::string jsonRatingStr = ""
131 "{ "
132 " \"tag\": \"rating\" "
133 "}";
134
135 /**
136 * @tc.steps: step2. call JsonUtil interface and create DomRating.
137 */
138 auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonRatingStr);
139 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot);
140 RefPtr<RatingComponent> ratingComponent = AceType::DynamicCast<RatingComponent>(boxChild->GetChild());
141
142 /**
143 * @tc.steps: step3. Verify whether all attributes of ratingComponent are as excepted.
144 * @tc.expected: step3. All attributes of ratingComponent are as excepted.
145 */
146 auto themeManager = AceType::MakeRefPtr<ThemeManagerImpl>();
147 RefPtr<RatingTheme> theme = themeManager->GetTheme<RatingTheme>();
148 ASSERT_TRUE(theme);
149 ASSERT_TRUE(ratingComponent != nullptr);
150 ASSERT_TRUE(ratingComponent->GetStarNum() == theme->GetStarNum());
151 ASSERT_TRUE(ratingComponent->GetRatingScore() == theme->GetRatingScore());
152 ASSERT_TRUE(ratingComponent->GetStepSize() == theme->GetStepSize());
153 ASSERT_TRUE(!ratingComponent->GetIndicator());
154 ASSERT_TRUE(ratingComponent->GetForegroundSrc().empty());
155 ASSERT_TRUE(ratingComponent->GetSecondarySrc().empty());
156 ASSERT_TRUE(ratingComponent->GetBackgroundSrc().empty());
157 ASSERT_TRUE(boxChild->GetWidthDimension().Value() == theme->GetRatingWidth().Value());
158 ASSERT_TRUE(boxChild->GetHeightDimension().Value() == theme->GetRatingHeight().Value());
159 }
160
161 } // namespace OHOS::Ace::Framework
162