• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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))->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));
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));
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));
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));
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));
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));
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     UIKit_Transform_Test_Rotate_001();
132     UIKit_Transform_Test_Scale_002();
133     UIKit_Transform_Test_Translate_003();
134 
135     layout_->LayoutChildren();
136     return container_;
137 }
138 
SetUpButton(UILabelButton * btn,const char * title)139 void UITestTransform::SetUpButton(UILabelButton* btn, const char* title)
140 {
141     if (btn == 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 }
156 
UIKit_Transform_Test_Rotate_001()157 void UITestTransform::UIKit_Transform_Test_Rotate_001()
158 {
159     rotateBtn_ = new UILabelButton();
160     SetUpButton(rotateBtn_, "旋转");
161 }
162 
UIKit_Transform_Test_Scale_002()163 void UITestTransform::UIKit_Transform_Test_Scale_002()
164 {
165     scaleBtn_ = new UILabelButton();
166     SetUpButton(scaleBtn_, "缩放");
167 }
168 
UIKit_Transform_Test_Translate_003()169 void UITestTransform::UIKit_Transform_Test_Translate_003()
170 {
171     translateBtn_ = new UILabelButton();
172     SetUpButton(translateBtn_, "平移");
173 }
174 
OnClick(UIView & view,const ClickEvent & event)175 bool UITestTransform::OnClick(UIView& view, const ClickEvent& event)
176 {
177     Rect viewRect = imageView_->GetOrigRect();
178     TransformMap transMap(viewRect);
179     Vector2<float> pivot(58, 58); // 58:x value 58:y value
180     if (&view == rotateBtn_) {
181         angle_ = (angle_ + 90) % 360; // 90: increase angle; 360: wrapping value
182     } else if (&view == scaleBtn_) {
183         scale_ += 0.1f; // 0.1: increase scale
184     } else if (&view == translateBtn_) {
185         trans_ += 10; // 10: increase translate
186     }
187     SetTransMap(angle_, scale_, trans_, pivot);
188     return true;
189 }
190 
SetScaleMode(ImageScaleMode mode)191 void UITestTransform::SetScaleMode(ImageScaleMode mode)
192 {
193     // must the position fisrt
194     switch (mode) {
195         case ImageScaleMode::AUTO:
196             imageView_->SetAutoEnable(true);
197             imageView_->SetResizeMode(UIImageView::ImageResizeMode::NONE);
198             break;
199         case ImageScaleMode::TILING:
200             imageView_->SetAutoEnable(false);
201             imageView_->SetResizeMode(UIImageView::ImageResizeMode::NONE);
202             break;
203         case ImageScaleMode::COVER:
204             imageView_->SetAutoEnable(false);
205             imageView_->SetResizeMode(UIImageView::ImageResizeMode::COVER);
206             break;
207         case ImageScaleMode::CONTAIN:
208             imageView_->SetAutoEnable(false);
209             imageView_->SetResizeMode(UIImageView::ImageResizeMode::CONTAIN);
210             break;
211         case ImageScaleMode::FILL:
212             imageView_->SetAutoEnable(false);
213             imageView_->SetResizeMode(UIImageView::ImageResizeMode::FILL);
214             break;
215         case ImageScaleMode::CENTER:
216             imageView_->SetAutoEnable(false);
217             imageView_->SetResizeMode(UIImageView::ImageResizeMode::CENTER);
218             break;
219         case ImageScaleMode::SCALE_DOWN:
220             imageView_->SetAutoEnable(false);
221             imageView_->SetResizeMode(UIImageView::ImageResizeMode::SCALE_DOWN);
222             break;
223         default:
224             break;
225     }
226     if (mode != ImageScaleMode::AUTO) {
227         imageView_->SetWidth(200); // 200: width
228         imageView_->SetHeight(200); // 200: height
229     }
230     // reset the transmap
231     SetTransMap(0, 1.0f, 0, {0.0f,  0.0f});
232     uiViewGroupFrame_->Invalidate();
233 }
234 
SetTransMap(int16_t angle,float scale,int16_t trans,Vector2<float> pivot)235 void UITestTransform::SetTransMap(int16_t angle, float scale, int16_t trans, Vector2<float> pivot)
236 {
237     angle_ = angle;
238     scale_ = scale;
239     trans_ = trans;
240     Rect viewRect = imageView_->GetOrigRect();
241     TransformMap transMap(viewRect);
242     transMap.Rotate(angle_, pivot);
243     transMap.Scale(Vector2<float>(scale_, scale_), pivot);
244     transMap.Translate(Vector2<int16_t>(trans, 0));
245     imageView_->SetTransformMap(transMap);
246 }
247 
AddLable(int16_t x,int16_t y,const char * data)248 UILabel* UITestTransform::AddLable(int16_t x, int16_t y, const char* data)
249 {
250     UILabel* label = new UILabel();
251     container_->Add(label);
252     label->SetPosition(x, y, Screen::GetInstance().GetWidth(),
253         TITLE_LABEL_DEFAULT_HEIGHT);
254     label->SetText(data);
255     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
256     return label;
257 }
258 
AddRadioButton(Rect rect,const char * name,UICheckBox::OnChangeListener * listener)259 UIRadioButton* UITestTransform::AddRadioButton(Rect rect, const char* name, UICheckBox::OnChangeListener* listener)
260 {
261     if (listener == nullptr || name == nullptr) {
262         return nullptr;
263     }
264     auto radioButton = new UITestRadioButton(name);
265     container_->Add(radioButton);
266     radioButton->SetListener(listener);
267     radioButton->SetPosition(rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight());
268     if (radioButton->GetState() == UICheckBox::SELECTED) {
269         listener->OnChange(UICheckBox::SELECTED);
270     }
271     return radioButton;
272 }
273 
StateChangeListener(ImageScaleMode mode,UITestTransform * testInstance)274 StateChangeListener::StateChangeListener(ImageScaleMode mode, UITestTransform* testInstance)
275     : mode_(mode),
276       testInstance_(testInstance)
277 {
278 }
279 
OnChange(UICheckBox::UICheckBoxState state)280 bool StateChangeListener::OnChange(UICheckBox::UICheckBoxState state)
281 {
282     if (state == UICheckBox::UICheckBoxState::SELECTED) {
283         testInstance_->SetScaleMode(mode_);
284     }
285     return true;
286 }
287 
UITestRadioButton(const char * name)288 UITestRadioButton::UITestRadioButton(const char*  name)
289     : UIRadioButton(name)
290 {
291 }
292 
~UITestRadioButton()293 UITestRadioButton::~UITestRadioButton()
294 {
295     if (listener_ != nullptr) {
296         delete listener_;
297         listener_ = nullptr;
298     }
299 }
300 
SetListener(UICheckBox::OnChangeListener * listener)301 void UITestRadioButton::SetListener(UICheckBox::OnChangeListener* listener)
302 {
303     SetOnChangeListener(listener);
304     if (listener_ != nullptr) {
305         delete listener_;
306     }
307     listener_ = listener;
308 }
309 } // namespace OHOS