Made layout unit-tests density independent.

This commit is contained in:
Hristo Hristov
2015-11-12 14:03:40 +02:00
parent c2cb4a8f61
commit fbbfe9b2b8
6 changed files with 47 additions and 51 deletions

View File

@ -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");
}

View File

@ -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<layout.GridLayout> {
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<layout.GridLayout> {
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<layout.GridLayout> {
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<layout.GridLayout> {
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<layout.GridLayout> {
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() {

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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,

View File

@ -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");
// <snippet module="ui/scroll-view" title="scroll-view">
// # ScrollView
@ -143,8 +144,8 @@ class ScrollLayoutTest extends testModule.UITest<scrollViewModule.ScrollView> {
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<scrollViewModule.ScrollView> {
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<scrollViewModule.ScrollView> {
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<scrollViewModule.ScrollView> {
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<scrollViewModule.ScrollView> {
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<scrollViewModule.ScrollView> {
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() {