From e016ac3035ffe4a622d6fa90152cef16b2062a17 Mon Sep 17 00:00:00 2001 From: Valentin Vignal <32538273+ValentinVignal@users.noreply.github.com> Date: Wed, 23 Oct 2024 14:57:13 +0800 Subject: [PATCH] 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` --- dev/bots/check_code_samples.dart | 1 - .../platform_menu_bar.0_test.dart | 124 ++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 examples/api/test/material/platform_menu_bar/platform_menu_bar.0_test.dart diff --git a/dev/bots/check_code_samples.dart b/dev/bots/check_code_samples.dart index 109f406f4b..244d52fbd0 100644 --- a/dev/bots/check_code_samples.dart +++ b/dev/bots/check_code_samples.dart @@ -310,7 +310,6 @@ class SampleChecker { // See https://github.com/flutter/flutter/issues/130459 final Set _knownMissingTests = { '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', diff --git a/examples/api/test/material/platform_menu_bar/platform_menu_bar.0_test.dart b/examples/api/test/material/platform_menu_bar/platform_menu_bar.0_test.dart new file mode 100644 index 0000000000..10aa59d565 --- /dev/null +++ b/examples/api/test/material/platform_menu_bar/platform_menu_bar.0_test.dart @@ -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 { + '0': >[ + { + 'id': 11, + 'label': 'Flutter API Sample', + 'enabled': true, + 'children': >[ + {'id': 2, 'label': 'About', 'enabled': true}, + {'id': 3, 'isDivider': true}, + { + 'id': 5, + 'label': 'Show Message', + 'enabled': true, + 'shortcutCharacter': 'm', + 'shortcutModifiers': 0, + }, + { + 'id': 8, + 'label': 'Messages', + 'enabled': true, + 'children': >[ + { + 'id': 6, + 'label': 'I am not throwing away my shot.', + 'enabled': true, + 'shortcutTrigger': 49, + 'shortcutModifiers': 1, + }, + { + 'id': 7, + 'label': "There's a million things I haven't done, but just you wait.", + 'enabled': true, + 'shortcutTrigger': 50, + 'shortcutModifiers': 1, + }, + ], + }, + {'id': 9, 'isDivider': true}, + {'id': 10, 'enabled': true, 'platformProvidedMenu': 1}, + ], + }, + ], + }), + ); + }, variant: const TargetPlatformVariant({TargetPlatform.macOS})); +} + +class _FakeMenuChannel implements MethodChannel { + _FakeMenuChannel(this.outgoing); + + Future Function(MethodCall) outgoing; + Future Function(MethodCall)? incoming; + + List outgoingCalls = []; + + @override + BinaryMessenger get binaryMessenger => throw UnimplementedError(); + + @override + MethodCodec get codec => const StandardMethodCodec(); + + @override + Future> invokeListMethod(String method, [dynamic arguments]) => throw UnimplementedError(); + + @override + Future> invokeMapMethod(String method, [dynamic arguments]) => throw UnimplementedError(); + + @override + Future invokeMethod(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 Function(MethodCall call)? handler) => incoming = handler; +}