loadScreen method

Future<EnsembleConfig> loadScreen({
  1. required WidgetTester tester,
  2. required EnsembleTestCase testCase,
  3. required EnsembleTestContext context,
  4. EnsembleConfig? existingConfig,
  5. EnsembleTestConfig suiteConfig = const EnsembleTestConfig(),
  6. Future<void> beforeBootstrap()?,
  7. Locale? forcedLocale,
})

Implementation

Future<EnsembleConfig> loadScreen({
  required WidgetTester tester,
  required EnsembleTestCase testCase,
  required EnsembleTestContext context,
  EnsembleConfig? existingConfig,
  EnsembleTestConfig suiteConfig = const EnsembleTestConfig(),
  Future<void> Function()? beforeBootstrap,
  Locale? forcedLocale,
}) async {
  return await RunnerBenchmark.async('lifecycle', 'loadScreen', () async {
    final ctx = context;
    // Independent tests need a new EnsembleApp state. Pumping another
    // EnsembleApp of the same type would otherwise reuse the prior route.
    await tester.pumpWidget(const SizedBox.shrink());
    await tester.pump();
    resetTestRuntime(navigationFlow: ctx.navigationFlow);
    ScreenTracker().clearAll();
    await beforeBootstrap?.call();

    await applyViewport(
      tester,
      ctx,
      screenshotDevice: screenshotDeviceForTestCase(testCase, ctx.config),
    );
    var config = existingConfig ?? await buildConfig();
    Ensemble().setEnsembleConfig(config);
    final bootstrapped = await LiveAsyncCallSupport.runUntracked(
      () => bootstrapRuntime(
        config,
        ctx.setup,
        apiOverlay: ctx.apiOverlay,
        clearPersistentState:
            runtimeAdapter.clearsPersistentStateBetweenIndependentTests &&
                testCase.session == null,
      ),
      runnerOverride: tester.runAsync,
    );
    if (bootstrapped == null) {
      final bootstrapError = tester.takeException();
      while (tester.takeException() != null) {}
      throw EnsembleTestFailure(
        'Ensemble runtime bootstrap completed without a configuration.'
        '${bootstrapError == null ? '' : ' Cause: $bootstrapError'}',
      );
    }
    config = bootstrapped;

    final startScreen = testCase.startScreen;
    if (startScreen == null || startScreen.isEmpty) {
      throw EnsembleTestFailure(
        'loadScreen requires startScreen on test "${testCase.id}"',
      );
    }

    final deviceTheme = testCase.deviceTarget?.theme;
    await seedEnsembleTestTheme(deviceTheme);

    // Skip re-initializing providers in EnsembleApp.initApp; bootstrapRuntime
    // already installed real providers and mock overlays.
    config.appBundle = null;
    await tester.pumpWidget(
      EnsembleApp(
        ensembleConfig: config,
        screenPayload: ScreenPayload(
          screenId: startScreen,
          screenName: startScreen,
          arguments: testCase.startScreenInputs,
        ),
        forcedLocale: forcedLocale,
      ),
    );

    await waitForInitialWidgets(tester, testCase: testCase);
    final appliedTheme = applyDeviceThemeForTestCase(testCase);
    if (appliedTheme != null) {
      ctx.runtime.themeMode = appliedTheme;
      await tester.pump();
    }
    return config;
  });
}