OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/renderer_host/text_input_client_mac.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "base/task.h" |
| 9 #include "base/threading/thread.h" |
| 10 #include "chrome/common/text_input_client_messages.h" |
| 11 #include "chrome/test/testing_profile.h" |
| 12 #include "content/browser/renderer_host/mock_render_process_host.h" |
| 13 #include "content/browser/renderer_host/render_widget_host.h" |
| 14 #include "ipc/ipc_test_sink.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "testing/gtest_mac.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 // This test does not test the WebKit side of the dictionary system (which |
| 21 // performs the actual data fetching), but rather this just tests that the |
| 22 // service's signaling system works. |
| 23 class TextInputClientMacTest : public testing::Test { |
| 24 public: |
| 25 TextInputClientMacTest() |
| 26 : message_loop_(MessageLoop::TYPE_UI), |
| 27 profile_(), |
| 28 process_(new MockRenderProcessHost(&profile_)), |
| 29 widget_(process_, 1), |
| 30 thread_("TextInputClientMacTestThread") {} |
| 31 |
| 32 // Accessor for the TextInputClientMac instance. |
| 33 TextInputClientMac* service() { |
| 34 return TextInputClientMac::GetInstance(); |
| 35 } |
| 36 |
| 37 // Helper method to post a task on the testing thread's MessageLoop after |
| 38 // a short delay. |
| 39 void PostTask(Task* task) { |
| 40 const int64 kTaskDelayMs = 200; |
| 41 thread_.message_loop()->PostDelayedTask(FROM_HERE, task, kTaskDelayMs); |
| 42 } |
| 43 |
| 44 RenderWidgetHost* widget() { |
| 45 return &widget_; |
| 46 } |
| 47 |
| 48 IPC::TestSink& ipc_sink() { |
| 49 return process_->sink(); |
| 50 } |
| 51 |
| 52 private: |
| 53 friend class ScopedTestingThread; |
| 54 |
| 55 MessageLoop message_loop_; |
| 56 TestingProfile profile_; |
| 57 |
| 58 // Gets deleted when the last RWH in the "process" gets destroyed. |
| 59 MockRenderProcessHost* process_; |
| 60 RenderWidgetHost widget_; |
| 61 |
| 62 base::Thread thread_; |
| 63 }; |
| 64 |
| 65 //////////////////////////////////////////////////////////////////////////////// |
| 66 |
| 67 // Helper class that Start()s and Stop()s a thread according to the scope of the |
| 68 // object. |
| 69 class ScopedTestingThread { |
| 70 public: |
| 71 ScopedTestingThread(TextInputClientMacTest* test) : thread_(test->thread_) { |
| 72 thread_.Start(); |
| 73 } |
| 74 ~ScopedTestingThread() { |
| 75 thread_.Stop(); |
| 76 } |
| 77 |
| 78 private: |
| 79 base::Thread& thread_; |
| 80 }; |
| 81 |
| 82 // Helper Tasks //////////////////////////////////////////////////////////////// |
| 83 // |
| 84 // These tasks are posted to the MessageLoop of TextInputClientMacTest::thread_ |
| 85 // to test the condition variable signaling. There is one Task for each piece |
| 86 // of information the TextInputClientMac service deals with. |
| 87 //////////////////////////////////////////////////////////////////////////////// |
| 88 |
| 89 class CharacterIndexTask : public Task { |
| 90 public: |
| 91 CharacterIndexTask(NSUInteger success_value) : success_value_(success_value) { |
| 92 } |
| 93 |
| 94 virtual void Run() { |
| 95 TextInputClientMac::GetInstance()->SetCharacterIndexAndSignal( |
| 96 success_value_); |
| 97 } |
| 98 |
| 99 private: |
| 100 NSUInteger success_value_; |
| 101 }; |
| 102 |
| 103 class RectForRangeTask : public Task { |
| 104 public: |
| 105 RectForRangeTask(NSRect success_value) : success_value_(success_value) { |
| 106 } |
| 107 |
| 108 virtual void Run() { |
| 109 TextInputClientMac::GetInstance()->SetFirstRectAndSignal(success_value_); |
| 110 } |
| 111 |
| 112 private: |
| 113 NSRect success_value_; |
| 114 }; |
| 115 |
| 116 class SubstringTask : public Task { |
| 117 public: |
| 118 SubstringTask(NSAttributedString* success_value) |
| 119 : success_value_(success_value) { |
| 120 } |
| 121 |
| 122 virtual void Run() { |
| 123 TextInputClientMac::GetInstance()->SetSubstringAndSignal(success_value_); |
| 124 } |
| 125 |
| 126 private: |
| 127 NSAttributedString* success_value_; // weak |
| 128 }; |
| 129 |
| 130 // Test Cases ////////////////////////////////////////////////////////////////// |
| 131 |
| 132 TEST_F(TextInputClientMacTest, GetCharacterIndex) { |
| 133 ScopedTestingThread thread(this); |
| 134 const NSUInteger kSuccessValue = 42; |
| 135 |
| 136 PostTask(new CharacterIndexTask(kSuccessValue)); |
| 137 NSUInteger index = service()->GetCharacterIndexAtPoint( |
| 138 widget(), gfx::Point(2, 2)); |
| 139 |
| 140 EXPECT_EQ(1U, ipc_sink().message_count()); |
| 141 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
| 142 TextInputClientMsg_CharacterIndexForPoint::ID)); |
| 143 EXPECT_EQ(kSuccessValue, index); |
| 144 } |
| 145 |
| 146 TEST_F(TextInputClientMacTest, TimeoutCharacterIndex) { |
| 147 NSUInteger index = service()->GetCharacterIndexAtPoint( |
| 148 widget(), gfx::Point(2, 2)); |
| 149 EXPECT_EQ(1U, ipc_sink().message_count()); |
| 150 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
| 151 TextInputClientMsg_CharacterIndexForPoint::ID)); |
| 152 EXPECT_EQ(NSNotFound, index); |
| 153 } |
| 154 |
| 155 TEST_F(TextInputClientMacTest, GetRectForRange) { |
| 156 ScopedTestingThread thread(this); |
| 157 const NSRect kSuccessValue = NSMakeRect(42, 43, 44, 45); |
| 158 |
| 159 PostTask(new RectForRangeTask(kSuccessValue)); |
| 160 NSRect rect = service()->GetFirstRectForRange(widget(), NSMakeRange(0, 32)); |
| 161 |
| 162 EXPECT_EQ(1U, ipc_sink().message_count()); |
| 163 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
| 164 TextInputClientMsg_FirstRectForCharacterRange::ID)); |
| 165 EXPECT_TRUE(NSEqualRects(kSuccessValue, rect)); |
| 166 } |
| 167 |
| 168 TEST_F(TextInputClientMacTest, TimeoutRectForRange) { |
| 169 NSRect rect = service()->GetFirstRectForRange(widget(), NSMakeRange(0, 32)); |
| 170 EXPECT_EQ(1U, ipc_sink().message_count()); |
| 171 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
| 172 TextInputClientMsg_FirstRectForCharacterRange::ID)); |
| 173 EXPECT_TRUE(NSEqualRects(NSZeroRect, rect)); |
| 174 } |
| 175 |
| 176 TEST_F(TextInputClientMacTest, GetSubstring) { |
| 177 ScopedTestingThread thread(this); |
| 178 NSDictionary* attributes = |
| 179 [NSDictionary dictionaryWithObject:[NSColor purpleColor] |
| 180 forKey:NSForegroundColorAttributeName]; |
| 181 scoped_nsobject<NSAttributedString> kSuccessValue( |
| 182 [[NSAttributedString alloc] initWithString:@"Barney is a purple dinosaur" |
| 183 attributes:attributes]); |
| 184 |
| 185 PostTask(new SubstringTask(kSuccessValue.get())); |
| 186 NSAttributedString* string = service()->GetAttributedSubstringFromRange( |
| 187 widget(), NSMakeRange(0, 32)); |
| 188 |
| 189 EXPECT_NSEQ(kSuccessValue, string); |
| 190 EXPECT_NE(kSuccessValue.get(), string); // |string| should be a copy. |
| 191 EXPECT_EQ(1U, ipc_sink().message_count()); |
| 192 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
| 193 TextInputClientMsg_StringForRange::ID)); |
| 194 } |
| 195 |
| 196 TEST_F(TextInputClientMacTest, TimeoutSubstring) { |
| 197 NSAttributedString* string = service()->GetAttributedSubstringFromRange( |
| 198 widget(), NSMakeRange(0, 32)); |
| 199 EXPECT_EQ(nil, string); |
| 200 EXPECT_EQ(1U, ipc_sink().message_count()); |
| 201 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
| 202 TextInputClientMsg_StringForRange::ID)); |
| 203 } |
| 204 |
| 205 } // namespace |
OLD | NEW |