Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6542)

Unified Diff: chrome/browser/renderer_host/text_input_client_mac_unittest.mm

Issue 6289009: [Mac] Implement the system dictionary popup by implementing NSTextInput methods. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address jam@ comments Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a287e47905beff400b47629074f541e02aab5590
--- /dev/null
+++ b/chrome/browser/renderer_host/text_input_client_mac_unittest.mm
@@ -0,0 +1,205 @@
+// 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/message_loop.h"
+#include "base/task.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(Task* 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_;
+};
+
+// Helper Tasks ////////////////////////////////////////////////////////////////
+//
+// These tasks are posted to the MessageLoop of TextInputClientMacTest::thread_
+// to test the condition variable signaling. There is one Task for each piece
+// of information the TextInputClientMac service deals with.
+////////////////////////////////////////////////////////////////////////////////
+
+class CharacterIndexTask : public Task {
+ public:
+ CharacterIndexTask(NSUInteger success_value) : success_value_(success_value) {
+ }
+
+ virtual void Run() {
+ TextInputClientMac::GetInstance()->SetCharacterIndexAndSignal(
+ success_value_);
+ }
+
+ private:
+ NSUInteger success_value_;
+};
+
+class RectForRangeTask : public Task {
+ public:
+ RectForRangeTask(NSRect success_value) : success_value_(success_value) {
+ }
+
+ virtual void Run() {
+ TextInputClientMac::GetInstance()->SetFirstRectAndSignal(success_value_);
+ }
+
+ private:
+ NSRect success_value_;
+};
+
+class SubstringTask : public Task {
+ public:
+ SubstringTask(NSAttributedString* success_value)
+ : success_value_(success_value) {
+ }
+
+ virtual void Run() {
+ TextInputClientMac::GetInstance()->SetSubstringAndSignal(success_value_);
+ }
+
+ private:
+ NSAttributedString* success_value_; // weak
+};
+
+// Test Cases //////////////////////////////////////////////////////////////////
+
+TEST_F(TextInputClientMacTest, GetCharacterIndex) {
+ ScopedTestingThread thread(this);
+ const NSUInteger kSuccessValue = 42;
+
+ PostTask(new CharacterIndexTask(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(new RectForRangeTask(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(new SubstringTask(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

Powered by Google App Engine
This is Rietveld 408576698