1 /*
2  * Copyright (c) 2020-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 "components/ui_video.h"
17 
18 #include <climits>
19 #include <gtest/gtest.h>
20 
21 using namespace testing::ext;
22 namespace OHOS {
23 namespace {
24     const int8_t STATE_PLAY = 1;
25     const int8_t STATE_PAUSE = 2;
26     const int8_t STATE_STOP = 3;
27     const int8_t STATE_COMPLETE = 4;
28     const int8_t STATE_REWIND = 5;
29     const int8_t STATE_ERROR = 6;
30     const int8_t STATE_INFO = 7;
31     const int8_t STATE_RESIZE = 8;
32 }
33 static int8_t g_state = 0;
34 
35 class TestVideoListener : public UIVideo::VideoPlayerListener {
36 public:
TestVideoListener()37     TestVideoListener() {}
~TestVideoListener()38     ~TestVideoListener() {}
39 
OnRewindToComplete()40     void OnRewindToComplete() override
41     {
42         EXPECT_EQ(g_state, STATE_REWIND);
43     }
44 
OnPlaybackPause()45     void OnPlaybackPause() override
46     {
47         EXPECT_EQ(g_state, STATE_PAUSE);
48     }
49 
OnPlaybackPlay()50     void OnPlaybackPlay() override
51     {
52         EXPECT_EQ(g_state, STATE_PLAY);
53     }
54 
OnPlaybackStop()55     void OnPlaybackStop() override
56     {
57         EXPECT_EQ(g_state, STATE_STOP);
58     }
59 
OnPlaybackComplete()60     void OnPlaybackComplete() override
61     {
62         EXPECT_EQ(g_state, STATE_ERROR);
63     }
64 
OnError(int32_t errorType,int32_t errorCode)65     void OnError(int32_t errorType, int32_t errorCode) override
66     {
67         EXPECT_EQ(g_state, STATE_COMPLETE);
68     }
69 
OnInfo(int type,int extra)70     void OnInfo(int type, int extra) override
71     {
72         EXPECT_EQ(g_state, STATE_INFO);
73     }
74 
OnVideoSizeChanged(int width,int height)75     void OnVideoSizeChanged(int width, int height) override
76     {
77         EXPECT_EQ(g_state, STATE_RESIZE);
78     }
79 };
80 
81 class UIVideoTest : public testing::Test {
82 public:
83     static void SetUpTestCase(void);
84     static void TearDownTestCase(void);
85     static UIVideo* video_;
86     static TestVideoListener* listener_;
87     const char* videoPath_ = "/user/data/video.mp4";
88 };
89 
90 UIVideo* UIVideoTest::video_ = nullptr;
91 TestVideoListener* UIVideoTest::listener_ = nullptr;
92 
SetUpTestCase(void)93 void UIVideoTest::SetUpTestCase(void)
94 {
95     if (listener_ == nullptr) {
96         listener_ = new TestVideoListener();
97     }
98 }
99 
TearDownTestCase(void)100 void UIVideoTest::TearDownTestCase(void)
101 {
102     if (video_ != nullptr) {
103         delete video_;
104         video_ = nullptr;
105     }
106     if (listener_ != nullptr) {
107         delete listener_;
108         listener_ = nullptr;
109     }
110 }
111 
112 /**
113  * @tc.name: UIVideoSetSrc_001
114  * @tc.desc: Verify SetSrc function, abnormal situation.
115  * @tc.type: FUNC
116  * @tc.require: SR000F3PEN
117  */
118 HWTEST_F(UIVideoTest, UIVideoSetSrc_001, TestSize.Level1)
119 {
120     if (video_ == nullptr) {
121         video_ = new UIVideo();
122     }
123 
124     const char* src = nullptr;
125     EXPECT_EQ(video_->SetSrc(src), false);
126 
127     src = "not exit path";
128     EXPECT_EQ(video_->SetSrc(src), false);
129     EXPECT_EQ(video_->GetSrc(), src);
130 
131     video_->Reset();
132     delete video_;
133     video_ = nullptr;
134 }
135 
136 /**
137  * @tc.name: VideoSetSrc_002
138  * @tc.desc: Verify SetSrc function, normal situation.
139  * @tc.type: FUNC
140  * @tc.require: AR000F4E5Q
141  */
142 HWTEST_F(UIVideoTest, UIVideoSetSrc_002, TestSize.Level1)
143 {
144     if (video_ == nullptr) {
145         video_ = new UIVideo();
146     }
147 
148     if (video_->SetSrc(videoPath_)) {
149         EXPECT_EQ(video_->GetSrc(), videoPath_);
150     }
151     video_->Reset();
152     delete video_;
153     video_ = nullptr;
154 }
155 
156 /**
157  * @tc.name: VideoPrepare_001
158  * @tc.desc: Verify Prepare function, abnormal situation.
159  * @tc.type: FUNC
160  * @tc.require: AR000F4E5Q
161  */
162 HWTEST_F(UIVideoTest, UIVideoPrepare_001, TestSize.Level1)
163 {
164     if (video_ == nullptr) {
165         video_ = new UIVideo();
166     }
167 
168     video_->SetSrc(nullptr);
169     EXPECT_EQ(video_->Prepare(), false);
170     video_->SetSrc("not exist path");
171     EXPECT_EQ(video_->Prepare(), false);
172 
173     video_->Reset();
174     delete video_;
175     video_ = nullptr;
176 }
177 
178 /**
179  * @tc.name: VideoPrepare_002
180  * @tc.desc: Verify Prepare function, normal situation.
181  * @tc.type: FUNC
182  * @tc.require: AR000F4E5Q
183  */
184 HWTEST_F(UIVideoTest, UIVideoPrepare_002, TestSize.Level1)
185 {
186     if (video_ == nullptr) {
187         video_ = new UIVideo();
188     }
189 
190     if (!video_->SetSrc(videoPath_)) {
191         return;
192     }
193     if (listener_ != nullptr) {
194         video_->SetVideoPlayerListener(listener_);
195     }
196 
197     g_state = STATE_RESIZE;
198     EXPECT_EQ(video_->Prepare(), true);
199 
200     video_->Reset();
201     delete video_;
202     video_ = nullptr;
203 }
204 
205 /**
206  * @tc.name: VideoPlay_001
207  * @tc.desc: Verify Play function, abnormal situation.
208  * @tc.type: FUNC
209  * @tc.require: AR000F4E5Q
210  */
211 HWTEST_F(UIVideoTest, UIVideoPlay_001, TestSize.Level1)
212 {
213     if (video_ == nullptr) {
214         video_ = new UIVideo();
215     }
216 
217     video_->SetSrc("not exist path");
218     video_->Prepare();
219     EXPECT_EQ(video_->Play(), false);
220 
221     video_->Reset();
222     delete video_;
223     video_ = nullptr;
224 }
225 
226 /**
227  * @tc.name: VideoPlay_002
228  * @tc.desc: Verify Play function, normal situation.
229  * @tc.type: FUNC
230  * @tc.require: AR000F4E5Q
231  */
232 HWTEST_F(UIVideoTest, UIVideoPlay_002, TestSize.Level0)
233 {
234     if (video_ == nullptr) {
235         video_ = new UIVideo();
236     }
237 
238     if (!video_->SetSrc(videoPath_)) {
239         return;
240     }
241     if (listener_ != nullptr) {
242         video_->SetVideoPlayerListener(listener_);
243     }
244 
245     g_state = STATE_RESIZE;
246     video_->Prepare();
247 
248     g_state = STATE_PLAY;
249     EXPECT_EQ(video_->Play(), true);
250 
251     g_state = STATE_PAUSE;
252     video_->Pause();
253 
254     g_state = STATE_PLAY;
255     EXPECT_EQ(video_->Play(), true);
256     g_state = STATE_STOP;
257     video_->Stop();
258     g_state = STATE_PLAY;
259     EXPECT_EQ(video_->Play(), false);
260 
261     video_->Reset();
262     delete video_;
263     video_ = nullptr;
264 }
265 
266 /**
267  * @tc.name: VideoIsPlaying_001
268  * @tc.desc: Verify IsPlaying function, equal.
269  * @tc.type: FUNC
270  * @tc.require: AR000F4E5Q
271  */
272 HWTEST_F(UIVideoTest, UIVideoIsPlaying_001, TestSize.Level1)
273 {
274     if (video_ == nullptr) {
275         video_ = new UIVideo();
276     }
277 
278     if (!video_->SetSrc(videoPath_)) {
279         return;
280     }
281     video_->Prepare();
282     EXPECT_EQ(video_->IsPlaying(), false);
283 
284     video_->Play();
285     EXPECT_EQ(video_->IsPlaying(), true);
286 
287     video_->Reset();
288     delete video_;
289     video_ = nullptr;
290 }
291 
292 /**
293  * @tc.name: VideoIsSingleLooping_001
294  * @tc.desc: Verify IsSingleLooping function, equal.
295  * @tc.type: FUNC
296  * @tc.require: AR000F4E5Q
297  */
298 HWTEST_F(UIVideoTest, UIVideoIsSingleLooping_001, TestSize.Level1)
299 {
300     if (video_ == nullptr) {
301         video_ = new UIVideo();
302     }
303 
304     if (!video_->SetSrc(videoPath_)) {
305         return;
306     }
307     video_->Prepare();
308     video_->Play();
309     EXPECT_EQ(video_->IsSingleLooping(), false);
310     video_->EnableSingleLooping(true);
311     EXPECT_EQ(video_->IsSingleLooping(), true);
312 
313     video_->EnableSingleLooping(false);
314     EXPECT_EQ(video_->IsSingleLooping(), false);
315     video_->Reset();
316     delete video_;
317     video_ = nullptr;
318 }
319 
320 /**
321  * @tc.name: VideoRewind_001
322  * @tc.desc: Verify Rewind function, equal.
323  * @tc.type: FUNC
324  * @tc.require: AR000F4E5Q
325  */
326 HWTEST_F(UIVideoTest, UIVideoRewind_001, TestSize.Level0)
327 {
328     if (video_ == nullptr) {
329         video_ = new UIVideo();
330     }
331 
332     if (!video_->SetSrc(videoPath_)) {
333         return;
334     }
335     if (listener_ != nullptr) {
336         video_->SetVideoPlayerListener(listener_);
337     }
338 
339     g_state = STATE_RESIZE;
340     video_->Prepare();
341     g_state = STATE_PLAY;
342     video_->Play();
343     int64_t currentTime = 0;
344     video_->GetCurrentTime(currentTime);
345     int64_t rewindTime = currentTime + 3000; // 3000:rewind milli second
346 
347     g_state = STATE_REWIND;
348     video_->Rewind(rewindTime);
349     video_->GetCurrentTime(currentTime);
350     EXPECT_EQ(currentTime, rewindTime);
351 
352     video_->Reset();
353     delete video_;
354     video_ = nullptr;
355 }
356 
357 /**
358  * @tc.name: VideoPause_001
359  * @tc.desc: Verify Pause function, equal.
360  * @tc.type: FUNC
361  * @tc.require: AR000F4E5Q
362  */
363 HWTEST_F(UIVideoTest, UIVideoPause_001, TestSize.Level0)
364 {
365     if (video_ == nullptr) {
366         video_ = new UIVideo();
367     }
368 
369     if (!video_->SetSrc(videoPath_)) {
370         return;
371     }
372     if (listener_ != nullptr) {
373         video_->SetVideoPlayerListener(listener_);
374     }
375 
376     g_state = STATE_RESIZE;
377     video_->Prepare();
378     g_state = STATE_PLAY;
379     video_->Play();
380 
381     g_state = STATE_PAUSE;
382     EXPECT_EQ(video_->Pause(), true);
383 
384     video_->Reset();
385     EXPECT_EQ(video_->Pause(), false);
386 
387     video_->Reset();
388     delete video_;
389     video_ = nullptr;
390 }
391 
392 /**
393  * @tc.name: VideoStop_001
394  * @tc.desc: Verify Stop function, equal.
395  * @tc.type: FUNC
396  * @tc.require: AR000F4E5Q
397  */
398 HWTEST_F(UIVideoTest, UIVideoStop_001, TestSize.Level0)
399 {
400     if (video_ == nullptr) {
401         video_ = new UIVideo();
402     }
403 
404     if (!video_->SetSrc(videoPath_)) {
405         return;
406     }
407     if (listener_ != nullptr) {
408         video_->SetVideoPlayerListener(listener_);
409     }
410 
411     g_state = STATE_RESIZE;
412     video_->Prepare();
413     g_state = STATE_PLAY;
414     video_->Play();
415 
416     g_state = STATE_STOP;
417     EXPECT_EQ(video_->Stop(), true);
418 
419     video_->Reset();
420     EXPECT_EQ(video_->Stop(), false);
421 
422     video_->Reset();
423     delete video_;
424     video_ = nullptr;
425 }
426 
427 /**
428  * @tc.name: VideoReset_001
429  * @tc.desc: Verify Reset function, equal.
430  * @tc.type: FUNC
431  * @tc.require: AR000F4E5Q
432  */
433 HWTEST_F(UIVideoTest, UIVideoReset_001, TestSize.Level0)
434 {
435     if (video_ == nullptr) {
436         video_ = new UIVideo();
437     }
438 
439     if (!video_->SetSrc(videoPath_)) {
440         return;
441     }
442     if (listener_ != nullptr) {
443         video_->SetVideoPlayerListener(listener_);
444     }
445 
446     g_state = STATE_RESIZE;
447     video_->Prepare();
448     g_state = STATE_PLAY;
449     video_->Play();
450 
451     EXPECT_EQ(video_->Reset(), true);
452 
453     video_->Reset();
454     delete video_;
455     video_ = nullptr;
456 }
457 } // namespace OHOS
458