1 // Copyright (c) 2012 The Chromium 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.
5 #import "content/browser/renderer_host/render_widget_host_view_mac_editcommand_helper.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/message_loop/message_loop.h"
10 #include "content/browser/renderer_host/render_widget_host_delegate.h"
11 #include "content/browser/renderer_host/render_widget_host_impl.h"
12 #include "content/common/input_messages.h"
13 #include "content/public/test/mock_render_process_host.h"
14 #include "content/public/test/test_browser_context.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/platform_test.h"
19 using content::RenderWidgetHostViewMac;
21 // Bare bones obj-c class for testing purposes.
22 @interface RenderWidgetHostViewMacEditCommandHelperTestClass : NSObject
25 @implementation RenderWidgetHostViewMacEditCommandHelperTestClass
28 // Class that owns a RenderWidgetHostViewMac.
29 @interface RenderWidgetHostViewMacOwner :
30 NSObject<RenderWidgetHostViewMacOwner> {
31 RenderWidgetHostViewMac* rwhvm_;
34 - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)rwhvm;
37 @implementation RenderWidgetHostViewMacOwner
39 - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)rwhvm {
40 if ((self = [super init])) {
46 - (RenderWidgetHostViewMac*)renderWidgetHostViewMac {
55 // Returns true if all the edit command names in the array are present in
56 // test_obj. edit_commands is a list of NSStrings, selector names are formed
57 // by appending a trailing ':' to the string.
58 bool CheckObjectRespondsToEditCommands(NSArray* edit_commands, id test_obj) {
59 for (NSString* edit_command_name in edit_commands) {
60 NSString* sel_str = [edit_command_name stringByAppendingString:@":"];
61 if (![test_obj respondsToSelector:NSSelectorFromString(sel_str)]) {
68 class MockRenderWidgetHostDelegate : public RenderWidgetHostDelegate {
70 MockRenderWidgetHostDelegate() {}
71 virtual ~MockRenderWidgetHostDelegate() {}
74 // Create a RenderWidget for which we can filter messages.
75 class RenderWidgetHostEditCommandCounter : public RenderWidgetHostImpl {
77 RenderWidgetHostEditCommandCounter(
78 RenderWidgetHostDelegate* delegate,
79 RenderProcessHost* process,
81 : RenderWidgetHostImpl(delegate, process, routing_id, false),
82 edit_command_message_count_(0) {
85 virtual bool Send(IPC::Message* message) OVERRIDE {
86 if (message->type() == InputMsg_ExecuteEditCommand::ID)
87 edit_command_message_count_++;
88 return RenderWidgetHostImpl::Send(message);
91 unsigned int edit_command_message_count_;
94 class RenderWidgetHostViewMacEditCommandHelperTest : public PlatformTest {
99 // Tests that editing commands make it through the pipeline all the way to
101 // Disabled, http://crbug.com/93286.
102 TEST_F(RenderWidgetHostViewMacEditCommandHelperTest,
103 DISABLED_TestEditingCommandDelivery) {
104 RenderWidgetHostViewMacEditCommandHelper helper;
105 NSArray* edit_command_strings = helper.GetEditSelectorNames();
107 // Set up a mock render widget and set expectations.
108 base::MessageLoopForUI message_loop;
109 TestBrowserContext browser_context;
110 MockRenderProcessHost mock_process(&browser_context);
111 MockRenderWidgetHostDelegate delegate;
112 RenderWidgetHostEditCommandCounter render_widget(&delegate, &mock_process, 0);
114 // RenderWidgetHostViewMac self destructs (RenderWidgetHostViewMacCocoa
115 // takes ownership) so no need to delete it ourselves.
116 RenderWidgetHostViewMac* rwhvm = static_cast<RenderWidgetHostViewMac*>(
117 RenderWidgetHostView::CreateViewForWidget(&render_widget));
119 RenderWidgetHostViewMacOwner* rwhwvm_owner =
120 [[[RenderWidgetHostViewMacOwner alloc]
121 initWithRenderWidgetHostViewMac:rwhvm] autorelease];
123 helper.AddEditingSelectorsToClass([rwhwvm_owner class]);
125 for (NSString* edit_command_name in edit_command_strings) {
126 NSString* sel_str = [edit_command_name stringByAppendingString:@":"];
127 [rwhwvm_owner performSelector:NSSelectorFromString(sel_str) withObject:nil];
130 size_t num_edit_commands = [edit_command_strings count];
131 EXPECT_EQ(render_widget.edit_command_message_count_, num_edit_commands);
134 // Test RenderWidgetHostViewMacEditCommandHelper::AddEditingSelectorsToClass
135 TEST_F(RenderWidgetHostViewMacEditCommandHelperTest,
136 TestAddEditingSelectorsToClass) {
137 RenderWidgetHostViewMacEditCommandHelper helper;
138 NSArray* edit_command_strings = helper.GetEditSelectorNames();
139 ASSERT_GT([edit_command_strings count], 0U);
141 // Create a class instance and add methods to the class.
142 RenderWidgetHostViewMacEditCommandHelperTestClass* test_obj =
143 [[[RenderWidgetHostViewMacEditCommandHelperTestClass alloc] init]
146 // Check that edit commands aren't already attached to the object.
147 ASSERT_FALSE(CheckObjectRespondsToEditCommands(edit_command_strings,
150 helper.AddEditingSelectorsToClass([test_obj class]);
152 // Check that all edit commands where added.
153 ASSERT_TRUE(CheckObjectRespondsToEditCommands(edit_command_strings,
156 // AddEditingSelectorsToClass() should be idempotent.
157 helper.AddEditingSelectorsToClass([test_obj class]);
159 // Check that all edit commands are still there.
160 ASSERT_TRUE(CheckObjectRespondsToEditCommands(edit_command_strings,
164 // Test RenderWidgetHostViewMacEditCommandHelper::IsMenuItemEnabled.
165 TEST_F(RenderWidgetHostViewMacEditCommandHelperTest, TestMenuItemEnabling) {
166 RenderWidgetHostViewMacEditCommandHelper helper;
167 RenderWidgetHostViewMacOwner* rwhvm_owner =
168 [[[RenderWidgetHostViewMacOwner alloc] init] autorelease];
170 // The select all menu should always be enabled.
171 SEL select_all = NSSelectorFromString(@"selectAll:");
172 ASSERT_TRUE(helper.IsMenuItemEnabled(select_all, rwhvm_owner));
174 // Random selectors should be enabled by the function.
175 SEL garbage_selector = NSSelectorFromString(@"randomGarbageSelector:");
176 ASSERT_FALSE(helper.IsMenuItemEnabled(garbage_selector, rwhvm_owner));
178 // TODO(jeremy): Currently IsMenuItemEnabled just returns true for all edit
179 // selectors. Once we go past that we should do more extensive testing here.
182 } // namespace content