Merge pull request #1396 from NativeScript/hhristov/layout-fixes

Fixed bug in GridLayout
This commit is contained in:
Hristo Hristov
2016-01-19 15:26:53 +02:00
8 changed files with 1142 additions and 772 deletions

View File

@ -1,8 +1,7 @@
import page = require("ui/page"); import {Page} from "ui/page";
import layout = require("ui/layouts/grid-layout"); import {GridLayout, ItemSpec, GridUnitType} from "ui/layouts/grid-layout";
import {Button} from "ui/button"; import {Button} from "ui/button";
import TKUnit = require("../TKUnit"); import TKUnit = require("../TKUnit");
import helper = require("./layout-helper");
import view = require("ui/core/view"); import view = require("ui/core/view");
import navHelper = require("../ui/helper"); import navHelper = require("../ui/helper");
import utils = require("utils/utils"); import utils = require("utils/utils");
@ -15,69 +14,46 @@ import commonTests = require("./common-layout-tests");
var DELTA = 1; var DELTA = 1;
export class MyGridLayout extends layout.GridLayout { export class GridLayoutTest extends testModule.UITest<GridLayout> {
public measureCount: number = 0;
public layoutCount: number = 0;
public get measured(): boolean { public create(): GridLayout {
return this.measureCount > 0; return new GridLayout();
}
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<layout.GridLayout> {
public create(): layout.GridLayout {
return new MyGridLayout();
} }
private row(view: view.View): number { private row(view: view.View): number {
return layout.GridLayout.getRow(view); return GridLayout.getRow(view);
} }
private rowSpan(view: view.View): number { private rowSpan(view: view.View): number {
return layout.GridLayout.getRowSpan(view); return GridLayout.getRowSpan(view);
} }
private col(view: view.View): number { private col(view: view.View): number {
return layout.GridLayout.getColumn(view); return GridLayout.getColumn(view);
} }
private colSpan(view: view.View): number { private colSpan(view: view.View): number {
return layout.GridLayout.getColumnSpan(view); return GridLayout.getColumnSpan(view);
} }
private prepareGridLayout(wait?: boolean) { private prepareGridLayout(wait?: boolean) {
this.testView.addRow(new layout.ItemSpec(1, layout.GridUnitType.star)); this.testView.addRow(new ItemSpec(1, GridUnitType.star));
this.testView.addRow(new layout.ItemSpec(2, layout.GridUnitType.star)); this.testView.addRow(new ItemSpec(2, GridUnitType.star));
this.testView.addRow(new layout.ItemSpec(layoutHelper.dp(50), layout.GridUnitType.pixel)); this.testView.addRow(new ItemSpec(layoutHelper.dp(50), GridUnitType.pixel));
this.testView.addRow(new layout.ItemSpec(50, layout.GridUnitType.auto)); this.testView.addRow(new ItemSpec(50, GridUnitType.auto));
this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); this.testView.addColumn(new ItemSpec(1, GridUnitType.star));
this.testView.addColumn(new layout.ItemSpec(2, layout.GridUnitType.star)); this.testView.addColumn(new ItemSpec(2, GridUnitType.star));
this.testView.addColumn(new layout.ItemSpec(layoutHelper.dp(50), layout.GridUnitType.pixel)); this.testView.addColumn(new ItemSpec(layoutHelper.dp(50), GridUnitType.pixel));
this.testView.addColumn(new layout.ItemSpec(50, layout.GridUnitType.auto)); this.testView.addColumn(new ItemSpec(50, GridUnitType.auto));
for (var r = 0; r < 4; r++) { for (var r = 0; r < 4; r++) {
for (var c = 0; c < 4; c++) { for (var c = 0; c < 4; c++) {
var btn = new helper.MyButton(); var btn = new layoutHelper.MyButton();
btn.text = "R" + r + "C" + c; btn.text = "R" + r + "C" + c;
layout.GridLayout.setColumn(btn, c); GridLayout.setColumn(btn, c);
layout.GridLayout.setRow(btn, r); GridLayout.setRow(btn, r);
if (c === 3) { if (c === 3) {
btn.width = layoutHelper.dp(100); // Auto column should take 100px for this test. btn.width = layoutHelper.dp(100); // Auto column should take 100px for this test.
} }
@ -124,73 +100,73 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
public test_getRow_shouldThrow_onNullValues() { public test_getRow_shouldThrow_onNullValues() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
layout.GridLayout.getRow(null); GridLayout.getRow(null);
}, "getRow called with null should throw exception"); }, "getRow called with null should throw exception");
} }
public test_getRowSpan_shouldThrow_onNullValues() { public test_getRowSpan_shouldThrow_onNullValues() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
layout.GridLayout.getRowSpan(null); GridLayout.getRowSpan(null);
}, "getRowSpan called with null should throw exception"); }, "getRowSpan called with null should throw exception");
} }
public test_getColumn_shouldThrow_onNullValues() { public test_getColumn_shouldThrow_onNullValues() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
layout.GridLayout.getColumn(null); GridLayout.getColumn(null);
}, "getColumn called with null should throw exception"); }, "getColumn called with null should throw exception");
} }
public test_getColumnSpan_shouldThrow_onNullValues() { public test_getColumnSpan_shouldThrow_onNullValues() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
layout.GridLayout.getColumnSpan(null); GridLayout.getColumnSpan(null);
}, "getColumnSpan called with null should throw exception"); }, "getColumnSpan called with null should throw exception");
} }
public test_setRow_shouldThrow_onNullValues() { public test_setRow_shouldThrow_onNullValues() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
layout.GridLayout.setRow(null, 1); GridLayout.setRow(null, 1);
}, "setRow called with null should throw exception"); }, "setRow called with null should throw exception");
} }
public test_setRowSpan_shouldThrow_onNullValues() { public test_setRowSpan_shouldThrow_onNullValues() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
layout.GridLayout.setRowSpan(null, 1); GridLayout.setRowSpan(null, 1);
}, "setRowSpan called with null should throw exception"); }, "setRowSpan called with null should throw exception");
} }
public test_setColumn_shouldThrow_onNullValues() { public test_setColumn_shouldThrow_onNullValues() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
layout.GridLayout.setColumn(null, 1); GridLayout.setColumn(null, 1);
}, "setColumn called with null should throw exception") }, "setColumn called with null should throw exception")
} }
public test_setColumnSpan_shouldThrow_onNullValues() { public test_setColumnSpan_shouldThrow_onNullValues() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
layout.GridLayout.setColumnSpan(null, 1); GridLayout.setColumnSpan(null, 1);
}, "setColumnSpan called with null should throw exception"); }, "setColumnSpan called with null should throw exception");
} }
public test_setRow_shouldThrow_onNegativeValues() { public test_setRow_shouldThrow_onNegativeValues() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
layout.GridLayout.setRow(new Button(), -1); GridLayout.setRow(new Button(), -1);
}, "setRow should throw when value < 0"); }, "setRow should throw when value < 0");
} }
public test_setRowSpan_shouldThrow_onNotPositiveValues() { public test_setRowSpan_shouldThrow_onNotPositiveValues() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
layout.GridLayout.setRowSpan(new Button(), 0); GridLayout.setRowSpan(new Button(), 0);
}, "setRowSpan should throw when value <= 0"); }, "setRowSpan should throw when value <= 0");
} }
public test_setColumn_shouldThrow_onNegativeValues() { public test_setColumn_shouldThrow_onNegativeValues() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
layout.GridLayout.setColumn(new Button(), -1); GridLayout.setColumn(new Button(), -1);
}, "setColumn should when value < 0"); }, "setColumn should when value < 0");
} }
public test_setColumnSpan_shouldThrow_onNotPositiveValues() { public test_setColumnSpan_shouldThrow_onNotPositiveValues() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
layout.GridLayout.setColumnSpan(new Button(), 0); GridLayout.setColumnSpan(new Button(), 0);
}, "setColumnSpan should throw when value <= 0"); }, "setColumnSpan should throw when value <= 0");
} }
@ -253,13 +229,13 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
public test_measuredWidth_when_not_stretched_two_columns() { public test_measuredWidth_when_not_stretched_two_columns() {
this.testView.horizontalAlignment = enums.HorizontalAlignment.center; this.testView.horizontalAlignment = enums.HorizontalAlignment.center;
this.testView.addColumn(new layout.ItemSpec(layoutHelper.dp(80), layout.GridUnitType.pixel)); this.testView.addColumn(new ItemSpec(layoutHelper.dp(80), GridUnitType.pixel));
this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); this.testView.addColumn(new ItemSpec(1, GridUnitType.star));
let btn = new Button(); let btn = new Button();
btn.text = "A"; btn.text = "A";
btn.width = layoutHelper.dp(100); btn.width = layoutHelper.dp(100);
MyGridLayout.setColumnSpan(btn, 2); GridLayout.setColumnSpan(btn, 2);
this.testView.addChild(btn); this.testView.addChild(btn);
this.waitUntilTestElementLayoutIsValid(); this.waitUntilTestElementLayoutIsValid();
@ -272,22 +248,22 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
public test_measuredWidth_when_not_stretched_three_columns() { public test_measuredWidth_when_not_stretched_three_columns() {
this.testView.horizontalAlignment = enums.HorizontalAlignment.center; this.testView.horizontalAlignment = enums.HorizontalAlignment.center;
this.testView.addColumn(new layout.ItemSpec(layoutHelper.dp(80), layout.GridUnitType.pixel)); this.testView.addColumn(new ItemSpec(layoutHelper.dp(80), GridUnitType.pixel));
this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); this.testView.addColumn(new ItemSpec(1, GridUnitType.star));
this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.auto)); this.testView.addColumn(new ItemSpec(1, GridUnitType.auto));
for (let i = 1; i < 4; i++) { for (let i = 1; i < 4; i++) {
let btn = new Button(); let btn = new Button();
btn.text = "A"; btn.text = "A";
btn.width = layoutHelper.dp(i * 20); btn.width = layoutHelper.dp(i * 20);
MyGridLayout.setColumn(btn, i - 1); GridLayout.setColumn(btn, i - 1);
this.testView.addChild(btn); this.testView.addChild(btn);
} }
let btn = new Button(); let btn = new Button();
btn.text = "B"; btn.text = "B";
btn.width = layoutHelper.dp(100); btn.width = layoutHelper.dp(100);
MyGridLayout.setColumnSpan(btn, 3); GridLayout.setColumnSpan(btn, 3);
this.testView.addChild(btn); this.testView.addChild(btn);
this.waitUntilTestElementLayoutIsValid(); this.waitUntilTestElementLayoutIsValid();
@ -310,21 +286,21 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
} }
public test_ItemSpec_actualLength_defaultValue() { 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."); TKUnit.assertEqual(def.actualLength, 0, "'actualLength' property default value should be 0.");
} }
public test_ItemSpec_constructor_throws_onNegativeValue() { public test_ItemSpec_constructor_throws_onNegativeValue() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
new layout.ItemSpec(-1, layout.GridUnitType.auto); new ItemSpec(-1, GridUnitType.auto);
}, "'value' should be positive number."); }, "'value' should be positive number.");
} }
public test_ItemSpec_constructor_doesnt_throw_onCorrectType() { public test_ItemSpec_constructor_doesnt_throw_onCorrectType() {
try { try {
new layout.ItemSpec(1, layout.GridUnitType.auto); new ItemSpec(1, GridUnitType.auto);
new layout.ItemSpec(1, layout.GridUnitType.star); new ItemSpec(1, GridUnitType.star);
new layout.ItemSpec(1, layout.GridUnitType.pixel); new ItemSpec(1, GridUnitType.pixel);
} }
catch (ex) { catch (ex) {
TKUnit.assert(false, "ItemSpec type should support auto, star and pixel."); TKUnit.assert(false, "ItemSpec type should support auto, star and pixel.");
@ -333,13 +309,13 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
public test_ItemSpec_constructor_throws_onWrongType() { public test_ItemSpec_constructor_throws_onWrongType() {
TKUnit.assertThrows(() => { TKUnit.assertThrows(() => {
new layout.ItemSpec(1, "unsupported"); new ItemSpec(1, "unsupported");
}, "'ItemSpec type' incorrect value."); }, "'ItemSpec type' incorrect value.");
} }
public test_ItemSpec_auto() { public test_ItemSpec_auto() {
var w = new layout.ItemSpec(1, layout.GridUnitType.auto); var w = new ItemSpec(1, GridUnitType.auto);
TKUnit.assertEqual(w.gridUnitType, layout.GridUnitType.auto, "'gridUnitType' property default value should be '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.isAbsolute, false, "'isAbsolute' property default value should be 'false'");
TKUnit.assertEqual(w.isAuto, true, "'isAuto' 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'"); TKUnit.assertEqual(w.isStar, false, "'isAuto' property default value should be 'true'");
@ -347,8 +323,8 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
} }
public test_ItemSpec_unitType_pixel() { public test_ItemSpec_unitType_pixel() {
var w = new layout.ItemSpec(6, layout.GridUnitType.pixel); var w = new ItemSpec(6, GridUnitType.pixel);
TKUnit.assertEqual(w.gridUnitType, layout.GridUnitType.pixel, "'gridUnitType' property default value should be '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.isAbsolute, true, "'isAbsolute' property default value should be 'false'");
TKUnit.assertEqual(w.isAuto, false, "'isAuto' 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'"); TKUnit.assertEqual(w.isStar, false, "'isAuto' property default value should be 'true'");
@ -356,8 +332,8 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
} }
public test_ItemSpec_unitType() { public test_ItemSpec_unitType() {
var w = new layout.ItemSpec(2, layout.GridUnitType.star); var w = new ItemSpec(2, GridUnitType.star);
TKUnit.assertEqual(w.gridUnitType, layout.GridUnitType.star, "'gridUnitType' property default value should be '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.isAbsolute, false, "'isAbsolute' property default value should be 'false'");
TKUnit.assertEqual(w.isAuto, false, "'isAuto' 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'"); TKUnit.assertEqual(w.isStar, true, "'isAuto' property default value should be 'true'");
@ -384,9 +360,9 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
width = 0; width = 0;
height = 0; height = 0;
for (var c = 0; c < 4; c++) { for (var c = 0; c < 4; c++) {
var btn = <helper.MyButton>this.testView.getChildAt(i++); var btn = <layoutHelper.MyButton>this.testView.getChildAt(i++);
if (cols[c].isAbsolute) { if (cols[c].isAbsolute) {
width += helper.dip(cols[c].actualLength); width += layoutHelper.dip(cols[c].actualLength);
} }
else { else {
width += btn.getMeasuredWidth(); width += btn.getMeasuredWidth();
@ -398,7 +374,7 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
maxWidth = Math.max(maxWidth, width); maxWidth = Math.max(maxWidth, width);
if (rows[r].isAbsolute) { if (rows[r].isAbsolute) {
maxHeight += helper.dip(rows[r].actualLength); maxHeight += layoutHelper.dip(rows[r].actualLength);
} }
else { else {
maxHeight += height; maxHeight += height;
@ -442,7 +418,7 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
for (var r = 0; r < 4; r++) { for (var r = 0; r < 4; r++) {
for (var c = 0; c < 4; c++) { for (var c = 0; c < 4; c++) {
var btn = <helper.MyButton>this.testView.getChildAt(i++); var btn = <layoutHelper.MyButton>this.testView.getChildAt(i++);
var col = cols[c]; var col = cols[c];
var row = rows[r]; var row = rows[r];
@ -479,10 +455,10 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
public test_ColumnWidth_when_4stars_and_width_110() { public test_ColumnWidth_when_4stars_and_width_110() {
this.testView.width = layoutHelper.dp(110); this.testView.width = layoutHelper.dp(110);
this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); this.testView.addColumn(new ItemSpec(1, GridUnitType.star));
this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); this.testView.addColumn(new ItemSpec(1, GridUnitType.star));
this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); this.testView.addColumn(new ItemSpec(1, GridUnitType.star));
this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); this.testView.addColumn(new ItemSpec(1, GridUnitType.star));
this.waitUntilTestElementLayoutIsValid(); this.waitUntilTestElementLayoutIsValid();
@ -498,7 +474,7 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
this.testView.height = layoutHelper.dp(200); this.testView.height = layoutHelper.dp(200);
this.testView.width = layoutHelper.dp(200); this.testView.width = layoutHelper.dp(200);
var btn = new helper.MyButton(); var btn = new layoutHelper.MyButton();
btn.text = "btn"; btn.text = "btn";
btn.height = layoutHelper.dp(100); btn.height = layoutHelper.dp(100);
btn.width = layoutHelper.dp(100); btn.width = layoutHelper.dp(100);
@ -513,19 +489,19 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
} }
public test_set_columns_in_XML_comma_separator() { public test_set_columns_in_XML_comma_separator() {
var p = <page.Page>builder.parse("<Page><GridLayout columns=\"auto, *, 10*, 100 \"><Button/></GridLayout></Page>"); var p = <Page>builder.parse("<Page><GridLayout columns=\"auto, *, 10*, 100 \"><Button/></GridLayout></Page>");
var grid = <layout.GridLayout>p.content; var grid = <GridLayout>p.content;
this.assertColumns(grid); this.assertColumns(grid);
} }
public test_set_columns_in_XML_space_separator() { public test_set_columns_in_XML_space_separator() {
var p = <page.Page>builder.parse("<Page><GridLayout columns=\"auto * 10* 100 \"><Button/></GridLayout></Page>"); var p = <Page>builder.parse("<Page><GridLayout columns=\"auto * 10* 100 \"><Button/></GridLayout></Page>");
var grid = <layout.GridLayout>p.content; var grid = <GridLayout>p.content;
this.assertColumns(grid); this.assertColumns(grid);
} }
private assertColumns(grid: layout.GridLayout) { private assertColumns(grid: GridLayout) {
var columns: Array<layout.ItemSpec> = grid.getColumns(); var columns: Array<ItemSpec> = grid.getColumns();
TKUnit.assertEqual(columns.length, 4, "columns.length"); TKUnit.assertEqual(columns.length, 4, "columns.length");
TKUnit.assert(columns[0].isAuto, "columns[0].isAuto"); TKUnit.assert(columns[0].isAuto, "columns[0].isAuto");
@ -541,19 +517,20 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
} }
public test_set_rows_in_XML_comma_separator() { public test_set_rows_in_XML_comma_separator() {
var p = <page.Page>builder.parse("<Page><GridLayout rows=\"auto, *, 10*, 100 \"><Button/></GridLayout></Page>"); var p = <Page>builder.parse("<Page><GridLayout rows=\"auto, *, 10*, 100 \"><Button/></GridLayout></Page>");
var grid = <layout.GridLayout>p.content; var grid = <GridLayout>p.content;
this.assertRows(grid);
}
public test_set_rows_in_XML_space_separator() {
var p = <page.Page>builder.parse("<Page><GridLayout rows=\"auto * 10* 100 \"><Button/></GridLayout></Page>");
var grid = <layout.GridLayout>p.content;
this.assertRows(grid); this.assertRows(grid);
} }
private assertRows(grid: layout.GridLayout) { public test_set_rows_in_XML_space_separator() {
var columns: Array<layout.ItemSpec> = grid.getColumns(); var p = <Page>builder.parse("<Page><GridLayout rows=\"auto * 10* 100 \"><Button/></GridLayout></Page>");
var rows: Array<layout.ItemSpec> = grid.getRows(); var grid = <GridLayout>p.content;
this.assertRows(grid);
}
private assertRows(grid: GridLayout) {
var columns: Array<ItemSpec> = grid.getColumns();
var rows: Array<ItemSpec> = grid.getRows();
TKUnit.assertEqual(rows.length, 4, "rows.length"); TKUnit.assertEqual(rows.length, 4, "rows.length");
TKUnit.assert(rows[0].isAuto, "rows[0].isAuto"); TKUnit.assert(rows[0].isAuto, "rows[0].isAuto");
@ -577,13 +554,13 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
this.testView.width = layoutHelper.dp(300); this.testView.width = layoutHelper.dp(300);
this.testView.height = layoutHelper.dp(300); this.testView.height = layoutHelper.dp(300);
var btn = new helper.MyButton(); var btn = new layoutHelper.MyButton();
this.testView.addChild(btn); this.testView.addChild(btn);
this.waitUntilTestElementLayoutIsValid(); this.waitUntilTestElementLayoutIsValid();
helper.assertMeasure(btn, 260, 240); layoutHelper.assertMeasure(btn, 260, 240);
helper.assertLayout(btn, 10, 20, 260, 240); layoutHelper.assertLayout(btn, 10, 20, 260, 240);
} }
public test_codesnippets = function () { public test_codesnippets = function () {
@ -592,7 +569,7 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
// ### Creating Grid Layout via code. // ### Creating Grid Layout via code.
// ``` JavaScript // ``` JavaScript
//var layout = require("ui/layouts/grid-layout"); //var layout = require("ui/layouts/grid-layout");
var gridLayout = new layout.GridLayout(); var gridLayout = new GridLayout();
// ``` // ```
// ### Create grid layout with an xml declaration // ### Create grid layout with an xml declaration
@ -620,34 +597,34 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
// ### Set column property on views - btn1 in first column, btn2 is second and btn3 in third // ### Set column property on views - btn1 in first column, btn2 is second and btn3 in third
// ``` JavaScript // ``` JavaScript
layout.GridLayout.setColumn(btn1, 0); GridLayout.setColumn(btn1, 0);
layout.GridLayout.setColumn(btn2, 1); GridLayout.setColumn(btn2, 1);
layout.GridLayout.setColumn(btn3, 2); GridLayout.setColumn(btn3, 2);
// ``` // ```
// ### Set row property on btn4. // ### Set row property on btn4.
// ``` JavaScript // ``` JavaScript
layout.GridLayout.setRow(btn4, 1); GridLayout.setRow(btn4, 1);
// ``` // ```
// ### Set columnSpan property on btn4 to stretch into all columns // ### Set columnSpan property on btn4 to stretch into all columns
// ``` JavaScript // ``` JavaScript
layout.GridLayout.setColumnSpan(btn4, 3); GridLayout.setColumnSpan(btn4, 3);
// ``` // ```
// ### Create ItemSpec for columns and rows 3 columns - 80px, *, auto size and 2 rows - 100px and auto size // ### Create ItemSpec for columns and rows 3 columns - 80px, *, auto size and 2 rows - 100px and auto size
// ``` JavaScript // ``` JavaScript
//// ItemSpec modes of the column refers to its width. //// ItemSpec modes of the column refers to its width.
//// Absolute size of the column //// Absolute size of the column
var firstColumn = new layout.ItemSpec(80, layout.GridUnitType.pixel); var firstColumn = new ItemSpec(80, GridUnitType.pixel);
//// Star width means that this column will expand to fill the gap left from other columns //// Star width means that this column will expand to fill the gap left from other columns
var secondColumn = new layout.ItemSpec(1, layout.GridUnitType.star); var secondColumn = new ItemSpec(1, GridUnitType.star);
//// Auto size means that column will expand or shrink in order to give enough place for all child UI elements. //// Auto size means that column will expand or shrink in order to give enough place for all child UI elements.
var thirdColumn = new layout.ItemSpec(1, layout.GridUnitType.auto); var thirdColumn = new ItemSpec(1, GridUnitType.auto);
//// Star and Auto modes for rows behave like corresponding setting for columns but refer to row height. //// Star and Auto modes for rows behave like corresponding setting for columns but refer to row height.
var firstRow = new layout.ItemSpec(1, layout.GridUnitType.auto); var firstRow = new ItemSpec(1, GridUnitType.auto);
var secondRow = new layout.ItemSpec(1, layout.GridUnitType.star); var secondRow = new ItemSpec(1, GridUnitType.star);
// ``` // ```
// ### Add columns and rows to GridLayout // ### Add columns and rows to GridLayout
@ -668,6 +645,34 @@ export class GridLayoutTest extends testModule.UITest<layout.GridLayout> {
public test_percent_support_nativeLayoutParams_are_correct() { public test_percent_support_nativeLayoutParams_are_correct() {
commonTests.percent_support_nativeLayoutParams_are_correct(this); commonTests.percent_support_nativeLayoutParams_are_correct(this);
} }
public test_layout_correctnes() {
this.testView.width = layoutHelper.dp(300);
this.testView.height = layoutHelper.dp(300);
let grid = new layoutHelper.MyGridLayout();
grid.width = layoutHelper.dp(150);
grid.height = layoutHelper.dp(150);
grid.horizontalAlignment = enums.HorizontalAlignment.right;
grid.verticalAlignment = enums.VerticalAlignment.bottom;
this.testView.addChild(grid);
let btn = new layoutHelper.MyButton();
btn.width = layoutHelper.dp(75);
btn.height = layoutHelper.dp(75);
btn.horizontalAlignment = enums.HorizontalAlignment.left;
btn.verticalAlignment = enums.VerticalAlignment.bottom;
grid.addChild(btn);
this.waitUntilTestElementLayoutIsValid();
layoutHelper.assertMeasure(grid, 150, 150);
layoutHelper.assertLayout(grid, 150, 150, 150, 150);
layoutHelper.assertMeasure(btn, 75, 75);
layoutHelper.assertLayout(btn, 0, 75, 75, 75);
}
} }
export function createTestCase(): GridLayoutTest { export function createTestCase(): GridLayoutTest {

View File

@ -1,16 +1,18 @@
import button = require("ui/button"); import {View} from "ui/core/view";
import {Button} from "ui/button";
import {StackLayout} from "ui/layouts/stack-layout"; import {StackLayout} from "ui/layouts/stack-layout";
import {GridLayout} from "ui/layouts/grid-layout";
import utils = require("utils/utils"); import utils = require("utils/utils");
import TKUnit = require("../TKUnit"); import TKUnit = require("../TKUnit");
import def = require("./layout-helper"); import def = require("./layout-helper");
var DELTA = 0.1; var DELTA = 0.1;
export class NativeButton extends android.widget.Button { class NativeButton extends android.widget.Button {
private owner: def.MeasuredView;
private owner: MyButton; constructor(context: android.content.Context, owner: def.MeasuredView) {
constructor(context: android.content.Context, owner: MyButton) {
super(context); super(context);
this.owner = owner; this.owner = owner;
return global.__native(this); return global.__native(this);
@ -18,33 +20,69 @@ export class NativeButton extends android.widget.Button {
protected onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void { protected onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.owner._oldWidthMeasureSpec = widthMeasureSpec; this.owner.widthMeasureSpec = widthMeasureSpec;
this.owner._oldHeightMeasureSpec = heightMeasureSpec; this.owner.heightMeasureSpec = heightMeasureSpec;
this.owner._measureWidth = utils.layout.getMeasureSpecSize(widthMeasureSpec);
this.owner._measureHeight = utils.layout.getMeasureSpecSize(heightMeasureSpec);
this.owner.measureCount++; this.owner.measureCount++;
} }
protected onLayout(changed: boolean, left: number, top: number, right: number, bottom: number): void { protected onLayout(changed: boolean, left: number, top: number, right: number, bottom: number): void {
this.owner._layoutLeft = left;
this.owner._layoutTop = top;
this.owner._layoutWidth = right - left;
this.owner._layoutHeight = bottom - top;
super.onLayout(changed, left, top, right, bottom); super.onLayout(changed, left, top, right, bottom);
this.owner.arrangeCount++; this.owner.arrangeCount++;
} }
} }
export class MyButton extends button.Button implements def.MyButton { class NativeStackLayout extends org.nativescript.widgets.StackLayout {
private _layout: NativeButton; private owner: def.MeasuredView;
get android(): NativeButton { constructor(context: android.content.Context, owner: def.MeasuredView) {
super(context);
this.owner = owner;
return global.__native(this);
}
protected onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.owner.widthMeasureSpec = widthMeasureSpec;
this.owner.heightMeasureSpec = heightMeasureSpec;
this.owner.measureCount++;
}
protected onLayout(changed: boolean, left: number, top: number, right: number, bottom: number): void {
super.onLayout(changed, left, top, right, bottom);
this.owner.arrangeCount++;
}
}
class NativeGridLayout extends org.nativescript.widgets.GridLayout {
private owner: def.MeasuredView;
constructor(context: android.content.Context, owner: def.MeasuredView) {
super(context);
this.owner = owner;
return global.__native(this);
}
protected onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.owner.widthMeasureSpec = widthMeasureSpec;
this.owner.heightMeasureSpec = heightMeasureSpec;
this.owner.measureCount++;
}
protected onLayout(changed: boolean, left: number, top: number, right: number, bottom: number): void {
super.onLayout(changed, left, top, right, bottom);
this.owner.arrangeCount++;
}
}
export class MyButton extends Button implements def.MyButton {
private _layout: android.view.View;
get android(): android.view.View {
return this._layout; return this._layout;
} }
get _nativeView(): NativeButton { get _nativeView(): android.view.View {
return this._layout; return this._layout;
} }
@ -55,16 +93,15 @@ export class MyButton extends button.Button implements def.MyButton {
public measureCount: number = 0; public measureCount: number = 0;
public arrangeCount: number = 0; public arrangeCount: number = 0;
_oldWidthMeasureSpec: number = Number.NaN; public widthMeasureSpec: number = Number.NaN;
_oldHeightMeasureSpec: number = Number.NaN; public heightMeasureSpec: number = Number.NaN;
_layoutLeft; public get measureWidth() {
_layoutTop; return utils.layout.getMeasureSpecSize(this.widthMeasureSpec);
_layoutWidth; }
_layoutHeight; public get measureHeight() {
return utils.layout.getMeasureSpecSize(this.heightMeasureSpec);
_measureWidth; }
_measureHeight;
public get measured(): boolean { public get measured(): boolean {
return this.measureCount > 0; return this.measureCount > 0;
@ -74,67 +111,31 @@ export class MyButton extends button.Button implements def.MyButton {
return this.arrangeCount > 0; return this.arrangeCount > 0;
} }
get measureHeight(): number {
return this._measureHeight;
}
get measureWidth(): number {
return this._measureWidth;
}
get layoutWidth(): number { get layoutWidth(): number {
return this._layoutWidth; return this._layout.getWidth();
} }
get layoutHeight(): number { get layoutHeight(): number {
return this._layoutHeight; return this._layout.getHeight();
} }
get layoutLeft(): number { get layoutLeft(): number {
return this._layoutLeft; return this._layout.getLeft();
} }
get layoutTop(): number { get layoutTop(): number {
return this._layoutTop; return this._layout.getTop();
}
_getCurrentMeasureSpecs(): { widthMeasureSpec: number; heightMeasureSpec: number } {
return {
widthMeasureSpec: this._oldWidthMeasureSpec,
heightMeasureSpec: this._oldHeightMeasureSpec
};
}
}
export class NativeStackLayout extends org.nativescript.widgets.StackLayout {
private owner: MyStackLayout;
constructor(context: android.content.Context, owner: MyStackLayout) {
super(context);
this.owner = owner;
return global.__native(this);
}
protected onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.owner.measureCount++;
}
protected onLayout(changed: boolean, left: number, top: number, right: number, bottom: number): void {
super.onLayout(changed, left, top, right, bottom);
this.owner.arrangeCount++;
} }
} }
export class MyStackLayout extends StackLayout implements def.MyStackLayout { export class MyStackLayout extends StackLayout implements def.MyStackLayout {
private _layout: NativeStackLayout; private _layout: android.view.View;
get android(): NativeStackLayout { get android(): android.view.View {
return this._layout; return this._layout;
} }
get _nativeView(): NativeStackLayout { get _nativeView(): android.view.View {
return this._layout; return this._layout;
} }
@ -145,6 +146,16 @@ export class MyStackLayout extends StackLayout implements def.MyStackLayout {
public measureCount: number = 0; public measureCount: number = 0;
public arrangeCount: number = 0; public arrangeCount: number = 0;
public widthMeasureSpec: number = Number.NaN;
public heightMeasureSpec: number = Number.NaN;
public get measureWidth() {
return utils.layout.getMeasureSpecSize(this.widthMeasureSpec);
}
public get measureHeight() {
return utils.layout.getMeasureSpecSize(this.heightMeasureSpec);
}
public get measured(): boolean { public get measured(): boolean {
return this.measureCount > 0; return this.measureCount > 0;
} }
@ -152,22 +163,91 @@ export class MyStackLayout extends StackLayout implements def.MyStackLayout {
public get arranged(): boolean { public get arranged(): boolean {
return this.arrangeCount > 0; return this.arrangeCount > 0;
} }
get layoutWidth(): number {
return this._layout.getWidth();
}
get layoutHeight(): number {
return this._layout.getHeight();
}
get layoutLeft(): number {
return this._layout.getLeft();
}
get layoutTop(): number {
return this._layout.getTop();
}
} }
export function assertMeasure(btn: MyButton, width: number, height: number, name?: string) { export class MyGridLayout extends GridLayout implements def.MyGridLayout {
name = name ? "[" + name + "]" : ""; private _layout: android.view.View;
TKUnit.assertAreClose(btn.measureWidth, width, DELTA, name + "width"); get android(): android.view.View {
TKUnit.assertAreClose(btn.measureHeight, height, DELTA, name + "height"); return this._layout;
}
get _nativeView(): android.view.View {
return this._layout;
}
public _createUI() {
this._layout = new NativeGridLayout(this._context, this);
}
public measureCount: number = 0;
public arrangeCount: number = 0;
public widthMeasureSpec: number = Number.NaN;
public heightMeasureSpec: number = Number.NaN;
public get measureWidth() {
return utils.layout.getMeasureSpecSize(this.widthMeasureSpec);
}
public get measureHeight() {
return utils.layout.getMeasureSpecSize(this.heightMeasureSpec);
}
public get measured(): boolean {
return this.measureCount > 0;
}
public get arranged(): boolean {
return this.arrangeCount > 0;
}
get layoutWidth(): number {
return this._layout.getWidth();
}
get layoutHeight(): number {
return this._layout.getHeight();
}
get layoutLeft(): number {
return this._layout.getLeft();
}
get layoutTop(): number {
return this._layout.getTop();
}
} }
export function assertLayout(btn: MyButton, left: number, top: number, width: number, height: number, name?: string): void { export function assertMeasure(view: def.MeasuredView, width: number, height: number, name?: string) {
name = name ? "[" + name + "]" : ""; name = name ? "[" + name + "]" : "";
TKUnit.assertAreClose(btn.layoutLeft, left, DELTA, name + "left"); TKUnit.assertAreClose(view.measureWidth, width, DELTA, name + "width");
TKUnit.assertAreClose(btn.layoutTop, top, DELTA, name + "top"); TKUnit.assertAreClose(view.measureHeight, height, DELTA, name + "height");
TKUnit.assertAreClose(btn.layoutWidth, width, DELTA, name + "width"); }
TKUnit.assertAreClose(btn.layoutHeight, height, DELTA, name + "height");
export function assertLayout(view: def.MeasuredView, left: number, top: number, width: number, height: number, name?: string): void {
name = name ? "[" + name + "]" : "";
TKUnit.assertAreClose(view.layoutLeft, left, DELTA, name + "left");
TKUnit.assertAreClose(view.layoutTop, top, DELTA, name + "top");
TKUnit.assertAreClose(view.layoutWidth, width, DELTA, name + "width");
TKUnit.assertAreClose(view.layoutHeight, height, DELTA, name + "height");
} }
export function dp(value: number): number { export function dp(value: number): number {

View File

@ -1,32 +1,85 @@
import button = require("ui/button"); import {View} from "ui/core/view";
import {Button} from "ui/button";
import {StackLayout} from "ui/layouts/stack-layout"; import {StackLayout} from "ui/layouts/stack-layout";
import {GridLayout} from "ui/layouts/grid-layout";
export class MyButton extends button.Button { export interface MeasuredView {
public measureCount: number; measureCount: number;
public arrangeCount: number; arrangeCount: number;
measured: boolean; measured: boolean;
arranged: boolean; arranged: boolean;
measureHeight: number; measureHeight: number;
measureWidth: number; measureWidth: number;
widthMeasureSpec: number;
heightMeasureSpec: number;
layoutWidth: number; layoutWidth: number;
layoutHeight: number; layoutHeight: number;
layoutLeft: number; layoutLeft: number;
layoutTop: number; layoutTop: number;
_getCurrentMeasureSpecs(): { widthMeasureSpec: number; heightMeasureSpec: number };
} }
export class MyStackLayout extends StackLayout { export class MyButton extends Button implements MeasuredView {
public measureCount: number; measureCount: number;
public arrangeCount: number; arrangeCount: number;
public measured: boolean; measured: boolean;
public arranged: boolean; arranged: boolean;
measureHeight: number;
measureWidth: number;
widthMeasureSpec: number;
heightMeasureSpec: number;
layoutWidth: number;
layoutHeight: number;
layoutLeft: number;
layoutTop: number;
} }
export function assertMeasure(btn: MyButton, width: number, height: number, name?: string); export class MyStackLayout extends StackLayout implements MeasuredView {
export function assertLayout(btn: MyButton, left: number, top: number, width: number, height: number, name?: string): void; measureCount: number;
arrangeCount: number;
measured: boolean;
arranged: boolean;
measureHeight: number;
measureWidth: number;
widthMeasureSpec: number;
heightMeasureSpec: number;
layoutWidth: number;
layoutHeight: number;
layoutLeft: number;
layoutTop: number;
}
export class MyGridLayout extends GridLayout implements MeasuredView {
measureCount: number;
arrangeCount: number;
measured: boolean;
arranged: boolean;
measureHeight: number;
measureWidth: number;
widthMeasureSpec: number;
heightMeasureSpec: number;
layoutWidth: number;
layoutHeight: number;
layoutLeft: number;
layoutTop: number;
}
export function assertMeasure(view: MeasuredView, width: number, height: number, name?: string);
export function assertLayout(view: MeasuredView, left: number, top: number, width: number, height: number, name?: string): void;
export function dip(value: number): number; export function dip(value: number): number;
export function dp(value: number): number; export function dp(value: number): number;

View File

@ -1,14 +1,31 @@
import button = require("ui/button"); import {Button} from "ui/button";
import {StackLayout} from "ui/layouts/stack-layout";
import {GridLayout} from "ui/layouts/grid-layout";
import utils = require("utils/utils"); import utils = require("utils/utils");
import TKUnit = require("../TKUnit"); import TKUnit = require("../TKUnit");
import {StackLayout} from "ui/layouts/stack-layout"; import def = require("./layout-helper");
var DELTA = 0.1; var DELTA = 0.1;
export class MyStackLayout extends StackLayout { export class MyGridLayout extends GridLayout implements def.MyGridLayout {
public measureCount: number = 0; public measureCount: number = 0;
public arrangeCount: number = 0; public arrangeCount: number = 0;
public widthMeasureSpec: number = Number.NaN;
public heightMeasureSpec: number = Number.NaN;
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.widthMeasureSpec = widthMeasureSpec;
this.heightMeasureSpec = heightMeasureSpec;
this.measureCount++;
}
public onLayout(left: number, top: number, right: number, bottom: number): void {
super.onLayout(left, top, right, bottom);
this.arrangeCount++;
}
public get measured(): boolean { public get measured(): boolean {
return this.measureCount > 0; return this.measureCount > 0;
} }
@ -17,106 +34,161 @@ export class MyStackLayout extends StackLayout {
return this.arrangeCount > 0; return this.arrangeCount > 0;
} }
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void { public get measureWidth() {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); return utils.layout.getMeasureSpecSize(this.widthMeasureSpec);
this.measureCount++;
} }
public onLayout(left: number, top: number, right: number, bottom: number): void { public get measureHeight() {
super.onLayout(left, top, right, bottom); return utils.layout.getMeasureSpecSize(this.heightMeasureSpec);
this.arrangeCount++;
}
}
export class MyButton extends button.Button {
public measureCount: number = 0;
public arrangeCount: number = 0;
private _layoutLeft;
private _layoutTop;
private _layoutWidth;
private _layoutHeight;
private _measureWidth;
private _measureHeight;
public get measured(): boolean {
return this.measureCount > 0;
}
public get arranged(): boolean {
return this.arrangeCount > 0;
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this._measureWidth = utils.layout.getMeasureSpecSize(widthMeasureSpec);
this._measureHeight = utils.layout.getMeasureSpecSize(heightMeasureSpec);
this.measureCount++;
}
public onLayout(left: number, top: number, right: number, bottom: number): void {
this._layoutLeft = left;
this._layoutTop = top;
this._layoutWidth = right - left;
this._layoutHeight = bottom - top;
super.onLayout(left, top, right, bottom);
this.arrangeCount++;
}
get measureHeight(): number {
return this._measureHeight;
}
get measureWidth(): number {
return this._measureWidth;
} }
get layoutWidth(): number { get layoutWidth(): number {
return this._layoutWidth; let bounds = this._getCurrentLayoutBounds();
return bounds.right - bounds.left;
} }
get layoutHeight(): number { get layoutHeight(): number {
return this._layoutHeight; let bounds = this._getCurrentLayoutBounds();
return bounds.bottom - bounds.top;
} }
get layoutLeft(): number { get layoutLeft(): number {
return this._layoutLeft; let bounds = this._getCurrentLayoutBounds();
return bounds.left;
} }
get layoutTop(): number { get layoutTop(): number {
return this._layoutTop; let bounds = this._getCurrentLayoutBounds();
} return bounds.top;
_getCurrentMeasureSpecs(): { widthMeasureSpec: number; heightMeasureSpec: number } {
return {
widthMeasureSpec: (<any>this)._oldWidthMeasureSpec,
heightMeasureSpec: (<any>this)._oldHeightMeasureSpec
};
} }
} }
export function assertMeasure(btn: MyButton, width: number, height: number, name?: string) { export class MyStackLayout extends StackLayout implements def.MyStackLayout {
var density = utils.layout.getDisplayDensity(); public measureCount: number = 0;
public arrangeCount: number = 0;
var delta = Math.floor(density) !== density ? 1.1 : DELTA; public widthMeasureSpec: number = Number.NaN;
name = name ? "[" + name + "]" : ""; public heightMeasureSpec: number = Number.NaN;
TKUnit.assertAreClose(Math.floor(btn.measureWidth / density), width, delta, name + "width"); public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
TKUnit.assertAreClose(Math.floor(btn.measureHeight / density), height, delta, name + "height"); super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.widthMeasureSpec = widthMeasureSpec;
this.heightMeasureSpec = heightMeasureSpec;
this.measureCount++;
}
public onLayout(left: number, top: number, right: number, bottom: number): void {
super.onLayout(left, top, right, bottom);
this.arrangeCount++;
}
public get measured(): boolean {
return this.measureCount > 0;
}
public get arranged(): boolean {
return this.arrangeCount > 0;
}
public get measureWidth() {
return utils.layout.getMeasureSpecSize(this.widthMeasureSpec);
}
public get measureHeight() {
return utils.layout.getMeasureSpecSize(this.heightMeasureSpec);
}
get layoutWidth(): number {
let bounds = this._getCurrentLayoutBounds();
return bounds.right - bounds.left;
}
get layoutHeight(): number {
let bounds = this._getCurrentLayoutBounds();
return bounds.bottom - bounds.top;
}
get layoutLeft(): number {
let bounds = this._getCurrentLayoutBounds();
return bounds.left;
}
get layoutTop(): number {
let bounds = this._getCurrentLayoutBounds();
return bounds.top;
}
} }
export function assertLayout(btn: MyButton, left: number, top: number, width: number, height: number, name?: string): void { export class MyButton extends Button implements def.MyButton {
var density = utils.layout.getDisplayDensity(); public measureCount: number = 0;
public arrangeCount: number = 0;
var delta = Math.floor(density) !== density ? 1.1 : DELTA; public widthMeasureSpec: number = Number.NaN;
public heightMeasureSpec: number = Number.NaN;
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.widthMeasureSpec = widthMeasureSpec;
this.heightMeasureSpec = heightMeasureSpec;
this.measureCount++;
}
public onLayout(left: number, top: number, right: number, bottom: number): void {
super.onLayout(left, top, right, bottom);
this.arrangeCount++;
}
public get measured(): boolean {
return this.measureCount > 0;
}
public get arranged(): boolean {
return this.arrangeCount > 0;
}
public get measureWidth() {
return utils.layout.getMeasureSpecSize(this.widthMeasureSpec);
}
public get measureHeight() {
return utils.layout.getMeasureSpecSize(this.heightMeasureSpec);
}
get layoutWidth(): number {
let bounds = this._getCurrentLayoutBounds();
return bounds.right - bounds.left;
}
get layoutHeight(): number {
let bounds = this._getCurrentLayoutBounds();
return bounds.bottom - bounds.top;
}
get layoutLeft(): number {
let bounds = this._getCurrentLayoutBounds();
return bounds.left;
}
get layoutTop(): number {
let bounds = this._getCurrentLayoutBounds();
return bounds.top;
}
}
export function assertMeasure(view: def.MeasuredView, width: number, height: number, name?: string) {
name = name ? "[" + name + "]" : ""; name = name ? "[" + name + "]" : "";
TKUnit.assertAreClose(Math.floor(btn.layoutLeft / density), left, delta, name + "left"); TKUnit.assertAreClose(view.measureWidth, width, DELTA, name + "width");
TKUnit.assertAreClose(Math.floor(btn.layoutTop / density), top, delta, name + "top"); TKUnit.assertAreClose(view.measureHeight, height, DELTA, name + "height");
TKUnit.assertAreClose(Math.floor(btn.layoutWidth / density), width, delta, name + "width"); }
TKUnit.assertAreClose(Math.floor(btn.layoutHeight / density), height, delta, name + "height");
export function assertLayout(view: def.MeasuredView, left: number, top: number, width: number, height: number, name?: string): void {
name = name ? "[" + name + "]" : "";
TKUnit.assertAreClose(view.layoutLeft, left, DELTA, name + "left");
TKUnit.assertAreClose(view.layoutTop, top, DELTA, name + "top");
TKUnit.assertAreClose(view.layoutWidth, width, DELTA, name + "width");
TKUnit.assertAreClose(view.layoutHeight, height, DELTA, name + "height");
} }
export function dp(value: number): number { export function dp(value: number): number {

View File

@ -56,9 +56,7 @@ export class StackLayoutTest extends testModule.UITest<StackLayout> {
TKUnit.assertTrue(this.rootLayout.measured, "Layout should be measured."); TKUnit.assertTrue(this.rootLayout.measured, "Layout should be measured.");
TKUnit.assertTrue(this.rootLayout.arranged, "Layout should be arranged."); TKUnit.assertTrue(this.rootLayout.arranged, "Layout should be arranged.");
var specs = this.btn1._getCurrentMeasureSpecs(); TKUnit.assertEqual(utils.layout.getMeasureSpecMode(this.btn1.heightMeasureSpec), utils.layout.AT_MOST, "Layout should measure child with AT_MOST Height in vertical orientation.");
TKUnit.assertEqual(utils.layout.getMeasureSpecMode(specs.heightMeasureSpec), utils.layout.AT_MOST, "Layout should measure child with AT_MOST Height in vertical orientation.");
} }
public test_ShouldMeasureWith_AtMost_OnHorizontal() { public test_ShouldMeasureWith_AtMost_OnHorizontal() {
@ -70,9 +68,7 @@ export class StackLayoutTest extends testModule.UITest<StackLayout> {
TKUnit.assert(this.rootLayout.measured, "Layout should be measured."); TKUnit.assert(this.rootLayout.measured, "Layout should be measured.");
TKUnit.assert(this.rootLayout.arranged, "Layout should be arranged."); TKUnit.assert(this.rootLayout.arranged, "Layout should be arranged.");
var specs = this.btn1._getCurrentMeasureSpecs(); TKUnit.assertEqual(utils.layout.getMeasureSpecMode(this.btn1.widthMeasureSpec), utils.layout.AT_MOST, "Layout should measure child with AT_MOST Width in horizontal orientation.");
TKUnit.assertEqual(utils.layout.getMeasureSpecMode(specs.widthMeasureSpec), utils.layout.AT_MOST, "Layout should measure child with AT_MOST Width in horizontal orientation.");
} }
public test_DesiredSize_Vertical() { public test_DesiredSize_Vertical() {

View File

@ -50,7 +50,8 @@ export function test_loadWithOptionsNoXML() {
var v = builder.load({ var v = builder.load({
path: "~/xml-declaration/mymodule", path: "~/xml-declaration/mymodule",
name: "MyControl", name: "MyControl",
exports: exports exports: exports,
page: new Page()
}); });
TKUnit.assert(v instanceof view.View, "Expected result: View; Actual result: " + v + ";"); TKUnit.assert(v instanceof view.View, "Expected result: View; Actual result: " + v + ";");
@ -102,7 +103,8 @@ export function test_loadWithOptionsWithXML() {
var v = builder.load({ var v = builder.load({
path: "~/xml-declaration/mymodulewithxml", path: "~/xml-declaration/mymodulewithxml",
name: "MyControl", name: "MyControl",
exports: exports exports: exports,
page: new Page()
}); });
TKUnit.assert(v instanceof view.View, "Expected result: View; Actual result: " + v + ";"); TKUnit.assert(v instanceof view.View, "Expected result: View; Actual result: " + v + ";");
}; };

View File

@ -107,8 +107,6 @@ export class ItemSpec extends bindable.Bindable implements definition.ItemSpec {
export class GridLayout extends layouts.LayoutBase implements definition.GridLayout, view.ApplyXmlAttributes { export class GridLayout extends layouts.LayoutBase implements definition.GridLayout, view.ApplyXmlAttributes {
private _rows: Array<ItemSpec> = new Array<ItemSpec>(); private _rows: Array<ItemSpec> = new Array<ItemSpec>();
private _cols: Array<ItemSpec> = new Array<ItemSpec>(); private _cols: Array<ItemSpec> = new Array<ItemSpec>();
protected _singleRow: ItemSpec = new ItemSpec();
protected _singleColumn: ItemSpec = new ItemSpec();
public static columnProperty = new dependencyObservable.Property("Column", "GridLayout", public static columnProperty = new dependencyObservable.Property("Column", "GridLayout",
new proxy.PropertyMetadata(0, dependencyObservable.PropertyMetadataSettings.None, GridLayout.onColumnPropertyChanged, numberUtils.notNegative)); new proxy.PropertyMetadata(0, dependencyObservable.PropertyMetadataSettings.None, GridLayout.onColumnPropertyChanged, numberUtils.notNegative));
@ -154,12 +152,6 @@ export class GridLayout extends layouts.LayoutBase implements definition.GridLay
validateArgs(element)._setValue(GridLayout.rowSpanProperty, value); validateArgs(element)._setValue(GridLayout.rowSpanProperty, value);
} }
constructor() {
super();
this._singleRow.index = 0
this._singleColumn.index = 0;
}
public addRow(itemSpec: ItemSpec) { public addRow(itemSpec: ItemSpec) {
GridLayout.validateItemSpec(itemSpec); GridLayout.validateItemSpec(itemSpec);
itemSpec.owner = this; itemSpec.owner = this;
@ -260,38 +252,12 @@ export class GridLayout extends layouts.LayoutBase implements definition.GridLay
return this._rows.slice(); return this._rows.slice();
} }
protected getColumn(view: view.View): ItemSpec { protected get columnsInternal(): Array<ItemSpec> {
if (this._cols.length === 0) { return this._cols;
return this._singleColumn;
}
var columnIndex = Math.min(GridLayout.getColumn(view), this._cols.length - 1);
return this._cols[columnIndex];
} }
protected getRow(view: view.View): ItemSpec { protected get rowsInternal(): Array<ItemSpec> {
if (this._rows.length === 0) { return this._rows;
return this._singleRow;
}
var columnIndex = Math.min(GridLayout.getRow(view), this._rows.length - 1);
return this._rows[columnIndex];
}
protected getColumnSpan(view: view.View, columnIndex: number): number {
if (this._cols.length === 0) {
return 1;
}
return Math.min(GridLayout.getColumnSpan(view), this._cols.length - columnIndex);
}
protected getRowSpan(view: view.View, rowIndex: number): number {
if (this._rows.length === 0) {
return 1;
}
return Math.min(GridLayout.getRowSpan(view), this._rows.length - rowIndex);
} }
protected invalidate(): void { protected invalidate(): void {

File diff suppressed because it is too large Load Diff