Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): launching app on simulator with different appId than pa… #2195

Merged
merged 1 commit into from
Dec 10, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('--appFolder', () => {
const androidProject: AndroidProjectConfig = {
appName: 'app',
packageName: 'com.test',
applicationId: 'com.test',
sourceDir: '/android',
mainActivity: '.MainActivity',
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import {AndroidProjectConfig} from '@react-native-community/cli-types';
import tryLaunchAppOnDevice from '../tryLaunchAppOnDevice';
import {Flags} from '..';
import execa from 'execa';

jest.mock('execa');
jest.mock('../getAdbPath');
jest.mock('../tryLaunchEmulator');

const adbPath = 'path/to/adb';
const device = 'emulator-5554';
let args: Flags = {
activeArchOnly: false,
packager: true,
port: 8081,
terminal: 'iTerm.app',
appId: '',
appIdSuffix: '',
listDevices: false,
};

let androidProject: AndroidProjectConfig = {
sourceDir: '/Users/thymikee/Developer/tmp/App73/android',
appName: 'app',
packageName: 'com.myapp',
applicationId: 'com.myapp.custom',
mainActivity: '.MainActivity',
dependencyConfiguration: undefined,
watchModeCommandParams: undefined,
unstable_reactLegacyComponentNames: undefined,
};

const shellStartCommand = ['shell', 'am', 'start'];
const actionCategoryFlags = [
'-a',
'android.intent.action.MAIN',
'-c',
'android.intent.category.LAUNCHER',
];

beforeEach(() => {
jest.clearAllMocks();
});

test('launches adb shell with intent to launch com.myapp.MainActivity with different appId than packageName on a simulator', () => {
tryLaunchAppOnDevice(device, androidProject, adbPath, args);

expect(execa.sync).toHaveBeenCalledWith(
'path/to/adb',
[
'-s',
'emulator-5554',
...shellStartCommand,
'-n',
'com.myapp.custom/com.myapp.MainActivity',
...actionCategoryFlags,
],
{stdio: 'inherit'},
);
});

test('launches adb shell with intent to launch com.myapp.MainActivity with same appId as packageName on a simulator', () => {
tryLaunchAppOnDevice(
device,
{...androidProject, applicationId: 'com.myapp'},
adbPath,
args,
);

expect(execa.sync).toHaveBeenCalledWith(
'path/to/adb',
[
'-s',
'emulator-5554',
...shellStartCommand,
'-n',
'com.myapp/com.myapp.MainActivity',
...actionCategoryFlags,
],
{stdio: 'inherit'},
);
});

test('launches adb shell with intent to launch com.myapp.MainActivity with different appId than packageName on a device (without calling simulator)', () => {
tryLaunchAppOnDevice(undefined, androidProject, adbPath, args);

expect(execa.sync).toHaveBeenCalledWith(
'path/to/adb',
[
...shellStartCommand,
'-n',
'com.myapp.custom/com.myapp.MainActivity',
...actionCategoryFlags,
],
{stdio: 'inherit'},
);
});

test('--appId flag overwrites applicationId setting in androidProject', () => {
tryLaunchAppOnDevice(undefined, androidProject, adbPath, {
...args,
appId: 'my.app.id',
});

expect(execa.sync).toHaveBeenCalledWith(
'path/to/adb',
[
...shellStartCommand,
'-n',
'my.app.id/com.myapp.MainActivity',
...actionCategoryFlags,
],
{stdio: 'inherit'},
);
});

test('appIdSuffix Staging is appended to applicationId', () => {
tryLaunchAppOnDevice(undefined, androidProject, adbPath, {
...args,
appIdSuffix: 'Staging',
});

expect(execa.sync).toHaveBeenCalledWith(
'path/to/adb',
[
...shellStartCommand,
'-n',
'com.myapp.custom.Staging/com.myapp.MainActivity',
...actionCategoryFlags,
],
{stdio: 'inherit'},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {checkUsers, promptForUser} from './listAndroidUsers';
export interface Flags extends BuildFlags {
appId: string;
appIdSuffix: string;
mainActivity: string;
mainActivity?: string;
port: number;
terminal?: string;
packager?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function tryLaunchAppOnDevice(
.join('.');

const activityToLaunch = mainActivity.includes('.')
? mainActivity
? [packageName, mainActivity].join('')
: [packageName, mainActivity].filter(Boolean).join('.');

try {
Expand Down
Loading