Skip to content

Commit

Permalink
load font & add goldens
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi Rousselet committed Feb 23, 2020
1 parent 464ff6d commit cbb2f28
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 18 deletions.
Binary file added assets/Roboto-Regular.ttf
Binary file not shown.
54 changes: 54 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_portal/flutter_portal.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
final notifier = ValueNotifier<Widget>(const Text('first'));

@override
void initState() {
super.initState();
Future.microtask(() {
notifier.value = PortalEntry(
portal: const Text('overlay'),
child: const Text('second'),
);
});
}

@override
Widget build(BuildContext context) {
final builder = ValueListenableBuilder<Widget>(
valueListenable: notifier,
builder: (_, child, __) => child,
);
return Boilerplate(
child: Portal(
child: Center(child: builder),
),
);
}
}

class Boilerplate extends StatelessWidget {
final Widget child;

const Boilerplate({Key key, this.child}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: child,
),
);
}
}
3 changes: 1 addition & 2 deletions lib/src/portal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ class RenderPortalTheater extends RenderProxyBox {

@override
bool hitTestChildren(BoxHitTestResult result, {Offset position}) {
print(position);
for (final overlay in overlayLink.overlays) {
if (overlay?.hitTest(result, position: position) ?? false) {
return true;
Expand All @@ -123,7 +122,7 @@ class RenderPortalTheater extends RenderProxyBox {
class PortalEntry<T extends Portal> extends SingleChildRenderObjectWidget {
PortalEntry({
Key key,
bool visible = false,
bool visible = true,
this.childAnchor,
this.portalAnchor,
Widget portal,
Expand Down
Binary file added test/mounted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/mounted_no_rebuild.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/unmounted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 40 additions & 16 deletions test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,34 @@
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_portal/src/portal.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_portal/flutter_portal.dart';
import 'package:mockito/mockito.dart';

void main() {
Future<ByteData> fetchFont() async {
final roboto = File.fromUri(
Uri.parse('${Directory.current.path}/../assets/Roboto-Regular.ttf'),
);
final bytes = Uint8List.fromList(await roboto.readAsBytes());
return ByteData.view(bytes.buffer);
}

/// Fetch Roboto font from local cache, or from the internet, if it's not
/// found in the cache.
/// It needs to be done because flutter_test blocks access to package assets
/// (see https://github.com/flutter/flutter/issues/12999).
void main() async {
final fontLoader = FontLoader('Roboto')..addFont(fetchFont());
await fontLoader.load();

testWidgets('PortalProvider updates child', (WidgetTester tester) async {
await tester.pumpWidget(
const Portal(
Expand Down Expand Up @@ -84,10 +104,8 @@ void main() {
tester.getCenter(find.text('firstChild')),
const Offset(400, 300),
);
expect(
tester.getSize(find.text('firstChild')),
const Size(140, 14),
);

await expectLater(find.byType(Portal), matchesGoldenFile('mounted.png'));
});
testWidgets(
"portals aren't inserted if mounted is false, and visible can be changed any time",
Expand Down Expand Up @@ -181,6 +199,8 @@ void main() {
expect(find.text('child'), findsNothing);
expect(find.text('portal'), findsNothing);
expect(find.text('newChild'), findsOneWidget);

await expectLater(find.byType(Portal), matchesGoldenFile('unmounted.png'));
});
testWidgets('throws if no PortalEntry were found', (tester) async {
await tester.pumpWidget(
Expand Down Expand Up @@ -211,36 +231,40 @@ Error: Could not find a Portal above this PortalEntry<Portal>(portalAnchor: null

expect(find.text('child'), findsOneWidget);
expect(find.text('portal'), findsOneWidget);
}, skip: true);
});
testWidgets(
'can insert a portal without rebuilding PortalProvider at the same time',
(tester) async {
Widget child = const Text('first');
final builder = Builder(builder: (_) => child);
var child = ValueNotifier<Widget>(const Text('first'));
final builder = ValueListenableBuilder<Widget>(
valueListenable: child,
builder: (_, child, __) => child,
);

await tester.pumpWidget(
Boilerplate(
child: Portal(
child: builder,
child: Center(child: builder),
),
),
);
final element = tester.element(find.byWidget(builder));

expect(find.text('first'), findsOneWidget);

element.markNeedsBuild();
child = PortalEntry(
child.value = PortalEntry(
portal: const Text('portal'),
child: const Text('second'),
);

await tester.pump();

expect(find.text('first'), findsNothing);
expect(find.text('second'), findsOneWidget);
expect(find.text('portal'), findsOneWidget);
}, skip: true);
await expectLater(
find.byType(Portal),
matchesGoldenFile('mounted_no_rebuild.png'),
);
});
testWidgets('clicking on portal if above child clicks only the portal',
(tester) async {
var portalClickCount = 0;
Expand All @@ -267,7 +291,7 @@ Error: Could not find a Portal above this PortalEntry<Portal>(portalAnchor: null

expect(portalClickCount, equals(1));
expect(childClickCount, equals(0));
}, skip: true);
});
testWidgets('if portal is not above child, we can click on both',
(tester) async {
var portalClickCount = 0;
Expand Down Expand Up @@ -435,7 +459,7 @@ Error: Could not find a Portal above this PortalEntry<Portal>(portalAnchor: null
tester.getTopLeft(find.byKey(portalKey)),
equals(Offset.zero),
);
}, skip: true);
});
testWidgets('click works when switching between anchor/fill', (tester) async {
final child = const Text('a', textDirection: TextDirection.ltr);
const portalKey = Key('portal');
Expand Down

0 comments on commit cbb2f28

Please sign in to comment.