246 lines
8.7 KiB
Dart
246 lines
8.7 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/application/pipelines_app.dart';
|
|
import 'package:pipelines_app/core/transport/pipelines_client.dart';
|
|
|
|
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
|
|
import '../features/pipelines/fixtures.dart';
|
|
import 'pipelines_app_test.dart' show webServer;
|
|
import 'pipelines_replies.dart';
|
|
|
|
Future<void> pumpApp(
|
|
WidgetTester tester, {
|
|
required PlatformRouteInformationProvider provider,
|
|
required PipelinesClient client,
|
|
}) async {
|
|
await tester.pumpWidget(
|
|
PipelinesApp(
|
|
server: webServer(),
|
|
client: client,
|
|
cupertino: false,
|
|
routeInformationProvider: provider,
|
|
),
|
|
);
|
|
await tester.pump();
|
|
}
|
|
|
|
PlatformRouteInformationProvider routeProvider() {
|
|
final provider = PlatformRouteInformationProvider(
|
|
initialRouteInformation: RouteInformation(uri: Uri.parse('/pipelines')),
|
|
);
|
|
addTearDown(provider.dispose);
|
|
return provider;
|
|
}
|
|
|
|
/// The app receives a real return-to-foreground as the engine's lifecycle
|
|
/// transition does: backgrounded, then resumed.
|
|
Future<void> resume(WidgetTester tester) async {
|
|
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
|
|
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
|
|
await tester.pump();
|
|
await tester.pump();
|
|
}
|
|
|
|
List<String> types(FakeChannel channel) => <String>[
|
|
for (final frame in channel.outgoing)
|
|
(jsonDecode(frame as String) as Map<String, dynamic>)['type'] as String,
|
|
];
|
|
|
|
int countType(FakeChannel channel, String type) =>
|
|
types(channel).where((value) => value == type).length;
|
|
|
|
void main() {
|
|
tearDown(() {
|
|
WidgetsBinding.instance.handleAppLifecycleStateChanged(
|
|
AppLifecycleState.resumed,
|
|
);
|
|
});
|
|
|
|
test('only a dropped socket is reopened on foreground', () {
|
|
expect(shouldReconnectOnForeground(ConnectionKind.disconnected), isTrue);
|
|
// A deliberate Disconnect reaches idle and must stay disconnected.
|
|
expect(shouldReconnectOnForeground(ConnectionKind.idle), isFalse);
|
|
// These states already have their own answer; foregrounding does not churn
|
|
// a live socket or restart an attempt that is already in flight.
|
|
expect(shouldReconnectOnForeground(ConnectionKind.connected), isFalse);
|
|
expect(shouldReconnectOnForeground(ConnectionKind.connecting), isFalse);
|
|
});
|
|
|
|
testWidgets(
|
|
'foreground resume reopens a dropped socket and re-watches it once',
|
|
(tester) async {
|
|
final first = FakeChannel()..readyCompleter.complete();
|
|
final second = FakeChannel()..readyCompleter.complete();
|
|
var sockets = 0;
|
|
final client = PipelinesClient(
|
|
channelFactory: (_) => sockets++ == 0 ? first : second,
|
|
// Keep the client's own backoff retry far away, so the new socket can
|
|
// only come from the foreground transition under test.
|
|
backoffBase: const Duration(minutes: 1),
|
|
);
|
|
await pumpApp(tester, provider: routeProvider(), client: client);
|
|
await replyPipelines(tester, first, PipelineFixture());
|
|
expect(sockets, 1);
|
|
expect(types(first), contains('subscribe'));
|
|
expect(client.state.kind, ConnectionKind.connected);
|
|
|
|
first.incoming.addError(StateError('network drop'));
|
|
await tester.pump();
|
|
await tester.pump();
|
|
expect(client.state.kind, ConnectionKind.disconnected);
|
|
expect(sockets, 1, reason: 'the backoff retry is still pending');
|
|
|
|
await resume(tester);
|
|
|
|
expect(sockets, 2);
|
|
expect(types(second), contains('subscribe'));
|
|
expect(client.state.kind, ConnectionKind.connected);
|
|
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
await tester.pump();
|
|
},
|
|
);
|
|
|
|
testWidgets('foreground resume refreshes the visible Pipelines list once', (
|
|
tester,
|
|
) async {
|
|
final channel = FakeChannel()..readyCompleter.complete();
|
|
final client = PipelinesClient(channelFactory: (_) => channel);
|
|
await pumpApp(tester, provider: routeProvider(), client: client);
|
|
await replyPipelines(tester, channel, PipelineFixture());
|
|
final before = countType(channel, 'list_pipelines');
|
|
expect(types(channel).where((type) => type == 'subscribe'), hasLength(1));
|
|
|
|
await resume(tester);
|
|
|
|
expect(countType(channel, 'list_pipelines'), before + 1);
|
|
expect(types(channel).where((type) => type == 'subscribe'), hasLength(1));
|
|
expect(client.state.kind, ConnectionKind.connected);
|
|
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
await tester.pump();
|
|
});
|
|
|
|
testWidgets('foreground resume refreshes the visible Runs list once', (
|
|
tester,
|
|
) async {
|
|
final channel = FakeChannel()..readyCompleter.complete();
|
|
final provider = PlatformRouteInformationProvider(
|
|
initialRouteInformation: RouteInformation(uri: Uri.parse('/runs')),
|
|
);
|
|
addTearDown(provider.dispose);
|
|
final client = PipelinesClient(channelFactory: (_) => channel);
|
|
await pumpApp(tester, provider: provider, client: client);
|
|
await waitForRequest(tester, channel, 'list_runs');
|
|
final initialRuns = pipelineRequest(channel, 'list_runs')!;
|
|
channel.incoming.add(
|
|
jsonEncode({
|
|
'id': initialRuns['id'],
|
|
'type': 'runs',
|
|
'runs': PipelineFixture().runs,
|
|
}),
|
|
);
|
|
await tester.pump();
|
|
final before = countType(channel, 'list_runs');
|
|
|
|
await resume(tester);
|
|
|
|
expect(countType(channel, 'list_runs'), before + 1);
|
|
expect(countType(channel, 'subscribe'), 1);
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
await tester.pump();
|
|
});
|
|
|
|
testWidgets('foreground resume preserves an open dialog and skips refresh', (
|
|
tester,
|
|
) async {
|
|
final channel = FakeChannel()..readyCompleter.complete();
|
|
final provider = PlatformRouteInformationProvider(
|
|
initialRouteInformation: RouteInformation(uri: Uri.parse('/runs')),
|
|
);
|
|
addTearDown(provider.dispose);
|
|
final client = PipelinesClient(channelFactory: (_) => channel);
|
|
await pumpApp(tester, provider: provider, client: client);
|
|
await waitForRequest(tester, channel, 'list_runs');
|
|
final initialRuns = pipelineRequest(channel, 'list_runs')!;
|
|
channel.incoming.add(jsonEncode({
|
|
'id': initialRuns['id'],
|
|
'type': 'runs',
|
|
'runs': PipelineFixture().runs,
|
|
}));
|
|
await tester.pump();
|
|
final before = countType(channel, 'list_runs');
|
|
showDialog<void>(
|
|
context: tester.element(find.byType(Scaffold).first),
|
|
builder: (_) => const AlertDialog(content: Text('Still open')),
|
|
);
|
|
await tester.pump();
|
|
|
|
await resume(tester);
|
|
|
|
expect(countType(channel, 'list_runs'), before);
|
|
expect(find.text('Still open'), findsOneWidget);
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
await tester.pump();
|
|
});
|
|
|
|
testWidgets('foreground resume skips refresh while a text field has focus', (
|
|
tester,
|
|
) async {
|
|
final channel = FakeChannel()..readyCompleter.complete();
|
|
final provider = PlatformRouteInformationProvider(
|
|
initialRouteInformation: RouteInformation(uri: Uri.parse('/runs')),
|
|
);
|
|
addTearDown(provider.dispose);
|
|
final client = PipelinesClient(channelFactory: (_) => channel);
|
|
await pumpApp(tester, provider: provider, client: client);
|
|
await waitForRequest(tester, channel, 'list_runs');
|
|
final initialRuns = pipelineRequest(channel, 'list_runs')!;
|
|
channel.incoming.add(jsonEncode({
|
|
'id': initialRuns['id'],
|
|
'type': 'runs',
|
|
'runs': PipelineFixture().runs,
|
|
}));
|
|
await tester.pump();
|
|
final before = countType(channel, 'list_runs');
|
|
showDialog<void>(
|
|
context: tester.element(find.byType(Scaffold).first),
|
|
builder: (_) => const AlertDialog(content: TextField()),
|
|
);
|
|
await tester.pump();
|
|
await tester.tap(find.byType(TextField).last);
|
|
await tester.pump();
|
|
|
|
await resume(tester);
|
|
|
|
expect(countType(channel, 'list_runs'), before);
|
|
expect(FocusManager.instance.primaryFocus?.hasFocus, isTrue);
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
await tester.pump();
|
|
});
|
|
|
|
testWidgets('foreground resume does not refresh an open run detail', (
|
|
tester,
|
|
) async {
|
|
final channel = FakeChannel()..readyCompleter.complete();
|
|
final provider = routeProvider();
|
|
final client = PipelinesClient(channelFactory: (_) => channel);
|
|
await pumpApp(tester, provider: provider, client: client);
|
|
await replyPipelines(tester, channel, PipelineFixture());
|
|
provider.routerReportsNewRouteInformation(
|
|
RouteInformation(uri: Uri.parse('/runs/1')),
|
|
type: RouteInformationReportingType.navigate,
|
|
);
|
|
await tester.pump();
|
|
final before = countType(channel, 'list_runs');
|
|
|
|
await resume(tester);
|
|
|
|
expect(countType(channel, 'list_runs'), before);
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
await tester.pump();
|
|
});
|
|
}
|