OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/renderer_host/text_input_client_mac.h" | 5 #import "chrome/browser/renderer_host/text_input_client_mac.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "chrome/browser/renderer_host/text_input_client_message_filter.h" | |
10 #include "chrome/common/text_input_client_messages.h" | 11 #include "chrome/common/text_input_client_messages.h" |
11 #include "chrome/test/testing_profile.h" | 12 #include "chrome/test/testing_profile.h" |
12 #include "content/browser/renderer_host/mock_render_process_host.h" | 13 #include "content/browser/renderer_host/mock_render_process_host.h" |
13 #include "content/browser/renderer_host/render_process_host.h" | 14 #include "content/browser/renderer_host/render_process_host.h" |
14 #include "content/browser/renderer_host/render_widget_host.h" | 15 #include "content/browser/renderer_host/render_widget_host.h" |
15 #include "ipc/ipc_test_sink.h" | 16 #include "ipc/ipc_test_sink.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "testing/gtest_mac.h" | 18 #include "testing/gtest_mac.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
22 const int64 kTaskDelayMs = 200; | |
23 | |
21 // This test does not test the WebKit side of the dictionary system (which | 24 // This test does not test the WebKit side of the dictionary system (which |
22 // performs the actual data fetching), but rather this just tests that the | 25 // performs the actual data fetching), but rather this just tests that the |
23 // service's signaling system works. | 26 // service's signaling system works. |
24 class TextInputClientMacTest : public testing::Test { | 27 class TextInputClientMacTest : public testing::Test { |
25 public: | 28 public: |
26 TextInputClientMacTest() | 29 TextInputClientMacTest() |
27 : message_loop_(MessageLoop::TYPE_UI), | 30 : message_loop_(MessageLoop::TYPE_UI), |
28 profile_(), | 31 profile_(), |
29 process_factory_(), | 32 process_factory_(), |
30 widget_(process_factory_.CreateRenderProcessHost(&profile_), 1), | 33 widget_(process_factory_.CreateRenderProcessHost(&profile_), 1), |
31 thread_("TextInputClientMacTestThread") {} | 34 thread_("TextInputClientMacTestThread") {} |
32 | 35 |
33 // Accessor for the TextInputClientMac instance. | 36 // Accessor for the TextInputClientMac instance. |
34 TextInputClientMac* service() { | 37 TextInputClientMac* service() { |
35 return TextInputClientMac::GetInstance(); | 38 return TextInputClientMac::GetInstance(); |
36 } | 39 } |
37 | 40 |
38 // Helper method to post a task on the testing thread's MessageLoop after | 41 // Helper method to post a task on the testing thread's MessageLoop after |
39 // a short delay. | 42 // a short delay. |
40 void PostTask(const base::Closure& task) { | 43 void PostTask(const base::Closure& task, const int64 delay = kTaskDelayMs) { |
41 const int64 kTaskDelayMs = 200; | 44 thread_.message_loop()->PostDelayedTask(FROM_HERE, task, delay); |
42 thread_.message_loop()->PostDelayedTask(FROM_HERE, task, kTaskDelayMs); | |
43 } | 45 } |
44 | 46 |
45 RenderWidgetHost* widget() { | 47 RenderWidgetHost* widget() { |
46 return &widget_; | 48 return &widget_; |
47 } | 49 } |
48 | 50 |
49 IPC::TestSink& ipc_sink() { | 51 IPC::TestSink& ipc_sink() { |
50 return static_cast<MockRenderProcessHost*>(widget()->process())->sink(); | 52 return static_cast<MockRenderProcessHost*>(widget()->process())->sink(); |
51 } | 53 } |
52 | 54 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 | 101 |
100 TEST_F(TextInputClientMacTest, TimeoutCharacterIndex) { | 102 TEST_F(TextInputClientMacTest, TimeoutCharacterIndex) { |
101 NSUInteger index = service()->GetCharacterIndexAtPoint( | 103 NSUInteger index = service()->GetCharacterIndexAtPoint( |
102 widget(), gfx::Point(2, 2)); | 104 widget(), gfx::Point(2, 2)); |
103 EXPECT_EQ(1U, ipc_sink().message_count()); | 105 EXPECT_EQ(1U, ipc_sink().message_count()); |
104 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( | 106 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
105 TextInputClientMsg_CharacterIndexForPoint::ID)); | 107 TextInputClientMsg_CharacterIndexForPoint::ID)); |
106 EXPECT_EQ(NSNotFound, index); | 108 EXPECT_EQ(NSNotFound, index); |
107 } | 109 } |
108 | 110 |
111 TEST_F(TextInputClientMacTest, NotFoundCharacterIndex) { | |
112 ScopedTestingThread thread(this); | |
113 const NSUInteger kPreviousValue = 42; | |
114 const size_t kNotFoundValue = static_cast<size_t>(-1); | |
115 | |
116 // Set an arbitrary value to ensure the index is not |NSNotFound|. | |
117 PostTask(base::Bind(&TextInputClientMac::SetCharacterIndexAndSignal, | |
118 base::Unretained(service()), kPreviousValue)); | |
119 | |
120 scoped_refptr<TextInputClientMessageFilter> filter( | |
121 new TextInputClientMessageFilter(widget()->process()->id())); | |
122 scoped_ptr<IPC::Message> message( | |
123 new TextInputClientReplyMsg_GotCharacterIndexForPoint( | |
124 widget()->routing_id(), kNotFoundValue)); | |
125 bool message_ok = true; | |
126 // Set |WTF::notFound| to the index |kTaskDelayMs| after the previous | |
127 // setting. | |
128 PostTask(base::Bind(&TextInputClientMessageFilter::OnMessageReceived, | |
129 filter.get(), *message, &message_ok), | |
130 kTaskDelayMs + kTaskDelayMs); | |
Robert Sesek
2011/05/23 12:31:39
Use *2?
bashi
2011/05/24 00:44:17
Done.
| |
131 | |
132 NSUInteger index = service()->GetCharacterIndexAtPoint( | |
133 widget(), gfx::Point(2, 2)); | |
134 EXPECT_EQ(kPreviousValue, index); | |
135 index = service()->GetCharacterIndexAtPoint(widget(), gfx::Point(2, 2)); | |
136 EXPECT_EQ(NSNotFound, index); | |
137 | |
138 EXPECT_EQ(2U, ipc_sink().message_count()); | |
139 for (unsigned i = 0; i < ipc_sink().message_count(); ++i) { | |
Robert Sesek
2011/05/23 12:31:39
Chromium prefers size_t to unsigned.
bashi
2011/05/24 00:44:17
Done.
| |
140 const IPC::Message* ipc_message = ipc_sink().GetMessageAt(i); | |
141 EXPECT_EQ(ipc_message->type(), | |
142 TextInputClientMsg_CharacterIndexForPoint::ID); | |
143 } | |
144 } | |
145 | |
109 TEST_F(TextInputClientMacTest, GetRectForRange) { | 146 TEST_F(TextInputClientMacTest, GetRectForRange) { |
110 ScopedTestingThread thread(this); | 147 ScopedTestingThread thread(this); |
111 const NSRect kSuccessValue = NSMakeRect(42, 43, 44, 45); | 148 const NSRect kSuccessValue = NSMakeRect(42, 43, 44, 45); |
112 | 149 |
113 PostTask(base::Bind(&TextInputClientMac::SetFirstRectAndSignal, | 150 PostTask(base::Bind(&TextInputClientMac::SetFirstRectAndSignal, |
114 base::Unretained(service()), kSuccessValue)); | 151 base::Unretained(service()), kSuccessValue)); |
115 NSRect rect = service()->GetFirstRectForRange(widget(), NSMakeRange(0, 32)); | 152 NSRect rect = service()->GetFirstRectForRange(widget(), NSMakeRange(0, 32)); |
116 | 153 |
117 EXPECT_EQ(1U, ipc_sink().message_count()); | 154 EXPECT_EQ(1U, ipc_sink().message_count()); |
118 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( | 155 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 TEST_F(TextInputClientMacTest, TimeoutSubstring) { | 189 TEST_F(TextInputClientMacTest, TimeoutSubstring) { |
153 NSAttributedString* string = service()->GetAttributedSubstringFromRange( | 190 NSAttributedString* string = service()->GetAttributedSubstringFromRange( |
154 widget(), NSMakeRange(0, 32)); | 191 widget(), NSMakeRange(0, 32)); |
155 EXPECT_EQ(nil, string); | 192 EXPECT_EQ(nil, string); |
156 EXPECT_EQ(1U, ipc_sink().message_count()); | 193 EXPECT_EQ(1U, ipc_sink().message_count()); |
157 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( | 194 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
158 TextInputClientMsg_StringForRange::ID)); | 195 TextInputClientMsg_StringForRange::ID)); |
159 } | 196 } |
160 | 197 |
161 } // namespace | 198 } // namespace |
OLD | NEW |