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_transform.h"
17 #include "common/screen.h"
18 #include "test_resource_config.h"
19 
20 namespace OHOS {
SetUp()21 void UITestTransform::SetUp()
22 {
23     constexpr int16_t IMAGE_PADDING = 10;
24     if (container_ == nullptr) {
25         container_ = new UIScrollView();
26         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
27         UIViewGroup* uiViewGroup = new UIViewGroup();
28         uiViewGroup->SetPosition(0, 0, 320, 390); // 320: width; 390: height
29         container_->Add(uiViewGroup);
30         UILabel* label = new UILabel();
31         uiViewGroup->Add(label);
32         // 288: width; 48: height
33         label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
34         label->SetText("UITransform效果");
35         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
36         uiViewGroupFrame_ = new UIViewGroup();
37         uiViewGroup->Add(uiViewGroupFrame_);
38         // 288: width; 336: height
39         uiViewGroupFrame_->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE, 288, 336);
40         uiViewGroupFrame_->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
41         uiViewGroupFrame_->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
42         uiViewGroupFrame_->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
43         uiViewGroupFrame_->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
44         uiViewGroupFrame_->SetStyle(STYLE_BACKGROUND_OPA, 0);
45 
46         imageView_ = new UIImageView();
47         imageView_->SetPosition(150, 50, 200, 200); // 150:poistion x 50:position y 200:width 200:height
48         imageView_->SetSrc(IMAGE_RESIZEMODE_PATH);
49         imageView_->SetStyle(STYLE_BORDER_COLOR, Color::Blue().full);
50         imageView_->SetStyle(STYLE_BORDER_WIDTH, 1);
51         imageView_->SetStyle(STYLE_PADDING_LEFT, IMAGE_PADDING);
52         imageView_->SetStyle(STYLE_PADDING_RIGHT, IMAGE_PADDING);
53         imageView_->SetStyle(STYLE_PADDING_TOP, IMAGE_PADDING);
54         imageView_->SetStyle(STYLE_PADDING_BOTTOM, IMAGE_PADDING);
55         imageView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
56 
57         uiViewGroupFrame_->Add(imageView_);
58         imageView_->LayoutCenterOfParent();
59     }
60 
61     if (layout_ == nullptr) {
62         layout_ = new GridLayout();
63         // 34: increase x-coordinate; 48: y-coordinate; 100: width; 150: height
64         layout_->SetPosition(uiViewGroupFrame_->GetWidth() + 34, 48, 100, 150);
65         container_->Add(layout_);
66         layout_->SetLayoutDirection(LAYOUT_VER);
67         layout_->SetRows(3); // 3:two rows
68         layout_->SetCols(1); // 1:three cols
69     }
70     AddRadioGroup();
71 }
72 
AddRadioGroup()73 void UITestTransform::AddRadioGroup()
74 {
75     if (layout_ == nullptr) {
76         return;
77     }
78     constexpr int16_t STEP = 50;
79     constexpr int16_t RADIO_OFFSET = -10;
80     constexpr int16_t RADIO_X = 560;
81     constexpr int16_t RADIO_SIZE = 50; // 50: the width and height of radio button
82     const int16_t lableX = layout_->GetOrigRect().GetRight() + 34; // 34 : increase x-coordinate;
83     int16_t y = 50;
84     AddLable(lableX, y, "Auto");
85     AddRadioButton(GetRect(RADIO_X, y + RADIO_OFFSET, RADIO_SIZE, RADIO_SIZE),
86         "RB", new StateChangeListener(ImageScaleMode::AUTO, this), UI_TEST_AUTO)->SetState(
87         UICheckBox::UICheckBoxState::SELECTED);
88 
89     y += STEP;
90     AddLable(lableX, y, "Tiling");
91     AddRadioButton(GetRect(RADIO_X, y + RADIO_OFFSET, RADIO_SIZE, RADIO_SIZE),
92         "RB", new StateChangeListener(ImageScaleMode::TILING, this), UI_TEST_TILING);
93 
94     y += STEP;
95     AddLable(lableX, y, "Cover");
96     AddRadioButton(GetRect(RADIO_X, y + RADIO_OFFSET, RADIO_SIZE, RADIO_SIZE),
97         "RB", new StateChangeListener(ImageScaleMode::COVER, this), UI_TEST_COVER);
98 
99     y += STEP;
100     AddLable(lableX, y, "Contain");
101     AddRadioButton(GetRect(RADIO_X, y + RADIO_OFFSET, RADIO_SIZE, RADIO_SIZE),
102         "RB", new StateChangeListener(ImageScaleMode::CONTAIN, this), UI_TEST_CONTAIN);
103 
104     y += STEP;
105     AddLable(lableX, y, "Fill");
106     AddRadioButton(GetRect(RADIO_X, y + RADIO_OFFSET, RADIO_SIZE, RADIO_SIZE),
107         "RB", new StateChangeListener(ImageScaleMode::FILL, this), UI_TEST_FILL);
108 
109     y += STEP;
110     AddLable(lableX, y, "Center");
111     AddRadioButton(GetRect(RADIO_X, y + RADIO_OFFSET, RADIO_SIZE, RADIO_SIZE),
112         "RB", new StateChangeListener(ImageScaleMode::CENTER, this), UI_TEST_CENTER);
113 
114     y += STEP;
115     AddLable(lableX, y, "Scale Down");
116     AddRadioButton(GetRect(RADIO_X, y + RADIO_OFFSET, RADIO_SIZE, RADIO_SIZE),
117         "RB", new StateChangeListener(ImageScaleMode::SCALE_DOWN, this), UI_TEST_SCALE_DOWN);
118 }
119 
TearDown()120 void UITestTransform::TearDown()
121 {
122     DeleteChildren(container_);
123     container_ = nullptr;
124     layout_ = nullptr;
125     uiViewGroupFrame_ = nullptr;
126     imageView_ = nullptr;
127 }
128 
GetTestView()129 const UIView* UITestTransform::GetTestView()
130 {
131     UIKitTransformTestRotate001();
132     UIKitTransformTestScale002();
133     UIKitTransformTestTranslate003();
134 
135     layout_->LayoutChildren();
136     return container_;
137 }
138 
SetUpButton(UILabelButton * btn,const char * title,const char * id)139 void UITestTransform::SetUpButton(UILabelButton* btn, const char* title, const char* id)
140 {
141     if (btn == nullptr || title == nullptr || id == nullptr) {
142         return;
143     }
144     layout_->Add(btn);
145     btn->Resize(BUTTON_WIDHT1, BUTTON_HEIGHT1);
146     btn->SetText(title);
147     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
148     btn->SetOnClickListener(this);
149     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
150     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
151     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
152     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
153     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
154     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
155     btn->SetViewId(id);
156 }
157 
UIKitTransformTestRotate001()158 void UITestTransform::UIKitTransformTestRotate001()
159 {
160     rotateBtn_ = new UILabelButton();
161     SetUpButton(rotateBtn_, "旋转", UI_TEST_ROTATE);
162 }
163 
UIKitTransformTestScale002()164 void UITestTransform::UIKitTransformTestScale002()
165 {
166     scaleBtn_ = new UILabelButton();
167     SetUpButton(scaleBtn_, "缩放", UI_TEST_ZOOM);
168 }
169 
UIKitTransformTestTranslate003()170 void UITestTransform::UIKitTransformTestTranslate003()
171 {
172     translateBtn_ = new UILabelButton();
173     SetUpButton(translateBtn_, "平移", UI_TEST_TRANSLATION);
174 }
175 
OnClick(UIView & view,const ClickEvent & event)176 bool UITestTransform::OnClick(UIView& view, const ClickEvent& event)
177 {
178     Rect viewRect = imageView_->GetOrigRect();
179     TransformMap transMap(viewRect);
180     Vector2<float> pivot(58, 58); // 58:x value 58:y value
181     if (&view == rotateBtn_) {
182         angle_ = (angle_ + 90) % 360; // 90: increase angle; 360: wrapping value
183     } else if (&view == scaleBtn_) {
184         scale_ += 0.1f; // 0.1: increase scale
185     } else if (&view == translateBtn_) {
186         trans_ += 10; // 10: increase translate
187     }
188     SetTransMap(angle_, scale_, trans_, pivot);
189     return true;
190 }
191 
SetScaleMode(ImageScaleMode mode)192 void UITestTransform::SetScaleMode(ImageScaleMode mode)
193 {
194     // must the position fisrt
195     switch (mode) {
196         case ImageScaleMode::AUTO:
197             imageView_->SetAutoEnable(true);
198             imageView_->SetResizeMode(UIImageView::ImageResizeMode::NONE);
199             break;
200         case ImageScaleMode::TILING:
201             imageView_->SetAutoEnable(false);
202             imageView_->SetResizeMode(UIImageView::ImageResizeMode::NONE);
203             break;
204         case ImageScaleMode::COVER:
205             imageView_->SetAutoEnable(false);
206             imageView_->SetResizeMode(UIImageView::ImageResizeMode::COVER);
207             break;
208         case ImageScaleMode::CONTAIN:
209             imageView_->SetAutoEnable(false);
210             imageView_->SetResizeMode(UIImageView::ImageResizeMode::CONTAIN);
211             break;
212         case ImageScaleMode::FILL:
213             imageView_->SetAutoEnable(false);
214             imageView_->SetResizeMode(UIImageView::ImageResizeMode::FILL);
215             break;
216         case ImageScaleMode::CENTER:
217             imageView_->SetAutoEnable(false);
218             imageView_->SetResizeMode(UIImageView::ImageResizeMode::CENTER);
219             break;
220         case ImageScaleMode::SCALE_DOWN:
221             imageView_->SetAutoEnable(false);
222             imageView_->SetResizeMode(UIImageView::ImageResizeMode::SCALE_DOWN);
223             break;
224         default:
225             break;
226     }
227     if (mode != ImageScaleMode::AUTO) {
228         imageView_->SetWidth(200); // 200: width
229         imageView_->SetHeight(200); // 200: height
230     }
231     // reset the transmap
232     SetTransMap(0, 1.0f, 0, {0.0f,  0.0f});
233     uiViewGroupFrame_->Invalidate();
234 }
235 
SetTransMap(int16_t angle,float scale,int16_t trans,Vector2<float> pivot)236 void UITestTransform::SetTransMap(int16_t angle, float scale, int16_t trans, Vector2<float> pivot)
237 {
238     angle_ = angle;
239     scale_ = scale;
240     trans_ = trans;
241     Rect viewRect = imageView_->GetOrigRect();
242     TransformMap transMap(viewRect);
243     transMap.Rotate(angle_, pivot);
244     transMap.Scale(Vector2<float>(scale_, scale_), pivot);
245     transMap.Translate(Vector2<int16_t>(trans, 0));
246     imageView_->SetTransformMap(transMap);
247 }
248 
AddLable(int16_t x,int16_t y,const char * data)249 UILabel* UITestTransform::AddLable(int16_t x, int16_t y, const char* data)
250 {
251     UILabel* label = new UILabel();
252     container_->Add(label);
253     label->SetPosition(x, y, Screen::GetInstance().GetWidth(),
254         TITLE_LABEL_DEFAULT_HEIGHT);
255     label->SetText(data);
256     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
257     return label;
258 }
259 
AddRadioButton(Rect rect,const char * name,UICheckBox::OnChangeListener * listener,const char * id)260 UIRadioButton* UITestTransform::AddRadioButton(Rect rect, const char* name,
261                                                UICheckBox::OnChangeListener* listener, const char* id)
262 {
263     if (listener == nullptr || name == nullptr) {
264         return nullptr;
265     }
266     auto radioButton = new UITestRadioButton(name);
267     container_->Add(radioButton);
268     radioButton->SetListener(listener);
269     radioButton->SetPosition(rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight());
270     radioButton->SetViewId(id);
271     if (radioButton->GetState() == UICheckBox::SELECTED) {
272         listener->OnChange(UICheckBox::SELECTED);
273     }
274     return radioButton;
275 }
276 
StateChangeListener(ImageScaleMode mode,UITestTransform * testInstance)277 StateChangeListener::StateChangeListener(ImageScaleMode mode, UITestTransform* testInstance)
278     : mode_(mode),
279       testInstance_(testInstance)
280 {
281 }
282 
OnChange(UICheckBox::UICheckBoxState state)283 bool StateChangeListener::OnChange(UICheckBox::UICheckBoxState state)
284 {
285     if (state == UICheckBox::UICheckBoxState::SELECTED) {
286         testInstance_->SetScaleMode(mode_);
287     }
288     return true;
289 }
290 
UITestRadioButton(const char * name)291 UITestRadioButton::UITestRadioButton(const char*  name)
292     : UIRadioButton(name)
293 {
294 }
295 
~UITestRadioButton()296 UITestRadioButton::~UITestRadioButton()
297 {
298     if (listener_ != nullptr) {
299         delete listener_;
300         listener_ = nullptr;
301     }
302 }
303 
SetListener(UICheckBox::OnChangeListener * listener)304 void UITestRadioButton::SetListener(UICheckBox::OnChangeListener* listener)
305 {
306     SetOnChangeListener(listener);
307     if (listener_ != nullptr) {
308         delete listener_;
309     }
310     listener_ = listener;
311 }
312 } // namespace OHOS
313