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/bind.h" |
| 8 #include "base/message_loop.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(const base::Closure& 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 // Test Cases ////////////////////////////////////////////////////////////////// |
| 83 |
| 84 TEST_F(TextInputClientMacTest, GetCharacterIndex) { |
| 85 ScopedTestingThread thread(this); |
| 86 const NSUInteger kSuccessValue = 42; |
| 87 |
| 88 PostTask(base::Bind(&TextInputClientMac::SetCharacterIndexAndSignal, |
| 89 base::Unretained(service()), kSuccessValue)); |
| 90 NSUInteger index = service()->GetCharacterIndexAtPoint( |
| 91 widget(), gfx::Point(2, 2)); |
| 92 |
| 93 EXPECT_EQ(1U, ipc_sink().message_count()); |
| 94 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
| 95 TextInputClientMsg_CharacterIndexForPoint::ID)); |
| 96 EXPECT_EQ(kSuccessValue, index); |
| 97 } |
| 98 |
| 99 TEST_F(TextInputClientMacTest, TimeoutCharacterIndex) { |
| 100 NSUInteger index = service()->GetCharacterIndexAtPoint( |
| 101 widget(), gfx::Point(2, 2)); |
| 102 EXPECT_EQ(1U, ipc_sink().message_count()); |
| 103 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
| 104 TextInputClientMsg_CharacterIndexForPoint::ID)); |
| 105 EXPECT_EQ(NSNotFound, index); |
| 106 } |
| 107 |
| 108 TEST_F(TextInputClientMacTest, GetRectForRange) { |
| 109 ScopedTestingThread thread(this); |
| 110 const NSRect kSuccessValue = NSMakeRect(42, 43, 44, 45); |
| 111 |
| 112 PostTask(base::Bind(&TextInputClientMac::SetFirstRectAndSignal, |
| 113 base::Unretained(service()), kSuccessValue)); |
| 114 NSRect rect = service()->GetFirstRectForRange(widget(), NSMakeRange(0, 32)); |
| 115 |
| 116 EXPECT_EQ(1U, ipc_sink().message_count()); |
| 117 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
| 118 TextInputClientMsg_FirstRectForCharacterRange::ID)); |
| 119 EXPECT_TRUE(NSEqualRects(kSuccessValue, rect)); |
| 120 } |
| 121 |
| 122 TEST_F(TextInputClientMacTest, TimeoutRectForRange) { |
| 123 NSRect rect = service()->GetFirstRectForRange(widget(), NSMakeRange(0, 32)); |
| 124 EXPECT_EQ(1U, ipc_sink().message_count()); |
| 125 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
| 126 TextInputClientMsg_FirstRectForCharacterRange::ID)); |
| 127 EXPECT_TRUE(NSEqualRects(NSZeroRect, rect)); |
| 128 } |
| 129 |
| 130 TEST_F(TextInputClientMacTest, GetSubstring) { |
| 131 ScopedTestingThread thread(this); |
| 132 NSDictionary* attributes = |
| 133 [NSDictionary dictionaryWithObject:[NSColor purpleColor] |
| 134 forKey:NSForegroundColorAttributeName]; |
| 135 scoped_nsobject<NSAttributedString> kSuccessValue( |
| 136 [[NSAttributedString alloc] initWithString:@"Barney is a purple dinosaur" |
| 137 attributes:attributes]); |
| 138 |
| 139 PostTask(base::Bind(&TextInputClientMac::SetSubstringAndSignal, |
| 140 base::Unretained(service()), base::Unretained(kSuccessValue.get()))); |
| 141 NSAttributedString* string = service()->GetAttributedSubstringFromRange( |
| 142 widget(), NSMakeRange(0, 32)); |
| 143 |
| 144 EXPECT_NSEQ(kSuccessValue, string); |
| 145 EXPECT_NE(kSuccessValue.get(), string); // |string| should be a copy. |
| 146 EXPECT_EQ(1U, ipc_sink().message_count()); |
| 147 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
| 148 TextInputClientMsg_StringForRange::ID)); |
| 149 } |
| 150 |
| 151 TEST_F(TextInputClientMacTest, TimeoutSubstring) { |
| 152 NSAttributedString* string = service()->GetAttributedSubstringFromRange( |
| 153 widget(), NSMakeRange(0, 32)); |
| 154 EXPECT_EQ(nil, string); |
| 155 EXPECT_EQ(1U, ipc_sink().message_count()); |
| 156 EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
| 157 TextInputClientMsg_StringForRange::ID)); |
| 158 } |
| 159 |
| 160 } // namespace |
OLD | NEW |