Index: chrome/browser/renderer_host/text_input_client_mac_unittest.mm |
diff --git a/chrome/browser/renderer_host/text_input_client_mac_unittest.mm b/chrome/browser/renderer_host/text_input_client_mac_unittest.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8d1c7947fae013059c91e5bb7a2e06f0647fa6f8 |
--- /dev/null |
+++ b/chrome/browser/renderer_host/text_input_client_mac_unittest.mm |
@@ -0,0 +1,160 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#import "chrome/browser/renderer_host/text_input_client_mac.h" |
+ |
+#include "base/bind.h" |
+#include "base/message_loop.h" |
+#include "base/threading/thread.h" |
+#include "chrome/common/text_input_client_messages.h" |
+#include "chrome/test/testing_profile.h" |
+#include "content/browser/renderer_host/mock_render_process_host.h" |
+#include "content/browser/renderer_host/render_widget_host.h" |
+#include "ipc/ipc_test_sink.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "testing/gtest_mac.h" |
+ |
+namespace { |
+ |
+// This test does not test the WebKit side of the dictionary system (which |
+// performs the actual data fetching), but rather this just tests that the |
+// service's signaling system works. |
+class TextInputClientMacTest : public testing::Test { |
+ public: |
+ TextInputClientMacTest() |
+ : message_loop_(MessageLoop::TYPE_UI), |
+ profile_(), |
+ process_(new MockRenderProcessHost(&profile_)), |
+ widget_(process_, 1), |
+ thread_("TextInputClientMacTestThread") {} |
+ |
+ // Accessor for the TextInputClientMac instance. |
+ TextInputClientMac* service() { |
+ return TextInputClientMac::GetInstance(); |
+ } |
+ |
+ // Helper method to post a task on the testing thread's MessageLoop after |
+ // a short delay. |
+ void PostTask(const base::Closure& task) { |
+ const int64 kTaskDelayMs = 200; |
+ thread_.message_loop()->PostDelayedTask(FROM_HERE, task, kTaskDelayMs); |
+ } |
+ |
+ RenderWidgetHost* widget() { |
+ return &widget_; |
+ } |
+ |
+ IPC::TestSink& ipc_sink() { |
+ return process_->sink(); |
+ } |
+ |
+ private: |
+ friend class ScopedTestingThread; |
+ |
+ MessageLoop message_loop_; |
+ TestingProfile profile_; |
+ |
+ // Gets deleted when the last RWH in the "process" gets destroyed. |
+ MockRenderProcessHost* process_; |
+ RenderWidgetHost widget_; |
+ |
+ base::Thread thread_; |
+}; |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+ |
+// Helper class that Start()s and Stop()s a thread according to the scope of the |
+// object. |
+class ScopedTestingThread { |
+ public: |
+ ScopedTestingThread(TextInputClientMacTest* test) : thread_(test->thread_) { |
+ thread_.Start(); |
+ } |
+ ~ScopedTestingThread() { |
+ thread_.Stop(); |
+ } |
+ |
+ private: |
+ base::Thread& thread_; |
+}; |
+ |
+// Test Cases ////////////////////////////////////////////////////////////////// |
+ |
+TEST_F(TextInputClientMacTest, GetCharacterIndex) { |
+ ScopedTestingThread thread(this); |
+ const NSUInteger kSuccessValue = 42; |
+ |
+ PostTask(base::Bind(&TextInputClientMac::SetCharacterIndexAndSignal, |
+ base::Unretained(service()), kSuccessValue)); |
+ NSUInteger index = service()->GetCharacterIndexAtPoint( |
+ widget(), gfx::Point(2, 2)); |
+ |
+ EXPECT_EQ(1U, ipc_sink().message_count()); |
+ EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
+ TextInputClientMsg_CharacterIndexForPoint::ID)); |
+ EXPECT_EQ(kSuccessValue, index); |
+} |
+ |
+TEST_F(TextInputClientMacTest, TimeoutCharacterIndex) { |
+ NSUInteger index = service()->GetCharacterIndexAtPoint( |
+ widget(), gfx::Point(2, 2)); |
+ EXPECT_EQ(1U, ipc_sink().message_count()); |
+ EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
+ TextInputClientMsg_CharacterIndexForPoint::ID)); |
+ EXPECT_EQ(NSNotFound, index); |
+} |
+ |
+TEST_F(TextInputClientMacTest, GetRectForRange) { |
+ ScopedTestingThread thread(this); |
+ const NSRect kSuccessValue = NSMakeRect(42, 43, 44, 45); |
+ |
+ PostTask(base::Bind(&TextInputClientMac::SetFirstRectAndSignal, |
+ base::Unretained(service()), kSuccessValue)); |
+ NSRect rect = service()->GetFirstRectForRange(widget(), NSMakeRange(0, 32)); |
+ |
+ EXPECT_EQ(1U, ipc_sink().message_count()); |
+ EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
+ TextInputClientMsg_FirstRectForCharacterRange::ID)); |
+ EXPECT_TRUE(NSEqualRects(kSuccessValue, rect)); |
+} |
+ |
+TEST_F(TextInputClientMacTest, TimeoutRectForRange) { |
+ NSRect rect = service()->GetFirstRectForRange(widget(), NSMakeRange(0, 32)); |
+ EXPECT_EQ(1U, ipc_sink().message_count()); |
+ EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
+ TextInputClientMsg_FirstRectForCharacterRange::ID)); |
+ EXPECT_TRUE(NSEqualRects(NSZeroRect, rect)); |
+} |
+ |
+TEST_F(TextInputClientMacTest, GetSubstring) { |
+ ScopedTestingThread thread(this); |
+ NSDictionary* attributes = |
+ [NSDictionary dictionaryWithObject:[NSColor purpleColor] |
+ forKey:NSForegroundColorAttributeName]; |
+ scoped_nsobject<NSAttributedString> kSuccessValue( |
+ [[NSAttributedString alloc] initWithString:@"Barney is a purple dinosaur" |
+ attributes:attributes]); |
+ |
+ PostTask(base::Bind(&TextInputClientMac::SetSubstringAndSignal, |
+ base::Unretained(service()), base::Unretained(kSuccessValue.get()))); |
+ NSAttributedString* string = service()->GetAttributedSubstringFromRange( |
+ widget(), NSMakeRange(0, 32)); |
+ |
+ EXPECT_NSEQ(kSuccessValue, string); |
+ EXPECT_NE(kSuccessValue.get(), string); // |string| should be a copy. |
+ EXPECT_EQ(1U, ipc_sink().message_count()); |
+ EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
+ TextInputClientMsg_StringForRange::ID)); |
+} |
+ |
+TEST_F(TextInputClientMacTest, TimeoutSubstring) { |
+ NSAttributedString* string = service()->GetAttributedSubstringFromRange( |
+ widget(), NSMakeRange(0, 32)); |
+ EXPECT_EQ(nil, string); |
+ EXPECT_EQ(1U, ipc_sink().message_count()); |
+ EXPECT_TRUE(ipc_sink().GetUniqueMessageMatching( |
+ TextInputClientMsg_StringForRange::ID)); |
+} |
+ |
+} // namespace |