Add test for platform_menu_bar.0.dart (#157328)

Contributes to https://github.com/flutter/flutter/issues/130459

It adds a test for
- `examples/api/test/material/platform_menu_bar/platform_menu_bar.0_test.dart`
This commit is contained in:
Valentin Vignal 2024-10-23 14:57:13 +08:00 committed by GitHub
parent 7504abcc14
commit e016ac3035
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 124 additions and 1 deletions

View File

@ -310,7 +310,6 @@ class SampleChecker {
// See https://github.com/flutter/flutter/issues/130459
final Set<String> _knownMissingTests = <String>{
'examples/api/test/material/color_scheme/dynamic_content_color.0_test.dart',
'examples/api/test/material/platform_menu_bar/platform_menu_bar.0_test.dart',
'examples/api/test/painting/star_border/star_border.0_test.dart',
'examples/api/test/widgets/navigator/navigator.restorable_push_and_remove_until.0_test.dart',
'examples/api/test/widgets/navigator/navigator.restorable_push.0_test.dart',

View File

@ -0,0 +1,124 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_api_samples/material/platform_menu_bar/platform_menu_bar.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late _FakeMenuChannel fakeMenuChannel;
late PlatformMenuDelegate originalDelegate;
late DefaultPlatformMenuDelegate delegate;
setUp(() {
fakeMenuChannel = _FakeMenuChannel((MethodCall call) async {});
delegate = DefaultPlatformMenuDelegate(channel: fakeMenuChannel);
originalDelegate = WidgetsBinding.instance.platformMenuDelegate;
WidgetsBinding.instance.platformMenuDelegate = delegate;
});
tearDown(() {
WidgetsBinding.instance.platformMenuDelegate = originalDelegate;
});
testWidgets('PlatformMenuBar creates a menu', (WidgetTester tester) async {
await tester.pumpWidget(
const example.ExampleApp(),
);
expect(
find.text('This space intentionally left blank.\nShow a message here using the menu.'),
findsOne,
);
expect(find.byType(PlatformMenuBar), findsOne);
expect(
fakeMenuChannel.outgoingCalls.last.method,
'Menu.setMenus',
);
expect(
fakeMenuChannel.outgoingCalls.last.arguments,
equals(const <String, Object?>{
'0': <Map<String, Object>>[
<String, Object>{
'id': 11,
'label': 'Flutter API Sample',
'enabled': true,
'children': <Map<String, Object>>[
<String, Object>{'id': 2, 'label': 'About', 'enabled': true},
<String, Object>{'id': 3, 'isDivider': true},
<String, Object>{
'id': 5,
'label': 'Show Message',
'enabled': true,
'shortcutCharacter': 'm',
'shortcutModifiers': 0,
},
<String, Object>{
'id': 8,
'label': 'Messages',
'enabled': true,
'children': <Map<String, Object>>[
<String, Object>{
'id': 6,
'label': 'I am not throwing away my shot.',
'enabled': true,
'shortcutTrigger': 49,
'shortcutModifiers': 1,
},
<String, Object>{
'id': 7,
'label': "There's a million things I haven't done, but just you wait.",
'enabled': true,
'shortcutTrigger': 50,
'shortcutModifiers': 1,
},
],
},
<String, Object>{'id': 9, 'isDivider': true},
<String, Object>{'id': 10, 'enabled': true, 'platformProvidedMenu': 1},
],
},
],
}),
);
}, variant: const TargetPlatformVariant(<TargetPlatform>{TargetPlatform.macOS}));
}
class _FakeMenuChannel implements MethodChannel {
_FakeMenuChannel(this.outgoing);
Future<dynamic> Function(MethodCall) outgoing;
Future<void> Function(MethodCall)? incoming;
List<MethodCall> outgoingCalls = <MethodCall>[];
@override
BinaryMessenger get binaryMessenger => throw UnimplementedError();
@override
MethodCodec get codec => const StandardMethodCodec();
@override
Future<List<T>> invokeListMethod<T>(String method, [dynamic arguments]) => throw UnimplementedError();
@override
Future<Map<K, V>> invokeMapMethod<K, V>(String method, [dynamic arguments]) => throw UnimplementedError();
@override
Future<T> invokeMethod<T>(String method, [dynamic arguments]) async {
final MethodCall call = MethodCall(method, arguments);
outgoingCalls.add(call);
return await outgoing(call) as T;
}
@override
String get name => 'flutter/menu';
@override
void setMethodCallHandler(Future<void> Function(MethodCall call)? handler) => incoming = handler;
}