- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / render_widget_host_view_mac_editcommand_helper_unittest.mm
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.
4
5 #import "content/browser/renderer_host/render_widget_host_view_mac_editcommand_helper.h"
6
7 #import <Cocoa/Cocoa.h>
8
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"
18
19 using content::RenderWidgetHostViewMac;
20
21 // Bare bones obj-c class for testing purposes.
22 @interface RenderWidgetHostViewMacEditCommandHelperTestClass : NSObject
23 @end
24
25 @implementation RenderWidgetHostViewMacEditCommandHelperTestClass
26 @end
27
28 // Class that owns a RenderWidgetHostViewMac.
29 @interface RenderWidgetHostViewMacOwner :
30     NSObject<RenderWidgetHostViewMacOwner> {
31   RenderWidgetHostViewMac* rwhvm_;
32 }
33
34 - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)rwhvm;
35 @end
36
37 @implementation RenderWidgetHostViewMacOwner
38
39 - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)rwhvm {
40   if ((self = [super init])) {
41     rwhvm_ = rwhvm;
42   }
43   return self;
44 }
45
46 - (RenderWidgetHostViewMac*)renderWidgetHostViewMac {
47   return rwhvm_;
48 }
49
50 @end
51
52 namespace content {
53 namespace {
54
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)]) {
62       return false;
63     }
64   }
65   return true;
66 }
67
68 class MockRenderWidgetHostDelegate : public RenderWidgetHostDelegate {
69  public:
70   MockRenderWidgetHostDelegate() {}
71   virtual ~MockRenderWidgetHostDelegate() {}
72 };
73
74 // Create a RenderWidget for which we can filter messages.
75 class RenderWidgetHostEditCommandCounter : public RenderWidgetHostImpl {
76  public:
77   RenderWidgetHostEditCommandCounter(
78       RenderWidgetHostDelegate* delegate,
79       RenderProcessHost* process,
80       int routing_id)
81     : RenderWidgetHostImpl(delegate, process, routing_id, false),
82       edit_command_message_count_(0) {
83   }
84
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);
89   }
90
91   unsigned int edit_command_message_count_;
92 };
93
94 class RenderWidgetHostViewMacEditCommandHelperTest : public PlatformTest {
95 };
96
97 }  // namespace
98
99 // Tests that editing commands make it through the pipeline all the way to
100 // RenderWidgetHost.
101 // Disabled, http://crbug.com/93286.
102 TEST_F(RenderWidgetHostViewMacEditCommandHelperTest,
103        DISABLED_TestEditingCommandDelivery) {
104   RenderWidgetHostViewMacEditCommandHelper helper;
105   NSArray* edit_command_strings = helper.GetEditSelectorNames();
106
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);
113
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));
118
119   RenderWidgetHostViewMacOwner* rwhwvm_owner =
120       [[[RenderWidgetHostViewMacOwner alloc]
121           initWithRenderWidgetHostViewMac:rwhvm] autorelease];
122
123   helper.AddEditingSelectorsToClass([rwhwvm_owner class]);
124
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];
128   }
129
130   size_t num_edit_commands = [edit_command_strings count];
131   EXPECT_EQ(render_widget.edit_command_message_count_, num_edit_commands);
132 }
133
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);
140
141   // Create a class instance and add methods to the class.
142   RenderWidgetHostViewMacEditCommandHelperTestClass* test_obj =
143       [[[RenderWidgetHostViewMacEditCommandHelperTestClass alloc] init]
144           autorelease];
145
146   // Check that edit commands aren't already attached to the object.
147   ASSERT_FALSE(CheckObjectRespondsToEditCommands(edit_command_strings,
148       test_obj));
149
150   helper.AddEditingSelectorsToClass([test_obj class]);
151
152   // Check that all edit commands where added.
153   ASSERT_TRUE(CheckObjectRespondsToEditCommands(edit_command_strings,
154       test_obj));
155
156   // AddEditingSelectorsToClass() should be idempotent.
157   helper.AddEditingSelectorsToClass([test_obj class]);
158
159   // Check that all edit commands are still there.
160   ASSERT_TRUE(CheckObjectRespondsToEditCommands(edit_command_strings,
161       test_obj));
162 }
163
164 // Test RenderWidgetHostViewMacEditCommandHelper::IsMenuItemEnabled.
165 TEST_F(RenderWidgetHostViewMacEditCommandHelperTest, TestMenuItemEnabling) {
166   RenderWidgetHostViewMacEditCommandHelper helper;
167   RenderWidgetHostViewMacOwner* rwhvm_owner =
168       [[[RenderWidgetHostViewMacOwner alloc] init] autorelease];
169
170   // The select all menu should always be enabled.
171   SEL select_all = NSSelectorFromString(@"selectAll:");
172   ASSERT_TRUE(helper.IsMenuItemEnabled(select_all, rwhvm_owner));
173
174   // Random selectors should be enabled by the function.
175   SEL garbage_selector = NSSelectorFromString(@"randomGarbageSelector:");
176   ASSERT_FALSE(helper.IsMenuItemEnabled(garbage_selector, rwhvm_owner));
177
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.
180 }
181
182 }  // namespace content