Skip to content

Commit 8cfeafe

Browse files
authored
[cpp-tests] improve items location of part cases (#19210)
* item location improvement * improve ui focus cases location * improve part node position * improve webview test cases location
1 parent 41d0474 commit 8cfeafe

File tree

5 files changed

+48
-58
lines changed

5 files changed

+48
-58
lines changed

tests/cpp-tests/Classes/BaseTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void TestList::runThisTest()
194194
auto autoTestItem = MenuItemLabel::create(autoTestLabel, [&](Ref* sender){
195195
TestController::getInstance()->startAutoTest();
196196
});
197-
autoTestItem->setPosition(Vec2(VisibleRect::left().x + 60, VisibleRect::bottom().y + 50));
197+
autoTestItem->setPosition(Vec2(VisibleRect::right().x - 60, VisibleRect::bottom().y + 50));
198198

199199
auto menu = Menu::create(closeItem, autoTestItem, nullptr);
200200
menu->setPosition(Vec2::ZERO);
@@ -487,4 +487,4 @@ void TestCase::onBackCallback(Ref* sender)
487487
{
488488
_testSuite->backsUpOneLevel();
489489
}
490-
}
490+
}

tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ bool LabelFNTMultiLineAlignment::init()
605605
this->_arrowsBar = Sprite::create("Images/arrowsBar.png");
606606
this->_arrows = Sprite::create("Images/arrows.png");
607607

608-
MenuItemFont::setFontSize(20);
608+
MenuItemFont::setFontSize(15);
609609
auto longSentences = MenuItemFont::create("Long Flowing Sentences", CC_CALLBACK_1(LabelFNTMultiLineAlignment::stringChanged, this));
610610
auto lineBreaks = MenuItemFont::create("Short Sentences With Intentional Line Breaks", CC_CALLBACK_1(LabelFNTMultiLineAlignment::stringChanged, this));
611611
auto mixed = MenuItemFont::create("Long Sentences Mixed With Intentional Line Breaks", CC_CALLBACK_1(LabelFNTMultiLineAlignment::stringChanged, this));
@@ -620,7 +620,7 @@ bool LabelFNTMultiLineAlignment::init()
620620
_menuItems.push_back(lineBreaks);
621621
_menuItems.push_back(mixed);
622622

623-
MenuItemFont::setFontSize(30);
623+
MenuItemFont::setFontSize(20);
624624

625625
auto left = MenuItemFont::create("Left", CC_CALLBACK_1(LabelFNTMultiLineAlignment::alignmentChanged, this));
626626
auto center = MenuItemFont::create("Center", CC_CALLBACK_1(LabelFNTMultiLineAlignment::alignmentChanged, this));
@@ -642,8 +642,8 @@ bool LabelFNTMultiLineAlignment::init()
642642
this->_arrowsBar->setScaleX(arrowsWidth / this->_arrowsBar->getContentSize().width);
643643
this->_arrowsBar->setPosition(Vec2(((ArrowsMax + ArrowsMin) / 2) * size.width, this->_label->getPosition().y));
644644

645-
stringMenu->setPosition(Vec2(size.width/2, size.height - menuItemPaddingCenter));
646-
alignmentMenu->setPosition(Vec2(size.width/2, menuItemPaddingCenter+15));
645+
stringMenu->setPosition(VisibleRect::top() + Vec2(0, - menuItemPaddingCenter));
646+
alignmentMenu->setPosition(VisibleRect::bottom() + Vec2(0, menuItemPaddingCenter + 15));
647647

648648
this->selectSentenceItem(longSentences);
649649
this->selectAlignmentItem(center);
@@ -1062,12 +1062,12 @@ LabelTTFCJKWrappingTest::LabelTTFCJKWrappingTest()
10621062
Vec2(size.width * 0.85, size.height * 0.8),
10631063
Vec2(size.width * 0.85, 0), 1, Color4F(1, 0, 0, 1));
10641064

1065-
TTFConfig ttfConfig("fonts/HKYuanMini.ttf", 25, GlyphCollection::DYNAMIC);
1065+
TTFConfig ttfConfig("fonts/HKYuanMini.ttf", 20, GlyphCollection::DYNAMIC);
10661066
auto label1 = Label::createWithTTF(ttfConfig,
10671067
"你好,Cocos2d-x v3的New Label.", TextHAlignment::LEFT, size.width * 0.75);
10681068
if(label1) {
10691069
label1->setTextColor(Color4B(128, 255, 255, 255));
1070-
label1->setPosition(Vec2(size.width * 0.1, size.height * 0.6));
1070+
label1->setPosition(Vec2(size.width * 0.1, VisibleRect::top().y * 0.7));
10711071
label1->setAnchorPoint(Vec2(0, 0.5));
10721072
this->addChild(label1);
10731073
// Demo for unloadFontAtlasTTF function, after it been called, all UI widget
@@ -1080,7 +1080,7 @@ LabelTTFCJKWrappingTest::LabelTTFCJKWrappingTest()
10801080
"早上好,Cocos2d-x v3的New Label.", TextHAlignment::LEFT, size.width * 0.75);
10811081
if(label2) {
10821082
label2->setTextColor(Color4B(255, 128, 255, 255));
1083-
label2->setPosition(Vec2(size.width * 0.1, size.height * 0.4));
1083+
label2->setPosition(Vec2(size.width * 0.1, VisibleRect::top().y * 0.5));
10841084
label2->setAnchorPoint(Vec2(0, 0.5));
10851085
this->addChild(label2);
10861086
}
@@ -1089,7 +1089,7 @@ LabelTTFCJKWrappingTest::LabelTTFCJKWrappingTest()
10891089
"美好的一天啊美好的一天啊美好的一天啊", TextHAlignment::LEFT, size.width * 0.75);
10901090
if(label3) {
10911091
label3->setTextColor(Color4B(255, 255, 128, 255));
1092-
label3->setPosition(Vec2(size.width * 0.1, size.height * 0.2));
1092+
label3->setPosition(Vec2(size.width * 0.1, VisibleRect::top().y * 0.3));
10931093
label3->setAnchorPoint(Vec2(0, 0.5));
10941094
this->addChild(label3);
10951095
}

tests/cpp-tests/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ void RemoveListenerWhenDispatching::onEnter()
356356
}
357357
}, MenuItemFont::create("Enabled"), MenuItemFont::create("Disabled"), nullptr);
358358

359-
toggleItem->setPosition(origin + Vec2(size.width/2, 80));
359+
toggleItem->setPosition(origin + Vec2(size.width * 0.8, 80));
360360
auto menu = Menu::create(toggleItem, nullptr);
361361
menu->setPosition(Vec2(0, 0));
362362
menu->setAnchorPoint(Vec2(0, 0));
@@ -650,7 +650,7 @@ void RemoveListenerAfterAddingTest::onEnter()
650650
_eventDispatcher->removeEventListener(listener);
651651
});
652652

653-
item1->setPosition(VisibleRect::center() + Vec2(0, 80));
653+
item1->setPosition(VisibleRect::leftBottom() + Vec2(0, 80));
654654

655655
auto addNextButton = [this](){
656656
auto next = MenuItemFont::create("Please Click Me To Reset!", [this](Ref* sender){
@@ -677,7 +677,7 @@ void RemoveListenerAfterAddingTest::onEnter()
677677
addNextButton();
678678
});
679679

680-
item2->setPosition(VisibleRect::center() + Vec2(0, 40));
680+
item2->setPosition(VisibleRect::leftBottom() + Vec2(0, 40));
681681

682682
auto item3 = MenuItemFont::create("Click Me 3", [=](Ref* sender){
683683
auto listener = EventListenerTouchOneByOne::create();
@@ -692,10 +692,10 @@ void RemoveListenerAfterAddingTest::onEnter()
692692
addNextButton();
693693
});
694694

695-
item3->setPosition(VisibleRect::center());
695+
item3->setPosition(VisibleRect::leftBottom());
696696

697697
auto menu = Menu::create(item1, item2, item3, nullptr);
698-
menu->setPosition(VisibleRect::leftBottom());
698+
menu->setPosition(VisibleRect::rightBottom() * 0.8 + Vec2(0, 40));
699699
menu->setAnchorPoint(Vec2::ZERO);
700700

701701
addChild(menu);

tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIFocusTest/UIFocusTest.cpp

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,21 @@ bool UIFocusTestBase::init()
6464

6565
_dpadMenu = Menu::create();
6666

67-
auto winSize = Director::getInstance()->getVisibleSize();
6867
auto leftItem = MenuItemFont::create("Left", CC_CALLBACK_0(UIFocusTestBase::onLeftKeyPressed, this));
69-
leftItem->setPosition(Vec2(winSize.width - 100, winSize.height/2));
68+
leftItem->setPosition(VisibleRect::right() + Vec2(-100, 0));
7069
_dpadMenu->addChild(leftItem);
7170

7271

7372
auto rightItem = MenuItemFont::create("Right", CC_CALLBACK_0(UIFocusTestBase::onRightKeyPressed, this));
74-
rightItem->setPosition(Vec2(winSize.width - 30, winSize.height/2));
73+
rightItem->setPosition(VisibleRect::right() + Vec2(-30, 0));
7574
_dpadMenu->addChild(rightItem);
7675

7776
auto upItem = MenuItemFont::create("Up", CC_CALLBACK_0(UIFocusTestBase::onUpKeyPressed, this));
78-
upItem->setPosition(Vec2(winSize.width - 60, winSize.height/2 + 50));
77+
upItem->setPosition(VisibleRect::right() + Vec2(-60, 50));
7978
_dpadMenu->addChild(upItem);
8079

8180
auto downItem = MenuItemFont::create("Down", CC_CALLBACK_0(UIFocusTestBase::onDownKeyPressed, this));
82-
downItem->setPosition(Vec2(winSize.width - 60, winSize.height/2 - 50));
81+
downItem->setPosition(VisibleRect::right() + Vec2(-60, -50));
8382
_dpadMenu->addChild(downItem);
8483

8584
_dpadMenu->setPosition(Vec2::ZERO);
@@ -95,7 +94,7 @@ bool UIFocusTestBase::init()
9594

9695
_toggleButton = Button::create("cocosui/switch-mask.png");
9796
_toggleButton->setTitleText("Toggle Loop");
98-
_toggleButton->setPosition(Vec2(60, winSize.height - 50));
97+
_toggleButton->setPosition(VisibleRect::leftTop() + Vec2(60, -50));
9998
_toggleButton->setTitleColor(Color3B::RED);
10099
_toggleButton->setFocusEnabled(false);
101100
this->addChild(_toggleButton);
@@ -181,10 +180,8 @@ bool UIFocusTestHorizontal::init()
181180
{
182181
if (UIFocusTestBase::init()) {
183182

184-
Size winSize = Director::getInstance()->getVisibleSize();
185-
186183
_horizontalLayout = HBox::create();
187-
_horizontalLayout->setPosition(Vec2(20, winSize.height/2 + 40));
184+
_horizontalLayout->setPosition(VisibleRect::left() + Vec2(20, 40));
188185
_uiLayer->addChild(_horizontalLayout);
189186

190187
_horizontalLayout->setFocused(true);
@@ -202,7 +199,7 @@ bool UIFocusTestHorizontal::init()
202199
}
203200

204201
_loopText = Text::create("loop enabled", "Arial", 20);
205-
_loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
202+
_loopText->setPosition(VisibleRect::top() + Vec2(0, -50));
206203
_loopText->setColor(Color3B::GREEN);
207204
this->addChild(_loopText);
208205

@@ -244,10 +241,8 @@ bool UIFocusTestVertical::init()
244241
{
245242
if (UIFocusTestBase::init()) {
246243

247-
Size winSize = Director::getInstance()->getVisibleSize();
248-
249244
_verticalLayout = VBox::create();
250-
_verticalLayout->setPosition(Vec2(winSize.width/2 - 100, winSize.height - 70));
245+
_verticalLayout->setPosition(VisibleRect::top() + Vec2(-100, -70));
251246
_uiLayer->addChild(_verticalLayout);
252247
_verticalLayout->setTag(100);
253248
_verticalLayout->setScale(0.5);
@@ -266,7 +261,7 @@ bool UIFocusTestVertical::init()
266261
}
267262

268263
_loopText = Text::create("loop enabled", "Arial", 20);
269-
_loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
264+
_loopText->setPosition(VisibleRect::top() + Vec2(0, -50));
270265
_loopText->setColor(Color3B::GREEN);
271266
this->addChild(_loopText);
272267

@@ -305,10 +300,8 @@ bool UIFocusTestNestedLayout1::init()
305300
{
306301
if (UIFocusTestBase::init()) {
307302

308-
Size winSize = Director::getInstance()->getVisibleSize();
309-
310303
_verticalLayout = VBox::create();
311-
_verticalLayout->setPosition(Vec2(winSize.width/2 - 80, winSize.height - 70));
304+
_verticalLayout->setPosition(VisibleRect::top() + Vec2(-80, -70));
312305
_uiLayer->addChild(_verticalLayout);
313306
_verticalLayout->setScale(0.5);
314307

@@ -362,7 +355,7 @@ bool UIFocusTestNestedLayout1::init()
362355
}
363356

364357
_loopText = Text::create("loop enabled", "Arial", 20);
365-
_loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
358+
_loopText->setPosition(VisibleRect::top() + Vec2(0, -50));
366359
_loopText->setColor(Color3B::GREEN);
367360
this->addChild(_loopText);
368361

@@ -401,10 +394,8 @@ bool UIFocusTestNestedLayout2::init()
401394
{
402395
if (UIFocusTestBase::init()) {
403396

404-
Size winSize = Director::getInstance()->getVisibleSize();
405-
406397
_horizontalLayout = HBox::create();
407-
_horizontalLayout->setPosition(Vec2(winSize.width/2 - 200, winSize.height - 70));
398+
_horizontalLayout->setPosition(VisibleRect::top() + Vec2(-200, -70));
408399
_uiLayer->addChild(_horizontalLayout);
409400
_horizontalLayout->setScale(0.6f);
410401

@@ -458,7 +449,7 @@ bool UIFocusTestNestedLayout2::init()
458449
}
459450

460451
_loopText = Text::create("loop enabled", "Arial", 20);
461-
_loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
452+
_loopText->setPosition(VisibleRect::top() + Vec2(0, -50));
462453
_loopText->setColor(Color3B::GREEN);
463454
this->addChild(_loopText);
464455

@@ -497,10 +488,9 @@ bool UIFocusTestNestedLayout3::init()
497488
{
498489
if (UIFocusTestBase::init()) {
499490

500-
Size winSize = Director::getInstance()->getVisibleSize();
501-
502491
_verticalLayout = VBox::create();
503-
_verticalLayout->setPosition(Vec2(40, winSize.height - 70));
492+
_verticalLayout->setPosition(VisibleRect::leftTop() + Vec2(40, -70));
493+
504494
_uiLayer->addChild(_verticalLayout);
505495
_verticalLayout->setScale(0.8f);
506496

@@ -562,7 +552,7 @@ bool UIFocusTestNestedLayout3::init()
562552

563553

564554
_loopText = Text::create("loop enabled", "Arial", 20);
565-
_loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
555+
_loopText->setPosition(VisibleRect::top() + Vec2(0, -50));
566556
_loopText->setColor(Color3B::GREEN);
567557
this->addChild(_loopText);
568558

tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ WebViewTests::WebViewTests()
3636
bool WebViewTest::init()
3737
{
3838
if (UIScene::init()) {
39-
Size winSize = Director::getInstance()->getVisibleSize();
39+
Size visableSize = Director::getInstance()->getVisibleSize();
4040

41-
4241
_webView = cocos2d::experimental::ui::WebView::create();
43-
_webView->setPosition(winSize/2);
44-
_webView->setContentSize(winSize * 0.5);
42+
_webView->setPosition(VisibleRect::center());
43+
_webView->setContentSize(visableSize * 0.5);
4544
_webView->loadURL("https://www.baidu.com");
4645
_webView->setScalesPageToFit(true);
4746

@@ -52,13 +51,14 @@ bool WebViewTest::init()
5251
this->addChild(_webView);
5352

5453
auto spriteHello = Sprite::create("Hello.png");
55-
spriteHello->setPosition(winSize/2);
54+
spriteHello->setContentSize(visableSize * 0.5);
55+
spriteHello->setPosition(VisibleRect::center());
5656
this->addChild(spriteHello);
5757

5858
TextField *urlTextField = TextField::create("Input a URL here", "Arial", 20);
5959
urlTextField->setPlaceHolderColor(Color3B::RED);
60-
urlTextField->setPosition(Vec2(winSize/2) + Vec2(-80, _webView->getContentSize().height/2 +
61-
urlTextField->getContentSize().height/2 + 10));
60+
urlTextField->setPosition(VisibleRect::center() + Vec2(-80, _webView->getContentSize().height/2 +
61+
urlTextField->getContentSize().height/2 + 10));
6262
this->addChild(urlTextField);
6363

6464
Text *httpLabel = Text::create("https:// ", "Arial", 20);
@@ -71,8 +71,8 @@ bool WebViewTest::init()
7171
Button *resetBtn = Button::create("cocosui/animationbuttonnormal.png",
7272
"cocosui/animationbuttonpressed.png");
7373
resetBtn->setTitleText("Visit URL");
74-
resetBtn->setPosition(Vec2(winSize/2) + Vec2(50, _webView->getContentSize().height/2 +
75-
resetBtn->getContentSize().height/2 + 10));
74+
resetBtn->setPosition(VisibleRect::center() + Vec2(50, _webView->getContentSize().height/2 +
75+
resetBtn->getContentSize().height/2 + 10));
7676
resetBtn->addClickEventListener([=](Ref*){
7777
if (urlTextField->getString().size() != 0)
7878
{
@@ -85,7 +85,7 @@ bool WebViewTest::init()
8585
Button *reloadBtn = Button::create("cocosui/animationbuttonnormal.png",
8686
"cocosui/animationbuttonpressed.png");
8787
reloadBtn->setTitleText("Reload");
88-
reloadBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 +
88+
reloadBtn->setPosition(VisibleRect::center() + Vec2( _webView->getContentSize().width/2 +
8989
reloadBtn->getContentSize().width/2 + 10,50 ));
9090
reloadBtn->addClickEventListener([=](Ref*){
9191
_webView->reload();
@@ -95,7 +95,7 @@ bool WebViewTest::init()
9595
Button *forwardBtn = Button::create("cocosui/animationbuttonnormal.png",
9696
"cocosui/animationbuttonpressed.png");
9797
forwardBtn->setTitleText("Forward");
98-
forwardBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 +
98+
forwardBtn->setPosition(VisibleRect::center() + Vec2( _webView->getContentSize().width/2 +
9999
forwardBtn->getContentSize().width/2 + 10,0 ));
100100
forwardBtn->addClickEventListener([=](Ref*){
101101
_webView->goForward();
@@ -107,7 +107,7 @@ bool WebViewTest::init()
107107
Button *backBtn = Button::create("cocosui/animationbuttonnormal.png",
108108
"cocosui/animationbuttonpressed.png");
109109
backBtn->setTitleText("Back");
110-
backBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 +
110+
backBtn->setPosition(VisibleRect::center() + Vec2( _webView->getContentSize().width/2 +
111111
backBtn->getContentSize().width/2 + 10,-50 ));
112112
backBtn->addClickEventListener([=](Ref*){
113113
_webView->goBack();
@@ -118,7 +118,7 @@ bool WebViewTest::init()
118118
Button *loadFileBtn = Button::create("cocosui/animationbuttonnormal.png",
119119
"cocosui/animationbuttonpressed.png");
120120
loadFileBtn->setTitleText("Load FILE");
121-
loadFileBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 +
121+
loadFileBtn->setPosition(VisibleRect::center() - Vec2( _webView->getContentSize().width/2 +
122122
loadFileBtn->getContentSize().width/2 + 10,50 ));
123123
loadFileBtn->addClickEventListener([=](Ref*){
124124
_webView->loadFile("Test.html");
@@ -128,7 +128,7 @@ bool WebViewTest::init()
128128
Button *loadHTMLBtn = Button::create("cocosui/animationbuttonnormal.png",
129129
"cocosui/animationbuttonpressed.png");
130130
loadHTMLBtn->setTitleText("Load Data");
131-
loadHTMLBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 +
131+
loadHTMLBtn->setPosition(VisibleRect::center() - Vec2( _webView->getContentSize().width/2 +
132132
loadHTMLBtn->getContentSize().width/2 + 10,0 ));
133133
loadHTMLBtn->addClickEventListener([=](Ref*){
134134
_webView->loadHTMLString("<body style=\"font-size:50px;\">Hello World <img src=\"Icon.png\"/> </body>","Images/");
@@ -141,7 +141,7 @@ bool WebViewTest::init()
141141
Button *evalJsBtn = Button::create("cocosui/animationbuttonnormal.png",
142142
"cocosui/animationbuttonpressed.png");
143143
evalJsBtn->setTitleText("Evaluate JS");
144-
evalJsBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 +
144+
evalJsBtn->setPosition(VisibleRect::center() - Vec2( _webView->getContentSize().width/2 +
145145
evalJsBtn->getContentSize().width/2 + 10,-50 ));
146146
evalJsBtn->addClickEventListener([=](Ref*){
147147
_webView->evaluateJS("alert(\"hello\")");
@@ -152,7 +152,7 @@ bool WebViewTest::init()
152152
Button *opacityBtn = Button::create("cocosui/animationbuttonnormal.png",
153153
"cocosui/animationbuttonpressed.png");
154154
opacityBtn->setTitleText("Opacity 1.f");
155-
opacityBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 +
155+
opacityBtn->setPosition(VisibleRect::center() - Vec2( _webView->getContentSize().width/2 +
156156
opacityBtn->getContentSize().width/2 + 10, 100 ));
157157
opacityBtn->addClickEventListener([=](Ref*){
158158
auto currentOpacity = _webView->getOpacityWebView();
@@ -175,7 +175,7 @@ bool WebViewTest::init()
175175
Button *transparentBgBtn = Button::create("cocosui/animationbuttonnormal.png",
176176
"cocosui/animationbuttonpressed.png");
177177
transparentBgBtn->setTitleText("Transparent BG");
178-
transparentBgBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 +
178+
transparentBgBtn->setPosition(VisibleRect::center() + Vec2( _webView->getContentSize().width/2 +
179179
transparentBgBtn->getContentSize().width/2 + 10,-100 ));
180180
transparentBgBtn->addClickEventListener([=](Ref*){
181181
_webView->setBackgroundTransparent();

0 commit comments

Comments
 (0)