Chromium Code Reviews| 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..04cba63333789f0429d2208dc54d4999ad2a73a8 |
| --- /dev/null |
| +++ b/chrome/browser/renderer_host/text_input_client_mac_unittest.mm |
| @@ -0,0 +1,170 @@ |
| +// 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 "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() |
| + : 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); |
| + } |
| + |
| + private: |
| + friend class ScopedTestingThread; |
| + 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; |
| + |
| + service()->BeforeRequest(); |
| + PostTask(new CharacterIndexTask(kSuccessValue)); |
| + NSUInteger index = service()->WaitForCharacterIndex(); |
| + service()->AfterRequest(); |
| + |
| + EXPECT_EQ(kSuccessValue, index); |
| +} |
| + |
| +TEST_F(TextInputClientMacTest, TimeoutCharacterIndex) { |
| + service()->BeforeRequest(); |
| + NSUInteger index = service()->WaitForCharacterIndex(); |
| + service()->AfterRequest(); |
| + EXPECT_EQ(NSNotFound, index); |
| +} |
| + |
| +TEST_F(TextInputClientMacTest, GetRectForRange) { |
| + ScopedTestingThread thread(this); |
| + const NSRect kSuccessValue = NSMakeRect(42, 43, 44, 45); |
| + |
| + service()->BeforeRequest(); |
| + PostTask(new RectForRangeTask(kSuccessValue)); |
| + NSRect rect = service()->WaitForFirstRect(); |
| + service()->AfterRequest(); |
| + |
| + EXPECT_TRUE(NSEqualRects(kSuccessValue, rect)); |
| +} |
| + |
| +TEST_F(TextInputClientMacTest, TimeoutRectForRange) { |
| + service()->BeforeRequest(); |
| + NSRect rect = service()->WaitForFirstRect(); |
| + service()->AfterRequest(); |
| + 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]); |
| + |
| + service()->BeforeRequest(); |
| + PostTask(new SubstringTask(kSuccessValue.get())); |
| + NSAttributedString* string = service()->WaitForSubstring(); |
| + service()->AfterRequest(); |
| + |
| + EXPECT_NSEQ(kSuccessValue, string); |
|
jeremy
2011/02/21 09:19:47
Does this test that the string attributes are the
Robert Sesek
2011/02/24 23:26:52
This does -[NSObject isEqual:]
|
| + EXPECT_NE(kSuccessValue.get(), string); // |string| should be a copy. |
| +} |
| + |
| +TEST_F(TextInputClientMacTest, TimeoutSubstring) { |
| + service()->BeforeRequest(); |
| + NSAttributedString* string = service()->WaitForSubstring(); |
| + service()->AfterRequest(); |
| + EXPECT_EQ(nil, string); |
| +} |
| + |
| +} // namespace |