From 20f8f8a852840331d960856e832182193c3d8b0f Mon Sep 17 00:00:00 2001 From: Hristo Hristov Date: Tue, 19 Jan 2016 10:34:33 +0200 Subject: [PATCH 1/3] Fixed bug in GridLayout Added tests for GridLayout --- apps/tests/layouts/grid-layout-tests.ts | 235 ++-- apps/tests/layouts/layout-helper.android.ts | 242 ++-- apps/tests/layouts/layout-helper.d.ts | 79 +- apps/tests/layouts/layout-helper.ios.ts | 232 ++-- apps/tests/layouts/stack-layout-tests.ts | 8 +- .../xml-declaration/xml-declaration-tests.ts | 3 +- ui/layouts/grid-layout/grid-layout-common.ts | 44 +- ui/layouts/grid-layout/grid-layout.ios.ts | 1068 ++++++++++------- 8 files changed, 1140 insertions(+), 771 deletions(-) diff --git a/apps/tests/layouts/grid-layout-tests.ts b/apps/tests/layouts/grid-layout-tests.ts index c8b085f8b..f1226a2f2 100644 --- a/apps/tests/layouts/grid-layout-tests.ts +++ b/apps/tests/layouts/grid-layout-tests.ts @@ -1,8 +1,7 @@ -import page = require("ui/page"); -import layout = require("ui/layouts/grid-layout"); +import {Page} from "ui/page"; +import {GridLayout, ItemSpec, GridUnitType} from "ui/layouts/grid-layout"; import {Button} from "ui/button"; import TKUnit = require("../TKUnit"); -import helper = require("./layout-helper"); import view = require("ui/core/view"); import navHelper = require("../ui/helper"); import utils = require("utils/utils"); @@ -15,69 +14,46 @@ import commonTests = require("./common-layout-tests"); var DELTA = 1; -export class MyGridLayout extends layout.GridLayout { - public measureCount: number = 0; - public layoutCount: number = 0; +export class GridLayoutTest extends testModule.UITest { - public get measured(): boolean { - return this.measureCount > 0; - } - - public get layouted(): boolean { - return this.layoutCount > 0; - } - - public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void { - super.onMeasure(widthMeasureSpec, heightMeasureSpec); - this.measureCount++; - } - - public onLayout(left: number, top: number, right: number, bottom: number): void { - super.onLayout(left, top, right, bottom); - this.layoutCount++; - } -} - -export class GridLayoutTest extends testModule.UITest { - - public create(): layout.GridLayout { - return new MyGridLayout(); + public create(): GridLayout { + return new GridLayout(); } private row(view: view.View): number { - return layout.GridLayout.getRow(view); + return GridLayout.getRow(view); } private rowSpan(view: view.View): number { - return layout.GridLayout.getRowSpan(view); + return GridLayout.getRowSpan(view); } private col(view: view.View): number { - return layout.GridLayout.getColumn(view); + return GridLayout.getColumn(view); } private colSpan(view: view.View): number { - return layout.GridLayout.getColumnSpan(view); + return GridLayout.getColumnSpan(view); } private prepareGridLayout(wait?: boolean) { - this.testView.addRow(new layout.ItemSpec(1, layout.GridUnitType.star)); - this.testView.addRow(new layout.ItemSpec(2, layout.GridUnitType.star)); - this.testView.addRow(new layout.ItemSpec(layoutHelper.dp(50), layout.GridUnitType.pixel)); - this.testView.addRow(new layout.ItemSpec(50, layout.GridUnitType.auto)); + this.testView.addRow(new ItemSpec(1, GridUnitType.star)); + this.testView.addRow(new ItemSpec(2, GridUnitType.star)); + this.testView.addRow(new ItemSpec(layoutHelper.dp(50), GridUnitType.pixel)); + this.testView.addRow(new ItemSpec(50, GridUnitType.auto)); - this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); - this.testView.addColumn(new layout.ItemSpec(2, layout.GridUnitType.star)); - this.testView.addColumn(new layout.ItemSpec(layoutHelper.dp(50), layout.GridUnitType.pixel)); - this.testView.addColumn(new layout.ItemSpec(50, layout.GridUnitType.auto)); + this.testView.addColumn(new ItemSpec(1, GridUnitType.star)); + this.testView.addColumn(new ItemSpec(2, GridUnitType.star)); + this.testView.addColumn(new ItemSpec(layoutHelper.dp(50), GridUnitType.pixel)); + this.testView.addColumn(new ItemSpec(50, GridUnitType.auto)); for (var r = 0; r < 4; r++) { for (var c = 0; c < 4; c++) { - var btn = new helper.MyButton(); + var btn = new layoutHelper.MyButton(); btn.text = "R" + r + "C" + c; - layout.GridLayout.setColumn(btn, c); - layout.GridLayout.setRow(btn, r); + GridLayout.setColumn(btn, c); + GridLayout.setRow(btn, r); if (c === 3) { btn.width = layoutHelper.dp(100); // Auto column should take 100px for this test. } @@ -124,73 +100,73 @@ export class GridLayoutTest extends testModule.UITest { public test_getRow_shouldThrow_onNullValues() { TKUnit.assertThrows(() => { - layout.GridLayout.getRow(null); + GridLayout.getRow(null); }, "getRow called with null should throw exception"); } public test_getRowSpan_shouldThrow_onNullValues() { TKUnit.assertThrows(() => { - layout.GridLayout.getRowSpan(null); + GridLayout.getRowSpan(null); }, "getRowSpan called with null should throw exception"); } public test_getColumn_shouldThrow_onNullValues() { TKUnit.assertThrows(() => { - layout.GridLayout.getColumn(null); + GridLayout.getColumn(null); }, "getColumn called with null should throw exception"); } public test_getColumnSpan_shouldThrow_onNullValues() { TKUnit.assertThrows(() => { - layout.GridLayout.getColumnSpan(null); + GridLayout.getColumnSpan(null); }, "getColumnSpan called with null should throw exception"); } public test_setRow_shouldThrow_onNullValues() { TKUnit.assertThrows(() => { - layout.GridLayout.setRow(null, 1); + GridLayout.setRow(null, 1); }, "setRow called with null should throw exception"); } public test_setRowSpan_shouldThrow_onNullValues() { TKUnit.assertThrows(() => { - layout.GridLayout.setRowSpan(null, 1); + GridLayout.setRowSpan(null, 1); }, "setRowSpan called with null should throw exception"); } public test_setColumn_shouldThrow_onNullValues() { TKUnit.assertThrows(() => { - layout.GridLayout.setColumn(null, 1); + GridLayout.setColumn(null, 1); }, "setColumn called with null should throw exception") } public test_setColumnSpan_shouldThrow_onNullValues() { TKUnit.assertThrows(() => { - layout.GridLayout.setColumnSpan(null, 1); + GridLayout.setColumnSpan(null, 1); }, "setColumnSpan called with null should throw exception"); } public test_setRow_shouldThrow_onNegativeValues() { TKUnit.assertThrows(() => { - layout.GridLayout.setRow(new Button(), -1); + GridLayout.setRow(new Button(), -1); }, "setRow should throw when value < 0"); } public test_setRowSpan_shouldThrow_onNotPositiveValues() { TKUnit.assertThrows(() => { - layout.GridLayout.setRowSpan(new Button(), 0); + GridLayout.setRowSpan(new Button(), 0); }, "setRowSpan should throw when value <= 0"); } public test_setColumn_shouldThrow_onNegativeValues() { TKUnit.assertThrows(() => { - layout.GridLayout.setColumn(new Button(), -1); + GridLayout.setColumn(new Button(), -1); }, "setColumn should when value < 0"); } public test_setColumnSpan_shouldThrow_onNotPositiveValues() { TKUnit.assertThrows(() => { - layout.GridLayout.setColumnSpan(new Button(), 0); + GridLayout.setColumnSpan(new Button(), 0); }, "setColumnSpan should throw when value <= 0"); } @@ -253,13 +229,13 @@ export class GridLayoutTest extends testModule.UITest { public test_measuredWidth_when_not_stretched_two_columns() { this.testView.horizontalAlignment = enums.HorizontalAlignment.center; - this.testView.addColumn(new layout.ItemSpec(layoutHelper.dp(80), layout.GridUnitType.pixel)); - this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); + this.testView.addColumn(new ItemSpec(layoutHelper.dp(80), GridUnitType.pixel)); + this.testView.addColumn(new ItemSpec(1, GridUnitType.star)); let btn = new Button(); btn.text = "A"; btn.width = layoutHelper.dp(100); - MyGridLayout.setColumnSpan(btn, 2); + GridLayout.setColumnSpan(btn, 2); this.testView.addChild(btn); this.waitUntilTestElementLayoutIsValid(); @@ -272,22 +248,22 @@ export class GridLayoutTest extends testModule.UITest { public test_measuredWidth_when_not_stretched_three_columns() { this.testView.horizontalAlignment = enums.HorizontalAlignment.center; - this.testView.addColumn(new layout.ItemSpec(layoutHelper.dp(80), layout.GridUnitType.pixel)); - this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); - this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.auto)); + this.testView.addColumn(new ItemSpec(layoutHelper.dp(80), GridUnitType.pixel)); + this.testView.addColumn(new ItemSpec(1, GridUnitType.star)); + this.testView.addColumn(new ItemSpec(1, GridUnitType.auto)); for (let i = 1; i < 4; i++) { let btn = new Button(); btn.text = "A"; btn.width = layoutHelper.dp(i * 20); - MyGridLayout.setColumn(btn, i - 1); + GridLayout.setColumn(btn, i - 1); this.testView.addChild(btn); } let btn = new Button(); btn.text = "B"; btn.width = layoutHelper.dp(100); - MyGridLayout.setColumnSpan(btn, 3); + GridLayout.setColumnSpan(btn, 3); this.testView.addChild(btn); this.waitUntilTestElementLayoutIsValid(); @@ -310,21 +286,21 @@ export class GridLayoutTest extends testModule.UITest { } public test_ItemSpec_actualLength_defaultValue() { - var def = new layout.ItemSpec(1, layout.GridUnitType.auto); + var def = new ItemSpec(1, GridUnitType.auto); TKUnit.assertEqual(def.actualLength, 0, "'actualLength' property default value should be 0."); } public test_ItemSpec_constructor_throws_onNegativeValue() { TKUnit.assertThrows(() => { - new layout.ItemSpec(-1, layout.GridUnitType.auto); + new ItemSpec(-1, GridUnitType.auto); }, "'value' should be positive number."); } public test_ItemSpec_constructor_doesnt_throw_onCorrectType() { try { - new layout.ItemSpec(1, layout.GridUnitType.auto); - new layout.ItemSpec(1, layout.GridUnitType.star); - new layout.ItemSpec(1, layout.GridUnitType.pixel); + new ItemSpec(1, GridUnitType.auto); + new ItemSpec(1, GridUnitType.star); + new ItemSpec(1, GridUnitType.pixel); } catch (ex) { TKUnit.assert(false, "ItemSpec type should support auto, star and pixel."); @@ -333,13 +309,13 @@ export class GridLayoutTest extends testModule.UITest { public test_ItemSpec_constructor_throws_onWrongType() { TKUnit.assertThrows(() => { - new layout.ItemSpec(1, "unsupported"); + new ItemSpec(1, "unsupported"); }, "'ItemSpec type' incorrect value."); } public test_ItemSpec_auto() { - var w = new layout.ItemSpec(1, layout.GridUnitType.auto); - TKUnit.assertEqual(w.gridUnitType, layout.GridUnitType.auto, "'gridUnitType' property default value should be 'auto'"); + var w = new ItemSpec(1, GridUnitType.auto); + TKUnit.assertEqual(w.gridUnitType, GridUnitType.auto, "'gridUnitType' property default value should be 'auto'"); TKUnit.assertEqual(w.isAbsolute, false, "'isAbsolute' property default value should be 'false'"); TKUnit.assertEqual(w.isAuto, true, "'isAuto' property default value should be 'false'"); TKUnit.assertEqual(w.isStar, false, "'isAuto' property default value should be 'true'"); @@ -347,8 +323,8 @@ export class GridLayoutTest extends testModule.UITest { } public test_ItemSpec_unitType_pixel() { - var w = new layout.ItemSpec(6, layout.GridUnitType.pixel); - TKUnit.assertEqual(w.gridUnitType, layout.GridUnitType.pixel, "'gridUnitType' property default value should be 'pixel'"); + var w = new ItemSpec(6, GridUnitType.pixel); + TKUnit.assertEqual(w.gridUnitType, GridUnitType.pixel, "'gridUnitType' property default value should be 'pixel'"); TKUnit.assertEqual(w.isAbsolute, true, "'isAbsolute' property default value should be 'false'"); TKUnit.assertEqual(w.isAuto, false, "'isAuto' property default value should be 'false'"); TKUnit.assertEqual(w.isStar, false, "'isAuto' property default value should be 'true'"); @@ -356,8 +332,8 @@ export class GridLayoutTest extends testModule.UITest { } public test_ItemSpec_unitType() { - var w = new layout.ItemSpec(2, layout.GridUnitType.star); - TKUnit.assertEqual(w.gridUnitType, layout.GridUnitType.star, "'gridUnitType' property default value should be 'star'"); + var w = new ItemSpec(2, GridUnitType.star); + TKUnit.assertEqual(w.gridUnitType, GridUnitType.star, "'gridUnitType' property default value should be 'star'"); TKUnit.assertEqual(w.isAbsolute, false, "'isAbsolute' property default value should be 'false'"); TKUnit.assertEqual(w.isAuto, false, "'isAuto' property default value should be 'false'"); TKUnit.assertEqual(w.isStar, true, "'isAuto' property default value should be 'true'"); @@ -384,9 +360,9 @@ export class GridLayoutTest extends testModule.UITest { width = 0; height = 0; for (var c = 0; c < 4; c++) { - var btn = this.testView.getChildAt(i++); + var btn = this.testView.getChildAt(i++); if (cols[c].isAbsolute) { - width += helper.dip(cols[c].actualLength); + width += layoutHelper.dip(cols[c].actualLength); } else { width += btn.getMeasuredWidth(); @@ -398,7 +374,7 @@ export class GridLayoutTest extends testModule.UITest { maxWidth = Math.max(maxWidth, width); if (rows[r].isAbsolute) { - maxHeight += helper.dip(rows[r].actualLength); + maxHeight += layoutHelper.dip(rows[r].actualLength); } else { maxHeight += height; @@ -442,7 +418,7 @@ export class GridLayoutTest extends testModule.UITest { for (var r = 0; r < 4; r++) { for (var c = 0; c < 4; c++) { - var btn = this.testView.getChildAt(i++); + var btn = this.testView.getChildAt(i++); var col = cols[c]; var row = rows[r]; @@ -479,10 +455,10 @@ export class GridLayoutTest extends testModule.UITest { public test_ColumnWidth_when_4stars_and_width_110() { this.testView.width = layoutHelper.dp(110); - this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); - this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); - this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); - this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); + this.testView.addColumn(new ItemSpec(1, GridUnitType.star)); + this.testView.addColumn(new ItemSpec(1, GridUnitType.star)); + this.testView.addColumn(new ItemSpec(1, GridUnitType.star)); + this.testView.addColumn(new ItemSpec(1, GridUnitType.star)); this.waitUntilTestElementLayoutIsValid(); @@ -498,7 +474,7 @@ export class GridLayoutTest extends testModule.UITest { this.testView.height = layoutHelper.dp(200); this.testView.width = layoutHelper.dp(200); - var btn = new helper.MyButton(); + var btn = new layoutHelper.MyButton(); btn.text = "btn"; btn.height = layoutHelper.dp(100); btn.width = layoutHelper.dp(100); @@ -513,19 +489,19 @@ export class GridLayoutTest extends testModule.UITest { } public test_set_columns_in_XML_comma_separator() { - var p = builder.parse("