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 "frameworks/bridge/common/dom/dom_document.h"
20 #include "frameworks/bridge/common/dom/dom_video.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
28 class DomVideoTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34 };
35
SetUpTestCase()36 void DomVideoTest::SetUpTestCase() {}
TearDownTestCase()37 void DomVideoTest::TearDownTestCase() {}
SetUp()38 void DomVideoTest::SetUp() {}
TearDown()39 void DomVideoTest::TearDown() {}
40
41 /**
42 * @tc.name: DomVideoCreatorTest001
43 * @tc.desc: Test create video node successfully and videocomponent create as desire.
44 * @tc.type: FUNC
45 */
46 HWTEST_F(DomVideoTest, DomVideoCreatorTest001, TestSize.Level1)
47 {
48 /**
49 * @tc.steps: step1. Construct string of DomVideo with all attributes and style.
50 */
51 const std::string videoTest001 = "{ "
52 " \"tag\": \"video\", "
53 " \"attr\": [{ "
54 " \"muted\" : \"true\" "
55 " }, "
56 " { "
57 " \"src\" : \"/1.mp4\" "
58 " }, "
59 " { "
60 " \"autoplay\" : \"true\" "
61 " }, "
62 " { "
63 " \"poster\" : \"/1.png\" "
64 " }, "
65 " { "
66 " \"controls\" : \"false\" "
67 " }], "
68 " \"style\": [{ "
69 " \"objectFit\" : \"cover \" "
70 " }]"
71 "}";
72 /**
73 * @tc.steps: step2. Create video node with the string.
74 * @tc.expected: step2. Video node and child video component are created successfully.
75 */
76 auto video = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(videoTest001);
77 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(video);
78 RefPtr<VideoComponent> videoComponent = AceType::DynamicCast<VideoComponent>(boxChild->GetChild());
79 ASSERT_TRUE(videoComponent != nullptr);
80
81 /**
82 * @tc.steps: step3. Check the style and attributes of created video node.
83 * @tc.expected: step3. The style and attributes are as expected.
84 */
85 EXPECT_TRUE(videoComponent->GetSrc() == "/1.mp4");
86 EXPECT_TRUE(videoComponent->GetPoster() == "/1.png");
87 EXPECT_TRUE(videoComponent->IsMute());
88 EXPECT_TRUE(videoComponent->IsAutoPlay());
89 EXPECT_TRUE(!videoComponent->NeedControls());
90 }
91
92 /**
93 * @tc.name: DomVideoCreatorTest002
94 * @tc.desc: Test the VideoComponent initialized by default value.
95 * @tc.type: FUNC
96 */
97 HWTEST_F(DomVideoTest, DomVideoCreatorTest002, TestSize.Level1)
98 {
99 /**
100 * @tc.steps: step1. Construct string of DomVideo with necessary parameters.
101 */
102 const std::string videoTest002 = "{ "
103 " \"tag\": \"video\", "
104 " \"attr\": [{ "
105 " \"src\" : \"/1.mp4\" "
106 " }] "
107 "}";
108 /**
109 * @tc.steps: step2. Create video node with the string.
110 * @tc.expected: step2. Video node and child video component are created successfully.
111 */
112 auto video = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(videoTest002);
113 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(video);
114 RefPtr<VideoComponent> videoComponent = AceType::DynamicCast<VideoComponent>(boxChild->GetChild());
115 ASSERT_TRUE(videoComponent != nullptr);
116
117 /**
118 * @tc.steps: step3. Check the style and attributes of created video node.
119 * @tc.expected: step3. The style and attributes are as expected.
120 */
121 EXPECT_TRUE(videoComponent->GetSrc() == "/1.mp4");
122 EXPECT_TRUE(videoComponent->GetPoster().empty());
123 EXPECT_TRUE(!videoComponent->IsMute());
124 EXPECT_TRUE(!videoComponent->IsAutoPlay());
125 EXPECT_TRUE(videoComponent->NeedControls());
126 }
127
128 /**
129 * @tc.name: DomVideoCreatorTest003
130 * @tc.desc: Test value validation. If src is invalid, create component failed.
131 * @tc.type: FUNC
132 */
133 HWTEST_F(DomVideoTest, DomVideoCreatorTest003, TestSize.Level1)
134 {
135 /**
136 * @tc.steps: step1. Construct string of DomVideo without src attributes.
137 */
138 const std::string videoTest003 = "{ "
139 " \"tag\": \"video\", "
140 " \"attr\": [{ "
141 " \"muted\" : \"true\" "
142 " }, "
143 " { "
144 " \"autoplay\" : \"true\" "
145 " }, "
146 " { "
147 " \"poster\" : \"/1.png\" "
148 " }, "
149 " { "
150 " \"controls\" : \"false\" "
151 " }], "
152 " \"style\": [{ "
153 " \"objectFit\" : \"cover \" "
154 " }]"
155 "}";
156 /**
157 * @tc.steps: step2. Create video node with the string.
158 * @tc.expected: step2. Video node created successfully, but child is nullptr.
159 */
160 auto video = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(videoTest003);
161 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(video);
162 ASSERT_TRUE(boxChild->GetChild() == nullptr);
163 }
164
165 /**
166 * @tc.name: DomVideoCreatorTest004
167 * @tc.desc: Test add event to Video component.
168 * @tc.type: FUNC
169 */
170 HWTEST_F(DomVideoTest, DomVideoCreatorTest004, TestSize.Level1)
171 {
172 /**
173 * @tc.steps: step1. Construct string of DomVideo with all events.
174 */
175 const std::string videoTest004 = "{ "
176 " \"tag\": \"video\", "
177 " \"attr\": [{ "
178 " \"src\" : \"/1.mp4\" "
179 " }], "
180 " \"event\": [ "
181 " \"prepared\", "
182 " \"start\", "
183 " \"pause\", "
184 " \"finish\", "
185 " \"error\", "
186 " \"seeking\", "
187 " \"seeked\", "
188 " \"timeupdate\", "
189 " \"fullscreenchange\" "
190 " ] "
191 "}";
192
193 /**
194 * @tc.steps: step2. Create video node with the string.
195 * @tc.expected: step2. Progress node and child video component are created successfully.
196 */
197 auto video = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(videoTest004);
198 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(video);
199 RefPtr<VideoComponent> videoComponent = AceType::DynamicCast<VideoComponent>(boxChild->GetChild());
200 ASSERT_TRUE(videoComponent != nullptr);
201
202 /**
203 * @tc.steps: step3. Check events of created video node.
204 * @tc.expected: step3. The events are as expected.
205 */
206 ASSERT_TRUE(videoComponent->GetPreparedEventId() == std::to_string(video->GetNodeId()));
207 ASSERT_TRUE(videoComponent->GetStartEventId() == std::to_string(video->GetNodeId()));
208 ASSERT_TRUE(videoComponent->GetPauseEventId() == std::to_string(video->GetNodeId()));
209 ASSERT_TRUE(videoComponent->GetFinishEventId() == std::to_string(video->GetNodeId()));
210 ASSERT_TRUE(videoComponent->GetErrorEventId() == std::to_string(video->GetNodeId()));
211 ASSERT_TRUE(videoComponent->GetSeekingEventId() == std::to_string(video->GetNodeId()));
212 ASSERT_TRUE(videoComponent->GetSeekedEventId() == std::to_string(video->GetNodeId()));
213 ASSERT_TRUE(videoComponent->GetTimeUpdateEventId() == std::to_string(video->GetNodeId()));
214 ASSERT_TRUE(videoComponent->GetFullscreenChangeEventId() == std::to_string(video->GetNodeId()));
215 }
216
217 } // namespace OHOS::Ace::Framework
218