Flutter macOS Embedder
FlutterMenuPluginTest.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
7 
18 #include "flutter/testing/autoreleasepool_test.h"
19 #include "flutter/testing/testing.h"
20 #include "gtest/gtest.h"
21 
23 @property(nonatomic, readonly) id<FlutterPlugin> plugin;
24 @property(nonatomic, readonly) FlutterMethodChannel* channel;
25 @end
26 
27 @implementation FakePluginRegistrar
28 
29 // Synthesize properties declared in FlutterPluginRegistrar protocol.
30 @synthesize engineIdentifier;
31 @synthesize messenger;
32 @synthesize textures;
33 @synthesize view;
34 
35 - (void)addMethodCallDelegate:(nonnull id<FlutterPlugin>)delegate
36  channel:(nonnull FlutterMethodChannel*)channel {
37  _plugin = delegate;
38  _channel = channel;
39  [_channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
40  [delegate handleMethodCall:call result:result];
41  }];
42 }
43 
44 - (void)addApplicationDelegate:(nonnull NSObject<FlutterAppLifecycleDelegate>*)delegate {
45 }
46 
47 - (void)registerViewFactory:(nonnull NSObject<FlutterPlatformViewFactory>*)factory
48  withId:(nonnull NSString*)factoryId {
49 }
50 
51 - (void)publish:(nonnull NSObject*)value {
52 }
53 
54 - (nullable NSObject*)valuePublishedByPlugin:(nonnull NSString*)pluginKey {
55  return nil;
56 }
57 
58 - (nonnull NSString*)lookupKeyForAsset:(nonnull NSString*)asset {
59  return @"";
60 }
61 
62 - (nonnull NSString*)lookupKeyForAsset:(nonnull NSString*)asset
63  fromPackage:(nonnull NSString*)package {
64  return @"";
65 }
66 
67 - (NSViewController*)viewController {
68  return nil;
69 }
70 @end
71 
72 namespace flutter::testing {
73 
74 // FlutterMenuPluginTest is an AutoreleasePoolTest that allocates an NSView.
75 //
76 // This supports the use of NSApplication features that rely on the assumption of a view, such as
77 // when modifying the application menu bar, or even accessing the NSApplication.localizedName
78 // property.
79 //
80 // See: https://github.com/flutter/flutter/issues/104748#issuecomment-1159336728
81 class FlutterMenuPluginTest : public AutoreleasePoolTest {
82  public:
85 
86  private:
87  NSView* view_;
88 };
89 
90 FlutterMenuPluginTest::FlutterMenuPluginTest() {
91  view_ = [[NSView alloc] initWithFrame:NSZeroRect];
92  view_.wantsLayer = YES;
93 }
94 
96  // Build a simulation of the default main menu.
97  NSMenu* mainMenu = [[NSMenu alloc] init];
98  NSMenuItem* appNameMenu = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"APP_NAME", nil)
99  action:nil
100  keyEquivalent:@""];
101  NSMenu* submenu =
102  [[NSMenu alloc] initWithTitle:NSLocalizedString(@"Prexisting APP_NAME menu", nil)];
103  [submenu addItem:[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"About APP_NAME", nil)
104  action:nil
105  keyEquivalent:@""]];
106  appNameMenu.submenu = submenu;
107  [mainMenu addItem:appNameMenu];
108  [NSApp setMainMenu:mainMenu];
109 
110  FakePluginRegistrar* registrar = [[FakePluginRegistrar alloc] init];
112  FlutterMenuPlugin* plugin = [registrar plugin];
113 
114  NSDictionary* testMenus = @{
115  @"0" : @[
116  @{
117  @"id" : [NSNumber numberWithInt:1],
118  @"label" : @"APP_NAME",
119  @"tooltip" : @"APP_NAME",
120  @"enabled" : @(YES),
121  @"children" : @[
122  @{
123  @"id" : [NSNumber numberWithInt:3],
124  @"platformProvidedMenu" : @(static_cast<int>(flutter::PlatformProvidedMenu::kQuit)),
125  @"enabled" : @(YES),
126  },
127  @{
128  @"id" : [NSNumber numberWithInt:2],
129  @"label" : @"APP_NAME Info",
130  @"tooltip" : @"APP_NAME Info Tooltip",
131  @"enabled" : @(YES),
132  @"shortcutTrigger" : [NSNumber numberWithUnsignedLongLong:0x61],
133  @"shortcutModifiers" : [NSNumber numberWithUnsignedInt:0],
134  },
135  ],
136  },
137  @{
138  @"id" : [NSNumber numberWithInt:4],
139  @"label" : @"Help for APP_NAME",
140  @"enabled" : @(YES),
141  @"children" : @[
142  @{
143  @"id" : [NSNumber numberWithInt:5],
144  @"label" : @"Help me!",
145  @"tooltip" : @"Help me tooltip!",
146  @"enabled" : @(YES),
147  },
148  @{
149  @"id" : [NSNumber numberWithInt:6],
150  @"label" : @"",
151  @"enabled" : @(NO),
152  @"isDivider" : @(YES),
153  },
154  @{
155  @"id" : [NSNumber numberWithInt:7],
156  @"label" : @"Search help",
157  @"enabled" : @(NO),
158  },
159  ],
160  },
161  ],
162  };
163 
164  __block id available = @NO;
165  [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"Menu.isPluginAvailable"
166  arguments:nil]
167  result:^(id _Nullable result) {
168  available = result;
169  }];
170 
171  EXPECT_TRUE(available);
172 
174  arguments:testMenus]
175  result:^(id _Nullable result){
176  }];
177 
178  EXPECT_EQ([NSApp.mainMenu numberOfItems], 2);
179  NSMenuItem* firstMenu = [NSApp.mainMenu itemAtIndex:0];
180  EXPECT_TRUE([[firstMenu title] isEqualToString:@"flutter_desktop_darwin_unittests"]);
181  EXPECT_TRUE([[firstMenu toolTip] isEqualToString:@"flutter_desktop_darwin_unittests"]);
182  EXPECT_EQ([firstMenu tag], 1);
183  EXPECT_TRUE([firstMenu isEnabled]);
184  EXPECT_FALSE([firstMenu isHidden]);
185  EXPECT_TRUE([[firstMenu keyEquivalent] isEqualToString:@"\0"]);
186 
187  EXPECT_EQ([[firstMenu submenu] numberOfItems], 1);
188  NSMenuItem* firstItem = [[firstMenu submenu] itemAtIndex:0];
189  EXPECT_TRUE([[firstItem title] isEqualToString:@"flutter_desktop_darwin_unittests Info"]);
190  EXPECT_TRUE(
191  [[firstItem toolTip] isEqualToString:@"flutter_desktop_darwin_unittests Info Tooltip"]);
192  EXPECT_TRUE([[firstItem keyEquivalent] isEqualToString:@"a"]);
193  EXPECT_TRUE([firstItem isEnabled]);
194  EXPECT_FALSE([firstItem isHidden]);
195  EXPECT_TRUE(
196  [NSStringFromSelector([firstItem action]) isEqualToString:@"flutterMenuItemSelected:"]);
197  EXPECT_EQ([firstItem tag], 2);
198 
199  NSMenuItem* secondMenu = [NSApp.mainMenu itemAtIndex:1];
200  EXPECT_TRUE([[secondMenu title] isEqualToString:@"Help for flutter_desktop_darwin_unittests"]);
201  EXPECT_EQ([secondMenu toolTip], nil);
202  EXPECT_EQ([secondMenu tag], 4);
203  EXPECT_TRUE([secondMenu isEnabled]);
204  EXPECT_FALSE([secondMenu isHidden]);
205 
206  EXPECT_EQ([[secondMenu submenu] numberOfItems], 3);
207  NSMenuItem* secondMenuFirst = [[secondMenu submenu] itemAtIndex:0];
208  EXPECT_TRUE([[secondMenuFirst title] isEqualToString:@"Help me!"]);
209  EXPECT_TRUE([[secondMenuFirst toolTip] isEqualToString:@"Help me tooltip!"]);
210  EXPECT_TRUE([secondMenuFirst isEnabled]);
211  EXPECT_FALSE([secondMenuFirst isHidden]);
212  EXPECT_TRUE(
213  [NSStringFromSelector([secondMenuFirst action]) isEqualToString:@"flutterMenuItemSelected:"]);
214  EXPECT_EQ([secondMenuFirst tag], 5);
215 
216  NSMenuItem* secondMenuDivider = [[secondMenu submenu] itemAtIndex:1];
217  EXPECT_TRUE([[secondMenuDivider title] isEqualToString:@""]);
218  EXPECT_EQ([secondMenuDivider toolTip], nil);
219  EXPECT_TRUE([[secondMenuDivider keyEquivalent] isEqualToString:@""]);
220  EXPECT_FALSE([secondMenuDivider isEnabled]);
221  EXPECT_FALSE([secondMenuDivider isHidden]);
222  EXPECT_EQ([secondMenuDivider action], nil);
223  EXPECT_EQ([secondMenuDivider tag], 0);
224 
225  NSMenuItem* secondMenuLast = [[secondMenu submenu] itemAtIndex:2];
226  EXPECT_TRUE([[secondMenuLast title] isEqualToString:@"Search help"]);
227  EXPECT_EQ([secondMenuLast toolTip], nil);
228  EXPECT_FALSE([secondMenuLast isEnabled]);
229  EXPECT_FALSE([secondMenuLast isHidden]);
230  EXPECT_TRUE(
231  [NSStringFromSelector([secondMenuLast action]) isEqualToString:@"flutterMenuItemSelected:"]);
232  EXPECT_EQ([secondMenuLast tag], 7);
233 }
234 
235 } // namespace flutter::testing
FlutterTextInputPlugin * _plugin
FlutterMethodChannel * channel
id< FlutterPlugin > plugin
void registerWithRegistrar:(nonnull id< FlutterPluginRegistrar > registrar)
instancetype methodCallWithMethodName:arguments:(NSString *method,[arguments] id _Nullable arguments)
TEST_F(FlutterMenuPluginTest, TestSetMenu)
void handleMethodCall:result:(FlutterMethodCall *call,[result] FlutterResult result)
id< FlutterBinaryMessenger > messenger
id< FlutterTextureRegistry > textures