import {StackLayout} from "ui/layouts/stack-layout"; import {Button} from "ui/button"; import * as TKUnit from "../../TKUnit"; import * as helper from "./layout-helper"; import * as enums from "ui/enums"; import * as utils from "utils/utils"; import * as testModule from "../../ui-test"; import * as layoutHelper from "./layout-helper"; import * as commonTests from "./common-layout-tests"; export class StackLayoutTest extends testModule.UITest { private rootLayout: helper.MyStackLayout; private btn1: helper.MyButton; private btn2: helper.MyButton; public create(): StackLayout { this.rootLayout = new helper.MyStackLayout(); this.btn1 = new helper.MyButton(); this.btn1.text = "btn1"; this.rootLayout.addChild(this.btn1); this.btn2 = new helper.MyButton(); this.btn2.text = "btn2"; this.rootLayout.addChild(this.btn2); return this.rootLayout; } public test_orientation_DefaultValue() { TKUnit.assertEqual(this.rootLayout.orientation, enums.Orientation.vertical, "Default orientation should be Vertical."); } public test_SetWrongOrientation_ShouldThrowError() { TKUnit.assertThrows(() => { (this.rootLayout).orientation = "not_valid"; }, "Setting invalid value for orientation should throw exception."); } public test_Orientation_Change() { this.waitUntilTestElementLayoutIsValid(); TKUnit.assertEqual(this.rootLayout.orientation, enums.Orientation.vertical, "Default orientation should be Vertical."); this.rootLayout.orientation = "horizontal"; this.waitUntilTestElementLayoutIsValid(); TKUnit.assertEqual(this.rootLayout.measureCount, 2, "Orientation change should invalidate measure."); TKUnit.assertEqual(this.rootLayout.arrangeCount, 2, "Orientation change should invalidate arrange."); } public test_ShouldMeasureWith_AtMost_OnVertical() { this.waitUntilTestElementLayoutIsValid(); TKUnit.assertEqual(this.rootLayout.orientation, enums.Orientation.vertical, "StackLayout should be vertical."); TKUnit.assertTrue(this.rootLayout.measured, "Layout should be measured."); TKUnit.assertTrue(this.rootLayout.arranged, "Layout should be arranged."); TKUnit.assertEqual(utils.layout.getMeasureSpecMode(this.btn1.heightMeasureSpec), utils.layout.AT_MOST, "Layout should measure child with AT_MOST Height in vertical orientation."); } public test_ShouldMeasureWith_AtMost_OnHorizontal() { this.rootLayout.orientation = "horizontal"; this.waitUntilTestElementLayoutIsValid(); TKUnit.assert(this.rootLayout.measured, "Layout should be measured."); TKUnit.assert(this.rootLayout.arranged, "Layout should be arranged."); TKUnit.assertEqual(utils.layout.getMeasureSpecMode(this.btn1.widthMeasureSpec), utils.layout.AT_MOST, "Layout should measure child with AT_MOST Width in horizontal orientation."); } public test_DesiredSize_Vertical() { this.rootLayout.verticalAlignment = "top"; this.rootLayout.horizontalAlignment = "left"; this.waitUntilTestElementLayoutIsValid(); TKUnit.assertEqual(this.rootLayout.getMeasuredWidth(), Math.max(this.btn1.getMeasuredWidth(), this.btn2.getMeasuredWidth()), "Layout getMeasuredWidth should be Max of children getMeasuredWidth"); TKUnit.assertEqual(this.rootLayout.getMeasuredHeight(), (this.btn1.getMeasuredHeight() + this.btn2.getMeasuredHeight()), "Layout getMeasuredHeight should be Sum of children getMeasuredHeight"); } public test_DesiredSize_Horizontal() { this.rootLayout.horizontalAlignment = "left"; this.rootLayout.verticalAlignment = "top"; this.rootLayout.orientation = "horizontal"; this.waitUntilTestElementLayoutIsValid(); TKUnit.assertEqual(this.rootLayout.getMeasuredWidth(), (this.btn1.getMeasuredWidth() + this.btn2.getMeasuredWidth()), "Layout getMeasuredWidth should be Sum of children getMeasuredWidth"); TKUnit.assertEqual(this.rootLayout.getMeasuredHeight(), Math.max(this.btn1.getMeasuredHeight(), this.btn2.getMeasuredHeight()), "Layout getMeasuredHeight should be Max of children getMeasuredHeight"); } public test_Padding_Vertical() { this.rootLayout.width = { value: 300, unit: "px" }; this.rootLayout.height = { value: 300, unit: "px" }; this.rootLayout.style.paddingLeft = { value: 10, unit: "px" }; this.rootLayout.style.paddingTop = { value: 20, unit: "px" }; this.rootLayout.style.paddingRight = { value: 30, unit: "px" }; this.rootLayout.style.paddingBottom = { value: 40, unit: "px" }; this.btn1.height = { value: 50, unit: "px" }; this.btn2.height = { value: 50, unit: "px" }; this.waitUntilTestElementLayoutIsValid(); helper.assertMeasure(this.btn1, 260, 50); helper.assertMeasure(this.btn2, 260, 50); helper.assertLayout(this.btn1, 10, 20, 260, 50, "btn1"); helper.assertLayout(this.btn2, 10, 70, 260, 50, "btn2"); } public test_Padding_Horizontal() { this.rootLayout.width = { value: 300, unit: "px" }; this.rootLayout.height = { value: 300, unit: "px" }; this.rootLayout.orientation = "horizontal"; this.rootLayout.style.paddingLeft = { value: 10, unit: "px" }; this.rootLayout.style.paddingTop = { value: 20, unit: "px" }; this.rootLayout.style.paddingRight = { value: 30, unit: "px" }; this.rootLayout.style.paddingBottom = { value: 40, unit: "px" }; this.btn1.width = { value: 50, unit: "px" }; this.btn2.width = { value: 50, unit: "px" }; this.waitUntilTestElementLayoutIsValid(); helper.assertMeasure(this.btn1, 50, 240); helper.assertMeasure(this.btn2, 50, 240); helper.assertLayout(this.btn1, 10, 20, 50, 240, "btn1"); helper.assertLayout(this.btn2, 60, 20, 50, 240, "btn2"); } private assertChildTexts(expected, layout, message) { let texts: Array = []; layout.eachChildView((child: { text: string }) => texts.push(child.text)); TKUnit.assertEqual(expected, texts.join('|'), message); } public test_insertChildAtPosition() { this.assertChildTexts("btn1|btn2", this.rootLayout, "initial 2 buttons"); let newChild = new Button(); newChild.text = 'in-between'; this.rootLayout.insertChild(newChild, 1); this.assertChildTexts("btn1|in-between|btn2", this.rootLayout, "button inserted at correct location"); } public test_getChildIndex() { let lastChildIndex = this.rootLayout.getChildrenCount() - 1; let lastButton =