import { GridLayout } from "tns-core-modules/ui/layouts/grid-layout"; import * as TKUnit from "../../TKUnit"; import * as view from "tns-core-modules/ui/core/view"; import * as testModule from "../../ui-test"; import * as platform from "tns-core-modules/platform"; import { ios as iosUtils } from "tns-core-modules/utils/utils"; import * as helper from "../helper"; import { parse } from "tns-core-modules/ui/builder"; import { dipToDp, left, top, right, bottom, height, width, equal, closeEnough, lessOrCloseEnough, check, isLeftAlignedWith, isRightAlignedWith, isTopAlignedWith, isBottomAlignedWith, isLeftWith, isAboveWith, isRightWith, isBelowWith } from "./layout-tests-helper"; export class SafeAreaTests extends testModule.UITest { public create(): any { return null; } private executeSnippet(ui: U, setup: (ui: U) => void, test: (ui: U) => void, pageOptions?: helper.PageOptions): void { function waitUntilTestElementLayoutIsValid(view: view.View, timeoutSec?: number): void { TKUnit.waitUntilReady(() => { return view.isLayoutValid; }, timeoutSec || 1); } setup(ui); helper.buildUIAndRunTest(ui.root, () => { waitUntilTestElementLayoutIsValid(ui.root); test(ui); }, pageOptions); }; private noop() { // no operation }; // Common private getViews(template: string) { let root = parse(template); return { root, child0: root.getViewById("child0") as view.View, child1: root.getViewById("child1") as view.View, child2: root.getViewById("child2") as view.View }; }; private layout_insets_top_action_bar_test(layout: view.View) { const app = iosUtils.getter(UIApplication, UIApplication.sharedApplication); const statusBarHeight = round(dipToDp(app.statusBarFrame.size.height)); const actionBarHeight = round(dipToDp(layout.page.actionBar.nativeViewProtected.frame.size.height)); const topInset = statusBarHeight + actionBarHeight; const insets = layout.getSafeAreaInsets(); equal(insets.top, topInset, `${layout}.topInset - actual:${insets.top}; expected: ${topInset}`) } private layout_insets_top_action_bar_hidden_test(layout: view.View) { const app = iosUtils.getter(UIApplication, UIApplication.sharedApplication); const statusBarHeight = round(dipToDp(app.statusBarFrame.size.height)); const topInset = statusBarHeight; const insets = layout.getSafeAreaInsets(); equal(insets.top, topInset, `${layout}.topInset - actual:${insets.top}; expected: ${topInset}`) } private layout_insets_top_action_bar_flat_test(layout: view.View) { const insets = layout.getSafeAreaInsets(); equal(insets.top, 0, `${layout}.topInset - actual:${insets.top}; expected: ${0}`) } private layout_in_full_screen_test(layout: view.View, pageOptions?: helper.PageOptions) { let expectedTop = 0; if (pageOptions && (pageOptions.actionBarFlat)) { const app = iosUtils.getter(UIApplication, UIApplication.sharedApplication); const statusBarHeight = round(dipToDp(app.statusBarFrame.size.height)); const actionBarHeight = round(dipToDp(layout.page.actionBar.nativeViewProtected.frame.size.height)); expectedTop = statusBarHeight + actionBarHeight; } const l = left(layout); const t = top(layout); const r = right(layout); const b = bottom(layout); equal(l, 0, `${layout}.left - actual:${l}; expected: ${0}`); equal(t, expectedTop, `${layout}.top - actual:${t}; expected: ${expectedTop}`); equal(r, platform.screen.mainScreen.widthPixels, `${layout}.right - actual:${r}; expected: ${platform.screen.mainScreen.widthPixels}`); equal(b, platform.screen.mainScreen.heightPixels, `${layout}.bottom - actual:${b}; expected: ${platform.screen.mainScreen.heightPixels}`); } // Absolute private absolute_in_full_screen(pageOptions?: helper.PageOptions) { const snippet = ` `; this.executeSnippet( this.getViews(snippet), this.noop, ({ root }) => { this.layout_in_full_screen_test(root, pageOptions); }, pageOptions ); } public test_absolute_in_full_screen_action_bar() { this.absolute_in_full_screen({ actionBar: true }); } public test_absolute_in_full_screen_action_bar_hidden() { this.absolute_in_full_screen({ actionBarHidden: true }); } public test_absolute_in_full_screen_action_bar_flat() { this.absolute_in_full_screen({ actionBarFlat: true }); } public test_absolute_in_full_screen_tab_bar() { this.absolute_in_full_screen({ tabBar: true }); } public test_absolute_in_full_screen_tab_bar_action_bar() { this.absolute_in_full_screen({ actionBar: true, tabBar: true }); } public test_absolute_insets_top_action_bar() { const snippet = ` `; this.executeSnippet( this.getViews(snippet), this.noop, ({ root }) => { this.layout_insets_top_action_bar_test(root); }, { actionBar: true } ); } public test_absolute_insets_top_action_bar_hidden() { const snippet = ` `; this.executeSnippet( this.getViews(snippet), this.noop, ({ root }) => { this.layout_insets_top_action_bar_hidden_test(root); }, { actionBarHidden: true } ); } public test_absolute_insets_top_action_bar_flat() { const snippet = ` `; this.executeSnippet( this.getViews(snippet), this.noop, ({ root }) => { this.layout_insets_top_action_bar_flat_test(root); }, { actionBarFlat: true } ); } public test_absolute_insets_top_tab_bar_flat() { const snippet = ` `; this.executeSnippet( this.getViews(snippet), this.noop, ({ root }) => { this.layout_insets_top_action_bar_hidden_test(root); }, { tabBar: true } ); } public test_absolute_insets_top_tab_bar_action_bar_flat() { const snippet = ` `; this.executeSnippet( this.getViews(snippet), this.noop, ({ root }) => { this.layout_insets_top_action_bar_test(root); }, { actionBar: true, tabBar: true } ); } private absolute_children_components_in_safe_area(pageOptions?: helper.PageOptions) { const snippet = ` `; this.executeSnippet( this.getViews(snippet), this.noop, ({ root, child0, child1 }) => { const insets = root.getSafeAreaInsets(); equal(left(child0), insets.left, `${child0}.left - actual: ${left(child0)} expected: ${insets.left}`); equal(top(child0), insets.top, `${child0}.top - actual: ${top(child0)} expected: ${insets.top}`); equal(right(child0), width(root) - insets.right, `${child0}.right - actual: ${right(child0)} expected: ${width(root) - insets.right}`); equal(bottom(child0), height(root) - insets.bottom, `${child0}.bottom - actual: ${bottom(child0)} expected: ${height(root) - insets.bottom}`); equal(height(child1), 0, `${child1} has been laid out, but should not`); equal(width(child1), 0, `${child1} has been laid out, but should not`); }, pageOptions ); } public test_wrap_horizontal_children_components_in_safe_area_action_bar() { this.wrap_horizontal_children_components_in_safe_area({ actionBar: true }); } public test_wrap_horizontal_children_components_in_safe_area_action_bar_hidden() { this.wrap_horizontal_children_components_in_safe_area({ actionBarHidden: true }); } public test_wrap_horizontal_children_components_in_safe_area_tab_bar() { this.wrap_horizontal_children_components_in_safe_area({ tabBar: true }); } private wrap_vertical_children_components_in_safe_area(pageOptions?: helper.PageOptions) { const snippet = ` `; this.executeSnippet( this.getViews(snippet), this.noop, ({ root, child0, child1 }) => { const insets = root.getSafeAreaInsets(); equal(left(child0), insets.left, `${child0}.left - actual: ${left(child0)} expected: ${insets.left}`); equal(top(child0), insets.top, `${child0}.top - actual: ${top(child0)} expected: ${insets.top}`); equal(right(child0), width(root) - insets.right, `${child0}.right - actual: ${right(child0)} expected: ${width(root) - insets.right}`); equal(bottom(child0), height(root) - insets.bottom, `${child0}.bottom - actual: ${bottom(child0)} expected: ${height(root) - insets.bottom}`); equal(height(child1), 0, `${child1} has been laid out, but should not`); equal(width(child1), 0, `${child1} has been laid out, but should not`); }, pageOptions ); } public test_wrap_vertical_children_components_in_safe_area_action_bar() { this.wrap_vertical_children_components_in_safe_area({ actionBar: true }); } public test_wrap_vertical_children_components_in_safe_area_action_bar_hidden() { this.wrap_vertical_children_components_in_safe_area({ actionBarHidden: true }); } public test_wrap_vertical_children_components_in_safe_area_tab_bar() { this.wrap_vertical_children_components_in_safe_area({ tabBar: true }); } private wrap_nested_layouts_beyond_safe_area(pageOptions?: helper.PageOptions) { const snippet = `