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 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_);
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 class ScreenBitmapListener : public UIView::OnClickListener {
64 public:
ScreenBitmapListener(UIViewGroup * container,UIImageView * img)65 ScreenBitmapListener(UIViewGroup* container, UIImageView* img) : container_(container), img_(img)
66 {
67 memset_s(&info_, sizeof(ImageInfo), 0, sizeof(ImageInfo));
68 if ((img != nullptr) && (container != nullptr)) {
69 container->Add(img);
70 img->SetVisible(false);
71 }
72 }
~ScreenBitmapListener()73 virtual ~ScreenBitmapListener()
74 {
75 if (info_.data != nullptr) {
76 ImageCacheFree(info_);
77 }
78 }
OnClick(UIView & view,const ClickEvent & event)79 bool OnClick(UIView& view, const ClickEvent& event) override
80 {
81 if (img_ == nullptr) {
82 return false;
83 }
84 if (info_.data != nullptr) {
85 ImageCacheFree(info_);
86 }
87 if (!Screen::GetInstance().GetCurrentScreenBitmap(info_)) {
88 return false;
89 }
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
SetUp()102 void UITestViewBitmap::SetUp()
103 {
104 if (container_ == nullptr) {
105 container_ = new UIScrollView();
106 container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
107 container_->SetHorizontalScrollState(false);
108 }
109 if (viewBitmap_ == nullptr) {
110 viewBitmap_ = new UIImageView();
111 // 500 : x pos of image; 50: y pos of image; 960 width of image; 480 : height of image.
112 viewBitmap_->SetPosition(500, 50, 960, 480);
113 }
114 if (screenBitmap_ == nullptr) {
115 screenBitmap_ = new UIImageView();
116 // 0 : x pos of image; 210: y pos of image; 960 width of image; 480 : height of image.
117 screenBitmap_->SetPosition(0, 210, 960, 480);
118 }
119 if (viewBitmapListener_ == nullptr) {
120 viewBitmapListener_ = new ViewBitmapListener(container_, viewBitmap_);
121 }
122 if (screenBitmapListener_ == nullptr) {
123 screenBitmapListener_ = new ScreenBitmapListener(container_, screenBitmap_);
124 }
125 }
126
TearDown()127 void UITestViewBitmap::TearDown()
128 {
129 if (viewBitmapListener_ != nullptr) {
130 delete viewBitmapListener_;
131 viewBitmapListener_ = nullptr;
132 }
133 if (screenBitmapListener_ != nullptr) {
134 delete screenBitmapListener_;
135 screenBitmapListener_ = nullptr;
136 }
137 DeleteChildren(container_);
138 container_ = nullptr;
139
140 viewBitmap_ = nullptr;
141 screenBitmap_ = nullptr;
142 }
143
GetTestView()144 const UIView* UITestViewBitmap::GetTestView()
145 {
146 UIKit_Bitmap_Test_GetViewBitmap_001();
147 UIKit_Bitmap_Test_GetScreenBitmap_001();
148 return container_;
149 }
150
UIKit_Bitmap_Test_GetViewBitmap_001()151 void UITestViewBitmap::UIKit_Bitmap_Test_GetViewBitmap_001()
152 {
153 UILabelButton* btn = new UILabelButton();
154 container_->Add(btn);
155 // 100 : x pos of button; 50: y pos of button.
156 btn->SetPosition(100, 50, BUTTON_WIDHT3, BUTTON_HEIGHT3);
157 btn->SetText("测试组件截屏");
158 btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
159 btn->SetOnClickListener(viewBitmapListener_);
160 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
161 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
162 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
163 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
164 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
165 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
166 }
167
UIKit_Bitmap_Test_GetScreenBitmap_001()168 void UITestViewBitmap::UIKit_Bitmap_Test_GetScreenBitmap_001()
169 {
170 UILabelButton* btn = new UILabelButton();
171 container_->Add(btn);
172 // 100 : x pos of button; 150: y pos of button.
173 btn->SetPosition(100, 150, BUTTON_WIDHT3, BUTTON_HEIGHT3);
174 btn->SetText("测试全屏截屏");
175 btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
176 btn->SetOnClickListener(screenBitmapListener_);
177 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
178 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
179 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
180 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
181 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
182 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
183 }
184 } // namespace OHOS
185