Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/material_ui/example/lib/app/app.snippet.0.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:material_ui/material_ui.dart';

/// Flutter code sample for [MaterialApp].

class MaterialAppExample extends StatelessWidget {
const MaterialAppExample({super.key});

@override
Widget build(BuildContext context) {
return
// #region body
MaterialApp(
home: Scaffold(appBar: AppBar(title: const Text('Home'))),
debugShowCheckedModeBanner: false,
)
// #endregion body
;
}
}
29 changes: 29 additions & 0 deletions packages/material_ui/example/lib/app/app.snippet.1.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:material_ui/material_ui.dart';

/// Flutter code sample for [MaterialApp].

class MaterialAppExample extends StatelessWidget {
const MaterialAppExample({super.key});

@override
Widget build(BuildContext context) {
return
// #region body
MaterialApp(
routes: <String, WidgetBuilder>{
'/': (BuildContext context) {
return Scaffold(appBar: AppBar(title: const Text('Home Route')));
},
'/about': (BuildContext context) {
return Scaffold(appBar: AppBar(title: const Text('About Route')));
},
},
)
// #endregion body
;
}
}
26 changes: 26 additions & 0 deletions packages/material_ui/example/lib/app/app.snippet.2.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:material_ui/material_ui.dart';

/// Flutter code sample for [MaterialApp].

class MaterialAppExample extends StatelessWidget {
const MaterialAppExample({super.key});

@override
Widget build(BuildContext context) {
return
// #region body
MaterialApp(
theme: ThemeData(
brightness: Brightness.dark,
primaryColor: Colors.blueGrey,
),
home: Scaffold(appBar: AppBar(title: const Text('MaterialApp Theme'))),
)
// #endregion body
;
}
}
29 changes: 29 additions & 0 deletions packages/material_ui/example/lib/app/app.snippet.3.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/services.dart';
import 'package:material_ui/material_ui.dart';

/// Flutter code sample for [MaterialApp.shortcuts].

class MaterialAppExample extends StatelessWidget {
const MaterialAppExample({super.key});

// #region body
@override
Widget build(BuildContext context) {
return WidgetsApp(
shortcuts: <ShortcutActivator, Intent>{
...WidgetsApp.defaultShortcuts,
const SingleActivator(LogicalKeyboardKey.keyE):
const ActivateIntent(),
},
color: const Color(0xFFFF0000),
builder: (BuildContext context, Widget? child) {
return const Placeholder();
},
);
}
// #endregion body
}
35 changes: 35 additions & 0 deletions packages/material_ui/example/lib/app/app.snippet.4.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:material_ui/material_ui.dart';

/// Flutter code sample for [MaterialApp.actions].

class MaterialAppExample extends StatelessWidget {
const MaterialAppExample({super.key, this.onActivated});

final VoidCallback? onActivated;

// #region body
@override
Widget build(BuildContext context) {
return WidgetsApp(
actions: <Type, Action<Intent>>{
...WidgetsApp.defaultActions,
ActivateIntent: CallbackAction<Intent>(
onInvoke: (Intent intent) {
// Do something here...
onActivated?.call();
return null;
},
),
},
color: const Color(0xFFFF0000),
builder: (BuildContext context, Widget? child) {
return const Placeholder();
},
);
}
// #endregion body
}
19 changes: 19 additions & 0 deletions packages/material_ui/example/test/app/app.snippet.0_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:material_ui/material_ui.dart';
import 'package:material_ui_examples/app/app.snippet.0.dart' as example;

void main() {
testWidgets(
'MaterialApp displays home route and disables debug mode banner',
(WidgetTester tester) async {
await tester.pumpWidget(const example.MaterialAppExample());

expect(find.byType(CheckedModeBanner), findsNothing);
expect(find.widgetWithText(AppBar, 'Home'), findsOneWidget);
},
);
}
24 changes: 24 additions & 0 deletions packages/material_ui/example/test/app/app.snippet.1_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:material_ui/material_ui.dart';
import 'package:material_ui_examples/app/app.snippet.1.dart' as example;

void main() {
testWidgets('MaterialApp routes map navigates between Home and About', (
WidgetTester tester,
) async {
await tester.pumpWidget(const example.MaterialAppExample());

expect(find.widgetWithText(AppBar, 'Home Route'), findsOneWidget);
expect(find.widgetWithText(AppBar, 'About Route'), findsNothing);

final BuildContext context = tester.element(find.text('Home Route'));
Navigator.of(context).pushNamed('/about');
Comment on lines +18 to +19

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using tester.element(find.text('Home Route')) to obtain a BuildContext for navigation makes the test fragile, as it relies on the specific text content of the UI. A more robust and idiomatic approach in Flutter widget tests is to access the NavigatorState directly using tester.state.

Suggested change
final BuildContext context = tester.element(find.text('Home Route'));
Navigator.of(context).pushNamed('/about');
tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/about');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this isn't significant.

await tester.pumpAndSettle();

expect(find.widgetWithText(AppBar, 'About Route'), findsOneWidget);
});
}
22 changes: 22 additions & 0 deletions packages/material_ui/example/test/app/app.snippet.2_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:material_ui/material_ui.dart';
import 'package:material_ui_examples/app/app.snippet.2.dart' as example;

void main() {
testWidgets('MaterialApp applies custom ThemeData', (
WidgetTester tester,
) async {
await tester.pumpWidget(const example.MaterialAppExample());

expect(find.widgetWithText(AppBar, 'MaterialApp Theme'), findsOneWidget);

final BuildContext context = tester.element(find.text('MaterialApp Theme'));
final ThemeData theme = Theme.of(context);
Comment on lines +17 to +18

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Obtaining the BuildContext from a specific text widget makes the test fragile if the text content changes. It is more robust to obtain the context from a structural widget like Scaffold.

Suggested change
final BuildContext context = tester.element(find.text('MaterialApp Theme'));
final ThemeData theme = Theme.of(context);
final BuildContext context = tester.element(find.byType(Scaffold));
final ThemeData theme = Theme.of(context);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this isn't significant.

expect(theme.brightness, Brightness.dark);
expect(theme.primaryColor, Colors.blueGrey);
});
}
38 changes: 38 additions & 0 deletions packages/material_ui/example/test/app/app.snippet.3_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:material_ui/material_ui.dart';
import 'package:material_ui_examples/app/app.snippet.3.dart' as example;

void main() {
testWidgets('WidgetsApp shortcuts maps select key to ActivateIntent', (
WidgetTester tester,
) async {
bool invoked = false;
await tester.pumpWidget(
Actions(
actions: <Type, Action<Intent>>{
ActivateIntent: CallbackAction<ActivateIntent>(
onInvoke: (ActivateIntent intent) {
invoked = true;
return null;
},
),
},
child: const example.MaterialAppExample(),
),
);

final Element placeholderElement = tester.element(find.byType(Placeholder));
final FocusNode focusNode = Focus.of(placeholderElement);
focusNode.canRequestFocus = true;
focusNode.requestFocus();
await tester.pump();

await tester.sendKeyEvent(LogicalKeyboardKey.keyE);
expect(invoked, isTrue);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe we can test this against iOS like

Suggested change
});
}, variant: TargetPlatformVariant.only(TargetPlatform.iOS));

This test still passes if the snippet's one extra shortcut, const SingleActivator(LogicalKeyboardKey.select): const ActivateIntent(), is removed because the same entry is already in WidgetsApp.defaultShortcuts on Android

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good point! I've changed the activating key from Select to keyE, which should be more useful as an example than having the platform set to iOS.

}
32 changes: 32 additions & 0 deletions packages/material_ui/example/test/app/app.snippet.4_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:material_ui/material_ui.dart';
import 'package:material_ui_examples/app/app.snippet.4.dart' as example;

void main() {
testWidgets('WidgetsApp actions handles ActivateIntent', (
WidgetTester tester,
) async {
bool activated = false;
await tester.pumpWidget(
example.MaterialAppExample(
onActivated: () {
activated = true;
},
),
);

final Element placeholderElement = tester.element(find.byType(Placeholder));
final FocusNode focusNode = Focus.of(placeholderElement);
focusNode.canRequestFocus = true;
focusNode.requestFocus();
await tester.pump();

await tester.sendKeyEvent(LogicalKeyboardKey.space);
expect(activated, isTrue);
});
}
Loading
Loading