Skip to content

Commit

Permalink
Added more tests for dashboard page & grid
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Aug 21, 2023
1 parent aa60611 commit 29ead74
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 9 deletions.
108 changes: 108 additions & 0 deletions test/pages/dashboard_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ 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/custom_appbar.dart';
import 'package:elastic_dashboard/widgets/dashboard_grid.dart';
import 'package:elastic_dashboard/widgets/dialog_widgets/dialog_text_input.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:elastic_dashboard/widgets/settings_dialog.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:titlebar_buttons/titlebar_buttons.dart';

import '../test_util.dart';

Expand Down Expand Up @@ -298,4 +301,109 @@ void main() {
expect(find.widgetWithText(AnimatedContainer, 'New Tab Name!'),
findsOneWidget);
});

testWidgets('Minimizing window', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
setupMockOfflineNT4();

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

await widgetTester.pumpAndSettle();

final minimizeButton = find.byType(DecoratedMinimizeButton);

expect(minimizeButton, findsOneWidget);

await widgetTester.tap(minimizeButton);
});

testWidgets('Maximizing/unmaximizing window', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
setupMockOfflineNT4();

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

await widgetTester.pumpAndSettle();

final appBar = find.byType(CustomAppBar);

expect(appBar, findsOneWidget);

await widgetTester.tap(appBar);
await widgetTester.pump(kDoubleTapMinTime);
await widgetTester.tap(appBar);

await widgetTester.pumpAndSettle();

final maximizeButton = find.byType(DecoratedMaximizeButton);

expect(maximizeButton, findsOneWidget);

await widgetTester.tap(maximizeButton);
});

testWidgets('Closing window', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
setupMockOfflineNT4();

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

await widgetTester.pumpAndSettle();

final closeButton = find.byType(DecoratedCloseButton);

expect(closeButton, findsOneWidget);

await widgetTester.tap(closeButton);
});

testWidgets('Opening settings', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
setupMockOfflineNT4();

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

await widgetTester.pumpAndSettle();

final settingsButton = find.widgetWithIcon(MenuItemButton, Icons.settings);

expect(settingsButton, findsOneWidget);

final settingsButtonWidget =
settingsButton.evaluate().first.widget as MenuItemButton;

settingsButtonWidget.onPressed?.call();

await widgetTester.pumpAndSettle();

expect(find.byType(SettingsDialog), findsOneWidget);
});
}
91 changes: 82 additions & 9 deletions test/widgets/dashboard_grid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,28 @@ import 'package:provider/provider.dart';

import '../test_util.dart';

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

testWidgets('Dashboard grid loading', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
widgetTester.view.physicalSize = const Size(1920, 1080);
widgetTester.view.devicePixelRatio = 1.0;
// WidgetController.hitTestWarningShouldBeFatal = true;
late String jsonString;
late Map<String, dynamic> jsonData;

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

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

String jsonString = File(filePath).readAsStringSync();
jsonString = File(filePath).readAsStringSync();
jsonData = jsonDecode(jsonString);
});

Map<String, dynamic> jsonData = jsonDecode(jsonString);
testWidgets('Dashboard grid loading', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
widgetTester.view.physicalSize = const Size(1920, 1080);
widgetTester.view.devicePixelRatio = 1.0;
// WidgetController.hitTestWarningShouldBeFatal = true;

expect(jsonData.containsKey('tabs'), true);

Expand Down Expand Up @@ -72,6 +76,28 @@ void main() {
expect(find.bySubtype<PowerDistribution>(), findsOneWidget);
expect(find.bySubtype<Gyro>(), findsOneWidget);
expect(find.bySubtype<PIDControllerWidget>(), findsOneWidget);
});

testWidgets('Editing properties', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
widgetTester.view.physicalSize = const Size(1920, 1080);
widgetTester.view.devicePixelRatio = 1.0;

await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ChangeNotifierProvider(
create: (context) => DashboardGridModel(),
child: DashboardGrid.fromJson(
key: GlobalKey(),
jsonData: jsonData['tabs'][0]['grid_layout'],
),
),
),
),
);

await widgetTester.pump(Duration.zero);

await widgetTester.ensureVisible(find.text('Test Number'));

Expand Down Expand Up @@ -132,4 +158,51 @@ void main() {
await widgetTester.tap(closeButton);
await widgetTester.pumpAndSettle();
});

testWidgets('Dragging widgets', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;
widgetTester.view.physicalSize = const Size(1920, 1080);
widgetTester.view.devicePixelRatio = 1.0;

await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ChangeNotifierProvider(
create: (context) => DashboardGridModel(),
child: DashboardGrid.fromJson(
key: GlobalKey(),
jsonData: jsonData['tabs'][0]['grid_layout'],
),
),
),
),
);

await widgetTester.pump(Duration.zero);

final gyroWidget = find.widgetWithText(WidgetContainer, 'Test Gyro');

expect(gyroWidget, findsOneWidget);

// Drag to a valid location
await widgetTester.drag(gyroWidget, const Offset(256, -128));

await widgetTester.pumpAndSettle();

expect(gyroWidget, findsOneWidget);

// Drag back to its original location
await widgetTester.drag(gyroWidget, const Offset(-256, 128));

await widgetTester.pumpAndSettle();

expect(gyroWidget, findsOneWidget);

// Drag to an invalid location
await widgetTester.drag(gyroWidget, const Offset(0, -128));

await widgetTester.pumpAndSettle();

expect(gyroWidget, findsOneWidget);
});
}

0 comments on commit 29ead74

Please sign in to comment.