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/mac/scoped_nsautorelease_pool.h"
10 #include "base/message_loop/message_loop.h"
11 #include "content/browser/compositor/test/no_transport_image_transport_factory.h"
12 #include "content/browser/gpu/compositor_util.h"
13 #include "content/browser/renderer_host/render_widget_host_delegate.h"
14 #include "content/browser/renderer_host/render_widget_host_impl.h"
15 #include "content/common/input_messages.h"
16 #include "content/public/test/mock_render_process_host.h"
17 #include "content/public/test/test_browser_context.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "testing/platform_test.h"
21 #include "ui/base/layout.h"
23 using content::RenderWidgetHostViewMac;
25 // Bare bones obj-c class for testing purposes.
26 @interface RenderWidgetHostViewMacEditCommandHelperTestClass : NSObject
29 @implementation RenderWidgetHostViewMacEditCommandHelperTestClass
32 // Class that owns a RenderWidgetHostViewMac.
33 @interface RenderWidgetHostViewMacOwner :
34 NSObject<RenderWidgetHostViewMacOwner> {
35 RenderWidgetHostViewMac* rwhvm_;
38 - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)rwhvm;
41 @implementation RenderWidgetHostViewMacOwner
43 - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)rwhvm {
44 if ((self = [super init])) {
50 - (RenderWidgetHostViewMac*)renderWidgetHostViewMac {
59 // Returns true if all the edit command names in the array are present in
60 // test_obj. edit_commands is a list of NSStrings, selector names are formed
61 // by appending a trailing ':' to the string.
62 bool CheckObjectRespondsToEditCommands(NSArray* edit_commands, id test_obj) {
63 for (NSString* edit_command_name in edit_commands) {
64 NSString* sel_str = [edit_command_name stringByAppendingString:@":"];
65 if (![test_obj respondsToSelector:NSSelectorFromString(sel_str)]) {
72 class MockRenderWidgetHostDelegate : public RenderWidgetHostDelegate {
74 MockRenderWidgetHostDelegate() {}
75 virtual ~MockRenderWidgetHostDelegate() {}
78 // Create a RenderWidget for which we can filter messages.
79 class RenderWidgetHostEditCommandCounter : public RenderWidgetHostImpl {
81 RenderWidgetHostEditCommandCounter(
82 RenderWidgetHostDelegate* delegate,
83 RenderProcessHost* process,
85 : RenderWidgetHostImpl(delegate, process, routing_id, false),
86 edit_command_message_count_(0) {
89 virtual bool Send(IPC::Message* message) OVERRIDE {
90 if (message->type() == InputMsg_ExecuteEditCommand::ID)
91 edit_command_message_count_++;
92 return RenderWidgetHostImpl::Send(message);
95 unsigned int edit_command_message_count_;
98 class RenderWidgetHostViewMacEditCommandHelperTest : public PlatformTest {
100 virtual void SetUp() {
101 if (IsDelegatedRendererEnabled()) {
102 ImageTransportFactory::InitializeForUnitTests(
103 scoped_ptr<ImageTransportFactory>(
104 new NoTransportImageTransportFactory));
107 virtual void TearDown() {
108 if (IsDelegatedRendererEnabled())
109 ImageTransportFactory::Terminate();
115 // Tests that editing commands make it through the pipeline all the way to
117 TEST_F(RenderWidgetHostViewMacEditCommandHelperTest,
118 TestEditingCommandDelivery) {
119 MockRenderWidgetHostDelegate delegate;
120 TestBrowserContext browser_context;
121 MockRenderProcessHost process_host(&browser_context);
123 // Populates |g_supported_scale_factors|.
124 std::vector<ui::ScaleFactor> supported_factors;
125 supported_factors.push_back(ui::SCALE_FACTOR_100P);
126 ui::test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors);
128 RenderWidgetHostEditCommandCounter* render_widget =
129 new RenderWidgetHostEditCommandCounter(
130 &delegate, &process_host, MSG_ROUTING_NONE);
132 base::mac::ScopedNSAutoreleasePool pool;
134 // Owned by its |cocoa_view()|, i.e. |rwhv_cocoa|.
135 RenderWidgetHostViewMac* rwhv_mac = new RenderWidgetHostViewMac(
137 base::scoped_nsobject<RenderWidgetHostViewCocoa> rwhv_cocoa(
138 [rwhv_mac->cocoa_view() retain]);
140 RenderWidgetHostViewMacEditCommandHelper helper;
141 NSArray* edit_command_strings = helper.GetEditSelectorNames();
142 RenderWidgetHostViewMacOwner* rwhwvm_owner =
143 [[[RenderWidgetHostViewMacOwner alloc]
144 initWithRenderWidgetHostViewMac:rwhv_mac] autorelease];
146 helper.AddEditingSelectorsToClass([rwhwvm_owner class]);
148 for (NSString* edit_command_name in edit_command_strings) {
149 NSString* sel_str = [edit_command_name stringByAppendingString:@":"];
150 [rwhwvm_owner performSelector:NSSelectorFromString(sel_str) withObject:nil];
153 size_t num_edit_commands = [edit_command_strings count];
154 EXPECT_EQ(render_widget->edit_command_message_count_, num_edit_commands);
159 // The |render_widget|'s process needs to be deleted within |message_loop|.
160 base::MessageLoop message_loop;
161 delete render_widget;
165 // Test RenderWidgetHostViewMacEditCommandHelper::AddEditingSelectorsToClass
166 TEST_F(RenderWidgetHostViewMacEditCommandHelperTest,
167 TestAddEditingSelectorsToClass) {
168 RenderWidgetHostViewMacEditCommandHelper helper;
169 NSArray* edit_command_strings = helper.GetEditSelectorNames();
170 ASSERT_GT([edit_command_strings count], 0U);
172 // Create a class instance and add methods to the class.
173 RenderWidgetHostViewMacEditCommandHelperTestClass* test_obj =
174 [[[RenderWidgetHostViewMacEditCommandHelperTestClass alloc] init]
177 // Check that edit commands aren't already attached to the object.
178 ASSERT_FALSE(CheckObjectRespondsToEditCommands(edit_command_strings,
181 helper.AddEditingSelectorsToClass([test_obj class]);
183 // Check that all edit commands where added.
184 ASSERT_TRUE(CheckObjectRespondsToEditCommands(edit_command_strings,
187 // AddEditingSelectorsToClass() should be idempotent.
188 helper.AddEditingSelectorsToClass([test_obj class]);
190 // Check that all edit commands are still there.
191 ASSERT_TRUE(CheckObjectRespondsToEditCommands(edit_command_strings,
195 // Test RenderWidgetHostViewMacEditCommandHelper::IsMenuItemEnabled.
196 TEST_F(RenderWidgetHostViewMacEditCommandHelperTest, TestMenuItemEnabling) {
197 RenderWidgetHostViewMacEditCommandHelper helper;
198 RenderWidgetHostViewMacOwner* rwhvm_owner =
199 [[[RenderWidgetHostViewMacOwner alloc] init] autorelease];
201 // The select all menu should always be enabled.
202 SEL select_all = NSSelectorFromString(@"selectAll:");
203 ASSERT_TRUE(helper.IsMenuItemEnabled(select_all, rwhvm_owner));
205 // Random selectors should be enabled by the function.
206 SEL garbage_selector = NSSelectorFromString(@"randomGarbageSelector:");
207 ASSERT_FALSE(helper.IsMenuItemEnabled(garbage_selector, rwhvm_owner));
209 // TODO(jeremy): Currently IsMenuItemEnabled just returns true for all edit
210 // selectors. Once we go past that we should do more extensive testing here.
213 } // namespace content