188 lines
6.7 KiB
Dart
188 lines
6.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/core/api_gateway.dart';
|
|
import 'package:pipelines_app/features/search/search_screen.dart';
|
|
import 'package:pipelines_app/features/search/ui/search_bar_controls.dart';
|
|
import 'package:pipelines_app/shell/adaptive_shell.dart';
|
|
import 'package:pipelines_app/shell/destination.dart';
|
|
import 'package:pipelines_app/shell/shell_list_shortcuts.dart';
|
|
|
|
class _Gateway implements ApiGateway {
|
|
@override
|
|
Stream<JsonMap> get frames => const Stream.empty();
|
|
@override
|
|
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async => {
|
|
'search_results': {'results': [], 'truncated': false},
|
|
};
|
|
}
|
|
|
|
Future<void> _chord(
|
|
WidgetTester tester,
|
|
List<LogicalKeyboardKey> modifiers,
|
|
LogicalKeyboardKey key,
|
|
) async {
|
|
for (final modifier in modifiers) {
|
|
await tester.sendKeyDownEvent(modifier);
|
|
}
|
|
await tester.sendKeyEvent(key);
|
|
for (final modifier in modifiers.reversed) {
|
|
await tester.sendKeyUpEvent(modifier);
|
|
}
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
void main() {
|
|
for (final platform in [TargetPlatform.macOS, TargetPlatform.windows]) {
|
|
testWidgets('Search accepts exact primary slash on $platform', (
|
|
tester,
|
|
) async {
|
|
final results = <bool>[];
|
|
bool handler(KeyEvent event) {
|
|
if (event is KeyDownEvent &&
|
|
event.logicalKey == LogicalKeyboardKey.slash) {
|
|
results.add(
|
|
shellSearchShortcut(
|
|
event,
|
|
HardwareKeyboard.instance,
|
|
platform: platform,
|
|
),
|
|
);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
HardwareKeyboard.instance.addHandler(handler);
|
|
addTearDown(() => HardwareKeyboard.instance.removeHandler(handler));
|
|
for (final modifiers in [
|
|
[LogicalKeyboardKey.controlLeft],
|
|
[LogicalKeyboardKey.metaLeft],
|
|
[LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.shiftLeft],
|
|
[LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.altLeft],
|
|
[LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.metaLeft],
|
|
<LogicalKeyboardKey>[],
|
|
]) {
|
|
await _chord(tester, modifiers, LogicalKeyboardKey.slash);
|
|
}
|
|
expect(results, [
|
|
true,
|
|
platform == TargetPlatform.macOS,
|
|
false,
|
|
false,
|
|
false,
|
|
false,
|
|
]);
|
|
});
|
|
}
|
|
|
|
for (final platform in [TargetPlatform.macOS, TargetPlatform.windows]) {
|
|
testWidgets(
|
|
'Search chord opens and refocuses retained query on $platform',
|
|
(tester) async {
|
|
final composer = TextEditingController(text: 'draft remains');
|
|
final focus = FocusNode();
|
|
addTearDown(composer.dispose);
|
|
addTearDown(focus.dispose);
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
// The theme deliberately disagrees with the actual macOS host.
|
|
theme: ThemeData(platform: TargetPlatform.android),
|
|
home: AdaptiveShell(
|
|
destination: AppDestination.chat,
|
|
destinationBodies: {
|
|
AppDestination.chat: (_) =>
|
|
TextField(controller: composer, focusNode: focus),
|
|
AppDestination.search: (_) => SearchScreen(
|
|
gateway: _Gateway(),
|
|
onOpen: (_) {},
|
|
embedded: true,
|
|
),
|
|
},
|
|
),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
focus.requestFocus();
|
|
await tester.pump();
|
|
final primary = platform == TargetPlatform.macOS
|
|
? LogicalKeyboardKey.metaLeft
|
|
: LogicalKeyboardKey.controlLeft;
|
|
await _chord(tester, [primary], LogicalKeyboardKey.slash);
|
|
var search = tester.widget<TextField>(find.byKey(searchFieldKey));
|
|
expect(search.focusNode!.hasFocus, isTrue);
|
|
expect(composer.text, 'draft remains');
|
|
await tester.enterText(find.byKey(searchFieldKey), 'release');
|
|
await tester.pumpAndSettle(const Duration(milliseconds: 300));
|
|
await tester.tap(find.byKey(const ValueKey('shell.destination.chat')));
|
|
await tester.pumpAndSettle();
|
|
focus.requestFocus();
|
|
await tester.pump();
|
|
await _chord(tester, [primary], LogicalKeyboardKey.slash);
|
|
search = tester.widget<TextField>(find.byKey(searchFieldKey));
|
|
expect(search.controller!.text, 'release');
|
|
expect(search.focusNode!.hasFocus, isTrue);
|
|
search.focusNode!.unfocus();
|
|
await tester.pump();
|
|
await _chord(tester, [primary], LogicalKeyboardKey.slash);
|
|
expect(search.focusNode!.hasFocus, isTrue);
|
|
},
|
|
variant: TargetPlatformVariant.only(platform),
|
|
);
|
|
}
|
|
testWidgets(
|
|
'Cupertino Command slash retains primary focus after query updates',
|
|
(tester) async {
|
|
final composer = TextEditingController(text: 'draft');
|
|
final focus = FocusNode();
|
|
addTearDown(composer.dispose);
|
|
addTearDown(focus.dispose);
|
|
await tester.pumpWidget(
|
|
CupertinoApp(
|
|
home: AdaptiveShell(
|
|
cupertino: true,
|
|
destination: AppDestination.chat,
|
|
destinationBodies: {
|
|
AppDestination.chat: (_) =>
|
|
CupertinoTextField(controller: composer, focusNode: focus),
|
|
AppDestination.search: (_) => SearchScreen(
|
|
gateway: _Gateway(),
|
|
onOpen: (_) {},
|
|
embedded: true,
|
|
),
|
|
},
|
|
),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
focus.requestFocus();
|
|
await tester.pump();
|
|
await _chord(tester, [
|
|
LogicalKeyboardKey.metaLeft,
|
|
], LogicalKeyboardKey.slash);
|
|
EditableText field() => tester.widget<EditableText>(
|
|
find.descendant(
|
|
of: find.byKey(searchFieldKey),
|
|
matching: find.byType(EditableText),
|
|
),
|
|
);
|
|
expect(field().focusNode.hasPrimaryFocus, isTrue);
|
|
await tester.enterText(find.byKey(searchFieldKey), 'release');
|
|
await tester.pumpAndSettle(const Duration(milliseconds: 300));
|
|
await tester.pump(const Duration(seconds: 1));
|
|
expect(field().controller.text, 'release');
|
|
expect(field().focusNode.hasPrimaryFocus, isTrue);
|
|
await tester.tap(find.byKey(const ValueKey('shell.destination.chat')));
|
|
await tester.pumpAndSettle();
|
|
focus.requestFocus();
|
|
await tester.pump();
|
|
await _chord(tester, [
|
|
LogicalKeyboardKey.metaLeft,
|
|
], LogicalKeyboardKey.slash);
|
|
expect(field().controller.text, 'release');
|
|
expect(field().focusNode.hasPrimaryFocus, isTrue);
|
|
},
|
|
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
|
|
);
|
|
}
|