Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement a PressKey action #153

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions example/flows/text_flow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ description: Testing the text page
- expectVisible: "Text"
- expectVisible: "Result: "
- pressOn: "Enter text"
- inputText: "Testing the text page"
- expectVisible: "Result: Testing the text page"
# - inputText: "Testing the text page"
# - expectVisible: "Result: Testing the text page"
- pressKey: "backspace"
- expectVisible: "FAILING ON PURPOSE TO TEST BACKSPACE KEY"
# Return to the app page
- pressOn: "Back"
- expectEnvironmentText:
8 changes: 8 additions & 0 deletions example/lib/text/view/text_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ class TextView extends StatefulWidget {

class _TextViewState extends State<TextView> {
late TextEditingController _controller;
late FocusNode focusNode;

@override
void initState() {
super.initState();
_controller = TextEditingController();
focusNode = FocusNode(
onKey: (node, event) {
print(event);
return KeyEventResult.handled;
},
);

_controller.addListener(_onChange);
}
Expand All @@ -59,6 +66,7 @@ class _TextViewState extends State<TextView> {
child: Column(
children: [
TextField(
focusNode: focusNode,
controller: _controller,
decoration: const InputDecoration(labelText: 'Enter text'),
),
Expand Down
53 changes: 53 additions & 0 deletions packages/fluttium/lib/src/actions/press_key.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:flutter/services.dart';
import 'package:fluttium/fluttium.dart';

/// {@template press_key}
/// TODO: docs
/// {@endtemplate}
class PressKey extends Action {
/// {@macro press_key}
const PressKey({
required this.key,
this.downFor = 1,
});

/// The key that is being pressed.
final String key;

/// For how long the key will be down.
final int downFor;

@override
Future<bool> execute(Tester tester) async {
final logicalKey = LogicalKeyboardKey.knownLogicalKeys.firstWhere((key) {
return key.debugName?.toLowerCase() == this.key.toLowerCase();
});

final physicalKey = PhysicalKeyboardKey.knownPhysicalKeys.firstWhere((key) {
return logicalKey.debugName == key.debugName;
});

await tester.emitKeyEvent(
KeyDownEvent(
physicalKey: physicalKey,
logicalKey: logicalKey,
timeStamp: Duration.zero,
),
);

await tester.pump(duration: Duration(milliseconds: downFor));

await tester.emitKeyEvent(
KeyUpEvent(
physicalKey: physicalKey,
logicalKey: logicalKey,
timeStamp: Duration.zero,
),
);

return true;
}

@override
String description() => 'Press key: $key';
}
2 changes: 2 additions & 0 deletions packages/fluttium/lib/src/registry.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:collection';

import 'package:fluttium/fluttium.dart';
import 'package:fluttium/src/actions/press_key.dart';

/// {@template registry}
/// The registry of all the actions a [Tester] can perform.
Expand All @@ -15,6 +16,7 @@ class Registry {
'expectVisible': ActionRegistration(ExpectVisible.new, #text),
'expectNotVisible': ActionRegistration(ExpectNotVisible.new, #text),
'takeScreenshot': ActionRegistration(TakeScreenshot.new, #fileName),
'pressKey': ActionRegistration(PressKey.new, #key),
};

/// Map of all the action that are registered.
Expand Down
Loading