macOS: cefclient: Create Tests menu with Interface Builder

This commit is contained in:
Marshall Greenblatt 2017-02-02 15:43:41 -05:00
parent 94f604975d
commit d6531b9fe6
2 changed files with 585 additions and 2916 deletions

View File

@ -17,11 +17,23 @@
namespace { namespace {
void AddMenuItem(NSMenu *menu, NSString* label, int idval) { // Returns the top menu bar with the specified |tag|.
NSMenuItem* item = [menu addItemWithTitle:label NSMenuItem* GetMenuBarMenuWithTag(NSInteger tag) {
action:@selector(menuItemSelected:) NSMenu* main_menu = NSApp.mainMenu;
keyEquivalent:@""]; NSInteger found_index = [main_menu indexOfItemWithTag:tag];
[item setTag:idval]; if (found_index >= 0)
return [main_menu itemAtIndex:found_index];
return nil;
}
// Returns the item in |menu| that has the specified |action_selector|.
NSMenuItem* GetMenuItemWithAction(NSMenu* menu, SEL action_selector) {
for (NSInteger i = 0; i < menu.numberOfItems; ++i) {
NSMenuItem* item = [menu itemAtIndex:i];
if (item.action == action_selector)
return item;
}
return nil;
} }
} // namespace } // namespace
@ -36,7 +48,23 @@ void AddMenuItem(NSMenu *menu, NSString* label, int idval) {
- (id)initWithControls:(bool)with_controls andOsr:(bool)with_osr; - (id)initWithControls:(bool)with_controls andOsr:(bool)with_osr;
- (void)createApplication:(id)object; - (void)createApplication:(id)object;
- (void)tryToTerminateApplication:(NSApplication*)app; - (void)tryToTerminateApplication:(NSApplication*)app;
- (IBAction)menuItemSelected:(id)sender; - (void)testsItemSelected:(int)command_id;
- (IBAction)menuTestsGetText:(id)sender;
- (IBAction)menuTestsGetSource:(id)sender;
- (IBAction)menuTestsWindowNew:(id)sender;
- (IBAction)menuTestsWindowPopup:(id)sender;
- (IBAction)menuTestsRequest:(id)sender;
- (IBAction)menuTestsPluginInfo:(id)sender;
- (IBAction)menuTestsZoomIn:(id)sender;
- (IBAction)menuTestsZoomOut:(id)sender;
- (IBAction)menuTestsZoomReset:(id)sender;
- (IBAction)menuTestsSetFPS:(id)sender;
- (IBAction)menuTestsSetScaleFactor:(id)sender;
- (IBAction)menuTestsTracingBegin:(id)sender;
- (IBAction)menuTestsTracingEnd:(id)sender;
- (IBAction)menuTestsPrint:(id)sender;
- (IBAction)menuTestsPrintToPdf:(id)sender;
- (IBAction)menuTestsOtherTests:(id)sender;
@end @end
// Provide the CefAppProtocol implementation required by CEF. // Provide the CefAppProtocol implementation required by CEF.
@ -119,39 +147,61 @@ void AddMenuItem(NSMenu *menu, NSString* label, int idval) {
// Create the application on the UI thread. // Create the application on the UI thread.
- (void)createApplication:(id)object { - (void)createApplication:(id)object {
NSApplication* application = [NSApplication sharedApplication]; NSApplication* application = [NSApplication sharedApplication];
// The top menu is configured using Interface Builder (IB). To modify the menu
// start by loading MainMenu.xib in IB.
//
// To associate MainMenu.xib with ClientAppDelegate:
// 1. Select "File's Owner" from the "Placeholders" section in the left side
// pane.
// 2. Load the "Identity inspector" tab in the top-right side pane.
// 3. In the "Custom Class" section set the "Class" value to
// "ClientAppDelegate".
// 4. Pass an instance of ClientAppDelegate as the |owner| parameter to
// loadNibNamed:.
//
// To create a new top menu:
// 1. Load the "Object library" tab in the bottom-right side pane.
// 2. Drag a "Submenu Menu Item" widget from the Object library to the desired
// location in the menu bar shown in the center pane.
// 3. Select the newly created top menu by left clicking on it.
// 4. Load the "Attributes inspector" tab in the top-right side pane.
// 5. Under the "Menu Item" section set the "Tag" value to a unique integer.
// This is necessary for the GetMenuBarMenuWithTag function to work
// properly.
//
// To create a new menu item in a top menu:
// 1. Add a new receiver method in ClientAppDelegate (e.g. menuTestsDoStuff:).
// 2. Load the "Object library" tab in the bottom-right side pane.
// 3. Drag a "Menu Item" widget from the Object library to the desired
// location in the menu bar shown in the center pane.
// 4. Double-click on the new menu item to set the label.
// 5. Right click on the new menu item to show the "Get Source" dialog.
// 6. In the "Sent Actions" section drag from the circle icon and drop on the
// new receiver method in the ClientAppDelegate source code file.
//
// Load the top menu from MainMenu.xib.
[[NSBundle mainBundle] loadNibNamed:@"MainMenu" [[NSBundle mainBundle] loadNibNamed:@"MainMenu"
owner:NSApp owner:self
topLevelObjects:nil]; topLevelObjects:nil];
// Set the delegate for application events. // Set the delegate for application events.
[application setDelegate:self]; [application setDelegate:self];
// Add the Tests menu. if (!with_osr_) {
NSMenu* menubar = [application mainMenu]; // Remove the OSR-related menu items when OSR is disabled.
NSMenuItem *testItem = [[[NSMenuItem alloc] initWithTitle:@"Tests" NSMenuItem* tests_menu = GetMenuBarMenuWithTag(8);
action:nil if (tests_menu) {
keyEquivalent:@""] autorelease]; NSMenuItem* set_fps_item = GetMenuItemWithAction(
NSMenu *testMenu = [[[NSMenu alloc] initWithTitle:@"Tests"] autorelease]; tests_menu.submenu, @selector(menuTestsSetFPS:));
AddMenuItem(testMenu, @"Get Text", ID_TESTS_GETSOURCE); if (set_fps_item)
AddMenuItem(testMenu, @"Get Source", ID_TESTS_GETTEXT); [tests_menu.submenu removeItem:set_fps_item];
AddMenuItem(testMenu, @"New Window", ID_TESTS_WINDOW_NEW); NSMenuItem* set_scale_factor_item = GetMenuItemWithAction(
AddMenuItem(testMenu, @"Popup Window", ID_TESTS_WINDOW_POPUP); tests_menu.submenu, @selector(menuTestsSetScaleFactor:));
AddMenuItem(testMenu, @"Request", ID_TESTS_REQUEST); if (set_scale_factor_item)
AddMenuItem(testMenu, @"Plugin Info", ID_TESTS_PLUGIN_INFO); [tests_menu.submenu removeItem:set_scale_factor_item];
AddMenuItem(testMenu, @"Zoom In", ID_TESTS_ZOOM_IN); }
AddMenuItem(testMenu, @"Zoom Out", ID_TESTS_ZOOM_OUT);
AddMenuItem(testMenu, @"Zoom Reset", ID_TESTS_ZOOM_RESET);
if (with_osr_) {
AddMenuItem(testMenu, @"Set FPS", ID_TESTS_OSR_FPS);
AddMenuItem(testMenu, @"Set Scale Factor", ID_TESTS_OSR_DSF);
} }
AddMenuItem(testMenu, @"Begin Tracing", ID_TESTS_TRACING_BEGIN);
AddMenuItem(testMenu, @"End Tracing", ID_TESTS_TRACING_END);
AddMenuItem(testMenu, @"Print", ID_TESTS_PRINT);
AddMenuItem(testMenu, @"Print to PDF", ID_TESTS_PRINT_TO_PDF);
AddMenuItem(testMenu, @"Other Tests", ID_TESTS_OTHER_TESTS);
[testItem setSubmenu:testMenu];
[menubar addItem:testItem];
// Create the first window. // Create the first window.
client::MainContext::Get()->GetRootWindowManager()->CreateRootWindow( client::MainContext::Get()->GetRootWindowManager()->CreateRootWindow(
@ -165,7 +215,11 @@ void AddMenuItem(NSMenu *menu, NSString* label, int idval) {
client::MainContext::Get()->GetRootWindowManager()->CloseAllWindows(false); client::MainContext::Get()->GetRootWindowManager()->CloseAllWindows(false);
} }
- (IBAction)menuItemSelected:(id)sender { - (void)orderFrontStandardAboutPanel:(id)sender {
[[NSApplication sharedApplication] orderFrontStandardAboutPanel:nil];
}
- (void)testsItemSelected:(int)command_id {
// Retrieve the active RootWindow. // Retrieve the active RootWindow.
NSWindow* key_window = [[NSApplication sharedApplication] keyWindow]; NSWindow* key_window = [[NSApplication sharedApplication] keyWindow];
if (!key_window) if (!key_window)
@ -175,10 +229,72 @@ void AddMenuItem(NSMenu *menu, NSString* label, int idval) {
client::RootWindow::GetForNSWindow(key_window); client::RootWindow::GetForNSWindow(key_window);
CefRefPtr<CefBrowser> browser = root_window->GetBrowser(); CefRefPtr<CefBrowser> browser = root_window->GetBrowser();
if (browser.get()) { if (browser.get())
NSMenuItem *item = (NSMenuItem*)sender; client::test_runner::RunTest(browser, command_id);
client::test_runner::RunTest(browser, [item tag]); }
}
- (IBAction)menuTestsGetText:(id)sender {
[self testsItemSelected:ID_TESTS_GETTEXT];
}
- (IBAction)menuTestsGetSource:(id)sender {
[self testsItemSelected:ID_TESTS_GETSOURCE];
}
- (IBAction)menuTestsWindowNew:(id)sender {
[self testsItemSelected:ID_TESTS_WINDOW_NEW];
}
- (IBAction)menuTestsWindowPopup:(id)sender {
[self testsItemSelected:ID_TESTS_WINDOW_POPUP];
}
- (IBAction)menuTestsRequest:(id)sender {
[self testsItemSelected:ID_TESTS_REQUEST];
}
- (IBAction)menuTestsPluginInfo:(id)sender {
[self testsItemSelected:ID_TESTS_PLUGIN_INFO];
}
- (IBAction)menuTestsZoomIn:(id)sender {
[self testsItemSelected:ID_TESTS_ZOOM_IN];
}
- (IBAction)menuTestsZoomOut:(id)sender {
[self testsItemSelected:ID_TESTS_ZOOM_OUT];
}
- (IBAction)menuTestsZoomReset:(id)sender {
[self testsItemSelected:ID_TESTS_ZOOM_RESET];
}
- (IBAction)menuTestsSetFPS:(id)sender {
[self testsItemSelected:ID_TESTS_OSR_FPS];
}
- (IBAction)menuTestsSetScaleFactor:(id)sender {
[self testsItemSelected:ID_TESTS_OSR_DSF];
}
- (IBAction)menuTestsTracingBegin:(id)sender {
[self testsItemSelected:ID_TESTS_TRACING_BEGIN];
}
- (IBAction)menuTestsTracingEnd:(id)sender {
[self testsItemSelected:ID_TESTS_TRACING_END];
}
- (IBAction)menuTestsPrint:(id)sender {
[self testsItemSelected:ID_TESTS_PRINT];
}
- (IBAction)menuTestsPrintToPdf:(id)sender {
[self testsItemSelected:ID_TESTS_PRINT_TO_PDF];
}
- (IBAction)menuTestsOtherTests:(id)sender {
[self testsItemSelected:ID_TESTS_OTHER_TESTS];
} }
- (NSApplicationTerminateReply)applicationShouldTerminate: - (NSApplicationTerminateReply)applicationShouldTerminate:

File diff suppressed because it is too large Load Diff