Chat: choosing a chat focuses its composer on desktop
A row click or tap, Next chat and a created chat put the keyboard in the composer, as Ctrl+1-9 does, on desktop hosts (native or desktop browser, any window width) and after a mouse or trackpad click anywhere. A finger tap on a phone or tablet (Android, iOS, mobile browsers) leaves focus alone so the on-screen keyboard does not cover the transcript. Closed chats are not focused, and a request yields when another control took focus meanwhile.
This commit is contained in:
@@ -0,0 +1,198 @@
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/composer_selection_focus.dart';
|
||||
import 'package:pipelines_app/features/chat/screen.dart';
|
||||
import 'package:pipelines_app/shell/adaptive_shell.dart';
|
||||
import 'package:pipelines_app/shell/destination.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'conversation/chat_thread_fixtures.dart';
|
||||
|
||||
/// The list shows Starred (apple), then Chats (Recent, Second), then Closed.
|
||||
const _rows = <JsonMap>[
|
||||
{'id': 'r', 'title': 'Recent', 'directory': '/w'},
|
||||
{'id': 'c', 'title': 'Done', 'directory': '/w', 'closed_at': 1},
|
||||
{'id': 's', 'title': 'Second', 'directory': '/w'},
|
||||
{'id': 'a', 'title': 'apple', 'directory': '/w', 'starred': true},
|
||||
];
|
||||
|
||||
class _Gateway extends FakeChatGateway {
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
if (type == 'chat_sessions') {
|
||||
return Future.value({
|
||||
'chat_sessions': [
|
||||
for (final row in _rows) {...row},
|
||||
],
|
||||
});
|
||||
}
|
||||
if (type == 'chat_shells') return Future.value({'chat_shells': []});
|
||||
return super.call(type, fields);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _pump(WidgetTester tester, Size size) async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
tester.view.physicalSize = size;
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final gateway = _Gateway()..history = (_) async => chatPage(const []);
|
||||
addTearDown(gateway.close);
|
||||
final route = ValueNotifier<String?>(null);
|
||||
addTearDown(route.dispose);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: AdaptiveShell(
|
||||
destination: AppDestination.chat,
|
||||
destinationBodies: {
|
||||
AppDestination.chat: (_) => ValueListenableBuilder<String?>(
|
||||
valueListenable: route,
|
||||
builder: (context, id, _) => ChatScreen(
|
||||
gateway: gateway,
|
||||
initialSessionId: id,
|
||||
onSessionSelected: (next) => route.value = next,
|
||||
onBack: () => route.value = null,
|
||||
),
|
||||
),
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
Finder _thread(String id) => find.byKey(ValueKey('chat-thread-$id'));
|
||||
|
||||
bool _composerFocused(WidgetTester tester, String id) => tester
|
||||
.widget<EditableText>(
|
||||
find.descendant(
|
||||
of: find.byKey(ValueKey('chat-composer-$id')),
|
||||
matching: find.byType(EditableText),
|
||||
),
|
||||
)
|
||||
.focusNode
|
||||
.hasPrimaryFocus;
|
||||
|
||||
Future<void> _tap(
|
||||
WidgetTester tester,
|
||||
Finder finder, {
|
||||
PointerDeviceKind kind = PointerDeviceKind.touch,
|
||||
}) async {
|
||||
await tester.tap(finder, kind: kind);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
Finder _row(String id) => find.byKey(Key('chat-row-$id'));
|
||||
final _next = find.byKey(const Key('chat-next'));
|
||||
|
||||
final _desktop = TargetPlatformVariant.only(TargetPlatform.macOS);
|
||||
final _mobile = TargetPlatformVariant.only(TargetPlatform.android);
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'desktop, wide: a clicked row and Next chat focus the composer',
|
||||
(tester) async {
|
||||
await _pump(tester, const Size(1280, 800));
|
||||
await _tap(tester, _row('r'), kind: PointerDeviceKind.mouse);
|
||||
expect(_thread('r'), findsOneWidget);
|
||||
expect(_composerFocused(tester, 'r'), isTrue);
|
||||
|
||||
// Focus elsewhere, then the chat already open: clicking returns it.
|
||||
FocusManager.instance.primaryFocus?.unfocus();
|
||||
await tester.pump();
|
||||
await _tap(tester, _row('r'), kind: PointerDeviceKind.mouse);
|
||||
expect(_composerFocused(tester, 'r'), isTrue);
|
||||
|
||||
await _tap(tester, _next, kind: PointerDeviceKind.mouse);
|
||||
expect(_thread('s'), findsOneWidget);
|
||||
expect(_composerFocused(tester, 's'), isTrue);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
},
|
||||
variant: _desktop,
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'desktop, narrow window: a tapped row focuses the chat page composer',
|
||||
(tester) async {
|
||||
await _pump(tester, const Size(390, 844));
|
||||
await _tap(tester, find.text('Second'));
|
||||
expect(_thread('s'), findsOneWidget);
|
||||
expect(_composerFocused(tester, 's'), isTrue);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
},
|
||||
variant: _desktop,
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'desktop: a closed chat leaves its composer unfocused',
|
||||
(tester) async {
|
||||
await _pump(tester, const Size(1280, 800));
|
||||
await _tap(tester, find.text('Closed'));
|
||||
await _tap(tester, _row('c'), kind: PointerDeviceKind.mouse);
|
||||
expect(_thread('c'), findsOneWidget);
|
||||
expect(_composerFocused(tester, 'c'), isFalse);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
},
|
||||
variant: _desktop,
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'mobile: a finger tap and Next chat do not raise the keyboard',
|
||||
(tester) async {
|
||||
await _pump(tester, const Size(390, 844));
|
||||
await _tap(tester, find.text('Recent'));
|
||||
expect(_thread('r'), findsOneWidget);
|
||||
expect(_composerFocused(tester, 'r'), isFalse);
|
||||
await _tap(tester, _next);
|
||||
expect(_thread('s'), findsOneWidget);
|
||||
expect(_composerFocused(tester, 's'), isFalse);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
},
|
||||
variant: _mobile,
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'mobile, wide: a touch tap leaves focus, a mouse click focuses',
|
||||
(tester) async {
|
||||
await _pump(tester, const Size(1280, 800));
|
||||
await _tap(tester, _row('r'));
|
||||
expect(_composerFocused(tester, 'r'), isFalse);
|
||||
await _tap(tester, _row('s'), kind: PointerDeviceKind.mouse);
|
||||
expect(_composerFocused(tester, 's'), isTrue);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
},
|
||||
variant: _mobile,
|
||||
);
|
||||
|
||||
test('the rule: desktop or a mouse, never a closed chat', () {
|
||||
const open = <String, dynamic>{'id': 'x'};
|
||||
const closed = <String, dynamic>{'id': 'x', 'closed_at': 1};
|
||||
for (final platform in TargetPlatform.values) {
|
||||
final desktop = {
|
||||
TargetPlatform.macOS,
|
||||
TargetPlatform.windows,
|
||||
TargetPlatform.linux,
|
||||
}.contains(platform);
|
||||
for (final kind in PointerDeviceKind.values) {
|
||||
final mouse =
|
||||
kind == PointerDeviceKind.mouse ||
|
||||
kind == PointerDeviceKind.trackpad;
|
||||
expect(
|
||||
chatSelectionFocusesComposer(open, platform: platform, pointer: kind),
|
||||
desktop || mouse,
|
||||
reason: '$platform $kind',
|
||||
);
|
||||
expect(
|
||||
chatSelectionFocusesComposer(
|
||||
closed,
|
||||
platform: platform,
|
||||
pointer: kind,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user