From 8cd1a9cae70526699860ad179c124a0904c4aba5 Mon Sep 17 00:00:00 2001 From: Vasil Chimev Date: Fri, 7 Sep 2018 14:45:58 +0300 Subject: [PATCH] test: extract layout-tests-helper --- tests/app/ui/layouts/flexbox-layout-tests.ts | 108 ++----------------- tests/app/ui/layouts/layout-tests-helper.ts | 98 +++++++++++++++++ 2 files changed, 107 insertions(+), 99 deletions(-) create mode 100644 tests/app/ui/layouts/layout-tests-helper.ts diff --git a/tests/app/ui/layouts/flexbox-layout-tests.ts b/tests/app/ui/layouts/flexbox-layout-tests.ts index f18e489ba..af492c352 100644 --- a/tests/app/ui/layouts/flexbox-layout-tests.ts +++ b/tests/app/ui/layouts/flexbox-layout-tests.ts @@ -55,13 +55,14 @@ import { View, unsetValue, Length, PercentLength } from "tns-core-modules/ui/cor import { Label } from "tns-core-modules/ui/label"; import * as TKUnit from "../../TKUnit"; import * as helper from "../helper"; -import { layout } from "tns-core-modules/utils/utils"; import { parse } from "tns-core-modules/ui/builder"; - -import dipToDp = layout.toDevicePixels; -import round = layout.round; - -const EPS = 1; +import { dipToDp, left, top, right, bottom, height, width, + paddingLeft, paddingTop, paddingRight, paddingBottom, + equal, closeEnough, notEqual, check, + heightEqual, widthEqual, + isLeftAlignedWith, isRightAlignedWith, isTopAlignedWith, isBottomAlignedWith, + isLeftOf, isRightOf, isBelow, isAbove, + isLeftWith, isAboveWith, isRightWith, isBelowWith } from "./layout-tests-helper"; function waitUntilTestElementLayoutIsValid(view: View, timeoutSec?: number): void { TKUnit.waitUntilReady(() => { @@ -74,97 +75,6 @@ function baseline(view: View): number { return 0; } -function height(view: View): number { - return round(dipToDp(view.getActualSize().height)); -} - -function width(view: View): number { - return round(dipToDp(view.getActualSize().width)); -} - - -function left(view: View): number { return round(dipToDp(view.getLocationInWindow().x)); } -function top(view: View): number { return round(dipToDp(view.getLocationInWindow().y)); } -function right(view: View): number { return left(view) + width(view); } -function bottom(view: View): number { return top(view) + height(view); } - -function paddingLeft(view: View): number { return Length.toDevicePixels(view.style.paddingLeft, 0) + Length.toDevicePixels(view.parent.style.paddingLeft, 0) + (view.parent).getSafeAreaInsets().left; }; -function paddingTop(view: View): number { return top(view) + Length.toDevicePixels(view.style.paddingTop, 0) }; -function paddingRight(view: View): number { return right(view) - Length.toDevicePixels(view.style.paddingRight, 0) }; -function paddingBottom(view: View): number { return bottom(view) - Length.toDevicePixels(view.style.paddingBottom, 0) }; - -function equal(a: T, b: T, message?: string) { - message ? TKUnit.assertEqual(a, b, message) : TKUnit.assertEqual(a, b); -} - -function closeEnough(a: number, b: number, message?: string) { - message ? TKUnit.assertTrue(Math.abs(a - b) <= EPS, message) : TKUnit.assertTrue(Math.abs(a - b) <= EPS); -} - -function notEqual(a: T, b: T, message?: string) { - message ? TKUnit.assertNotEqual(a, b, message) : TKUnit.assertNotEqual(a, b); -} - -function check(exp: boolean, message?: string) { - message ? TKUnit.assert(exp, message) : TKUnit.assert(exp); -} - -function widthEqual(view1: View, view2: View) { - equal(width(view1), width(view2), `Expected width of ${view1} to equal width of ${view2}.`); -} - -function heightEqual(view1: View, view2: View) { - equal(height(view1), height(view2), `Expected height of ${view1} to equal height of ${view2}.`); -} - -function isLeftAlignedWith(view1: View, view2: View) { - TKUnit.assertEqual(left(view1), left(view2), `${view1} is not left-aligned with ${view2}`); -} - -function isRightAlignedWith(view1: View, view2: View) { - TKUnit.assertEqual(right(view1), right(view2), `${view1} is not right-aligned with ${view2}`); -} - -function isTopAlignedWith(view1: View, view2: View) { - TKUnit.assertEqual(top(view1), top(view2), `${view1} is not top-aligned with ${view2}`); -} - -function isBottomAlignedWith(view1: View, view2: View) { - TKUnit.assertEqual(bottom(view1), bottom(view2), `${view1} is not bottom-aligned with ${view2}`); -} - -function isLeftOf(view1: View, view2: View) { - TKUnit.assert(right(view1) <= left(view2), `${view1}.right is not left of ${view2}.left`); -} - -function isRightOf(view1: View, view2: View) { - TKUnit.assert(left(view1) >= right(view2), `${view1}.left is not right of ${view2}.right`); -} - -function isBelow(view1: View, view2: View, distance?: number) { - TKUnit.assert(top(view1) >= bottom(view2), `${view1}.top is not below ${view2}.bottom`); -} - -function isAbove(view1: View, view2: View) { - TKUnit.assert(bottom(view1) <= top(view2), `${view1}.bottom is not above ${view2}.top`); -} - -function isLeftWith(view1: View, view2: View, distance: number) { - TKUnit.assertTrue(Math.abs(left(view1) + distance - left(view2)) <= EPS, `${view1}.left is not ${distance} of ${view2}.left`); -} - -function isRightWith(view1: View, view2: View, distance: number) { - TKUnit.assertTrue(Math.abs(right(view1) + distance - right(view2)) <= EPS, `${view1}.right is not ${distance} of ${view2}.right`); -} - -function isAboveWith(view1: View, view2: View, distance: number) { - TKUnit.assertTrue(Math.abs(bottom(view1) + distance - bottom(view2)) <= EPS, `${view1}.bottom is not ${distance} of ${view2}.bottom`); -} - -function isBelowWith(view1: View, view2: View, distance: number) { - TKUnit.assertTrue(Math.abs(top(view1) + distance - top(view2)) <= EPS, `${view1}.top is not ${distance} of ${view2}.top`); -} - export function testFlexboxPage() { let page = helper.navigateWithEntry({ moduleName: "ui/layouts/flexbox-layout-page" }); function view(id: string) { @@ -1259,8 +1169,8 @@ export const testAlignItems_flexEnd_parentPadding_column = test( }, ({ root, flexbox, text1, text2, text3 }) => { isBelow(text2, text1); - isRightWith(text1, flexbox,Length.toDevicePixels(flexbox.style.paddingRight, 0)); - isRightWith(text2, flexbox,Length.toDevicePixels(flexbox.style.paddingRight, 0)); + isRightWith(text1, flexbox, Length.toDevicePixels(flexbox.style.paddingRight, 0)); + isRightWith(text2, flexbox, Length.toDevicePixels(flexbox.style.paddingRight, 0)); } ); diff --git a/tests/app/ui/layouts/layout-tests-helper.ts b/tests/app/ui/layouts/layout-tests-helper.ts new file mode 100644 index 000000000..a3a1bd3bb --- /dev/null +++ b/tests/app/ui/layouts/layout-tests-helper.ts @@ -0,0 +1,98 @@ +import { View, Length } from "tns-core-modules/ui/core/view"; +import * as TKUnit from "../../TKUnit"; +import { layout } from "tns-core-modules/utils/utils"; + +import round = layout.round; +export const dipToDp = layout.toDevicePixels; + +const EPS = 1; + +export function left(view: View): number { return round(dipToDp(view.getLocationInWindow().x)); } +export function top(view: View): number { return round(dipToDp(view.getLocationInWindow().y)); } +export function right(view: View): number { return left(view) + width(view); } +export function bottom(view: View): number { return top(view) + height(view); } + +export function height(view: View): number { + return round(dipToDp(view.getActualSize().height)); +} + +export function width(view: View): number { + return round(dipToDp(view.getActualSize().width)); +} + +export function paddingLeft(view: View): number { return Length.toDevicePixels(view.style.paddingLeft, 0) + Length.toDevicePixels(view.parent.style.paddingLeft, 0) + (view.parent).getSafeAreaInsets().left; }; +export function paddingTop(view: View): number { return top(view) + Length.toDevicePixels(view.style.paddingTop, 0) }; +export function paddingRight(view: View): number { return right(view) - Length.toDevicePixels(view.style.paddingRight, 0) }; +export function paddingBottom(view: View): number { return bottom(view) - Length.toDevicePixels(view.style.paddingBottom, 0) }; + +export function equal(a: T, b: T, message?: string) { + message ? TKUnit.assertEqual(a, b, message) : TKUnit.assertEqual(a, b); +} + +export function closeEnough(a: number, b: number, message?: string) { + message ? TKUnit.assertTrue(Math.abs(a - b) <= EPS, message) : TKUnit.assertTrue(Math.abs(a - b) <= EPS); +} + +export function notEqual(a: T, b: T, message?: string) { + message ? TKUnit.assertNotEqual(a, b, message) : TKUnit.assertNotEqual(a, b); +} + +export function check(exp: boolean, message?: string) { + message ? TKUnit.assert(exp, message) : TKUnit.assert(exp); +} + +export function heightEqual(view1: View, view2: View) { + equal(height(view1), height(view2), `Expected height of ${view1} to equal height of ${view2}.`); +} + +export function widthEqual(view1: View, view2: View) { + equal(width(view1), width(view2), `Expected width of ${view1} to equal width of ${view2}.`); +} + +export function isLeftAlignedWith(view1: View, view2: View) { + TKUnit.assertEqual(left(view1), left(view2), `${view1} is not left-aligned with ${view2}`); +} + +export function isRightAlignedWith(view1: View, view2: View) { + TKUnit.assertEqual(right(view1), right(view2), `${view1} is not right-aligned with ${view2}`); +} + +export function isTopAlignedWith(view1: View, view2: View) { + TKUnit.assertEqual(top(view1), top(view2), `${view1} is not top-aligned with ${view2}`); +} + +export function isBottomAlignedWith(view1: View, view2: View) { + TKUnit.assertEqual(bottom(view1), bottom(view2), `${view1} is not bottom-aligned with ${view2}`); +} + +export function isLeftOf(view1: View, view2: View) { + TKUnit.assert(right(view1) <= left(view2), `${view1}.right is not left of ${view2}.left`); +} + +export function isAbove(view1: View, view2: View) { + TKUnit.assert(bottom(view1) <= top(view2), `${view1}.bottom is not above ${view2}.top`); +} + +export function isRightOf(view1: View, view2: View) { + TKUnit.assert(left(view1) >= right(view2), `${view1}.left is not right of ${view2}.right`); +} + +export function isBelow(view1: View, view2: View, distance?: number) { + TKUnit.assert(top(view1) >= bottom(view2), `${view1}.top is not below ${view2}.bottom`); +} + +export function isLeftWith(view1: View, view2: View, distance: number) { + TKUnit.assertTrue(Math.abs(left(view1) + distance - left(view2)) <= EPS, `${view1}.left is not ${distance} of ${view2}.left`); +} + +export function isRightWith(view1: View, view2: View, distance: number) { + TKUnit.assertTrue(Math.abs(right(view1) + distance - right(view2)) <= EPS, `${view1}.right is not ${distance} of ${view2}.right`); +} + +export function isAboveWith(view1: View, view2: View, distance: number) { + TKUnit.assertTrue(Math.abs(bottom(view1) + distance - bottom(view2)) <= EPS, `${view1}.bottom is not ${distance} of ${view2}.bottom`); +} + +export function isBelowWith(view1: View, view2: View, distance: number) { + TKUnit.assertTrue(Math.abs(top(view1) + distance - top(view2)) <= EPS, `${view1}.top is not ${distance} of ${view2}.top`); +}