Files
NativeScript/tests/app/ui/view/view-tests.android.ts
Alexander Vakrilov cc97a16800 feat: Scoped Packages (#7911)
* chore: move tns-core-modules to nativescript-core

* chore: preparing compat generate script

* chore: add missing definitions

* chore: no need for http-request to be private

* chore: packages chore

* test: generate tests for tns-core-modules

* chore: add anroid module for consistency

* chore: add .npmignore

* chore: added privateModulesWhitelist

* chore(webpack): added bundle-entry-points

* chore: scripts

* chore: tests changed to use @ns/core

* test: add scoped-packages test project

* test: fix types

* test: update test project

* chore: build scripts

* chore: update build script

* chore: npm scripts cleanup

* chore: make the compat pgk work with old wp config

* test: generate diff friendly tests

* chore: create barrel exports

* chore: move files after rebase

* chore: typedoc config

* chore: compat mode

* chore: review of barrels

* chore: remove tns-core-modules import after rebase

* chore: dev workflow setup

* chore: update developer-workflow

* docs: experiment with API extractor

* chore: api-extractor and barrel exports

* chore: api-extractor configs

* chore: generate d.ts rollup with api-extractor

* refactor: move methods inside Frame

* chore: fic tests to use Frame static methods

* refactor: create Builder class

* refactor: use Builder class in tests

* refactor: include Style in ui barrel

* chore: separate compat build script

* chore: fix tslint errors

* chore: update NATIVESCRIPT_CORE_ARGS

* chore: fix compat pack

* chore: fix ui-test-app build with linked modules

* chore: Application, ApplicationSettings, Connectivity and Http

* chore: export Trace, Profiling and Utils

* refactor: Static create methods for ImageSource

* chore: fix deprecated usages of ImageSource

* chore: move Span and FormattedString to ui

* chore: add events-args and ImageSource to index files

* chore: check for CLI >= 6.2 when building for IOS

* chore: update travis build

* chore: copy Pod file to compat package

* chore: update error msg ui-tests-app

* refactor: Apply suggestions from code review

Co-Authored-By: Martin Yankov <m.i.yankov@gmail.com>

* chore: typings and refs

* chore: add missing d.ts files for public API

* chore: adress code review FB

* chore: update api-report

* chore: dev-workflow for other apps

* chore: api update

* chore: update api-report
2019-10-17 00:45:33 +03:00

299 lines
11 KiB
TypeScript

import * as TKUnit from "../../tk-unit";
import * as helper from "../../ui-helper";
import { View, isIOS, unsetValue } from "@nativescript/core/ui/core/view";
import { Button } from "@nativescript/core/ui/button";
import * as types from "@nativescript/core/utils/types";
import { StackLayout } from "@nativescript/core/ui/layouts/stack-layout";
import { Label } from "@nativescript/core/ui/label";
import * as trace from "@nativescript/core/trace";
import { Color } from "@nativescript/core/color";
// enable the trace, it is disabled by default
trace.enable();
export * from "./view-tests-common";
// function setup(): StackLayout {
// const page = helper.getClearCurrentPage();
// const stack = new StackLayout();
// page.content = stack;
// return stack;
// }
// export function test_recycle_native_view_never() {
// const stack = setup();
// const btn = new Button();
// btn.recycleNativeView = 'never';
// stack.addChild(btn);
// TKUnit.assertNotNull(btn.nativeViewProtected);
// const oldNativeView = btn.nativeViewProtected;
// stack.removeChild(btn);
// stack.addChild(btn);
// const newNativeView = btn.nativeViewProtected;
// TKUnit.assertNotEqual(oldNativeView, newNativeView);
// }
// export function test_recycle_native_view_always() {
// const stack = setup();
// const btn = new Button();
// btn.recycleNativeView = 'always';
// stack.addChild(btn);
// TKUnit.assertNotNull(btn.nativeViewProtected);
// const oldNativeView = btn.nativeViewProtected;
// stack.removeChild(btn);
// stack.addChild(btn);
// const newNativeView = btn.nativeViewProtected;
// TKUnit.assertEqual(oldNativeView, newNativeView);
// }
// export function test_recycle_native_view_auto_access_nativeView() {
// const stack = setup();
// const btn = new Button();
// btn.recycleNativeView = 'auto';
// stack.addChild(btn);
// TKUnit.assertNotNull(btn.nativeView);
// const oldNativeView = btn.nativeViewProtected;
// stack.removeChild(btn);
// stack.addChild(btn);
// const newNativeView = btn.nativeViewProtected;
// TKUnit.assertNotEqual(oldNativeView, newNativeView);
// }
// export function test_recycle_native_view_auto_access_android() {
// const stack = setup();
// const btn = new Button();
// btn.recycleNativeView = 'auto';
// stack.addChild(btn);
// TKUnit.assertNotNull(btn.android);
// const oldNativeView = btn.nativeViewProtected;
// stack.removeChild(btn);
// stack.addChild(btn);
// const newNativeView = btn.nativeViewProtected;
// TKUnit.assertNotEqual(oldNativeView, newNativeView);
// }
// export function test_recycle_property_counter_few_properties() {
// const stack = setup();
// const btn = new Button();
// btn.text = "text";
// btn.recycleNativeView = 'auto';
// stack.addChild(btn);
// TKUnit.assertNotNull(btn.nativeViewProtected);
// const oldNativeView = btn.nativeViewProtected;
// stack.removeChild(btn);
// stack.addChild(btn);
// const newNativeView = btn.nativeViewProtected;
// TKUnit.assertEqual(oldNativeView, newNativeView);
// }
// export function test_recycle_property_counter_more_properties() {
// const stack = setup();
// const btn = new Button();
// btn.recyclePropertyCounter = 1;
// btn.recycleNativeView = 'auto';
// btn.text = "text";
// btn.style.margin = "20";
// stack.addChild(btn);
// TKUnit.assertNotNull(btn.nativeViewProtected);
// const oldNativeView = btn.nativeViewProtected;
// stack.removeChild(btn);
// stack.addChild(btn);
// const newNativeView = btn.nativeViewProtected;
// TKUnit.assertNotEqual(oldNativeView, newNativeView);
// }
export function test_event_setupUI_IsRaised() {
const test = function (views: Array<View>) {
views.forEach(v => {
TKUnit.assertNotNull(v.nativeViewProtected, "_setupUI should initialize set nativeView");
});
};
helper.do_PageTest_WithStackLayout_AndButton(test);
}
export function test_event_setupUI_IsRaised_WhenAttached_Dynamically() {
const test = function (views: Array<View>) {
// add new button to the visual tree and ensure its _setupUI event
const btn = new Button();
(<StackLayout>views[1]).addChild(btn);
TKUnit.assertNotNull(btn.nativeViewProtected, "_setupUI should initialize nativeViewProtected");
};
helper.do_PageTest_WithStackLayout_AndButton(test);
}
export function test_event_onContextChanged_IsRaised_WhenAttached() {
const test = function (views: Array<View>) {
views.forEach(v => {
TKUnit.assertNotNull(v._context, "_setupUI should initialize _context");
});
};
helper.do_PageTest_WithStackLayout_AndButton(test);
}
export function test_event_onContextChanged_IsRaised_WhenAttached_Dynamically() {
const test = function (views: Array<View>) {
// add new button to the visual tree and ensure its _onContextChanged event
const btn = new Button();
(<StackLayout>views[1]).addChild(btn);
TKUnit.assertNotNull(btn._context, "_setupUI should initialize _context");
};
helper.do_PageTest_WithStackLayout_AndButton(test);
}
export function test_event_tearDownUI_IsRaised() {
let btn;
let stack;
const test = function (views: Array<View>) {
stack = views[1];
btn = views[1];
// once the above method completes goBack on the current frame is called which will detach the tested views
};
helper.do_PageTest_WithStackLayout_AndButton(test);
// _tearDownUI event is propagated to nested children first
TKUnit.assertNull(stack.nativeViewProtected, "_tearDownUI should clear nativeViewProtected");
TKUnit.assertNull(btn.nativeViewProtected, "_tearDownUI should clear nativeViewProtected");
}
export function test_event_tearDownUI_IsRaised_WhenRemoved_Dynamically() {
const test = function (views: Array<View>) {
// add new button to the visual tree and ensure its _tearDownUI event
// remove the button from the layout
const v = views[2];
(<StackLayout>views[1]).removeChild(v);
TKUnit.assertNull(v.nativeViewProtected, "_tearDownUI should clear nativeViewProtected");
};
helper.do_PageTest_WithStackLayout_AndButton(test);
}
export function test_events_tearDownUIAndRemovedFromNativeVisualTree_AreRaised_WhenNavigateBack() {
const test = function (views: Array<View>) {
const page = views[0].page;
const stack = views[1];
const btn = views[2];
const nativeBtn = btn.nativeViewProtected;
const nativeStack = stack.nativeViewProtected;
page.content = null;
// 2 tearDownUI calls: stack, button
TKUnit.assertNull(btn.nativeViewProtected);
TKUnit.assertNull(stack.nativeViewProtected);
TKUnit.assertNull(btn._context);
TKUnit.assertNull(stack._context);
const btnNativeParent = isIOS ? nativeBtn.superview : nativeBtn.getParent();
const stackNativeParent = isIOS ? nativeStack.superview : nativeStack.getParent();
TKUnit.assertNull(btnNativeParent, "button NativeParent should be null");
TKUnit.assertNull(stackNativeParent, "stack NativeParent should be null");
};
helper.do_PageTest_WithStackLayout_AndButton(test);
}
export function test_cachedProperties_Applied_WhenNativeWidged_IsCreated() {
const test = function (views: Array<View>) {
const newButton = new Button();
newButton.text = "Test Button";
TKUnit.assertNull(newButton.android);
(<StackLayout>views[1]).addChild(newButton);
TKUnit.assertTrue(!!newButton.android);
// TODO: There is currently an issue with the getText conversion to JavaScript string
TKUnit.assertEqual(newButton.android.getText(), "Test Button");
};
helper.do_PageTest_WithStackLayout_AndButton(test);
}
export function test_automation_text_set_to_native() {
const test = function (views: Array<View>) {
const newButton = new Button();
newButton.automationText = "Button1";
(<StackLayout>views[1]).addChild(newButton);
TKUnit.assertEqual((<android.widget.Button>newButton.android).getContentDescription(), "Button1", "contentDescription not set to native ");
};
helper.do_PageTest_WithStackLayout_AndButton(test);
}
export const test_StylePropertiesDefaultValuesCache = function () {
const testValue = 35;
const test = function (views: [View, StackLayout, Button, View]) {
const testLabel = new Label();
const testButton = new Button();
const stack = views[1];
stack.addChild(testLabel);
stack.addChild(testButton);
const defaultLabelFontSize = (<android.widget.TextView>(testLabel.android)).getTextSize();
const defaultButtonFontSize = (<android.widget.Button>(testButton.android)).getTextSize();
testLabel.style.fontSize = testValue;
testButton.style.fontSize = testValue;
let actualLabelTextSize = (<android.widget.TextView>(testLabel.android)).getTextSize();
let actualButtonTextSize = (<android.widget.Button>(testButton.android)).getTextSize();
TKUnit.assert(actualLabelTextSize !== defaultLabelFontSize, "Label text size should be different from default!");
TKUnit.assert(actualButtonTextSize !== defaultButtonFontSize, "Button text size should be different from default!");
testLabel.style.fontSize = unsetValue;
testButton.style.fontSize = unsetValue;
actualLabelTextSize = (<android.widget.TextView>(testLabel.android)).getTextSize();
actualButtonTextSize = (<android.widget.Button>(testButton.android)).getTextSize();
TKUnit.assertEqual(actualLabelTextSize, defaultLabelFontSize, "Label text size should be default!");
TKUnit.assertEqual(actualButtonTextSize, defaultButtonFontSize, "Button text size should be default!");
};
helper.do_PageTest_WithStackLayout_AndButton(test);
};
export function getUniformNativeBorderWidth(v: View): number {
const bkg = <org.nativescript.widgets.BorderDrawable>v.android.getBackground();
return bkg ? bkg.getUniformBorderWidth() : 0;
}
export function checkUniformNativeBorderColor(v: View): boolean {
const bkg = <org.nativescript.widgets.BorderDrawable>(<android.view.View>v.android).getBackground();
return bkg && bkg.getUniformBorderColor() === (<Color>v.borderColor).android;
}
export function getUniformNativeCornerRadius(v: View): number {
const bkg = <org.nativescript.widgets.BorderDrawable>v.android.getBackground();
return bkg ? bkg.getUniformBorderRadius() : 0;
}
export function checkNativeBackgroundColor(v: View): boolean {
const bkg = <org.nativescript.widgets.BorderDrawable>(<android.view.View>v.android).getBackground();
return v.backgroundColor && bkg && bkg.getBackgroundColor() === (<Color>v.backgroundColor).android;
}
export function checkNativeBackgroundImage(v: View): boolean {
const bkg = <org.nativescript.widgets.BorderDrawable>(<android.view.View>v.android).getBackground();
return bkg && !types.isNullOrUndefined(bkg.getBackgroundImage());
}