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 #include "chrome/browser/ui/cocoa/lookup_in_dictionary.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "base/task.h" |
| 9 #include "base/threading/thread.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/gtest_mac.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 // This test does not test the WebKit side of the dictionary system (which |
| 16 // performs the actual data fetching), but rather this just tests that the |
| 17 // service's signaling system works. |
| 18 class LookupInDictionaryTest : public testing::Test { |
| 19 public: |
| 20 LookupInDictionaryTest() |
| 21 : thread_("LookupInDictionaryTestThread") {} |
| 22 |
| 23 // Accessor for the LookupInDictionary instance. |
| 24 LookupInDictionary* service() { |
| 25 return LookupInDictionary::GetInstance(); |
| 26 } |
| 27 |
| 28 // Helper method to post a task on the testing thread's MessageLoop after |
| 29 // a short delay. |
| 30 void PostTask(Task* task) { |
| 31 const int64 kTaskDelayMs = 200; |
| 32 thread_.message_loop()->PostDelayedTask(FROM_HERE, task, kTaskDelayMs); |
| 33 } |
| 34 |
| 35 private: |
| 36 friend class ScopedTestingThread; |
| 37 base::Thread thread_; |
| 38 }; |
| 39 |
| 40 //////////////////////////////////////////////////////////////////////////////// |
| 41 |
| 42 // Helper class that Start()s and Stop()s a thread according to the scope of the |
| 43 // object. |
| 44 class ScopedTestingThread { |
| 45 public: |
| 46 ScopedTestingThread(LookupInDictionaryTest* test) : thread_(test->thread_) { |
| 47 thread_.Start(); |
| 48 } |
| 49 ~ScopedTestingThread() { |
| 50 thread_.Stop(); |
| 51 } |
| 52 |
| 53 private: |
| 54 base::Thread& thread_; |
| 55 }; |
| 56 |
| 57 // Helper Tasks //////////////////////////////////////////////////////////////// |
| 58 // |
| 59 // These tasks are posted to the MessageLoop of LookupInDictionaryTest::thread_ |
| 60 // to test the condition variable signaling. There is one Task for each piece |
| 61 // of information the LookupInDictionary service deals with. |
| 62 //////////////////////////////////////////////////////////////////////////////// |
| 63 |
| 64 class CharacterIndexTask : public Task { |
| 65 public: |
| 66 CharacterIndexTask(NSUInteger success_value) : success_value_(success_value) { |
| 67 } |
| 68 |
| 69 virtual void Run() { |
| 70 LookupInDictionary::GetInstance()->SetCharacterIndexAndSignal( |
| 71 success_value_); |
| 72 } |
| 73 |
| 74 private: |
| 75 NSUInteger success_value_; |
| 76 }; |
| 77 |
| 78 class RectForRangeTask : public Task { |
| 79 public: |
| 80 RectForRangeTask(NSRect success_value) : success_value_(success_value) { |
| 81 } |
| 82 |
| 83 virtual void Run() { |
| 84 LookupInDictionary::GetInstance()->SetFirstRectAndSignal(success_value_); |
| 85 } |
| 86 |
| 87 private: |
| 88 NSRect success_value_; |
| 89 }; |
| 90 |
| 91 class SubstringTask : public Task { |
| 92 public: |
| 93 SubstringTask(NSAttributedString* success_value) |
| 94 : success_value_(success_value) { |
| 95 } |
| 96 |
| 97 virtual void Run() { |
| 98 LookupInDictionary::GetInstance()->SetSubstringAndSignal(success_value_); |
| 99 } |
| 100 |
| 101 private: |
| 102 NSAttributedString* success_value_; // weak |
| 103 }; |
| 104 |
| 105 // Test Cases ////////////////////////////////////////////////////////////////// |
| 106 |
| 107 TEST_F(LookupInDictionaryTest, GetCharacterIndex) { |
| 108 ScopedTestingThread thread(this); |
| 109 const NSUInteger kSuccessValue = 42; |
| 110 |
| 111 service()->BeforeRequest(); |
| 112 PostTask(new CharacterIndexTask(kSuccessValue)); |
| 113 NSUInteger index = service()->WaitForCharacterIndex(); |
| 114 service()->AfterRequest(); |
| 115 |
| 116 EXPECT_EQ(kSuccessValue, index); |
| 117 } |
| 118 |
| 119 TEST_F(LookupInDictionaryTest, TimeoutCharacterIndex) { |
| 120 service()->BeforeRequest(); |
| 121 NSUInteger index = service()->WaitForCharacterIndex(); |
| 122 service()->AfterRequest(); |
| 123 EXPECT_EQ(NSNotFound, index); |
| 124 } |
| 125 |
| 126 TEST_F(LookupInDictionaryTest, GetRectForRange) { |
| 127 ScopedTestingThread thread(this); |
| 128 const NSRect kSuccessValue = NSMakeRect(42, 43, 44, 45); |
| 129 |
| 130 service()->BeforeRequest(); |
| 131 PostTask(new RectForRangeTask(kSuccessValue)); |
| 132 NSRect rect = service()->WaitForFirstRect(); |
| 133 service()->AfterRequest(); |
| 134 |
| 135 EXPECT_TRUE(NSEqualRects(kSuccessValue, rect)); |
| 136 } |
| 137 |
| 138 TEST_F(LookupInDictionaryTest, TimeoutRectForRange) { |
| 139 service()->BeforeRequest(); |
| 140 NSRect rect = service()->WaitForFirstRect(); |
| 141 service()->AfterRequest(); |
| 142 EXPECT_TRUE(NSEqualRects(NSZeroRect, rect)); |
| 143 } |
| 144 |
| 145 TEST_F(LookupInDictionaryTest, GetSubstring) { |
| 146 ScopedTestingThread thread(this); |
| 147 NSDictionary* attributes = |
| 148 [NSDictionary dictionaryWithObject:[NSColor purpleColor] |
| 149 forKey:NSForegroundColorAttributeName]; |
| 150 scoped_nsobject<NSAttributedString> kSuccessValue( |
| 151 [[NSAttributedString alloc] initWithString:@"Barney is a purple dinosaur" |
| 152 attributes:attributes]); |
| 153 |
| 154 service()->BeforeRequest(); |
| 155 PostTask(new SubstringTask(kSuccessValue.get())); |
| 156 NSAttributedString* string = service()->WaitForSubstring(); |
| 157 service()->AfterRequest(); |
| 158 |
| 159 EXPECT_NSEQ(kSuccessValue, string); |
| 160 EXPECT_NE(kSuccessValue.get(), string); // |string| should be a copy. |
| 161 } |
| 162 |
| 163 TEST_F(LookupInDictionaryTest, TimeoutSubstring) { |
| 164 service()->BeforeRequest(); |
| 165 NSAttributedString* string = service()->WaitForSubstring(); |
| 166 service()->AfterRequest(); |
| 167 EXPECT_EQ(nil, string); |
| 168 } |
| 169 |
| 170 } // namespace |
OLD | NEW |