From fbbfe9b2b83df60e66134881d3677b22dfef6ea1 Mon Sep 17 00:00:00 2001 From: Hristo Hristov Date: Thu, 12 Nov 2015 14:03:40 +0200 Subject: [PATCH] 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() {