Skip to content

Commit

Permalink
Added more test cases for dashboard grid, dashboard page, and editabl…
Browse files Browse the repository at this point in the history
…e tab bar
  • Loading branch information
Gold872 committed Aug 19, 2023
1 parent 53de9da commit 91205ee
Show file tree
Hide file tree
Showing 6 changed files with 498 additions and 27 deletions.
167 changes: 143 additions & 24 deletions test/pages/dashboard_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,51 @@ import 'dart:io';
import 'package:elastic_dashboard/pages/dashboard_page.dart';
import 'package:elastic_dashboard/services/field_images.dart';
import 'package:elastic_dashboard/services/globals.dart';
import 'package:elastic_dashboard/widgets/dashboard_grid.dart';
import 'package:elastic_dashboard/widgets/draggable_dialog.dart';
import 'package:elastic_dashboard/widgets/draggable_widget_container.dart';
import 'package:elastic_dashboard/widgets/editable_tab_bar.dart';
import 'package:elastic_dashboard/widgets/network_tree/network_table_tree.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/combo_box_chooser.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../test_util.dart';

void main() async {
void main() {
TestWidgetsFlutterBinding.ensureInitialized();

await FieldImages.loadFields('assets/fields/');

String jsonFilePath =
'${Directory.current.path}/test_resources/test-layout.json';

String jsonString = File(jsonFilePath).readAsStringSync();
late String jsonString;

testWidgets('Dashboard page offline', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
setupMockOfflineNT4();
late SharedPreferences preferences;

setUpAll(() async {
await FieldImages.loadFields('assets/fields/');

jsonString = File(jsonFilePath).readAsStringSync();

SharedPreferences.setMockInitialValues({
'layout': jsonString,
PrefKeys.layout: jsonString,
PrefKeys.teamNumber: 353,
PrefKeys.ipAddress: '10.3.53.2',
});

final preferences = await SharedPreferences.getInstance();
preferences = await SharedPreferences.getInstance();
});

testWidgets('Dashboard page loading offline', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
setupMockOfflineNT4();

await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DashboardPage(
connectionStream: Stream.value(false), preferences: preferences),
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
),
),
);
Expand All @@ -51,23 +63,15 @@ void main() async {
expect(find.text('Autonomous'), findsOneWidget);
});

testWidgets('Dashboard page online', (widgetTester) async {
testWidgets('Dashboard page loading online', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
setupMockOnlineNT4();

SharedPreferences.setMockInitialValues({
PrefKeys.layout: jsonString,
PrefKeys.teamNumber: 353,
PrefKeys.ipAddress: '10.3.53.2',
});

final preferences = await SharedPreferences.getInstance();

await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DashboardPage(
connectionStream: Stream.value(true), preferences: preferences),
home: DashboardPage(
connectionStream: Stream.value(true),
preferences: preferences,
),
),
);
Expand All @@ -82,4 +86,119 @@ void main() async {
expect(find.text('Teleoperated'), findsOneWidget);
expect(find.text('Autonomous'), findsOneWidget);
});

testWidgets('Add widget dialog', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
setupMockOnlineNT4();

await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(true),
preferences: preferences,
),
),
);

await widgetTester.pumpAndSettle();

expect(find.text('Add Widget'), findsOneWidget);
expect(find.widgetWithText(DraggableDialog, 'Add Widget'), findsNothing);

await widgetTester.tap(find.text('Add Widget'));

await widgetTester.pumpAndSettle();

expect(find.widgetWithText(DraggableDialog, 'Add Widget'), findsOneWidget);

final smartDashboardTile = find.widgetWithText(TreeTile, 'SmartDashboard');

expect(smartDashboardTile, findsOneWidget);

await widgetTester.tap(smartDashboardTile);
await widgetTester.pumpAndSettle();

final testValueTile = find.widgetWithText(TreeTile, 'Test Value 1');
final testValueContainer =
find.widgetWithText(WidgetContainer, 'Test Value 1');

expect(testValueTile, findsOneWidget);
expect(find.widgetWithText(TreeTile, 'Test Value 2'), findsOneWidget);

await widgetTester.drag(testValueTile, const Offset(100, 100));
await widgetTester.pumpAndSettle();

expect(testValueContainer, findsNothing);

await widgetTester.drag(testValueTile, const Offset(300, -150));
await widgetTester.pumpAndSettle(const Duration(seconds: 5));

expect(testValueContainer, findsOneWidget);

final dialogDragHandle = find.byIcon(Icons.drag_handle);

expect(dialogDragHandle, findsOneWidget);

await widgetTester.drag(dialogDragHandle, const Offset(100, 0));
await widgetTester.pumpAndSettle();
});

testWidgets('Changing tabs', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
setupMockOfflineNT4();

await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
),
),
);

await widgetTester.pumpAndSettle();

expect(find.byType(ComboBoxChooser), findsNothing);

expect(find.byType(EditableTabBar), findsOneWidget);

final autonomousTab = find.widgetWithText(AnimatedContainer, 'Autonomous');

expect(autonomousTab, findsOneWidget);

await widgetTester.tap(autonomousTab);
await widgetTester.pumpAndSettle();

expect(find.byType(ComboBoxChooser), findsOneWidget);
});

testWidgets('Creating new tab', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
setupMockOfflineNT4();

await widgetTester.pumpWidget(
MaterialApp(
home: DashboardPage(
connectionStream: Stream.value(false),
preferences: preferences,
),
),
);

await widgetTester.pumpAndSettle();

expect(find.byType(DashboardGrid, skipOffstage: false), findsNWidgets(2));

expect(find.byType(EditableTabBar), findsOneWidget);

final createNewTabButton = find.descendant(
of: find.byType(EditableTabBar), matching: find.byIcon(Icons.add));

expect(createNewTabButton, findsOneWidget);

await widgetTester.tap(createNewTabButton);
await widgetTester.pumpAndSettle();

expect(find.byType(DashboardGrid, skipOffstage: false), findsNWidgets(3));
});
}
13 changes: 13 additions & 0 deletions test/test_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ void setupMockOnlineNT4() {
final mockNT4Client = MockNT4Client();
final mockSubscription = MockNT4Subscription();

when(mockNT4Client.announcedTopics).thenReturn({
1: NT4Topic(
name: '/SmartDashboard/Test Value 1',
type: NT4TypeStr.kInt,
properties: {},
),
2: NT4Topic(
name: '/SmartDashboard/Test Value 2',
type: NT4TypeStr.kFloat32,
properties: {},
),
});

when(mockSubscription.periodicStream()).thenAnswer((_) => Stream.value(null));

when(mockNT4Connection.nt4Client).thenReturn(mockNT4Client);
Expand Down
4 changes: 2 additions & 2 deletions test/widgets/dashboard_grid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ void main() {

// await widgetTester.pumpAndSettle();

expect(find.bySubtype<DraggableWidgetContainer>(), findsNWidgets(6));
expect(find.bySubtype<NT4Widget>(), findsNWidgets(6));
expect(find.bySubtype<DraggableWidgetContainer>(), findsNWidgets(9));
expect(find.bySubtype<NT4Widget>(), findsNWidgets(9));

expect(find.bySubtype<TextDisplay>(), findsOneWidget);
expect(find.bySubtype<BooleanBox>(), findsOneWidget);
Expand Down
Loading

0 comments on commit 91205ee

Please sign in to comment.