Skip to content

Commit

Permalink
Merge pull request #1106 from nextcloud/fix/neon_dashboard/goldens
Browse files Browse the repository at this point in the history
  • Loading branch information
provokateurin authored Nov 4, 2023
2 parents 00e58b5 + 86385b1 commit 98d2d95
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
11 changes: 6 additions & 5 deletions packages/neon/neon/lib/src/widgets/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,20 @@ class NeonCachedImage extends StatefulWidget {
final CacheWriter? writeCache,
final String cacheKey,
) async {
final cached = await checkCache?.call(_cacheManager) ?? await _defaultCacheReviver(cacheKey);
final cached = await checkCache?.call(cacheManager) ?? await _defaultCacheReviver(cacheKey);
if (cached != null) {
return cached;
}

final data = await getImage();

unawaited(writeCache?.call(_cacheManager, data) ?? _defaultCacheWriter(data, cacheKey));
unawaited(writeCache?.call(cacheManager, data) ?? _defaultCacheWriter(data, cacheKey));

return data;
}

static Future<Uint8List?> _defaultCacheReviver(final String cacheKey) async {
final cacheFile = await _cacheManager.getFileFromCache(cacheKey);
final cacheFile = await cacheManager.getFileFromCache(cacheKey);
if (cacheFile != null && cacheFile.validTill.isAfter(DateTime.now())) {
return cacheFile.file.readAsBytes();
}
Expand All @@ -120,14 +120,15 @@ class NeonCachedImage extends StatefulWidget {
final Uint8List data,
final String cacheKey,
) async {
await _cacheManager.putFile(
await cacheManager.putFile(
cacheKey,
data,
maxAge: const Duration(days: 7),
);
}

static final _cacheManager = DefaultCacheManager();
@visibleForTesting
static DefaultCacheManager cacheManager = DefaultCacheManager();

@override
State<NeonCachedImage> createState() => _NeonCachedImageState();
Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon_dashboard/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies:
dev_dependencies:
build_runner: ^2.4.6
built_collection: ^5.1.1
flutter_cache_manager: ^3.3.1
flutter_test:
sdk: flutter
go_router_builder: ^2.3.4
Expand Down
29 changes: 17 additions & 12 deletions packages/neon/neon_dashboard/test/widget_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:built_collection/built_collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:neon/blocs.dart';
Expand All @@ -16,6 +17,8 @@ import 'package:rxdart/rxdart.dart';

class MockAccountsBloc extends Mock implements AccountsBloc {}

class MockCacheManager extends Mock implements DefaultCacheManager {}

Widget wrapWidget(final AccountsBloc accountsBloc, final Widget child) => MaterialApp(
localizationsDelegates: DashboardLocalizations.localizationsDelegates,
supportedLocales: DashboardLocalizations.supportedLocales,
Expand All @@ -29,6 +32,8 @@ Widget wrapWidget(final AccountsBloc accountsBloc, final Widget child) => Materi
);

void main() {
NeonCachedImage.cacheManager = MockCacheManager();

final accountsBloc = MockAccountsBloc();
when(() => accountsBloc.activeAccount).thenAnswer(
(final invocation) => BehaviorSubject.seeded(
Expand Down Expand Up @@ -83,7 +88,7 @@ void main() {
);
expect(find.byType(NeonCachedImage), findsNWidgets(2));

expect(find.byType(DashboardWidgetItem), matchesGoldenFile('goldens/widget_item.png'));
await expectLater(find.byType(DashboardWidgetItem), matchesGoldenFile('goldens/widget_item.png'));
});

testWidgets('Not round', (final tester) async {
Expand All @@ -106,7 +111,7 @@ void main() {
),
);

expect(find.byType(DashboardWidgetItem), matchesGoldenFile('goldens/widget_item_not_round.png'));
await expectLater(find.byType(DashboardWidgetItem), matchesGoldenFile('goldens/widget_item_not_round.png'));
});

testWidgets('Without link', (final tester) async {
Expand Down Expand Up @@ -166,7 +171,7 @@ void main() {
expect(find.byIcon(Icons.add), findsOneWidget);
expect(find.text('Button'), findsOneWidget);

expect(find.byType(DashboardWidgetButton), matchesGoldenFile('goldens/widget_button_new.png'));
await expectLater(find.byType(DashboardWidgetButton), matchesGoldenFile('goldens/widget_button_new.png'));
});

testWidgets('More', (final tester) async {
Expand All @@ -182,7 +187,7 @@ void main() {
expect(find.byIcon(Icons.more_outlined), findsOneWidget);
expect(find.text('Button'), findsOneWidget);

expect(find.byType(DashboardWidgetButton), matchesGoldenFile('goldens/widget_button_more.png'));
await expectLater(find.byType(DashboardWidgetButton), matchesGoldenFile('goldens/widget_button_more.png'));
});

testWidgets('Setup', (final tester) async {
Expand All @@ -198,7 +203,7 @@ void main() {
expect(find.byIcon(Icons.launch), findsOneWidget);
expect(find.text('Button'), findsOneWidget);

expect(find.byType(DashboardWidgetButton), matchesGoldenFile('goldens/widget_button_setup.png'));
await expectLater(find.byType(DashboardWidgetButton), matchesGoldenFile('goldens/widget_button_setup.png'));
});

testWidgets('Invalid', (final tester) async {
Expand All @@ -214,7 +219,7 @@ void main() {
expect(find.byType(Icon), findsNothing);
expect(find.text('Button'), findsOneWidget);

expect(find.byType(DashboardWidgetButton), matchesGoldenFile('goldens/widget_button_invalid.png'));
await expectLater(find.byType(DashboardWidgetButton), matchesGoldenFile('goldens/widget_button_invalid.png'));
});
});

Expand Down Expand Up @@ -290,7 +295,7 @@ void main() {
expect(find.byIcon(Icons.add), findsOneWidget);
expect(find.text('Button'), findsOneWidget);

expect(find.byType(DashboardWidget), matchesGoldenFile('goldens/widget.png'));
await expectLater(find.byType(DashboardWidget), matchesGoldenFile('goldens/widget.png'));
});

testWidgets('Without widgetUrl', (final tester) async {
Expand Down Expand Up @@ -334,7 +339,7 @@ void main() {
),
);

expect(find.byType(DashboardWidget), matchesGoldenFile('goldens/widget_not_round.png'));
await expectLater(find.byType(DashboardWidget), matchesGoldenFile('goldens/widget_not_round.png'));
});

testWidgets('With halfEmptyContentMessage', (final tester) async {
Expand All @@ -351,7 +356,7 @@ void main() {
expect(find.text('Half empty'), findsOneWidget);
expect(find.byIcon(Icons.check), findsOneWidget);

expect(find.byType(DashboardWidget), matchesGoldenFile('goldens/widget_with_half_empty.png'));
await expectLater(find.byType(DashboardWidget), matchesGoldenFile('goldens/widget_with_half_empty.png'));
});

testWidgets('With emptyContentMessage', (final tester) async {
Expand All @@ -368,7 +373,7 @@ void main() {
expect(find.text('Empty'), findsOneWidget);
expect(find.byIcon(Icons.check), findsOneWidget);

expect(find.byType(DashboardWidget), matchesGoldenFile('goldens/widget_with_empty.png'));
await expectLater(find.byType(DashboardWidget), matchesGoldenFile('goldens/widget_with_empty.png'));
});

testWidgets('Without items', (final tester) async {
Expand All @@ -385,7 +390,7 @@ void main() {
expect(find.text('No entries'), findsOneWidget);
expect(find.byIcon(Icons.check), findsOneWidget);

expect(find.byType(DashboardWidget), matchesGoldenFile('goldens/widget_without_items.png'));
await expectLater(find.byType(DashboardWidget), matchesGoldenFile('goldens/widget_without_items.png'));
});

testWidgets('Without buttons', (final tester) async {
Expand All @@ -401,7 +406,7 @@ void main() {

expect(find.bySubtype<FilledButton>(), findsNothing);

expect(find.byType(DashboardWidget), matchesGoldenFile('goldens/widget_without_buttons.png'));
await expectLater(find.byType(DashboardWidget), matchesGoldenFile('goldens/widget_without_buttons.png'));
});
});
}

0 comments on commit 98d2d95

Please sign in to comment.