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 "ui_test_video.h"
17 #include "securec.h"
18 #include "test_resource_config.h"
19 #ifndef VERSION_LITE
20 namespace OHOS {
SetUp()21 void UITestVideo::SetUp()
22 {
23     if (container_ == nullptr) {
24         container_ = new UIScrollView();
25         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - 30); // 30:offset
26     }
27 }
28 
TearDown()29 void UITestVideo::TearDown()
30 {
31     if (container_ != nullptr) {
32         container_->RemoveAll();
33         delete container_;
34         container_ = nullptr;
35     }
36     if (video_ != nullptr) {
37         video_->Stop();
38         video_->Reset();
39         delete video_;
40         video_ = nullptr;
41     }
42     if (infoLabel_ != nullptr) {
43         delete infoLabel_;
44         infoLabel_ = nullptr;
45     }
46     if (testVideoListener_ != nullptr) {
47         delete testVideoListener_;
48         testVideoListener_ = nullptr;
49     }
50     DeleteTestButtons();
51 }
52 
DeleteTestButtons()53 void UITestVideo::DeleteTestButtons()
54 {
55     if (loopButton_ != nullptr) {
56         delete loopButton_;
57         loopButton_ = nullptr;
58     }
59     if (totleTimeButton_ != nullptr) {
60         delete totleTimeButton_;
61         totleTimeButton_ = nullptr;
62     }
63     if (volButtonAdd_ != nullptr) {
64         delete volButtonAdd_;
65         volButtonAdd_ = nullptr;
66     }
67     if (volButtonSub_ != nullptr) {
68         delete volButtonSub_;
69         volButtonSub_ = nullptr;
70     }
71     if (rewindButtonAdd_ != nullptr) {
72         delete rewindButtonAdd_;
73         rewindButtonAdd_ = nullptr;
74     }
75     if (rewindButtonSub_ != nullptr) {
76         delete rewindButtonSub_;
77         rewindButtonSub_ = nullptr;
78     }
79     if (playButton_ != nullptr) {
80         delete playButton_;
81         playButton_ = nullptr;
82     }
83     if (pauseButton_ != nullptr) {
84         delete pauseButton_;
85         pauseButton_ = nullptr;
86     }
87     if (stopButton_ != nullptr) {
88         delete stopButton_;
89         stopButton_ = nullptr;
90     }
91     if (resetButton_ != nullptr) {
92         delete resetButton_;
93         resetButton_ = nullptr;
94     }
95 }
96 
SetupTestButton(UILabelButton * button,const char * text)97 void UITestVideo::SetupTestButton(UILabelButton* button, const char* text)
98 {
99     if (button != nullptr) {
100         int16_t buttonWidth = 60;  // 60:button width
101         int16_t buttonHeight = 40; // 40:button height
102         button->SetText(text);
103         button->Resize(buttonWidth, buttonHeight);
104         button->SetOnClickListener(this);
105     }
106 }
107 
InitTestButtons()108 void UITestVideo::InitTestButtons()
109 {
110     int16_t posX1 = Screen::GetInstance().GetWidth() - 160; // 160:posX1 offset
111     int16_t posX2 = Screen::GetInstance().GetWidth() - 80;  // 80:posX2 offset
112 
113     loopButton_ = new UILabelButton();
114     loopButton_->SetPosition(posX1, 50); // 50:position y
115     SetupTestButton(loopButton_, "loop");
116 
117     totleTimeButton_ = new UILabelButton();
118     totleTimeButton_->SetPosition(posX2, 50); // 50:position y
119     SetupTestButton(totleTimeButton_, "time");
120 
121     volButtonAdd_ = new UILabelButton();
122     volButtonAdd_->SetPosition(posX1, 100); // 100:position y
123     SetupTestButton(volButtonAdd_, "vol+");
124 
125     volButtonSub_ = new UILabelButton();
126     volButtonSub_->SetPosition(posX2, 100); // 100:position y
127     SetupTestButton(volButtonSub_, "vol-");
128 
129     rewindButtonAdd_ = new UILabelButton();
130     rewindButtonAdd_->SetPosition(posX1, 150); // 150:position y
131     SetupTestButton(rewindButtonAdd_, "pos+");
132 
133     rewindButtonSub_ = new UILabelButton();
134     rewindButtonSub_->SetPosition(posX2, 150); // 150:position y
135     SetupTestButton(rewindButtonSub_, "pos-");
136 
137     playButton_ = new UILabelButton();
138     playButton_->SetPosition(posX1, 200); // 200:position y
139     SetupTestButton(playButton_, "play");
140 
141     pauseButton_ = new UILabelButton();
142     pauseButton_->SetPosition(posX2, 200); // 200:position y
143     SetupTestButton(pauseButton_, "pause");
144 
145     stopButton_ = new UILabelButton();
146     stopButton_->SetPosition(posX1, 250); // 250:position y
147     SetupTestButton(stopButton_, "stop");
148 
149     resetButton_ = new UILabelButton();
150     resetButton_->SetPosition(posX2, 250); // 250:position y
151     SetupTestButton(resetButton_, "reset");
152 }
153 
GetTestView()154 const UIView* UITestVideo::GetTestView()
155 {
156     if (video_ == nullptr) {
157         UIKitUIVideoTestSetVideo001();
158     }
159     return container_;
160 }
161 
UIKitUIVideoTestSetVideo001()162 void UITestVideo::UIKitUIVideoTestSetVideo001()
163 {
164     if (container_ != nullptr) {
165         if (video_ == nullptr) {
166             video_ = new UIVideo();
167         }
168 
169         infoLabel_ = new UILabel;
170         infoLabel_->SetPosition(20, 60); // 20:position x 60:position y
171         infoLabel_->Resize(200, 40);     // 200:label width 40:label height
172 
173         if (!video_->SetSrc(VIDEO_SOURCE_DIRECTORY)) {
174             infoLabel_->SetText("SetSrc error");
175             return;
176         }
177         video_->SetPosition(0, 0);
178         video_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
179         if (!video_->Prepare()) {
180             infoLabel_->SetText("Prepare error");
181             return;
182         }
183 
184         testVideoListener_ = new TestVideoListener();
185         testVideoListener_->SetInfoLabel(infoLabel_);
186         video_->SetVideoPlayerListener(testVideoListener_);
187 
188         InitTestButtons();
189         container_->Add(video_);
190         container_->Add(infoLabel_);
191         container_->Add(loopButton_);
192         container_->Add(totleTimeButton_);
193         container_->Add(volButtonAdd_);
194         container_->Add(volButtonSub_);
195         container_->Add(rewindButtonAdd_);
196         container_->Add(rewindButtonSub_);
197         container_->Add(playButton_);
198         container_->Add(pauseButton_);
199         container_->Add(stopButton_);
200         container_->Add(resetButton_);
201 
202         if (!video_->Play()) {
203             infoLabel_->SetText("Play error");
204             return;
205         }
206         video_->ShowController(true);
207     }
208 }
209 
DealClickLoopButton() const210 void UITestVideo::DealClickLoopButton() const
211 {
212     if ((video_ == nullptr) || (infoLabel_ == nullptr)) {
213         return;
214     }
215     if (video_->IsSingleLooping()) {
216         if (video_->EnableSingleLooping(false)) {
217             infoLabel_->SetText("single");
218         } else {
219             infoLabel_->SetText("single error");
220         }
221     } else {
222         if (video_->EnableSingleLooping(true)) {
223             infoLabel_->SetText("loop");
224         } else {
225             infoLabel_->SetText("loop error");
226         }
227     }
228 }
229 
OnClick(UIView & view,const ClickEvent & event)230 bool UITestVideo::OnClick(UIView &view, const ClickEvent& event)
231 {
232     if ((video_ == nullptr) || (infoLabel_ == nullptr)) {
233         return true;
234     }
235     if (&view == loopButton_) {
236         DealClickLoopButton();
237     } else if (&view == totleTimeButton_) {
238         int64_t duration = 0;
239         if (video_->GetDuration(duration)) {
240             char buf[10] = {0}; // 10:buf size
241             if (sprintf_s(buf, sizeof(buf), "%lld", duration) < 0) {
242                 return true;
243             }
244             infoLabel_->SetText(&buf[0]);
245         } else {
246             infoLabel_->SetText("GetDuration error");
247         }
248     } else if (&view == volButtonAdd_) {
249         if (video_->SetVolume(0.8, 0.8)) { // 0.8:left volume 0.8:right volume
250             infoLabel_->SetText("volume 0.8");
251         } else {
252             infoLabel_->SetText("SetVolume error");
253         }
254     } else if (&view == volButtonSub_) {
255         if (video_->SetVolume(0, 0)) {
256             infoLabel_->SetText("volume 0");
257         } else {
258             infoLabel_->SetText("SetVolume error");
259         }
260     } else {
261         return OnClick1(view, event);
262     }
263     return true;
264 }
265 
OnClick1(UIView & view,const ClickEvent & event)266 bool UITestVideo::OnClick1(UIView &view, const ClickEvent& event)
267 {
268     if ((video_ == nullptr) || (infoLabel_ == nullptr)) {
269         return true;
270     }
271     if (&view == rewindButtonAdd_) {
272         int64_t time = 0;
273         if (video_->GetCurrentTime(time)) {
274             if (video_->Rewind(time + 3000)) { // 3000:rewind add ms
275                 infoLabel_->SetText("Rewind + 3000");
276             } else {
277                 infoLabel_->SetText("Rewind error");
278             }
279         } else {
280             infoLabel_->SetText("GetCurrentTime error");
281         }
282     } else if (&view == rewindButtonSub_) {
283         int64_t time = 0;
284         if (video_->GetCurrentTime(time)) {
285             if (video_->Rewind(time - 3000)) { // 3000:rewind sub ms
286                 infoLabel_->SetText("Rewind - 3000");
287             } else {
288                 infoLabel_->SetText("Rewind error");
289             }
290         } else {
291             infoLabel_->SetText("GetCurrentTime error");
292         }
293     } else {
294         return OnClick2(view, event);
295     }
296     return true;
297 }
298 
OnClick2(UIView & view,const ClickEvent & event)299 bool UITestVideo::OnClick2(UIView &view, const ClickEvent& event)
300 {
301     if ((video_ == nullptr) || (infoLabel_ == nullptr)) {
302         return true;
303     }
304     if (&view == playButton_) {
305         if (video_->Play()) {
306             infoLabel_->SetText("Play");
307         } else {
308             infoLabel_->SetText("Play error");
309         }
310     } else if (&view == pauseButton_) {
311         if (video_->Pause()) {
312             infoLabel_->SetText("Pause");
313         } else {
314             infoLabel_->SetText("Pause error");
315         }
316     } else if (&view == stopButton_) {
317         if (video_->Stop()) {
318             infoLabel_->SetText("Stop");
319         } else {
320             infoLabel_->SetText("Stop error");
321         }
322     } else if (&view == resetButton_) {
323         if (video_->Reset()) {
324             infoLabel_->SetText("Reset");
325         } else {
326             infoLabel_->SetText("Reset error");
327         }
328     }
329     return true;
330 }
331 } // namespace OHOS
332 
333 #endif // VERSION_LITE
334