mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
test: standardized jest unit testing (#10047)
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
import * as types from './types';
|
||||
import { dispatchToMainThread, dispatchToUIThread, isMainThread } from './mainthread-helper';
|
||||
import { sanitizeModuleName } from '../ui/builder/module-name-sanitizer';
|
||||
import * as layout from './layout-helper';
|
||||
|
||||
import { GC } from './index';
|
||||
|
||||
export { layout };
|
||||
export * from './mainthread-helper';
|
||||
export * from './macrotask-scheduler';
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { FileSystemAccess } from '../file-system/file-system-access';
|
||||
import { Trace } from '../trace';
|
||||
|
||||
export { ad, dataDeserialize, dataSerialize, iOSNativeHelper } from './native-helper';
|
||||
export * from './layout-helper';
|
||||
export * from './common';
|
||||
export { Source } from './debug';
|
||||
|
||||
|
||||
83
packages/core/utils/index.d.ts
vendored
83
packages/core/utils/index.d.ts
vendored
@@ -4,7 +4,9 @@ export * from './mainthread-helper';
|
||||
export * from './macrotask-scheduler';
|
||||
export { Source } from './debug';
|
||||
|
||||
export * from './layout-helper';
|
||||
export * from './native-helper';
|
||||
export * from './common';
|
||||
|
||||
export const RESOURCE_PREFIX: string;
|
||||
export const FILE_PREFIX: string;
|
||||
@@ -21,87 +23,6 @@ interface Owned {
|
||||
}
|
||||
//@endprivate
|
||||
|
||||
/**
|
||||
* Utility module related to layout.
|
||||
*/
|
||||
export namespace layout {
|
||||
/**
|
||||
* Bits that provide the actual measured size.
|
||||
*/
|
||||
export const MEASURED_HEIGHT_STATE_SHIFT: number;
|
||||
export const MEASURED_SIZE_MASK: number;
|
||||
export const MEASURED_STATE_MASK: number;
|
||||
export const MEASURED_STATE_TOO_SMALL: number;
|
||||
export const UNSPECIFIED: number;
|
||||
export const EXACTLY: number;
|
||||
export const AT_MOST: number;
|
||||
|
||||
/**
|
||||
* Gets layout mode from a given specification as string.
|
||||
* @param mode - The measure specification mode.
|
||||
*/
|
||||
export function getMode(mode: number): string;
|
||||
|
||||
/**
|
||||
* Gets measure specification mode from a given specification.
|
||||
* @param spec - The measure specification.
|
||||
*/
|
||||
export function getMeasureSpecMode(spec: number): number;
|
||||
|
||||
/**
|
||||
* Gets measure specification size from a given specification.
|
||||
* @param spec - The measure specification.
|
||||
*/
|
||||
export function getMeasureSpecSize(spec: number): number;
|
||||
|
||||
/**
|
||||
* Creates measure specification size from size and mode.
|
||||
* @param size - The size component of measure specification.
|
||||
* @param mode - The mode component of measure specification.
|
||||
*/
|
||||
export function makeMeasureSpec(px: number, mode: number): number;
|
||||
|
||||
/**
|
||||
* Gets display density for the current device.
|
||||
*/
|
||||
export function getDisplayDensity(): number;
|
||||
|
||||
/**
|
||||
* Convert device independent pixels to device pixels - dip to px.
|
||||
* @param value - The pixel to convert.
|
||||
*/
|
||||
export function toDevicePixels(value: CoreTypes.dip): CoreTypes.px;
|
||||
|
||||
/**
|
||||
* Convert device pixels to device independent pixels - px to dip.
|
||||
* @param value - The pixel to convert.
|
||||
*/
|
||||
export function toDeviceIndependentPixels(value: CoreTypes.px): CoreTypes.dip;
|
||||
|
||||
/**
|
||||
* Rounds value used in layout.
|
||||
* @param px to round.
|
||||
*/
|
||||
export function round(px: CoreTypes.px): CoreTypes.px;
|
||||
|
||||
/**
|
||||
* Converts device pixels to device independent pixes and measure the nativeView.
|
||||
* Returns the desired size of the nativeView in device pixels.
|
||||
* @param nativeView the nativeView to measure (UIView or android.view.View)
|
||||
* @param width the available width
|
||||
* @param widthMode width mode - UNSPECIFIED, EXACTLY or AT_MOST
|
||||
* @param height the available hegiht
|
||||
* @param heightMode height mode - UNSPECIFIED, EXACTLY or AT_MOST
|
||||
*/
|
||||
export function measureNativeView(nativeView: any /* UIView or android.view.View */, width: number, widthMode: number, height: number, heightMode: number): { width: number; height: number };
|
||||
|
||||
/**
|
||||
* Prints user friendly version of the measureSpec.
|
||||
* @param measureSpec the spec to print
|
||||
*/
|
||||
export function measureSpecToString(measureSpec: number): string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Module with android specific utilities.
|
||||
*/
|
||||
|
||||
@@ -2,6 +2,7 @@ import { iOSNativeHelper } from './native-helper';
|
||||
import { Trace } from '../trace';
|
||||
|
||||
export { dataDeserialize, dataSerialize, iOSNativeHelper } from './native-helper';
|
||||
export * from './layout-helper';
|
||||
export * from './common';
|
||||
export { Source } from './debug';
|
||||
|
||||
|
||||
@@ -1,49 +1,85 @@
|
||||
import { MODE_MASK } from './layout-helper-common';
|
||||
import * as layoutCommon from './layout-helper-common';
|
||||
import { ad } from '../native-helper';
|
||||
|
||||
export * from './layout-helper-common';
|
||||
// export * from './layout-helper-common';
|
||||
|
||||
let density: number;
|
||||
|
||||
let sdkVersion: number;
|
||||
let useOldMeasureSpec = false;
|
||||
|
||||
export function makeMeasureSpec(size: number, mode: number): number {
|
||||
if (sdkVersion === undefined) {
|
||||
// check whether the old layout is needed
|
||||
sdkVersion = ad.getApplicationContext().getApplicationInfo().targetSdkVersion;
|
||||
useOldMeasureSpec = sdkVersion <= 17;
|
||||
export namespace layout {
|
||||
// cache the MeasureSpec constants here, to prevent extensive marshaling calls to and from Java
|
||||
// TODO: While this boosts the performance it is error-prone in case Google changes these constants
|
||||
export const MODE_SHIFT = 30;
|
||||
export const MODE_MASK = 0x3 << MODE_SHIFT;
|
||||
|
||||
export const UNSPECIFIED = 0 << MODE_SHIFT;
|
||||
export const EXACTLY = 1 << MODE_SHIFT;
|
||||
export const AT_MOST = 2 << MODE_SHIFT;
|
||||
|
||||
export const MEASURED_HEIGHT_STATE_SHIFT = 0x00000010; /* 16 */
|
||||
export const MEASURED_STATE_TOO_SMALL = 0x01000000;
|
||||
export const MEASURED_STATE_MASK = 0xff000000;
|
||||
export const MEASURED_SIZE_MASK = 0x00ffffff;
|
||||
|
||||
export function getMode(mode: number) {
|
||||
return layoutCommon.getMode(mode);
|
||||
}
|
||||
|
||||
if (useOldMeasureSpec) {
|
||||
return size + mode;
|
||||
export function getMeasureSpecMode(spec: number): number {
|
||||
return layoutCommon.getMeasureSpecMode(spec);
|
||||
}
|
||||
|
||||
return (size & ~MODE_MASK) | (mode & MODE_MASK);
|
||||
}
|
||||
|
||||
export function getDisplayDensity(): number {
|
||||
if (density === undefined) {
|
||||
density = ad.getResources().getDisplayMetrics().density;
|
||||
export function getMeasureSpecSize(spec: number) {
|
||||
return layoutCommon.getMeasureSpecSize(spec);
|
||||
}
|
||||
|
||||
return density;
|
||||
}
|
||||
export function makeMeasureSpec(size: number, mode: number): number {
|
||||
if (sdkVersion === undefined) {
|
||||
// check whether the old layout is needed
|
||||
sdkVersion = ad.getApplicationContext().getApplicationInfo().targetSdkVersion;
|
||||
useOldMeasureSpec = sdkVersion <= 17;
|
||||
}
|
||||
|
||||
export function toDevicePixels(value: number): number {
|
||||
return value * getDisplayDensity();
|
||||
}
|
||||
if (useOldMeasureSpec) {
|
||||
return size + mode;
|
||||
}
|
||||
|
||||
export function toDeviceIndependentPixels(value: number): number {
|
||||
return value / getDisplayDensity();
|
||||
}
|
||||
return (size & ~layoutCommon.MODE_MASK) | (mode & layoutCommon.MODE_MASK);
|
||||
}
|
||||
|
||||
export function measureNativeView(nativeView: any /* android.view.View */, width: number, widthMode: number, height: number, heightMode: number): { width: number; height: number } {
|
||||
const view = <android.view.View>nativeView;
|
||||
view.measure(makeMeasureSpec(width, widthMode), makeMeasureSpec(height, heightMode));
|
||||
export function getDisplayDensity(): number {
|
||||
if (density === undefined) {
|
||||
density = ad.getResources().getDisplayMetrics().density;
|
||||
}
|
||||
|
||||
return {
|
||||
width: view.getMeasuredWidth(),
|
||||
height: view.getMeasuredHeight(),
|
||||
};
|
||||
return density;
|
||||
}
|
||||
|
||||
export function toDevicePixels(value: number): number {
|
||||
return value * getDisplayDensity();
|
||||
}
|
||||
|
||||
export function toDeviceIndependentPixels(value: number): number {
|
||||
return value / getDisplayDensity();
|
||||
}
|
||||
|
||||
export function round(value: number) {
|
||||
return layoutCommon.round(value);
|
||||
}
|
||||
|
||||
export function measureNativeView(nativeView: any /* android.view.View */, width: number, widthMode: number, height: number, heightMode: number): { width: number; height: number } {
|
||||
const view = <android.view.View>nativeView;
|
||||
view.measure(makeMeasureSpec(width, widthMode), makeMeasureSpec(height, heightMode));
|
||||
|
||||
return {
|
||||
width: view.getMeasuredWidth(),
|
||||
height: view.getMeasuredHeight(),
|
||||
};
|
||||
}
|
||||
|
||||
export function measureSpecToString(measureSpec: number) {
|
||||
return layoutCommon.measureSpecToString(measureSpec);
|
||||
}
|
||||
}
|
||||
|
||||
131
packages/core/utils/layout-helper/index.d.ts
vendored
131
packages/core/utils/layout-helper/index.d.ts
vendored
@@ -1,77 +1,82 @@
|
||||
import { CoreTypes } from '../../core-types';
|
||||
|
||||
/**
|
||||
* Bits that provide the actual measured size.
|
||||
* Utility module related to layout.
|
||||
*/
|
||||
export const MEASURED_HEIGHT_STATE_SHIFT: number;
|
||||
export const MEASURED_SIZE_MASK: number;
|
||||
export const MEASURED_STATE_MASK: number;
|
||||
export const MEASURED_STATE_TOO_SMALL: number;
|
||||
export const UNSPECIFIED: number;
|
||||
export const EXACTLY: number;
|
||||
export const AT_MOST: number;
|
||||
export namespace layout {
|
||||
/**
|
||||
* Bits that provide the actual measured size.
|
||||
*/
|
||||
export const MEASURED_HEIGHT_STATE_SHIFT: number;
|
||||
export const MEASURED_SIZE_MASK: number;
|
||||
export const MEASURED_STATE_MASK: number;
|
||||
export const MEASURED_STATE_TOO_SMALL: number;
|
||||
export const UNSPECIFIED: number;
|
||||
export const EXACTLY: number;
|
||||
export const AT_MOST: number;
|
||||
|
||||
/**
|
||||
* Gets layout mode from a given specification as string.
|
||||
* @param mode - The measure specification mode.
|
||||
*/
|
||||
export function getMode(mode: number): string;
|
||||
/**
|
||||
* Gets layout mode from a given specification as string.
|
||||
* @param mode - The measure specification mode.
|
||||
*/
|
||||
export function getMode(mode: number): string;
|
||||
|
||||
/**
|
||||
* Gets measure specification mode from a given specification.
|
||||
* @param spec - The measure specification.
|
||||
*/
|
||||
export function getMeasureSpecMode(spec: number): number;
|
||||
/**
|
||||
* Gets measure specification mode from a given specification.
|
||||
* @param spec - The measure specification.
|
||||
*/
|
||||
export function getMeasureSpecMode(spec: number): number;
|
||||
|
||||
/**
|
||||
* Gets measure specification size from a given specification.
|
||||
* @param spec - The measure specification.
|
||||
*/
|
||||
export function getMeasureSpecSize(spec: number): number;
|
||||
/**
|
||||
* Gets measure specification size from a given specification.
|
||||
* @param spec - The measure specification.
|
||||
*/
|
||||
export function getMeasureSpecSize(spec: number): number;
|
||||
|
||||
/**
|
||||
* Creates measure specification size from size and mode.
|
||||
* @param size - The size component of measure specification.
|
||||
* @param mode - The mode component of measure specification.
|
||||
*/
|
||||
export function makeMeasureSpec(px: number, mode: number): number;
|
||||
/**
|
||||
* Creates measure specification size from size and mode.
|
||||
* @param size - The size component of measure specification.
|
||||
* @param mode - The mode component of measure specification.
|
||||
*/
|
||||
export function makeMeasureSpec(px: number, mode: number): number;
|
||||
|
||||
/**
|
||||
* Gets display density for the current device.
|
||||
*/
|
||||
export function getDisplayDensity(): number;
|
||||
/**
|
||||
* Gets display density for the current device.
|
||||
*/
|
||||
export function getDisplayDensity(): number;
|
||||
|
||||
/**
|
||||
* Convert device independent pixels to device pixels - dip to px.
|
||||
* @param value - The pixel to convert.
|
||||
*/
|
||||
export function toDevicePixels(value: CoreTypes.dip): CoreTypes.px;
|
||||
/**
|
||||
* Convert device independent pixels to device pixels - dip to px.
|
||||
* @param value - The pixel to convert.
|
||||
*/
|
||||
export function toDevicePixels(value: CoreTypes.dip): CoreTypes.px;
|
||||
|
||||
/**
|
||||
* Convert device pixels to device independent pixels - px to dip.
|
||||
* @param value - The pixel to convert.
|
||||
*/
|
||||
export function toDeviceIndependentPixels(value: CoreTypes.px): CoreTypes.dip;
|
||||
/**
|
||||
* Convert device pixels to device independent pixels - px to dip.
|
||||
* @param value - The pixel to convert.
|
||||
*/
|
||||
export function toDeviceIndependentPixels(value: CoreTypes.px): CoreTypes.dip;
|
||||
|
||||
/**
|
||||
* Rounds value used in layout.
|
||||
* @param px to round.
|
||||
*/
|
||||
export function round(px: CoreTypes.px): CoreTypes.px;
|
||||
/**
|
||||
* Rounds value used in layout.
|
||||
* @param px to round.
|
||||
*/
|
||||
export function round(px: CoreTypes.px): CoreTypes.px;
|
||||
|
||||
/**
|
||||
* Converts device pixels to device independent pixes and measure the nativeView.
|
||||
* Returns the desired size of the nativeView in device pixels.
|
||||
* @param nativeView the nativeView to measure (UIView or android.view.View)
|
||||
* @param width the available width
|
||||
* @param widthMode width mode - UNSPECIFIED, EXACTLY or AT_MOST
|
||||
* @param height the available hegiht
|
||||
* @param heightMode height mode - UNSPECIFIED, EXACTLY or AT_MOST
|
||||
*/
|
||||
export function measureNativeView(nativeView: any /* UIView or android.view.View */, width: number, widthMode: number, height: number, heightMode: number): { width: number; height: number };
|
||||
/**
|
||||
* Converts device pixels to device independent pixes and measure the nativeView.
|
||||
* Returns the desired size of the nativeView in device pixels.
|
||||
* @param nativeView the nativeView to measure (UIView or android.view.View)
|
||||
* @param width the available width
|
||||
* @param widthMode width mode - UNSPECIFIED, EXACTLY or AT_MOST
|
||||
* @param height the available hegiht
|
||||
* @param heightMode height mode - UNSPECIFIED, EXACTLY or AT_MOST
|
||||
*/
|
||||
export function measureNativeView(nativeView: any /* UIView or android.view.View */, width: number, widthMode: number, height: number, heightMode: number): { width: number; height: number };
|
||||
|
||||
/**
|
||||
* Prints user friendly version of the measureSpec.
|
||||
* @param measureSpec the spec to print
|
||||
*/
|
||||
export function measureSpecToString(measureSpec: number): string;
|
||||
/**
|
||||
* Prints user friendly version of the measureSpec.
|
||||
* @param measureSpec the spec to print
|
||||
*/
|
||||
export function measureSpecToString(measureSpec: number): string;
|
||||
}
|
||||
|
||||
@@ -1,32 +1,66 @@
|
||||
import { round, MODE_MASK } from './layout-helper-common';
|
||||
import * as layoutCommon from './layout-helper-common';
|
||||
|
||||
export * from './layout-helper-common';
|
||||
export namespace layout {
|
||||
// cache the MeasureSpec constants here, to prevent extensive marshaling calls to and from Objective C
|
||||
// TODO: While this boosts the performance it is error-prone in case Google changes these constants
|
||||
export const MODE_SHIFT = 30;
|
||||
export const MODE_MASK = 0x3 << MODE_SHIFT;
|
||||
|
||||
export function makeMeasureSpec(size: number, mode: number): number {
|
||||
return (Math.round(Math.max(0, size)) & ~MODE_MASK) | (mode & MODE_MASK);
|
||||
}
|
||||
|
||||
export function getDisplayDensity(): number {
|
||||
return UIScreen.mainScreen.scale;
|
||||
}
|
||||
|
||||
export function toDevicePixels(value: number): number {
|
||||
return value * UIScreen.mainScreen.scale;
|
||||
}
|
||||
|
||||
export function toDeviceIndependentPixels(value: number): number {
|
||||
return value / UIScreen.mainScreen.scale;
|
||||
}
|
||||
|
||||
export function measureNativeView(nativeView: any /* UIView */, width: number, widthMode: number, height: number, heightMode: number): { width: number; height: number } {
|
||||
const view = <UIView>nativeView;
|
||||
const nativeSize = view.sizeThatFits({
|
||||
width: widthMode === 0 /* layout.UNSPECIFIED */ ? Number.POSITIVE_INFINITY : toDeviceIndependentPixels(width),
|
||||
height: heightMode === 0 /* layout.UNSPECIFIED */ ? Number.POSITIVE_INFINITY : toDeviceIndependentPixels(height),
|
||||
});
|
||||
|
||||
nativeSize.width = round(toDevicePixels(nativeSize.width));
|
||||
nativeSize.height = round(toDevicePixels(nativeSize.height));
|
||||
|
||||
return nativeSize;
|
||||
export const UNSPECIFIED = 0 << MODE_SHIFT;
|
||||
export const EXACTLY = 1 << MODE_SHIFT;
|
||||
export const AT_MOST = 2 << MODE_SHIFT;
|
||||
|
||||
export const MEASURED_HEIGHT_STATE_SHIFT = 0x00000010; /* 16 */
|
||||
export const MEASURED_STATE_TOO_SMALL = 0x01000000;
|
||||
export const MEASURED_STATE_MASK = 0xff000000;
|
||||
export const MEASURED_SIZE_MASK = 0x00ffffff;
|
||||
|
||||
export function getMode(mode: number) {
|
||||
return layoutCommon.getMode(mode);
|
||||
}
|
||||
|
||||
export function getMeasureSpecMode(spec: number): number {
|
||||
return layoutCommon.getMeasureSpecMode(spec);
|
||||
}
|
||||
|
||||
export function getMeasureSpecSize(spec: number) {
|
||||
return layoutCommon.getMeasureSpecSize(spec);
|
||||
}
|
||||
|
||||
export function makeMeasureSpec(size: number, mode: number): number {
|
||||
return (Math.round(Math.max(0, size)) & ~layoutCommon.MODE_MASK) | (mode & layoutCommon.MODE_MASK);
|
||||
}
|
||||
|
||||
export function getDisplayDensity(): number {
|
||||
return UIScreen.mainScreen.scale;
|
||||
}
|
||||
|
||||
export function toDevicePixels(value: number): number {
|
||||
return value * UIScreen.mainScreen.scale;
|
||||
}
|
||||
|
||||
export function toDeviceIndependentPixels(value: number): number {
|
||||
return value / UIScreen.mainScreen.scale;
|
||||
}
|
||||
|
||||
export function round(value: number) {
|
||||
return layoutCommon.round(value);
|
||||
}
|
||||
|
||||
export function measureNativeView(nativeView: any /* UIView */, width: number, widthMode: number, height: number, heightMode: number): { width: number; height: number } {
|
||||
const view = <UIView>nativeView;
|
||||
const nativeSize = view.sizeThatFits({
|
||||
width: widthMode === 0 /* layout.UNSPECIFIED */ ? Number.POSITIVE_INFINITY : toDeviceIndependentPixels(width),
|
||||
height: heightMode === 0 /* layout.UNSPECIFIED */ ? Number.POSITIVE_INFINITY : toDeviceIndependentPixels(height),
|
||||
});
|
||||
|
||||
nativeSize.width = round(toDevicePixels(nativeSize.width));
|
||||
nativeSize.height = round(toDevicePixels(nativeSize.height));
|
||||
|
||||
return nativeSize;
|
||||
}
|
||||
|
||||
export function measureSpecToString(measureSpec: number) {
|
||||
return layoutCommon.measureSpecToString(measureSpec);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user