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/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 * @tc.require: AR000DD66G
58 * @tc.author: chenxuankai
59 */
60 HWTEST_F(DomRatingTest, CreateDOMNodeFromDsl001, TestSize.Level1)
61 {
62 /**
63 * @tc.steps: step1. the json string of DomRating with all attributes set.
64 */
65 const std::string jsonRatingStr = ""
66 "{ "
67 " \"tag\": \"rating\", "
68 " \"attr\": [{ "
69 " \"numstars\" : \"5\" "
70 " }, "
71 " { "
72 " \"rating\" : \"2.5\" "
73 " }, "
74 " { "
75 " \"stepsize\" : \"0.5\" "
76 " }, "
77 " { "
78 " \"indicator\" : \"true\" "
79 " }], "
80 " \"style\": [{ "
81 " \"starBackground\":\"/example/background\" "
82 " }, "
83 " { "
84 " \"starSecondary\" : \"/example/secondary\" "
85 " }, "
86 " { "
87 " \"starForeground\" : \"/example/foreground\" "
88 " }, "
89 " { "
90 " \"height\" : \"200px\" "
91 " }, "
92 " { "
93 " \"width\" : \"600px\" "
94 " }], "
95 " \"event\": [ \"change\" ] "
96 "}";
97
98 /**
99 * @tc.steps: step2. call JsonUtil interface and create DomRating.
100 */
101 auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonRatingStr);
102 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot);
103 RefPtr<RatingComponent> ratingComponent = AceType::DynamicCast<RatingComponent>(boxChild->GetChild());
104
105 /**
106 * @tc.steps: step3. Verify whether all attributes of ratingComponent are as excepted.
107 * @tc.expected: step3. All attributes of ratingComponent are as excepted.
108 */
109 ASSERT_TRUE(ratingComponent != nullptr);
110 ASSERT_TRUE(ratingComponent->GetStarNum() == RATING_STAR_NUM_VALUE);
111 ASSERT_TRUE(ratingComponent->GetRatingScore() == RATING_SCORE_VALUE);
112 ASSERT_TRUE(ratingComponent->GetStepSize() == RATING_STEPSIZE_VALUE);
113 ASSERT_TRUE(ratingComponent->GetIndicator());
114 ASSERT_TRUE(ratingComponent->GetForegroundSrc() == FOREGROUND_SRC_VALUE);
115 ASSERT_TRUE(ratingComponent->GetSecondarySrc() == SECONDARY_SRC_VALUE);
116 ASSERT_TRUE(ratingComponent->GetBackgroundSrc() == BACKGROUND_SRC_VALUE);
117 ASSERT_TRUE(boxChild->GetWidthDimension().Value() == RATING_WIDTH_VALUE);
118 ASSERT_TRUE(boxChild->GetHeightDimension().Value() == RATING_HEIGHT_VALUE);
119 ASSERT_TRUE(ratingComponent->GetChangeEventId() == std::to_string(domNodeRoot->GetNodeId()));
120 }
121
122 /**
123 * @tc.name: CreateDOMNodeFromDsl002
124 * @tc.desc: Verify that DomRating can create ratingComponent correctly with no attribute set.
125 * @tc.type: FUNC
126 * @tc.require: AR000DD66G
127 * @tc.author: chenxuankai
128 */
129 HWTEST_F(DomRatingTest, CreateDOMNodeFromDsl002, TestSize.Level1)
130 {
131 /**
132 * @tc.steps: step1. the json string of DomRating with no attribute set.
133 */
134 const std::string jsonRatingStr = ""
135 "{ "
136 " \"tag\": \"rating\" "
137 "}";
138
139 /**
140 * @tc.steps: step2. call JsonUtil interface and create DomRating.
141 */
142 auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonRatingStr);
143 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot);
144 RefPtr<RatingComponent> ratingComponent = AceType::DynamicCast<RatingComponent>(boxChild->GetChild());
145
146 /**
147 * @tc.steps: step3. Verify whether all attributes of ratingComponent are as excepted.
148 * @tc.expected: step3. All attributes of ratingComponent are as excepted.
149 */
150 auto themeManager = AceType::MakeRefPtr<ThemeManager>();
151 RefPtr<RatingTheme> theme = themeManager->GetTheme<RatingTheme>();
152 ASSERT_TRUE(theme);
153 ASSERT_TRUE(ratingComponent != nullptr);
154 ASSERT_TRUE(ratingComponent->GetStarNum() == theme->GetStarNum());
155 ASSERT_TRUE(ratingComponent->GetRatingScore() == theme->GetRatingScore());
156 ASSERT_TRUE(ratingComponent->GetStepSize() == theme->GetStepSize());
157 ASSERT_TRUE(!ratingComponent->GetIndicator());
158 ASSERT_TRUE(ratingComponent->GetForegroundSrc().empty());
159 ASSERT_TRUE(ratingComponent->GetSecondarySrc().empty());
160 ASSERT_TRUE(ratingComponent->GetBackgroundSrc().empty());
161 ASSERT_TRUE(boxChild->GetWidthDimension().Value() == theme->GetRatingWidth().Value());
162 ASSERT_TRUE(boxChild->GetHeightDimension().Value() == theme->GetRatingHeight().Value());
163 }
164
165 } // namespace OHOS::Ace::Framework
166