|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +vi.hoisted(() => { |
| 4 | + (globalThis as any).NativeScriptGlobals.addEventWiring = () => {}; |
| 5 | +}); |
| 6 | + |
| 7 | +import { AndroidApplication } from './application.android'; |
| 8 | +import type { AndroidNativeWindow } from '../native-window/native-window.android'; |
| 9 | + |
| 10 | +function activity(name: string, nativeScript = true) { |
| 11 | + return { |
| 12 | + ...(nativeScript ? { isNativeScriptActivity: true } : {}), |
| 13 | + getClass: () => name, |
| 14 | + isFinishing: () => false, |
| 15 | + isDestroyed: () => false, |
| 16 | + isInMultiWindowMode: () => false, |
| 17 | + startActivity: vi.fn(), |
| 18 | + } as unknown as androidx.appcompat.app.AppCompatActivity; |
| 19 | +} |
| 20 | + |
| 21 | +describe('Android openWindow activity selection', () => { |
| 22 | + let app: AndroidApplication; |
| 23 | + let context: any; |
| 24 | + let windows: AndroidNativeWindow[]; |
| 25 | + let start: androidx.appcompat.app.AppCompatActivity; |
| 26 | + let foreground: androidx.appcompat.app.AppCompatActivity; |
| 27 | + let launchComponent: any; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + app = new AndroidApplication(); |
| 31 | + start = undefined; |
| 32 | + foreground = undefined; |
| 33 | + windows = []; |
| 34 | + launchComponent = 'com.example.LaunchActivity'; |
| 35 | + context = { |
| 36 | + startActivity: vi.fn(), |
| 37 | + getPackageName: () => 'com.example', |
| 38 | + getPackageManager: () => ({ getLaunchIntentForPackage: () => launchComponent && { getComponent: () => launchComponent } }), |
| 39 | + }; |
| 40 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 41 | + vi.spyOn(app, 'context', 'get').mockReturnValue(context); |
| 42 | + vi.spyOn(app, 'startActivity', 'get').mockImplementation(() => start); |
| 43 | + vi.spyOn(app, 'foregroundActivity', 'get').mockImplementation(() => foreground); |
| 44 | + vi.spyOn(app, '_getWindows').mockImplementation(() => windows); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => vi.restoreAllMocks()); |
| 48 | + |
| 49 | + function addWindow(owner: androidx.appcompat.app.AppCompatActivity, overrides = {}) { |
| 50 | + windows.push({ activity: owner, state: 'attached', _surfaceGone: false, ...overrides } as AndroidNativeWindow); |
| 51 | + } |
| 52 | + |
| 53 | + function openedIntent() { |
| 54 | + const launchers = new Set([context, start, foreground, ...windows.map((window) => window.activity)]); |
| 55 | + const calls = [...launchers].filter(Boolean).flatMap((launcher) => vi.mocked(launcher.startActivity).mock.calls); |
| 56 | + expect(calls).toHaveLength(1); |
| 57 | + return calls[0][0]; |
| 58 | + } |
| 59 | + |
| 60 | + it('continues opening windows after the original activity has finished', () => { |
| 61 | + const surviving = activity('com.example.DocumentActivity'); |
| 62 | + addWindow(surviving); |
| 63 | + |
| 64 | + app.openWindow(); |
| 65 | + |
| 66 | + expect(openedIntent().component).toBe(surviving.getClass()); |
| 67 | + }); |
| 68 | + |
| 69 | + it('keeps using the original NativeScript activity while it is available', () => { |
| 70 | + start = activity('com.example.MainActivity'); |
| 71 | + foreground = activity('com.example.DocumentActivity'); |
| 72 | + addWindow(foreground); |
| 73 | + |
| 74 | + app.openWindow(); |
| 75 | + |
| 76 | + expect(openedIntent().component).toBe(start.getClass()); |
| 77 | + }); |
| 78 | + |
| 79 | + it('uses a foreground NativeScript activity when the original is gone', () => { |
| 80 | + foreground = activity('com.example.DocumentActivity'); |
| 81 | + |
| 82 | + app.openWindow(); |
| 83 | + |
| 84 | + expect(openedIntent().component).toBe(foreground.getClass()); |
| 85 | + }); |
| 86 | + |
| 87 | + it('does not reopen an SDK activity registered ahead of a surviving NativeScript window', () => { |
| 88 | + start = foreground = activity('com.example.PaymentActivity', false); |
| 89 | + addWindow(start); |
| 90 | + const surviving = activity('com.example.DocumentActivity'); |
| 91 | + addWindow(surviving); |
| 92 | + |
| 93 | + app.openWindow(); |
| 94 | + |
| 95 | + expect(openedIntent().component).toBe(surviving.getClass()); |
| 96 | + }); |
| 97 | + |
| 98 | + it.each(['isFinishing', 'isDestroyed'] as const)('skips an activity whose %s is true', (method) => { |
| 99 | + start = activity('com.example.OldActivity'); |
| 100 | + vi.spyOn(start, method).mockReturnValue(true); |
| 101 | + addWindow(start); |
| 102 | + const surviving = activity('com.example.DocumentActivity'); |
| 103 | + addWindow(surviving); |
| 104 | + |
| 105 | + app.openWindow(); |
| 106 | + |
| 107 | + expect(openedIntent().component).toBe(surviving.getClass()); |
| 108 | + }); |
| 109 | + |
| 110 | + it.each([{ state: 'detached' }, { _surfaceGone: true }])('skips a window without a live surface: %j', (overrides) => { |
| 111 | + addWindow(activity('com.example.OldActivity'), overrides); |
| 112 | + const surviving = activity('com.example.DocumentActivity'); |
| 113 | + addWindow(surviving); |
| 114 | + |
| 115 | + app.openWindow(); |
| 116 | + |
| 117 | + expect(openedIntent().component).toBe(surviving.getClass()); |
| 118 | + }); |
| 119 | + |
| 120 | + it('uses the configured launch component when there is no surviving NativeScript activity', () => { |
| 121 | + app.openWindow(); |
| 122 | + |
| 123 | + const intent = context.startActivity.mock.calls[0][0]; |
| 124 | + expect(intent.component).toBe(launchComponent); |
| 125 | + expect(intent.flags).toBe(android.content.Intent.FLAG_ACTIVITY_NEW_TASK | android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK | android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
| 126 | + }); |
| 127 | + |
| 128 | + it('reports a missing launch component without starting an undeclared activity', () => { |
| 129 | + launchComponent = undefined; |
| 130 | + |
| 131 | + expect(() => app.openWindow()).toThrow(/no NativeScript activity or launch activity/i); |
| 132 | + expect(context.startActivity).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | +}); |
0 commit comments