-
Notifications
You must be signed in to change notification settings - Fork 22
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
[RSDK-4265] Sensor widget tests #81
Changes from 7 commits
4bd492f
bc0786a
4e12935
8b36791
aa32e0d
5d0865a
2898b14
438beeb
3d810e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
int generateTestingPortFromName(String name) => 50000 + (name.hashCode % 10000); | ||
|
||
class TestableWidget extends StatelessWidget { | ||
const TestableWidget({ | ||
super.key, | ||
required this.child, | ||
}); | ||
|
||
final Widget child; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
title: 'Flutter Demo', | ||
home: Scaffold( | ||
body: child, | ||
), | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:viam_sdk/widgets.dart'; | ||
|
||
import '../test_utils.dart'; | ||
import '../unit_test/components/sensor_test.dart'; | ||
|
||
void main() { | ||
group('ViamSensorWidget', () { | ||
testWidgets('displays data', (tester) async { | ||
final widget = TestableWidget(child: ViamSensorWidget(sensor: FakeSensor('sensor'))); | ||
await tester.pumpWidget(widget); | ||
|
||
final dataTable = find.byType(DataTable); | ||
|
||
expect(dataTable, findsOneWidget); | ||
}); | ||
|
||
testWidgets('shows last refresh time', (tester) async { | ||
final widget = TestableWidget(child: ViamSensorWidget(sensor: FakeSensor('sensor'), showLastRefreshed: true)); | ||
await tester.pumpWidget(widget); | ||
|
||
final refreshButton = find.widgetWithIcon(ViamButton, Icons.refresh); | ||
await tester.press(refreshButton); | ||
await tester.pump(); | ||
|
||
final refreshTime = find.textContaining(RegExp(r'Updated at:.*')); | ||
|
||
expect(refreshTime, findsOneWidget); | ||
}); | ||
|
||
testWidgets('hides last refresh time', (tester) async { | ||
final widget = TestableWidget(child: ViamSensorWidget(sensor: FakeSensor('sensor'), showLastRefreshed: false)); | ||
await tester.pumpWidget(widget); | ||
|
||
final refreshButton = find.widgetWithIcon(ViamButton, Icons.refresh); | ||
await tester.press(refreshButton); | ||
await tester.pump(); | ||
|
||
final refreshTime = find.textContaining(RegExp(r'Updated at:.*')); | ||
|
||
expect(refreshTime, findsNothing); | ||
}); | ||
|
||
testWidgets('shows refresh controls', (tester) async { | ||
final widget = TestableWidget(child: ViamSensorWidget(sensor: FakeSensor('sensor'), showRefreshControls: true)); | ||
await tester.pumpWidget(widget); | ||
|
||
final playButton = find.widgetWithIcon(ViamButton, Icons.play_arrow); | ||
final pauseButton = find.widgetWithIcon(ViamButton, Icons.pause); | ||
final refreshButton = find.widgetWithIcon(ViamButton, Icons.refresh); | ||
|
||
expect(playButton, findsNothing); | ||
expect(pauseButton, findsOneWidget); | ||
expect(refreshButton, findsOneWidget); | ||
|
||
await tester.press(pauseButton); | ||
await tester.pump(); | ||
|
||
expect(playButton, findsNothing); | ||
expect(pauseButton, findsOneWidget); | ||
}); | ||
|
||
testWidgets('hides refresh controls', (tester) async { | ||
final widget = TestableWidget(child: ViamSensorWidget(sensor: FakeSensor('sensor'), showRefreshControls: false)); | ||
await tester.pumpWidget(widget); | ||
|
||
final playButton = find.widgetWithIcon(ViamButton, Icons.play_arrow); | ||
final pauseButton = find.widgetWithIcon(ViamButton, Icons.pause); | ||
final refreshButton = find.widgetWithIcon(ViamButton, Icons.refresh); | ||
|
||
expect(playButton, findsNothing); | ||
expect(pauseButton, findsNothing); | ||
expect(refreshButton, findsNothing); | ||
}); | ||
|
||
testWidgets('hides play/pause when refresh interval is null', (tester) async { | ||
final widget = TestableWidget(child: ViamSensorWidget(sensor: FakeSensor('sensor'), refreshInterval: null)); | ||
await tester.pumpWidget(widget); | ||
|
||
final playButton = find.widgetWithIcon(ViamButton, Icons.play_arrow); | ||
final pauseButton = find.widgetWithIcon(ViamButton, Icons.pause); | ||
|
||
expect(playButton, findsNothing); | ||
expect(pauseButton, findsNothing); | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (q) maybe obvious but does it also hide the refresh button? should we still test for that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope! When There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But you are correct that we should have a test to make sure the refresh button is visible so I added it :) |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(q) how come you aren't calling tester.pump() in this test like you are in the other ones?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tester.pump
only needs to be called if the state changes in order to reevaluate the UI with the new state see docs