From c2cb4a8f612df75e570331456c4c49e3477f4946 Mon Sep 17 00:00:00 2001 From: hshristov Date: Tue, 10 Nov 2015 17:17:42 +0200 Subject: [PATCH 1/2] Stabilizing layout tests --- apps/tests/layouts/absolute-layout-tests.ts | 38 +++--- apps/tests/layouts/dock-layout-tests.ts | 29 ++--- apps/tests/layouts/grid-layout-tests.ts | 125 +++++++++----------- apps/tests/layouts/layout-helper.android.ts | 27 ++--- apps/tests/layouts/layout-helper.d.ts | 3 +- apps/tests/layouts/layout-helper.ios.ts | 7 +- apps/tests/layouts/stack-layout-tests.ts | 39 +++--- apps/tests/layouts/wrap-layout-tests.ts | 86 +++++++------- utils/utils.android.ts | 22 ++-- utils/utils.d.ts | 10 ++ utils/utils.ios.ts | 23 ++-- 11 files changed, 209 insertions(+), 200 deletions(-) diff --git a/apps/tests/layouts/absolute-layout-tests.ts b/apps/tests/layouts/absolute-layout-tests.ts index 44d7eeacc..8c28fb3f6 100644 --- a/apps/tests/layouts/absolute-layout-tests.ts +++ b/apps/tests/layouts/absolute-layout-tests.ts @@ -56,15 +56,15 @@ export class AbsoluteLayoutTest extends testModule.UITest // # DockLayout @@ -37,8 +38,8 @@ export class DockLayoutTest extends testModule.UITest { public create(): DockLayout { let rootLayout = new DockLayout(); - rootLayout.height = 300; - rootLayout.width = 300; + rootLayout.height = layoutHelper.dp(300); + rootLayout.width = layoutHelper.dp(300); return rootLayout; } @@ -62,7 +63,7 @@ export class DockLayoutTest extends testModule.UITest { public test_dock_left() { var testBtn = new helper.MyButton(); - testBtn.width = 20; + testBtn.width = layoutHelper.dp(20); this.testView.stretchLastChild = false; this.testView.addChild(testBtn); @@ -73,7 +74,7 @@ export class DockLayoutTest extends testModule.UITest { public test_dock_right() { var testBtn = new helper.MyButton(); - testBtn.width = 20; + testBtn.width = layoutHelper.dp(20); dockModule.DockLayout.setDock(testBtn, enums.Dock.right); this.testView.stretchLastChild = false; this.testView.addChild(testBtn); @@ -85,7 +86,7 @@ export class DockLayoutTest extends testModule.UITest { public test_dock_top() { var testBtn = new helper.MyButton(); - testBtn.height = 20; + testBtn.height = layoutHelper.dp(20); dockModule.DockLayout.setDock(testBtn, enums.Dock.top); this.testView.stretchLastChild = false; this.testView.addChild(testBtn); @@ -97,7 +98,7 @@ export class DockLayoutTest extends testModule.UITest { public test_dock_button() { var testBtn = new helper.MyButton(); - testBtn.height = 20; + testBtn.height = layoutHelper.dp(20); dockModule.DockLayout.setDock(testBtn, enums.Dock.bottom); this.testView.stretchLastChild = false; this.testView.addChild(testBtn); @@ -118,21 +119,21 @@ export class DockLayoutTest extends testModule.UITest { public test_dock_left_top_righ_bottom_fill() { var testBtnLeft = new helper.MyButton(); - testBtnLeft.width = 20; + testBtnLeft.width = layoutHelper.dp(20); this.testView.addChild(testBtnLeft); var testBtnTop = new helper.MyButton(); - testBtnTop.height = 20; + testBtnTop.height = layoutHelper.dp(20); dockModule.DockLayout.setDock(testBtnTop, enums.Dock.top); this.testView.addChild(testBtnTop); var testBtnRight = new helper.MyButton(); - testBtnRight.width = 20; + testBtnRight.width = layoutHelper.dp(20); dockModule.DockLayout.setDock(testBtnRight, enums.Dock.right); this.testView.addChild(testBtnRight); var testBtnBottom = new helper.MyButton(); - testBtnBottom.height = 20; + testBtnBottom.height = layoutHelper.dp(20); dockModule.DockLayout.setDock(testBtnBottom, enums.Dock.bottom); this.testView.addChild(testBtnBottom); @@ -152,10 +153,10 @@ export class DockLayoutTest extends testModule.UITest { public test_padding() { var testBtn = new helper.MyButton(); this.testView.addChild(testBtn); - this.testView.paddingLeft = 10; - this.testView.paddingTop = 20; - this.testView.paddingRight = 30; - this.testView.paddingBottom = 40; + this.testView.paddingLeft = layoutHelper.dp(10); + this.testView.paddingTop = layoutHelper.dp(20); + this.testView.paddingRight = layoutHelper.dp(30); + this.testView.paddingBottom = layoutHelper.dp(40); this.waitUntilTestElementLayoutIsValid(); diff --git a/apps/tests/layouts/grid-layout-tests.ts b/apps/tests/layouts/grid-layout-tests.ts index bd2fb6a3e..6edc208c2 100644 --- a/apps/tests/layouts/grid-layout-tests.ts +++ b/apps/tests/layouts/grid-layout-tests.ts @@ -9,6 +9,7 @@ import utils = require("utils/utils"); import builder = require("ui/builder"); import enums = require("ui/enums"); import testModule = require("../ui-test"); +import layoutHelper = require("./layout-helper"); var DELTA = 1; @@ -61,12 +62,12 @@ export class GridLayoutTest extends testModule.UITest { 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(50, layout.GridUnitType.pixel)); + this.testView.addRow(new layout.ItemSpec(layoutHelper.dp(50), layout.GridUnitType.pixel)); this.testView.addRow(new layout.ItemSpec(50, layout.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(50, layout.GridUnitType.pixel)); + this.testView.addColumn(new layout.ItemSpec(layoutHelper.dp(50), layout.GridUnitType.pixel)); this.testView.addColumn(new layout.ItemSpec(50, layout.GridUnitType.auto)); for (var r = 0; r < 4; r++) { @@ -76,19 +77,19 @@ export class GridLayoutTest extends testModule.UITest { layout.GridLayout.setColumn(btn, c); layout.GridLayout.setRow(btn, r); if (c === 3) { - btn.width = 100; // Auto column should take 100px for this test. + btn.width = layoutHelper.dp(100); // Auto column should take 100px for this test. } if (r === 3) { - btn.height = 100; // Auto row should take 100px for this test. + btn.height = layoutHelper.dp(100); // Auto row should take 100px for this test. } this.testView.addChild(btn); } } - this.testView.width = 300; - this.testView.height = 300; + this.testView.width = layoutHelper.dp(300); + this.testView.height = layoutHelper.dp(300); if (wait) { this.waitUntilTestElementLayoutIsValid(); @@ -245,58 +246,55 @@ export class GridLayoutTest extends testModule.UITest { this.waitUntilTestElementLayoutIsValid(); TKUnit.assertTrue(btn.getMeasuredWidth() === this.testView.getMeasuredWidth()); + TKUnit.assertTrue(this.testView.getMeasuredWidth() < 50); } public test_measuredWidth_when_not_stretched_two_columns() { this.testView.horizontalAlignment = enums.HorizontalAlignment.center; - this.testView.addColumn(new layout.ItemSpec(80, layout.GridUnitType.pixel)); + this.testView.addColumn(new layout.ItemSpec(layoutHelper.dp(80), layout.GridUnitType.pixel)); this.testView.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star)); let btn = new Button(); btn.text = "A"; - btn.width = 100; + btn.width = layoutHelper.dp(100); MyGridLayout.setColumnSpan(btn, 2); this.testView.addChild(btn); this.waitUntilTestElementLayoutIsValid(); - var density = utils.layout.getDisplayDensity(); - var delta = Math.floor(density) !== density ? 2 : DELTA; var cols = this.testView.getColumns(); - TKUnit.assertAreClose(cols[0].actualLength, 80, delta); - TKUnit.assertAreClose(cols[1].actualLength, 20, delta); - TKUnit.assertAreClose(this.testView.getMeasuredWidth(), 100 * density, delta); + TKUnit.assertAreClose(cols[0].actualLength, 80, DELTA); + TKUnit.assertAreClose(cols[1].actualLength, 20, DELTA); + TKUnit.assertAreClose(this.testView.getMeasuredWidth(), 100, DELTA); } public test_measuredWidth_when_not_stretched_three_columns() { this.testView.horizontalAlignment = enums.HorizontalAlignment.center; - this.testView.addColumn(new layout.ItemSpec(80, layout.GridUnitType.pixel)); + 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)); for (let i = 1; i < 4; i++) { let btn = new Button(); btn.text = "A"; - btn.width = i * 20; + btn.width = layoutHelper.dp(i * 20); MyGridLayout.setColumn(btn, i - 1); this.testView.addChild(btn); } let btn = new Button(); btn.text = "B"; - btn.width = 100; + btn.width = layoutHelper.dp(100); MyGridLayout.setColumnSpan(btn, 3); this.testView.addChild(btn); this.waitUntilTestElementLayoutIsValid(); - var density = utils.layout.getDisplayDensity(); - var delta = Math.floor(density) !== density ? 2 : DELTA; var cols = this.testView.getColumns(); - TKUnit.assertAreClose(cols[0].actualLength, 80, delta); - TKUnit.assertAreClose(cols[1].actualLength, 40, delta); - TKUnit.assertAreClose(cols[2].actualLength, 60, delta); - TKUnit.assertAreClose(this.testView.getMeasuredWidth(), 180 * density, delta); + TKUnit.assertAreClose(cols[0].actualLength, 80, DELTA); + TKUnit.assertAreClose(cols[1].actualLength, 40, DELTA); + TKUnit.assertAreClose(cols[2].actualLength, 60, DELTA); + TKUnit.assertAreClose(this.testView.getMeasuredWidth(), 180, DELTA); } public test_getRows_shouldNotReturnNULL() { @@ -405,12 +403,10 @@ export class GridLayoutTest extends testModule.UITest { } } - var delta = 1.1; // Set to an overly high value to avoid failing on some emulators. - let measuredWidth = this.testView.getMeasuredWidth(); let measuredHeight = this.testView.getMeasuredHeight(); - TKUnit.assertAreClose(measuredWidth, maxWidth, delta, "GridLayout incorrect measured width"); - TKUnit.assertAreClose(measuredHeight, maxHeight, delta, "GridLayout incorrect measured height"); + TKUnit.assertAreClose(measuredWidth, maxWidth, DELTA, "GridLayout incorrect measured width"); + TKUnit.assertAreClose(measuredHeight, maxHeight, DELTA, "GridLayout incorrect measured height"); } public test_columnsActualWidth_isCorrect() { @@ -427,10 +423,10 @@ export class GridLayoutTest extends testModule.UITest { this.prepareGridLayout(true); var rows = this.testView.getRows(); - TKUnit.assertEqual(rows[0].actualLength, 50, "Star row should be 50px width"); - TKUnit.assertEqual(rows[1].actualLength, 100, "2*Star row should be 100px width"); - TKUnit.assertEqual(rows[2].actualLength, 50, "Absolute row should be 50px width"); - TKUnit.assertEqual(rows[3].actualLength, 100, "Auto row should be 100px width"); + TKUnit.assertEqual(rows[0].actualLength, layoutHelper.dip(50), "Star row should be 50px width"); + TKUnit.assertEqual(rows[1].actualLength, layoutHelper.dip(100), "2*Star row should be 100px width"); + TKUnit.assertEqual(rows[2].actualLength, layoutHelper.dip(50), "Absolute row should be 50px width"); + TKUnit.assertEqual(rows[3].actualLength, layoutHelper.dip(100), "Auto row should be 100px width"); } public test_Measure_and_Layout_Children_withCorrect_size() { @@ -440,8 +436,6 @@ export class GridLayoutTest extends testModule.UITest { var rows = this.testView.getRows(); var cols = this.testView.getColumns(); var i = 0; - var density = utils.layout.getDisplayDensity(); - var delta = Math.floor(density) !== density ? 1.1 : DELTA; for (var r = 0; r < 4; r++) { @@ -453,31 +447,28 @@ export class GridLayoutTest extends testModule.UITest { var h = r % 2 === 0 ? 50 : 100; var w = c % 2 === 0 ? 50 : 100; - h = Math.round(h * density); - w = Math.round(w * density); - if (row.isAuto) { - TKUnit.assertAreClose(btn.layoutHeight, btn.getMeasuredHeight(), delta, "Auto rows should layout with measured height"); + TKUnit.assertAreClose(btn.layoutHeight, btn.getMeasuredHeight(), DELTA, "Auto rows should layout with measured height"); } else if (row.isAbsolute) { - TKUnit.assertAreClose(btn.measureHeight, h, delta, "Absolute rows should measure with specific height"); - TKUnit.assertAreClose(btn.layoutHeight, h, delta, "Absolute rows should layout with specific height"); + TKUnit.assertAreClose(btn.measureHeight, h, DELTA, "Absolute rows should measure with specific height"); + TKUnit.assertAreClose(btn.layoutHeight, h, DELTA, "Absolute rows should layout with specific height"); } else { - TKUnit.assertAreClose(btn.measureHeight, h, delta, "Star rows should measure with specific height"); - TKUnit.assertAreClose(btn.layoutHeight, h, delta, "Star rows should layout with exact length"); + TKUnit.assertAreClose(btn.measureHeight, h, DELTA, "Star rows should measure with specific height"); + TKUnit.assertAreClose(btn.layoutHeight, h, DELTA, "Star rows should layout with exact length"); } if (col.isAuto) { - TKUnit.assertAreClose(btn.layoutWidth, btn.getMeasuredWidth(), delta, "Auto columns should layout with measured width"); + TKUnit.assertAreClose(btn.layoutWidth, btn.getMeasuredWidth(), DELTA, "Auto columns should layout with measured width"); } else if (col.isAbsolute) { - TKUnit.assertAreClose(btn.measureWidth, w, delta, "Absolute columns should measure with specific width"); - TKUnit.assertAreClose(btn.layoutWidth, w, delta, "Absolute columns should layout with specific width"); + TKUnit.assertAreClose(btn.measureWidth, w, DELTA, "Absolute columns should measure with specific width"); + TKUnit.assertAreClose(btn.layoutWidth, w, DELTA, "Absolute columns should layout with specific width"); } else { - TKUnit.assertAreClose(btn.measureWidth, w, delta, "Star columns should measure with specific width"); - TKUnit.assertAreClose(btn.layoutWidth, w, delta, "Star columns should layout with exact length"); + TKUnit.assertAreClose(btn.measureWidth, w, DELTA, "Star columns should measure with specific width"); + TKUnit.assertAreClose(btn.layoutWidth, w, DELTA, "Star columns should layout with exact length"); } } } @@ -485,7 +476,7 @@ export class GridLayoutTest extends testModule.UITest { public test_ColumnWidth_when_4stars_and_width_110() { - this.testView.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)); @@ -495,34 +486,28 @@ export class GridLayoutTest extends testModule.UITest { var cols = this.testView.getColumns(); - var density = utils.layout.getDisplayDensity(); - var delta = Math.floor(density) !== density ? 1.1 : DELTA; - - TKUnit.assertAreClose(cols[0].actualLength, 28, delta, "Column[0] actual length should be 28"); - TKUnit.assertAreClose(cols[1].actualLength, 27, delta, "Column[1] actual length should be 27"); - TKUnit.assertAreClose(cols[2].actualLength, 28, delta, "Column[2] actual length should be 28"); - TKUnit.assertAreClose(cols[3].actualLength, 27, delta, "Column[3] actual length should be 27"); + TKUnit.assertAreClose(cols[0].actualLength, layoutHelper.dip(28), DELTA, "Column[0] actual length should be 28"); + TKUnit.assertAreClose(cols[1].actualLength, layoutHelper.dip(27), DELTA, "Column[1] actual length should be 27"); + TKUnit.assertAreClose(cols[2].actualLength, layoutHelper.dip(28), DELTA, "Column[2] actual length should be 28"); + TKUnit.assertAreClose(cols[3].actualLength, layoutHelper.dip(27), DELTA, "Column[3] actual length should be 27"); } public test_margins_and_verticalAlignment_center() { - this.testView.height = 200; - this.testView.width = 200; + this.testView.height = layoutHelper.dp(200); + this.testView.width = layoutHelper.dp(200); var btn = new helper.MyButton(); btn.text = "btn"; - btn.height = 100; - btn.width = 100; - btn.marginBottom = 50; - btn.marginRight = 50; + btn.height = layoutHelper.dp(100); + btn.width = layoutHelper.dp(100); + btn.marginBottom = layoutHelper.dp(50); + btn.marginRight = layoutHelper.dp(50); this.testView.addChild(btn); this.waitUntilTestElementLayoutIsValid(); - var density = utils.layout.getDisplayDensity(); - var delta = Math.floor(density) !== density ? 1.1 : DELTA; - - TKUnit.assertAreClose(btn.layoutTop, 25 * density, delta, "vertical margins"); - TKUnit.assertAreClose(btn.layoutLeft, 25 * density, delta, "horizontal margins"); + TKUnit.assertAreClose(btn.layoutTop, 25, DELTA, "vertical margins"); + TKUnit.assertAreClose(btn.layoutLeft, 25, DELTA, "horizontal margins"); } public test_set_columns_in_XML() { @@ -564,13 +549,13 @@ export class GridLayoutTest extends testModule.UITest { } public test_padding() { - this.testView.paddingLeft = 10; - this.testView.paddingTop = 20; - this.testView.paddingRight = 30; - this.testView.paddingBottom = 40; + this.testView.paddingLeft = layoutHelper.dp(10); + this.testView.paddingTop = layoutHelper.dp(20); + this.testView.paddingRight = layoutHelper.dp(30); + this.testView.paddingBottom = layoutHelper.dp(40); - this.testView.width = 300; - this.testView.height = 300; + this.testView.width = layoutHelper.dp(300); + this.testView.height = layoutHelper.dp(300); var btn = new helper.MyButton(); this.testView.addChild(btn); diff --git a/apps/tests/layouts/layout-helper.android.ts b/apps/tests/layouts/layout-helper.android.ts index 684d9af55..4611ba825 100644 --- a/apps/tests/layouts/layout-helper.android.ts +++ b/apps/tests/layouts/layout-helper.android.ts @@ -155,28 +155,25 @@ export class MyStackLayout extends StackLayout implements def.MyStackLayout { } export function assertMeasure(btn: MyButton, width: number, height: number, name?: string) { - var density = utils.layout.getDisplayDensity(); - - var delta = Math.floor(density) !== density ? 1.1 : DELTA; name = name ? "[" + name + "]" : ""; - TKUnit.assertAreClose(Math.floor(btn.measureWidth / density), width, delta, name + "width"); - TKUnit.assertAreClose(Math.floor(btn.measureHeight / density), height, delta, name + "height"); + TKUnit.assertAreClose(btn.measureWidth, width, DELTA, name + "width"); + TKUnit.assertAreClose(btn.measureHeight, height, DELTA, name + "height"); } export function assertLayout(btn: MyButton, left: number, top: number, width: number, height: number, name?: string): void { - var density = utils.layout.getDisplayDensity(); - - var delta = Math.floor(density) !== density ? 1.1 : DELTA; name = name ? "[" + name + "]" : ""; - TKUnit.assertAreClose(Math.floor(btn.layoutLeft / density), left, delta, name + "left"); - TKUnit.assertAreClose(Math.floor(btn.layoutTop / density), top, delta, name + "top"); - TKUnit.assertAreClose(Math.floor(btn.layoutWidth / density), width, delta, name + "width"); - TKUnit.assertAreClose(Math.floor(btn.layoutHeight / density), height, delta, name + "height"); + TKUnit.assertAreClose(btn.layoutLeft, left, DELTA, name + "left"); + TKUnit.assertAreClose(btn.layoutTop, top, DELTA, name + "top"); + TKUnit.assertAreClose(btn.layoutWidth, width, DELTA, name + "width"); + TKUnit.assertAreClose(btn.layoutHeight, height, DELTA, name + "height"); +} + +export function dp(value: number): number { + return utils.layout.toDevicePixels(value); } export function dip(value: number): number { - var density = utils.layout.getDisplayDensity(); - return Math.floor(value * density); -} + return utils.layout.toDeviceIndependentPixels(value); +} \ No newline at end of file diff --git a/apps/tests/layouts/layout-helper.d.ts b/apps/tests/layouts/layout-helper.d.ts index 606fe7a95..17ef67905 100644 --- a/apps/tests/layouts/layout-helper.d.ts +++ b/apps/tests/layouts/layout-helper.d.ts @@ -28,4 +28,5 @@ export class MyStackLayout extends StackLayout { export function assertMeasure(btn: MyButton, width: number, height: number, name?: string); export function assertLayout(btn: MyButton, left: number, top: number, width: number, height: number, name?: string): void; -export function dip(value: number): number; \ No newline at end of file +export function dip(value: number): number; +export function dp(value: number): number; \ No newline at end of file diff --git a/apps/tests/layouts/layout-helper.ios.ts b/apps/tests/layouts/layout-helper.ios.ts index 748cc89e5..546683219 100644 --- a/apps/tests/layouts/layout-helper.ios.ts +++ b/apps/tests/layouts/layout-helper.ios.ts @@ -119,7 +119,10 @@ export function assertLayout(btn: MyButton, left: number, top: number, width: nu TKUnit.assertAreClose(Math.floor(btn.layoutHeight / density), height, delta, name + "height"); } +export function dp(value: number): number { + return utils.layout.toDevicePixels(value); +} + export function dip(value: number): number { - var density = utils.layout.getDisplayDensity(); - return Math.floor(value * density); + return utils.layout.toDeviceIndependentPixels(value); } \ No newline at end of file diff --git a/apps/tests/layouts/stack-layout-tests.ts b/apps/tests/layouts/stack-layout-tests.ts index c6092e115..8ec4bab73 100644 --- a/apps/tests/layouts/stack-layout-tests.ts +++ b/apps/tests/layouts/stack-layout-tests.ts @@ -7,6 +7,7 @@ import navHelper = require("../ui/helper"); import enums = require("ui/enums"); import utils = require("utils/utils"); import testModule = require("../ui-test"); +import layoutHelper = require("./layout-helper"); export class StackLayoutTest extends testModule.UITest { @@ -38,7 +39,7 @@ export class StackLayoutTest extends testModule.UITest { this.waitUntilTestElementLayoutIsValid(); var arrangeCount = this.rootLayout.arrangeCount; - TKUnit.assert(this.rootLayout.orientation === enums.Orientation.vertical, "Default orientation should be Vertical."); + TKUnit.assertEqual(this.rootLayout.orientation, enums.Orientation.vertical, "Default orientation should be Vertical."); this.rootLayout.orientation = enums.Orientation.horizontal; this.waitUntilTestElementLayoutIsValid(); @@ -51,8 +52,8 @@ export class StackLayoutTest extends testModule.UITest { this.waitUntilTestElementLayoutIsValid(); TKUnit.assertEqual(this.rootLayout.orientation, enums.Orientation.vertical, "StackLayout should be vertical."); - TKUnit.assert(this.rootLayout.measured, "Layout should be measured."); - TKUnit.assert(this.rootLayout.arranged, "Layout should be arranged."); + TKUnit.assertTrue(this.rootLayout.measured, "Layout should be measured."); + TKUnit.assertTrue(this.rootLayout.arranged, "Layout should be arranged."); var specs = this.btn1._getCurrentMeasureSpecs(); @@ -97,16 +98,16 @@ export class StackLayoutTest extends testModule.UITest { } public test_Padding_Vertical() { - this.rootLayout.width = 300; - this.rootLayout.height = 300; + this.rootLayout.width = layoutHelper.dp(300); + this.rootLayout.height = layoutHelper.dp(300); - this.rootLayout.paddingLeft = 10; - this.rootLayout.paddingTop = 20; - this.rootLayout.paddingRight = 30; - this.rootLayout.paddingBottom = 40; + this.rootLayout.paddingLeft = layoutHelper.dp(10); + this.rootLayout.paddingTop = layoutHelper.dp(20); + this.rootLayout.paddingRight = layoutHelper.dp(30); + this.rootLayout.paddingBottom = layoutHelper.dp(40); - this.btn1.height = 50; - this.btn2.height = 50; + this.btn1.height = layoutHelper.dp(50); + this.btn2.height = layoutHelper.dp(50); this.waitUntilTestElementLayoutIsValid(); @@ -118,17 +119,17 @@ export class StackLayoutTest extends testModule.UITest { } public test_Padding_Horizontal() { - this.rootLayout.width = 300; - this.rootLayout.height = 300; + this.rootLayout.width = layoutHelper.dp(300); + this.rootLayout.height = layoutHelper.dp(300); this.rootLayout.orientation = enums.Orientation.horizontal; - this.rootLayout.paddingLeft = 10; - this.rootLayout.paddingTop = 20; - this.rootLayout.paddingRight = 30; - this.rootLayout.paddingBottom = 40; + this.rootLayout.paddingLeft = layoutHelper.dp(10); + this.rootLayout.paddingTop = layoutHelper.dp(20); + this.rootLayout.paddingRight = layoutHelper.dp(30); + this.rootLayout.paddingBottom = layoutHelper.dp(40); - this.btn1.width = 50; - this.btn2.width = 50; + this.btn1.width = layoutHelper.dp(50); + this.btn2.width = layoutHelper.dp(50); this.waitUntilTestElementLayoutIsValid(); diff --git a/apps/tests/layouts/wrap-layout-tests.ts b/apps/tests/layouts/wrap-layout-tests.ts index b178ee656..23f042e13 100644 --- a/apps/tests/layouts/wrap-layout-tests.ts +++ b/apps/tests/layouts/wrap-layout-tests.ts @@ -41,8 +41,8 @@ export class WrapLayoutTest extends testModule.UITest - wrapLayout.width = 200; - wrapLayout.height = 200; + wrapLayout.width = layoutHelper.dp(200); + wrapLayout.height = layoutHelper.dp(200); var label; var i; @@ -51,8 +51,8 @@ export class WrapLayoutTest extends testModule.UITest { + var hashSet = new java.util.HashSet(); if ("undefined" !== typeof str) { for (var element in str) { hashSet.add('' + str[element]); diff --git a/utils/utils.d.ts b/utils/utils.d.ts index a6b93a2bd..2799cedf6 100644 --- a/utils/utils.d.ts +++ b/utils/utils.d.ts @@ -50,6 +50,16 @@ * Gets display density for the current device. */ export function getDisplayDensity(): number; + /** + * Convert value to device pixels. + * @param value - The pixel to convert. + */ + export function toDevicePixels(value: number): number; + /** + * Convert value to device independent pixels. + * @param value - The pixel to convert. + */ + export function toDeviceIndependentPixels(value: number): number; } /** diff --git a/utils/utils.ios.ts b/utils/utils.ios.ts index 3647b372d..8c61ef83c 100644 --- a/utils/utils.ios.ts +++ b/utils/utils.ios.ts @@ -18,24 +18,27 @@ export module layout { export function getDisplayDensity(): number { return 1; } + + export function toDevicePixels(value: number): number { + return value * getDisplayDensity(); + } + + export function toDeviceIndependentPixels(value: number): number { + return value / getDisplayDensity(); + } } export module ios { export module collections { - export function jsArrayToNSArray(str: string[]): any { - var arr = new NSMutableArray(); - if ("undefined" !== typeof str) { - for (var element in str) { - arr.addObject(str[element]); - } - } - return arr; + export function jsArrayToNSArray(str: string[]): NSArray { + return NSArray.arrayWithArray(str); } - export function nsArrayToJSArray(a: any): string[] { + export function nsArrayToJSArray(a: NSArray): Array { var arr = []; if ("undefined" !== typeof a) { - for (var i = 0; i < a.count; i++) { + let count = a.count; + for (let i = 0; i < count; i++) { arr.push(a.objectAtIndex(i)); } } From fbbfe9b2b83df60e66134881d3677b22dfef6ea1 Mon Sep 17 00:00:00 2001 From: Hristo Hristov Date: Thu, 12 Nov 2015 14:03:40 +0200 Subject: [PATCH 2/2] Made layout unit-tests density independent. --- apps/tests/image-source-tests.ts | 10 ++--- apps/tests/layouts/grid-layout-tests.ts | 37 ++++++++++--------- apps/tests/layouts/layout-helper.android.ts | 4 +- apps/tests/layouts/layout-helper.ios.ts | 4 +- apps/tests/testRunner.ts | 6 --- .../tests/ui/scroll-view/scroll-view-tests.ts | 37 ++++++++++--------- 6 files changed, 47 insertions(+), 51 deletions(-) diff --git a/apps/tests/image-source-tests.ts b/apps/tests/image-source-tests.ts index e8195974f..6fa2756c3 100644 --- a/apps/tests/image-source-tests.ts +++ b/apps/tests/image-source-tests.ts @@ -19,8 +19,8 @@ import app = require("application"); import TKUnit = require("./TKUnit"); import platform = require("platform"); -var imagePath = __dirname + "/logo.png"; -var smallImagePath = __dirname + "/small-image.png"; +var imagePath = fs.path.join(__dirname, "/logo.png"); +var smallImagePath = fs.path.join(__dirname, "/small-image.png"); /* TODO: We need a way to programmatically add an image to resources and then load it from, otherwise we do not know if there is such resource in the target native app. export function testFromResource() { @@ -155,7 +155,7 @@ export function testLoadFromBase64Encode_JPEG() { TKUnit.assert(img !== null, "Actual: " + img); TKUnit.assertEqual(img.width, 4, "img.width"); - TKUnit.assertEqual(img.height , 4, "img.width"); + TKUnit.assertEqual(img.height, 4, "img.height"); } export function testLoadFromBase64Encode_PNG() { @@ -173,5 +173,5 @@ export function testLoadFromBase64Encode_PNG() { TKUnit.assert(img !== null, "Actual: " + img); TKUnit.assertEqual(img.width, 4, "img.width"); - TKUnit.assertEqual(img.height, 4, "img.width"); -} + TKUnit.assertEqual(img.height, 4, "img.height"); +} \ No newline at end of file diff --git a/apps/tests/layouts/grid-layout-tests.ts b/apps/tests/layouts/grid-layout-tests.ts index 6edc208c2..625891517 100644 --- a/apps/tests/layouts/grid-layout-tests.ts +++ b/apps/tests/layouts/grid-layout-tests.ts @@ -10,6 +10,7 @@ import builder = require("ui/builder"); import enums = require("ui/enums"); import testModule = require("../ui-test"); import layoutHelper = require("./layout-helper"); +import platform = require("platform"); var DELTA = 1; @@ -246,7 +247,7 @@ export class GridLayoutTest extends testModule.UITest { this.waitUntilTestElementLayoutIsValid(); TKUnit.assertTrue(btn.getMeasuredWidth() === this.testView.getMeasuredWidth()); - TKUnit.assertTrue(this.testView.getMeasuredWidth() < 50); + TKUnit.assertTrue(this.testView.getMeasuredWidth() < platform.screen.mainScreen.widthPixels); } public test_measuredWidth_when_not_stretched_two_columns() { @@ -263,8 +264,8 @@ export class GridLayoutTest extends testModule.UITest { this.waitUntilTestElementLayoutIsValid(); var cols = this.testView.getColumns(); - TKUnit.assertAreClose(cols[0].actualLength, 80, DELTA); - TKUnit.assertAreClose(cols[1].actualLength, 20, DELTA); + TKUnit.assertAreClose(cols[0].actualLength, Math.round(layoutHelper.dp(80)), DELTA); + TKUnit.assertAreClose(cols[1].actualLength, Math.round(layoutHelper.dp(20)), DELTA); TKUnit.assertAreClose(this.testView.getMeasuredWidth(), 100, DELTA); } @@ -291,9 +292,9 @@ export class GridLayoutTest extends testModule.UITest { this.waitUntilTestElementLayoutIsValid(); var cols = this.testView.getColumns(); - TKUnit.assertAreClose(cols[0].actualLength, 80, DELTA); - TKUnit.assertAreClose(cols[1].actualLength, 40, DELTA); - TKUnit.assertAreClose(cols[2].actualLength, 60, DELTA); + TKUnit.assertAreClose(cols[0].actualLength, Math.round(layoutHelper.dp(80)), DELTA); + TKUnit.assertAreClose(cols[1].actualLength, Math.round(layoutHelper.dp(40)), DELTA); + TKUnit.assertAreClose(cols[2].actualLength, Math.round(layoutHelper.dp(60)), DELTA); TKUnit.assertAreClose(this.testView.getMeasuredWidth(), 180, DELTA); } @@ -413,20 +414,20 @@ export class GridLayoutTest extends testModule.UITest { this.prepareGridLayout(true); var cols = this.testView.getColumns(); - TKUnit.assertEqual(cols[0].actualLength, 50, "Star column should be 50px width"); - TKUnit.assertEqual(cols[1].actualLength, 100, "2*Star column should be 100px width"); - TKUnit.assertEqual(cols[2].actualLength, 50, "Absolute column should be 50px width"); - TKUnit.assertEqual(cols[3].actualLength, 100, "Auto column should be 100px width"); + TKUnit.assertEqual(cols[0].actualLength, Math.round(layoutHelper.dp(50)), "Star column should be 50px width"); + TKUnit.assertEqual(cols[1].actualLength, Math.round(layoutHelper.dp(100)), "2*Star column should be 100px width"); + TKUnit.assertEqual(cols[2].actualLength, Math.round(layoutHelper.dp(50)), "Absolute column should be 50px width"); + TKUnit.assertEqual(cols[3].actualLength, Math.round(layoutHelper.dp(100)), "Auto column should be 100px width"); } public test_rowsActualHeight_isCorrect() { this.prepareGridLayout(true); var rows = this.testView.getRows(); - TKUnit.assertEqual(rows[0].actualLength, layoutHelper.dip(50), "Star row should be 50px width"); - TKUnit.assertEqual(rows[1].actualLength, layoutHelper.dip(100), "2*Star row should be 100px width"); - TKUnit.assertEqual(rows[2].actualLength, layoutHelper.dip(50), "Absolute row should be 50px width"); - TKUnit.assertEqual(rows[3].actualLength, layoutHelper.dip(100), "Auto row should be 100px width"); + TKUnit.assertEqual(rows[0].actualLength, Math.round(layoutHelper.dp(50)), "Star row should be 50px width"); + TKUnit.assertEqual(rows[1].actualLength, Math.round(layoutHelper.dp(100)), "2*Star row should be 100px width"); + TKUnit.assertEqual(rows[2].actualLength, Math.round(layoutHelper.dp(50)), "Absolute row should be 50px width"); + TKUnit.assertEqual(rows[3].actualLength, Math.round(layoutHelper.dp(100)), "Auto row should be 100px width"); } public test_Measure_and_Layout_Children_withCorrect_size() { @@ -486,10 +487,10 @@ export class GridLayoutTest extends testModule.UITest { var cols = this.testView.getColumns(); - TKUnit.assertAreClose(cols[0].actualLength, layoutHelper.dip(28), DELTA, "Column[0] actual length should be 28"); - TKUnit.assertAreClose(cols[1].actualLength, layoutHelper.dip(27), DELTA, "Column[1] actual length should be 27"); - TKUnit.assertAreClose(cols[2].actualLength, layoutHelper.dip(28), DELTA, "Column[2] actual length should be 28"); - TKUnit.assertAreClose(cols[3].actualLength, layoutHelper.dip(27), DELTA, "Column[3] actual length should be 27"); + TKUnit.assertAreClose(cols[0].actualLength, Math.round(layoutHelper.dp(28)), DELTA, "Column[0] actual length should be 28"); + TKUnit.assertAreClose(cols[1].actualLength, Math.round(layoutHelper.dp(27)), DELTA, "Column[1] actual length should be 27"); + TKUnit.assertAreClose(cols[2].actualLength, Math.round(layoutHelper.dp(28)), DELTA, "Column[2] actual length should be 28"); + TKUnit.assertAreClose(cols[3].actualLength, Math.round(layoutHelper.dp(27)), DELTA, "Column[3] actual length should be 27"); } public test_margins_and_verticalAlignment_center() { diff --git a/apps/tests/layouts/layout-helper.android.ts b/apps/tests/layouts/layout-helper.android.ts index 4611ba825..5ef196e8f 100644 --- a/apps/tests/layouts/layout-helper.android.ts +++ b/apps/tests/layouts/layout-helper.android.ts @@ -171,9 +171,9 @@ export function assertLayout(btn: MyButton, left: number, top: number, width: nu } export function dp(value: number): number { - return utils.layout.toDevicePixels(value); + return utils.layout.toDeviceIndependentPixels(value); } export function dip(value: number): number { - return utils.layout.toDeviceIndependentPixels(value); + return utils.layout.toDevicePixels(value); } \ No newline at end of file diff --git a/apps/tests/layouts/layout-helper.ios.ts b/apps/tests/layouts/layout-helper.ios.ts index 546683219..2d2e17e20 100644 --- a/apps/tests/layouts/layout-helper.ios.ts +++ b/apps/tests/layouts/layout-helper.ios.ts @@ -120,9 +120,9 @@ export function assertLayout(btn: MyButton, left: number, top: number, width: nu } export function dp(value: number): number { - return utils.layout.toDevicePixels(value); + return utils.layout.toDeviceIndependentPixels(value); } export function dip(value: number): number { - return utils.layout.toDeviceIndependentPixels(value); + return utils.layout.toDevicePixels(value); } \ No newline at end of file diff --git a/apps/tests/testRunner.ts b/apps/tests/testRunner.ts index f3888245b..4d5c0634b 100644 --- a/apps/tests/testRunner.ts +++ b/apps/tests/testRunner.ts @@ -92,12 +92,6 @@ if (!isRunningOnEmulator()) { // Navigation tests should always be last. allTests["NAVIGATION"] = require("./navigation-tests"); -import utils = require("utils/utils"); -var density = utils.layout.getDisplayDensity(); -utils.layout.getDisplayDensity = function () { - return Math.round(density * 100) / 100; -} - var testsWithLongDelay = { testLocation: 10000, testLocationOnce: 10000, diff --git a/apps/tests/ui/scroll-view/scroll-view-tests.ts b/apps/tests/ui/scroll-view/scroll-view-tests.ts index ccd0e7407..8ff74fbfd 100644 --- a/apps/tests/ui/scroll-view/scroll-view-tests.ts +++ b/apps/tests/ui/scroll-view/scroll-view-tests.ts @@ -6,6 +6,7 @@ import page = require("ui/page"); import button = require("ui/button"); import enums = require("ui/enums"); import testModule = require("../../ui-test"); +import layoutHelper = require("../../layouts/layout-helper"); //  // # ScrollView @@ -143,8 +144,8 @@ class ScrollLayoutTest extends testModule.UITest { this.waitUntilTestElementLayoutIsValid(); TKUnit.assertEqual(this.testView.verticalOffset, 0, "this.testView.verticalOffset"); - this.testView.scrollToVerticalOffset(100, false); - TKUnit.assertAreClose(this.testView.verticalOffset, 100, 0.1, "this.testView.verticalOffset"); + this.testView.scrollToVerticalOffset(layoutHelper.dp(100), false); + TKUnit.assertAreClose(layoutHelper.dip(this.testView.verticalOffset), 100, 0.1, "this.testView.verticalOffset"); } public test_scrollToVerticalOffset_with_animation() { @@ -160,15 +161,15 @@ class ScrollLayoutTest extends testModule.UITest { this.waitUntilTestElementLayoutIsValid(); TKUnit.assertEqual(this.testView.verticalOffset, 0, "this.testView.verticalOffset"); - this.testView.scrollToVerticalOffset(100, true); + this.testView.scrollToVerticalOffset(layoutHelper.dp(100), true); // No synchronous change. TKUnit.assertEqual(this.testView.verticalOffset, 0, "this.testView.verticalOffset"); - TKUnit.waitUntilReady(() => { return TKUnit.areClose(this.testView.verticalOffset, 100, 0.9); }); + TKUnit.waitUntilReady(() => { return TKUnit.areClose(layoutHelper.dip(this.testView.verticalOffset), 100, 0.9); }); // The scrolling animation should be finished by now - TKUnit.assertAreClose(this.testView.verticalOffset, 100, 0.9, "this.testView.verticalOffset"); + TKUnit.assertAreClose(layoutHelper.dip(this.testView.verticalOffset), 100, 0.9, "this.testView.verticalOffset"); } public test_scrollToHorizontalOffset_no_animation() { @@ -184,8 +185,8 @@ class ScrollLayoutTest extends testModule.UITest { this.waitUntilTestElementLayoutIsValid(); TKUnit.assertEqual(this.testView.horizontalOffset, 0, "this.testView.horizontalOffset"); - this.testView.scrollToHorizontalOffset(100, false); - TKUnit.assertAreClose(this.testView.horizontalOffset, 100, 0.1, "this.testView.horizontalOffset"); + this.testView.scrollToHorizontalOffset(layoutHelper.dp(100), false); + TKUnit.assertAreClose(layoutHelper.dip(this.testView.horizontalOffset), 100, 0.1, "this.testView.horizontalOffset"); } public test_scrollToHorizontalOffset_with_animation() { @@ -201,15 +202,15 @@ class ScrollLayoutTest extends testModule.UITest { this.waitUntilTestElementLayoutIsValid(); TKUnit.assertEqual(this.testView.horizontalOffset, 0, "this.testView.horizontalOffset"); - this.testView.scrollToHorizontalOffset(100, true); + this.testView.scrollToHorizontalOffset(layoutHelper.dp(100), true); // No synchronous change. TKUnit.assertEqual(this.testView.horizontalOffset, 0, "this.testView.horizontalOffset"); - TKUnit.waitUntilReady(() => { return TKUnit.areClose(this.testView.horizontalOffset, 100, 0.9); }); + TKUnit.waitUntilReady(() => { return TKUnit.areClose(layoutHelper.dip(this.testView.horizontalOffset), 100, 0.9); }); // The scrolling animation should be finished by now - TKUnit.assertAreClose(this.testView.horizontalOffset, 100, 0.9, "this.testView.horizontalOffset"); + TKUnit.assertAreClose(layoutHelper.dip(this.testView.horizontalOffset), 100, 0.9, "this.testView.horizontalOffset"); } public test_scrollView_persistsState_vertical() { @@ -224,19 +225,19 @@ class ScrollLayoutTest extends testModule.UITest { this.testView.content = btn; this.waitUntilTestElementLayoutIsValid(); - this.testView.scrollToVerticalOffset(100, false); + this.testView.scrollToVerticalOffset(layoutHelper.dp(100), false); - TKUnit.assertAreClose(this.testView.verticalOffset, 100, 0.1, "this.testView.verticalOffset before navigation"); + TKUnit.assertAreClose(layoutHelper.dip(this.testView.verticalOffset), 100, 0.1, "this.testView.verticalOffset before navigation"); helper.do_PageTest_WithButton((t) => { // Just navigate forward and back. }); // Wait for the page to reload. - TKUnit.waitUntilReady(() => { return TKUnit.areClose(this.testView.verticalOffset, 100, 0.1); }); + TKUnit.waitUntilReady(() => { return TKUnit.areClose(layoutHelper.dip(this.testView.verticalOffset), 100, 0.1); }); // Check verticalOffset after navigation - TKUnit.assertAreClose(this.testView.verticalOffset, 100, 0.1, "this.testView.verticalOffset after navigation"); + TKUnit.assertAreClose(layoutHelper.dip(this.testView.verticalOffset), 100, 0.1, "this.testView.verticalOffset after navigation"); } public test_scrollView_persistsState_horizontal() { @@ -251,19 +252,19 @@ class ScrollLayoutTest extends testModule.UITest { this.testView.content = btn; this.waitUntilTestElementLayoutIsValid(); - this.testView.scrollToHorizontalOffset(100, false); + this.testView.scrollToHorizontalOffset(layoutHelper.dp(100), false); - TKUnit.assertAreClose(this.testView.horizontalOffset, 100, 0.1, "this.testView.horizontalOffset before navigation"); + TKUnit.assertAreClose(layoutHelper.dip(this.testView.horizontalOffset), 100, 0.1, "this.testView.horizontalOffset before navigation"); helper.do_PageTest_WithButton((t) => { // Just navigate forward and back. }); // Wait for the page to reload. - TKUnit.waitUntilReady(() => { return TKUnit.areClose(this.testView.horizontalOffset, 100, 0.1); }); + TKUnit.waitUntilReady(() => { return TKUnit.areClose(layoutHelper.dip(this.testView.horizontalOffset), 100, 0.1); }); // Check verticalOffset after navigation - TKUnit.assertAreClose(this.testView.horizontalOffset, 100, 0.1, "this.testView.horizontalOffset after navigation"); + TKUnit.assertAreClose(layoutHelper.dip(this.testView.horizontalOffset), 100, 0.1, "this.testView.horizontalOffset after navigation"); } public test_scrollView_vertical_raised_scroll_event() {