Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion apps/automated/src/ui/action-bar/action-bar-tests.android.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
import * as TKUnit from '../../tk-unit';
import { createPageAndNavigate } from './action-bar-tests-common';
import { ActionItem } from '@nativescript/core/ui/action-bar';
import { CoreTypes } from '@nativescript/core';
import { CoreTypes, Utils } from '@nativescript/core';
import { Button } from '@nativescript/core/ui/button';

export * from './action-bar-tests-common';

function collectJsAndJavaGarbage() {
for (let i = 0; i < 3; i++) {
Utils.GC();
java.lang.System.gc();
java.lang.System.runFinalization();
TKUnit.wait(0.05);
}
}

export function test_actionItem_tap_after_garbage_collection() {
const actionItem = new ActionItem();
actionItem.text = 'Test';
let tapped = false;
actionItem.on(ActionItem.tapEvent, () => (tapped = true));
const page = createPageAndNavigate();
page.actionBar.actionItems.addItem(actionItem);

collectJsAndJavaGarbage();
const toolbar = <androidx.appcompat.widget.Toolbar>page.actionBar.nativeViewProtected;
toolbar.getMenu().performIdentifierAction((<any>actionItem)._getItemId(), 0);

TKUnit.assertTrue(tapped, 'action item tap should fire after a garbage collection');
}

export function test_actionItem_visibility() {
const actionItem = new ActionItem();
actionItem.text = 'Test';
Expand Down
26 changes: 25 additions & 1 deletion apps/automated/src/ui/button/button-tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as TKUnit from '../../tk-unit';
import * as helper from '../../ui-helper';
import { View, EventData, Button, Observable, Color, Page, FormattedString, BindingOptions, Span } from '@nativescript/core';
import { View, EventData, Button, Observable, Color, Page, FormattedString, BindingOptions, Span, Utils } from '@nativescript/core';
import * as buttonTestsNative from './button-tests-native';

export function test_recycling() {
Expand All @@ -15,6 +15,30 @@ export var testOnClick = function () {
helper.buildUIAndRunTest(_createButtonFunc(), _testOnClick);
};

function collectJsAndJavaGarbage() {
for (let i = 0; i < 3; i++) {
Utils.GC();
if (__ANDROID__) {
java.lang.System.gc();
java.lang.System.runFinalization();
}
TKUnit.wait(0.05);
}
}

export function testOnClickAfterGarbageCollection() {
helper.buildUIAndRunTest(_createButtonFunc(), function (views: Array<View>) {
const button = <Button>views[0];
let tapped = false;
button.on(Button.tapEvent, () => (tapped = true));

collectJsAndJavaGarbage();
buttonTestsNative.performNativeClick(button);

TKUnit.assertTrue(tapped, 'tap should fire after a garbage collection');
});
}

export var testBindTextDirectlyToModel = function () {
helper.buildUIAndRunTest(_createButtonFunc(), _testBindTextDirectlyToModel);
};
Expand Down
11 changes: 8 additions & 3 deletions packages/core/ui/action-bar/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,19 @@ function initializeMenuItemClickListener(): void {
@NativeClass
@Interfaces([androidx.appcompat.widget.Toolbar.OnMenuItemClickListener])
class MenuItemClickListenerImpl extends java.lang.Object implements androidx.appcompat.widget.Toolbar.OnMenuItemClickListener {
constructor(public owner: ActionBar) {
// Weak: the toolbar holds this listener, so a strong owner would be a cycle that
// only disposeNativeView breaks.
public owner: WeakRef<ActionBar>;

constructor(owner: ActionBar) {
super();
this.owner = new WeakRef(owner);
}

onMenuItemClick(item: android.view.MenuItem): boolean {
const itemId = item.getItemId();
const owner = this.owner?.deref();

return this.owner._onAndroidItemSelected(itemId);
return owner ? owner._onAndroidItemSelected(item.getItemId()) : false;
}
}

Expand Down
9 changes: 7 additions & 2 deletions packages/core/ui/button/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ function initializeClickListener(): void {
@NativeClass
@Interfaces([android.view.View.OnClickListener])
class ClickListenerImpl extends java.lang.Object implements android.view.View.OnClickListener {
constructor(public owner: Button) {
// Weak: the button's native view holds this listener, so a strong owner would be
// a cycle that only disposeNativeView breaks.
public owner: WeakRef<Button>;

constructor(owner: Button) {
super();
this.owner = new WeakRef(owner);
}

public onClick(v: android.view.View): void {
const owner = this.owner;
const owner = this.owner?.deref();
if (owner) {
owner._emit(ButtonBase.tapEvent);
}
Expand Down
Loading