Файли
openidea/e2e/flutterDevices.spec.ts
T
LU XIANGRUIтаwildcatDownstairs 1e4388490c feat(flutter): start emulators and simulators from the device chooser (#5783)
The Flutter device chooser lists emulators that aren't running (Open iOS Simulator, Open Android Emulator: <name>), from the project's flutter daemon (emulator.getEmulators, asked on connect and whenever the chooser opens; one a device already runs on is left out). Choosing one shows Starting <name>… and selects its device once it connects; a failed launch shows what the tool printed. AVDs are started by ide-android in their own process group so they outlive the daemon and the IDE; the iOS Simulator goes through emulator.launch.

Part of #4096

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-25 14:09:37 +08:00

118 рядки
4.9 KiB
TypeScript

/**
* The Flutter device selector next to the run configuration. The daemon is stubbed at the IPC
* layer; `crates/ide-flutter` covers its protocol and process.
*/
import type { Page } from "@playwright/test";
import { expect, ipcCalls, stubDesktop, test } from "./fixtures";
const device = (id: string, name: string, category: string, unavailable: string | null = null) => ({
id,
name,
platformType: null,
category,
emulator: false,
unavailable,
});
const MACOS = device("macos", "macOS", "desktop");
const CHROME = device("chrome", "Chrome", "web");
const UNAUTHORIZED = device("R5C", "R5C", "mobile", "unauthorized");
async function stubFlutter(page: Page, status: unknown = { kind: "ready" }, emulators: unknown[] = []) {
await stubDesktop(page);
await page.evaluate(
async ({ list }) => {
const config = {
id: "flutter:run",
name: "flutter run",
kind: "flutter",
command: "flutter",
args: ["run", "-t", "lib/main.dart"],
cwd: null,
env: {},
detected: true,
isTest: false,
debugAdapter: "dart",
};
const h = window.__ipc.handlers;
h.run_configs = () => [config];
h.run_apply_settings = (args) => args.configs;
h.flutter_devices = () => list;
h.flutter_devices_refresh = () => null;
h.flutter_emulator_launch = () => null;
h.run_start = () => ({ id: 3, configId: config.id, name: config.name, pid: 42, running: true, pty: false });
const runModule = "/src/features/run/store.ts";
const { loadConfigs, selectConfig } = await import(/* @vite-ignore */ runModule);
await loadConfigs();
selectConfig(config.id);
},
{ list: { status, devices: [MACOS, CHROME, UNAUTHORIZED], emulators } },
);
}
const chooser = (page: Page) => page.getByRole("button", { name: /^Flutter device:/ });
test("the chosen device is used for Run, and replaced while it is disconnected", async ({ workbench: page }) => {
await stubFlutter(page);
await expect(chooser(page)).toContainText("macOS");
await chooser(page).click();
await expect(page.getByRole("menuitemcheckbox", { name: "R5C (unauthorized)" })).toBeDisabled();
await page.getByRole("menuitemcheckbox", { name: "Chrome" }).click();
await expect(chooser(page)).toContainText("Chrome");
await page.keyboard.press("Control+r");
await expect.poll(async () => (await ipcCalls(page, "run_start")).length).toBe(1);
const [started] = await ipcCalls(page, "run_start");
expect((started!.config as { args: string[] }).args).toEqual(["run", "-t", "lib/main.dart", "-d", "chrome"]);
const emit = (devices: unknown[]) =>
page.evaluate(
(devices) => window.__ipc.emit({ type: "flutterDevices", list: { status: { kind: "ready" }, devices } }),
devices,
);
await emit([MACOS]);
await expect(chooser(page)).toContainText("macOS");
await emit([MACOS, CHROME]);
await expect(chooser(page)).toContainText("Chrome");
});
test("says when the Flutter SDK is missing", async ({ workbench: page }) => {
await stubFlutter(page, { kind: "notInstalled" });
await expect(chooser(page)).toContainText("Flutter SDK not found");
});
test("starts an emulator and selects its device once it connects", async ({ workbench: page }) => {
const pixel = { id: "Pixel_9", name: "Pixel 9", platformType: "android" };
const simulator = { id: "apple_ios_simulator", name: "iOS Simulator", platformType: "ios" };
await stubFlutter(page, { kind: "ready" }, [pixel, simulator]);
await chooser(page).click();
await expect(page.getByRole("menuitem", { name: "Open iOS Simulator" })).toBeVisible();
await page.getByRole("menuitem", { name: "Open Android Emulator: Pixel 9" }).click();
await expect.poll(async () => ipcCalls(page, "flutter_emulator_launch")).toEqual([{ id: "Pixel_9" }]);
await expect(chooser(page)).toContainText("Starting Pixel 9…");
const booted = { ...device("emulator-5554", "sdk gphone64 arm64", "mobile"), emulator: true, emulatorId: "Pixel_9" };
await page.evaluate((list) => window.__ipc.emit({ type: "flutterDevices", list }), {
status: { kind: "ready" },
devices: [MACOS, booted],
emulators: [simulator],
});
await expect(chooser(page)).toContainText("sdk gphone64 arm64");
});
test("says why an emulator could not start", async ({ workbench: page }) => {
await stubFlutter(page, { kind: "ready" }, [
{ id: "apple_ios_simulator", name: "iOS Simulator", platformType: "ios" },
]);
await page.evaluate(() => {
window.__ipc.handlers.flutter_emulator_launch = () => {
throw { kind: "unavailable", message: "Unable to find Xcode" };
};
});
await chooser(page).click();
await page.getByRole("menuitem", { name: "Open iOS Simulator" }).click();
await expect(page.getByText("Could not start iOS Simulator")).toBeVisible();
await expect(page.getByText("Unable to find Xcode")).toBeVisible();
await expect(chooser(page)).toContainText("macOS");
});