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_view_bitmap.h"
17 
18 #include "securec.h"
19 
20 #include "common/screen.h"
21 #include "components/ui_label_button.h"
22 #include "test_resource_config.h"
23 
24 namespace OHOS {
25 class ViewBitmapListener : public UIView::OnClickListener {
26 public:
ViewBitmapListener(UIViewGroup * container,UIImageView * img)27     ViewBitmapListener(UIViewGroup* container, UIImageView* img) : container_(container), img_(img)
28     {
29         (void)memset_s(&info_, sizeof(ImageInfo), 0, sizeof(ImageInfo));
30         if ((img != nullptr) && (container != nullptr)) {
31             container->Add(img);
32             img->SetVisible(false);
33         }
34     }
~ViewBitmapListener()35     virtual ~ViewBitmapListener()
36     {
37         if (info_.data != nullptr) {
38             ImageCacheFree(info_);
39         }
40     }
41 
OnClick(UIView & view,const ClickEvent & event)42     bool OnClick(UIView& view, const ClickEvent& event) override
43     {
44         if (img_ == nullptr) {
45             return false;
46         }
47         if (info_.data != nullptr) {
48             ImageCacheFree(info_);
49         }
50         view.GetBitmap(info_, ARGB8888);
51         img_->SetVisible(true);
52         img_->SetSrc(&info_);
53         img_->Invalidate();
54         return false;
55     }
56 
57 private:
58     UIViewGroup* container_;
59     UIImageView* img_;
60     ImageInfo info_;
61 };
62 
63 
64 class ViewBitmapListener2 : public UIView::OnClickListener {
65 public:
ViewBitmapListener2(UIViewGroup * container,UIImageView * img)66     ViewBitmapListener2(UIViewGroup* container, UIImageView* img) : container_(container), img_(img)
67     {
68         (void)memset_s(&info_, sizeof(ImageInfo), 0, sizeof(ImageInfo));
69         if ((img != nullptr) && (container != nullptr)) {
70             container->Add(img);
71             img->SetVisible(false);
72         }
73     }
~ViewBitmapListener2()74     virtual ~ViewBitmapListener2()
75     {
76         if (info_.data != nullptr) {
77             ImageCacheFree(info_);
78         }
79     }
80 
OnClick(UIView & view,const ClickEvent & event)81     bool OnClick(UIView& view, const ClickEvent& event) override
82     {
83         if (img_ == nullptr) {
84             return false;
85         }
86         if (info_.data != nullptr) {
87             ImageCacheFree(info_);
88         }
89         view.GetBitmap(info_, XRGB8888);
90         img_->SetVisible(true);
91         img_->SetSrc(&info_);
92         img_->Invalidate();
93         return false;
94     }
95 
96 private:
97     UIViewGroup* container_;
98     UIImageView* img_;
99     ImageInfo info_;
100 };
101 
102 class ViewBitmapListener3 : public UIView::OnClickListener {
103 public:
ViewBitmapListener3(UIViewGroup * container,UIImageView * img)104     ViewBitmapListener3(UIViewGroup* container, UIImageView* img) : container_(container), img_(img)
105     {
106         (void)memset_s(&info_, sizeof(ImageInfo), 0, sizeof(ImageInfo));
107         if ((img != nullptr) && (container != nullptr)) {
108             container->Add(img);
109             img->SetVisible(false);
110         }
111     }
~ViewBitmapListener3()112     virtual ~ViewBitmapListener3()
113     {
114         if (info_.data != nullptr) {
115             ImageCacheFree(info_);
116         }
117     }
118 
OnClick(UIView & view,const ClickEvent & event)119     bool OnClick(UIView& view, const ClickEvent& event) override
120     {
121         if (img_ == nullptr) {
122             return false;
123         }
124         if (info_.data != nullptr) {
125             ImageCacheFree(info_);
126         }
127         view.GetBitmap(info_, RGB888);
128         img_->SetVisible(true);
129         img_->SetSrc(&info_);
130         img_->Invalidate();
131         return false;
132     }
133 
134 private:
135     UIViewGroup* container_;
136     UIImageView* img_;
137     ImageInfo info_;
138 };
139 
140 class ViewBitmapListener4 : public UIView::OnClickListener {
141 public:
ViewBitmapListener4(UIViewGroup * container,UIImageView * img)142     ViewBitmapListener4(UIViewGroup* container, UIImageView* img) : container_(container), img_(img)
143     {
144         (void)memset_s(&info_, sizeof(ImageInfo), 0, sizeof(ImageInfo));
145         if ((img != nullptr) && (container != nullptr)) {
146             container->Add(img);
147             img->SetVisible(false);
148         }
149     }
~ViewBitmapListener4()150     virtual ~ViewBitmapListener4()
151     {
152         if (info_.data != nullptr) {
153             ImageCacheFree(info_);
154         }
155     }
156 
OnClick(UIView & view,const ClickEvent & event)157     bool OnClick(UIView& view, const ClickEvent& event) override
158     {
159         if (img_ == nullptr) {
160             return false;
161         }
162         if (info_.data != nullptr) {
163             ImageCacheFree(info_);
164         }
165         view.GetBitmap(info_, RGB565);
166         img_->SetVisible(true);
167         img_->SetSrc(&info_);
168         img_->Invalidate();
169         return false;
170     }
171 
172 private:
173     UIViewGroup* container_;
174     UIImageView* img_;
175     ImageInfo info_;
176 };
177 
178 class ScreenBitmapListener : public UIView::OnClickListener {
179 public:
ScreenBitmapListener(UIViewGroup * container,UIImageView * img)180     ScreenBitmapListener(UIViewGroup* container, UIImageView* img) : container_(container), img_(img)
181     {
182         memset_s(&info_, sizeof(ImageInfo), 0, sizeof(ImageInfo));
183         if ((img != nullptr) && (container != nullptr)) {
184             container->Add(img);
185             img->SetVisible(false);
186         }
187     }
~ScreenBitmapListener()188     virtual ~ScreenBitmapListener()
189     {
190         if (info_.data != nullptr) {
191             ImageCacheFree(info_);
192         }
193     }
OnClick(UIView & view,const ClickEvent & event)194     bool OnClick(UIView& view, const ClickEvent& event) override
195     {
196         if (img_ == nullptr) {
197             return false;
198         }
199         if (info_.data != nullptr) {
200             ImageCacheFree(info_);
201         }
202         if (!Screen::GetInstance().GetCurrentScreenBitmap(info_)) {
203             return false;
204         }
205         img_->SetVisible(true);
206         img_->SetSrc(&info_);
207         img_->Invalidate();
208         return false;
209     }
210 
211 private:
212     UIViewGroup* container_;
213     UIImageView* img_;
214     ImageInfo info_;
215 };
216 
SetUp()217 void UITestViewBitmap::SetUp()
218 {
219     if (container_ == nullptr) {
220         container_ = new UIScrollView();
221         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
222         container_->SetHorizontalScrollState(false);
223         container_->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
224     }
225     if (viewBitmap_ == nullptr) {
226         viewBitmap_ = new UIImageView();
227         // 100 : x pos of image; 100: y pos of image; 960 width of image; 480 : height of image.
228         viewBitmap_->SetPosition(100, 100, 960, 480);
229     }
230     if (viewBitmap2_ == nullptr) {
231         viewBitmap2_ = new UIImageView();
232         // 300 : x pos of image; 100: y pos of image; 960 width of image; 480 : height of image.
233         viewBitmap2_->SetPosition(300, 100, 960, 480);
234     }
235     if (viewBitmap3_ == nullptr) {
236         viewBitmap3_ = new UIImageView();
237         // 500 : x pos of image; 100: y pos of image; 960 width of image; 480 : height of image.
238         viewBitmap3_->SetPosition(500, 100, 960, 480);
239     }
240     if (viewBitmap4_ == nullptr) {
241         viewBitmap4_ = new UIImageView();
242         // 700 : x pos of image; 100: y pos of image; 960 width of image; 480 : height of image.
243         viewBitmap4_->SetPosition(700, 100, 960, 480);
244     }
245     if (screenBitmap_ == nullptr) {
246         screenBitmap_ = new UIImageView();
247         // 0 : x pos of image; 210: y pos of image; 960 width of image; 480 : height of image.
248         screenBitmap_->SetPosition(0, 210, 960, 480);
249     }
250     if (viewBitmapListener_ == nullptr) {
251         viewBitmapListener_ = new ViewBitmapListener(container_, viewBitmap_);
252     }
253     if (viewBitmapListener2_ == nullptr) {
254         viewBitmapListener2_ = new ViewBitmapListener2(container_, viewBitmap2_);
255     }
256     if (viewBitmapListener3_ == nullptr) {
257         viewBitmapListener3_ = new ViewBitmapListener3(container_, viewBitmap3_);
258     }
259     if (viewBitmapListener4_ == nullptr) {
260         viewBitmapListener4_ = new ViewBitmapListener4(container_, viewBitmap4_);
261     }
262     if (screenBitmapListener_ == nullptr) {
263         screenBitmapListener_ = new ScreenBitmapListener(container_, screenBitmap_);
264     }
265 }
266 
TearDown()267 void UITestViewBitmap::TearDown()
268 {
269     if (viewBitmapListener_ != nullptr) {
270         delete viewBitmapListener_;
271         viewBitmapListener_ = nullptr;
272     }
273     if (viewBitmapListener2_ != nullptr) {
274         delete viewBitmapListener2_;
275         viewBitmapListener2_ = nullptr;
276     }
277     if (viewBitmapListener3_ != nullptr) {
278         delete viewBitmapListener3_;
279         viewBitmapListener3_ = nullptr;
280     }
281     if (viewBitmapListener4_ != nullptr) {
282         delete viewBitmapListener4_;
283         viewBitmapListener4_ = nullptr;
284     }
285     if (screenBitmapListener_ != nullptr) {
286         delete screenBitmapListener_;
287         screenBitmapListener_ = nullptr;
288     }
289     DeleteChildren(container_);
290     container_ = nullptr;
291     viewBitmap_ = nullptr;
292     viewBitmap2_ = nullptr;
293     viewBitmap3_ = nullptr;
294     viewBitmap4_ = nullptr;
295     screenBitmap_ = nullptr;
296 }
297 
GetTestView()298 const UIView* UITestViewBitmap::GetTestView()
299 {
300     UIKitBitmapTestGetViewBitmap001();
301     UIKitBitmapTestGetViewBitmap002();
302     UIKitBitmapTestGetViewBitmap003();
303     UIKitBitmapTestGetViewBitmap004();
304 #if !(defined ENABLE_WINDOW && ENABLE_WINDOW)
305     UIKitBitmapTestGetScreenBitmap001();
306 #endif
307     return container_;
308 }
309 
UIKitBitmapTestGetViewBitmap001()310 void UITestViewBitmap::UIKitBitmapTestGetViewBitmap001()
311 {
312     UILabelButton* btn = new UILabelButton();
313     container_->Add(btn);
314     // 100 : x pos of button; 50: y pos of  button.
315     btn->SetPosition(100, 50, BUTTON_WIDHT3, BUTTON_HEIGHT3);
316     btn->SetText("组件ARGB截屏");
317     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
318     btn->SetOnClickListener(viewBitmapListener_);
319     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
320     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
321     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
322     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
323     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
324     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
325 }
326 
UIKitBitmapTestGetViewBitmap002()327 void UITestViewBitmap::UIKitBitmapTestGetViewBitmap002()
328 {
329     UILabelButton* btn = new UILabelButton();
330     container_->Add(btn);
331     // 300 : x pos of button; 50: y pos of  button.
332     btn->SetPosition(300, 50, BUTTON_WIDHT3, BUTTON_HEIGHT3);
333     btn->SetText("组件XRGB截屏");
334     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
335     btn->SetOnClickListener(viewBitmapListener2_);
336     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
337     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
338     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
339     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
340     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
341     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
342 }
343 
UIKitBitmapTestGetViewBitmap003()344 void UITestViewBitmap::UIKitBitmapTestGetViewBitmap003()
345 {
346     UILabelButton* btn = new UILabelButton();
347     container_->Add(btn);
348     // 500 : x pos of button; 50: y pos of  button.
349     btn->SetPosition(500, 50, BUTTON_WIDHT3, BUTTON_HEIGHT3);
350     btn->SetText("组件RGB888截屏");
351     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
352     btn->SetOnClickListener(viewBitmapListener3_);
353     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
354     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
355     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
356     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
357     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
358     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
359 }
360 
UIKitBitmapTestGetViewBitmap004()361 void UITestViewBitmap::UIKitBitmapTestGetViewBitmap004()
362 {
363     UILabelButton* btn = new UILabelButton();
364     container_->Add(btn);
365     // 700 : x pos of button; 50: y pos of  button.
366     btn->SetPosition(700, 50, BUTTON_WIDHT3, BUTTON_HEIGHT3);
367     btn->SetText("组件RGB565截屏");
368     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
369     btn->SetOnClickListener(viewBitmapListener4_);
370     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
371     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
372     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
373     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
374     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
375     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
376 }
377 
378 #if !(defined ENABLE_WINDOW && ENABLE_WINDOW)
UIKitBitmapTestGetScreenBitmap001()379 void UITestViewBitmap::UIKitBitmapTestGetScreenBitmap001()
380 {
381     UILabelButton* btn = new UILabelButton();
382     container_->Add(btn);
383     // 100 : x pos of button; 150: y pos of  button.
384     btn->SetPosition(100, 150, BUTTON_WIDHT3, BUTTON_HEIGHT3);
385     btn->SetText("测试全屏截屏");
386     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
387     btn->SetOnClickListener(screenBitmapListener_);
388     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
389     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
390     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
391     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
392     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
393     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
394     btn->SetViewId(UI_TEST_FULL_SCREEN_CAPTURE);
395 }
396 #endif
397 } // namespace OHOS
398