Index: chrome/browser/ui/cocoa/lookup_in_dictionary_unittest.mm |
diff --git a/chrome/browser/ui/cocoa/lookup_in_dictionary_unittest.mm b/chrome/browser/ui/cocoa/lookup_in_dictionary_unittest.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..520424556ce35928fcf6df3453cdbde9a9deb487 |
--- /dev/null |
+++ b/chrome/browser/ui/cocoa/lookup_in_dictionary_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. |
+ |
+#include "chrome/browser/ui/cocoa/lookup_in_dictionary.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 LookupInDictionaryTest : public testing::Test { |
+ public: |
+ LookupInDictionaryTest() |
+ : thread_("LookupInDictionaryTestThread") {} |
+ |
+ // Accessor for the LookupInDictionary instance. |
+ LookupInDictionary* service() { |
+ return LookupInDictionary::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(LookupInDictionaryTest* test) : thread_(test->thread_) { |
+ thread_.Start(); |
+ } |
+ ~ScopedTestingThread() { |
+ thread_.Stop(); |
+ } |
+ |
+ private: |
+ base::Thread& thread_; |
+}; |
+ |
+// Helper Tasks //////////////////////////////////////////////////////////////// |
+// |
+// These tasks are posted to the MessageLoop of LookupInDictionaryTest::thread_ |
+// to test the condition variable signaling. There is one Task for each piece |
+// of information the LookupInDictionary service deals with. |
+//////////////////////////////////////////////////////////////////////////////// |
+ |
+class CharacterIndexTask : public Task { |
+ public: |
+ CharacterIndexTask(NSUInteger success_value) : success_value_(success_value) { |
+ } |
+ |
+ virtual void Run() { |
+ LookupInDictionary::GetInstance()->SetCharacterIndexAndSignal( |
+ success_value_); |
+ } |
+ |
+ private: |
+ NSUInteger success_value_; |
+}; |
+ |
+class RectForRangeTask : public Task { |
+ public: |
+ RectForRangeTask(NSRect success_value) : success_value_(success_value) { |
+ } |
+ |
+ virtual void Run() { |
+ LookupInDictionary::GetInstance()->SetFirstRectAndSignal(success_value_); |
+ } |
+ |
+ private: |
+ NSRect success_value_; |
+}; |
+ |
+class SubstringTask : public Task { |
+ public: |
+ SubstringTask(NSAttributedString* success_value) |
+ : success_value_(success_value) { |
+ } |
+ |
+ virtual void Run() { |
+ LookupInDictionary::GetInstance()->SetSubstringAndSignal(success_value_); |
+ } |
+ |
+ private: |
+ NSAttributedString* success_value_; // weak |
+}; |
+ |
+// Test Cases ////////////////////////////////////////////////////////////////// |
+ |
+TEST_F(LookupInDictionaryTest, 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(LookupInDictionaryTest, TimeoutCharacterIndex) { |
+ service()->BeforeRequest(); |
+ NSUInteger index = service()->WaitForCharacterIndex(); |
+ service()->AfterRequest(); |
+ EXPECT_EQ(NSNotFound, index); |
+} |
+ |
+TEST_F(LookupInDictionaryTest, 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(LookupInDictionaryTest, TimeoutRectForRange) { |
+ service()->BeforeRequest(); |
+ NSRect rect = service()->WaitForFirstRect(); |
+ service()->AfterRequest(); |
+ EXPECT_TRUE(NSEqualRects(NSZeroRect, rect)); |
+} |
+ |
+TEST_F(LookupInDictionaryTest, 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); |
+ EXPECT_NE(kSuccessValue.get(), string); // |string| should be a copy. |
+} |
+ |
+TEST_F(LookupInDictionaryTest, TimeoutSubstring) { |
+ service()->BeforeRequest(); |
+ NSAttributedString* string = service()->WaitForSubstring(); |
+ service()->AfterRequest(); |
+ EXPECT_EQ(nil, string); |
+} |
+ |
+} // namespace |