Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/scoped_temp_dir.h" | |
| 7 #include "chrome/browser/spellchecker/spellcheck_host.h" | 8 #include "chrome/browser/spellchecker/spellcheck_host.h" |
| 8 #include "chrome/browser/spellchecker/spellcheck_profile.h" | 9 #include "chrome/browser/spellchecker/spellcheck_profile.h" |
| 9 #include "content/browser/browser_thread.h" | 10 #include "content/browser/browser_thread.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 class MockSpellCheckHost : public SpellCheckHost { | 16 class MockSpellCheckHost : public SpellCheckHost { |
| 16 public: | 17 public: |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 bool IsCreatedHostReady() { | 50 bool IsCreatedHostReady() { |
| 50 return GetHost() == returning_from_create_.get(); | 51 return GetHost() == returning_from_create_.get(); |
| 51 } | 52 } |
| 52 | 53 |
| 53 void SetHostToBeCreated(MockSpellCheckHost* host) { | 54 void SetHostToBeCreated(MockSpellCheckHost* host) { |
| 54 EXPECT_CALL(*host, UnsetProfile()).Times(1); | 55 EXPECT_CALL(*host, UnsetProfile()).Times(1); |
| 55 EXPECT_CALL(*host, IsReady()).WillRepeatedly(testing::Return(true)); | 56 EXPECT_CALL(*host, IsReady()).WillRepeatedly(testing::Return(true)); |
| 56 returning_from_create_ = host; | 57 returning_from_create_ = host; |
| 57 } | 58 } |
| 58 | 59 |
| 60 FilePath GetCustomDictionaryDir() { | |
| 61 if (!custom_dictionary_dir_.IsValid()) | |
| 62 EXPECT_TRUE(custom_dictionary_dir_.CreateUniqueTempDir()); | |
| 63 | |
| 64 return custom_dictionary_dir_.path(); | |
| 65 } | |
| 66 | |
| 59 size_t create_host_calls_; | 67 size_t create_host_calls_; |
| 60 scoped_refptr<SpellCheckHost> returning_from_create_; | 68 scoped_refptr<SpellCheckHost> returning_from_create_; |
| 69 ScopedTempDir custom_dictionary_dir_; | |
| 61 }; | 70 }; |
| 62 | 71 |
| 63 typedef SpellCheckProfile::ReinitializeResult ResultType; | 72 typedef SpellCheckProfile::ReinitializeResult ResultType; |
| 64 } // namespace | 73 } // namespace |
| 65 | 74 |
| 66 class SpellCheckProfileTest : public testing::Test { | 75 class SpellCheckProfileTest : public testing::Test { |
| 67 protected: | 76 protected: |
| 68 SpellCheckProfileTest() | 77 SpellCheckProfileTest() |
| 69 : file_thread_(BrowserThread::FILE) { | 78 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 79 file_thread_(BrowserThread::FILE, &message_loop_) { | |
|
gmorrita
2011/10/12 11:03:36
I guess we need one MessageLoop instance per threa
| |
| 70 } | 80 } |
| 71 | 81 |
| 72 // SpellCheckHost will be deleted on FILE thread. | 82 // SpellCheckHost will be deleted on FILE thread. |
| 83 MessageLoop message_loop_; | |
| 84 BrowserThread ui_thread_; | |
| 73 BrowserThread file_thread_; | 85 BrowserThread file_thread_; |
| 74 }; | 86 }; |
| 75 | 87 |
| 76 TEST_F(SpellCheckProfileTest, ReinitializeEnabled) { | 88 TEST_F(SpellCheckProfileTest, ReinitializeEnabled) { |
| 77 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); | 89 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); |
| 78 TestingSpellCheckProfile target; | 90 scoped_refptr<TestingSpellCheckProfile> target( |
| 79 target.SetHostToBeCreated(host.get()); | 91 new TestingSpellCheckProfile()); |
| 92 target->SetHostToBeCreated(host.get()); | |
| 80 | 93 |
| 81 // The first call should create host. | 94 // The first call should create host. |
| 82 ResultType result1 = target.ReinitializeHost(false, true, "", NULL); | 95 ResultType result1 = target->ReinitializeHost(false, true, "", NULL); |
| 83 EXPECT_EQ(target.create_host_calls_, 1U); | 96 EXPECT_EQ(target->create_host_calls_, 1U); |
| 84 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); | 97 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); |
| 85 | 98 |
| 86 // The second call should be ignored. | 99 // The second call should be ignored. |
| 87 ResultType result2 = target.ReinitializeHost(false, true, "", NULL); | 100 ResultType result2 = target->ReinitializeHost(false, true, "", NULL); |
| 88 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING); | 101 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING); |
| 89 EXPECT_EQ(target.create_host_calls_, 1U); | 102 EXPECT_EQ(target->create_host_calls_, 1U); |
| 90 | 103 |
| 91 // Host should become ready after the notification. | 104 // Host should become ready after the notification. |
| 92 EXPECT_FALSE(target.IsCreatedHostReady()); | 105 EXPECT_FALSE(target->IsCreatedHostReady()); |
| 93 target.SpellCheckHostInitialized(0); | 106 target->SpellCheckHostInitialized(); |
| 94 EXPECT_TRUE(target.IsCreatedHostReady()); | 107 EXPECT_TRUE(target->IsCreatedHostReady()); |
| 95 } | 108 } |
| 96 | 109 |
| 97 TEST_F(SpellCheckProfileTest, ReinitializeDisabled) { | 110 TEST_F(SpellCheckProfileTest, ReinitializeDisabled) { |
| 98 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); | 111 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); |
| 99 TestingSpellCheckProfile target; | 112 scoped_refptr<TestingSpellCheckProfile> target( |
| 100 target.returning_from_create_ = host.get(); | 113 new TestingSpellCheckProfile()); |
| 114 target->returning_from_create_ = host.get(); | |
| 101 | 115 |
| 102 // If enabled is false, nothing should happen | 116 // If enabled is false, nothing should happen |
| 103 ResultType result1 = target.ReinitializeHost(false, false, "", NULL); | 117 ResultType result1 = target->ReinitializeHost(false, false, "", NULL); |
| 104 EXPECT_EQ(target.create_host_calls_, 0U); | 118 EXPECT_EQ(target->create_host_calls_, 0U); |
| 105 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_DID_NOTHING); | 119 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_DID_NOTHING); |
| 106 | 120 |
| 107 // Nothing should happen even if forced. | 121 // Nothing should happen even if forced. |
| 108 ResultType result2 = target.ReinitializeHost(true, false, "", NULL); | 122 ResultType result2 = target->ReinitializeHost(true, false, "", NULL); |
| 109 EXPECT_EQ(target.create_host_calls_, 0U); | 123 EXPECT_EQ(target->create_host_calls_, 0U); |
| 110 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING); | 124 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING); |
| 111 } | 125 } |
| 112 | 126 |
| 113 TEST_F(SpellCheckProfileTest, ReinitializeRemove) { | 127 TEST_F(SpellCheckProfileTest, ReinitializeRemove) { |
| 114 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); | 128 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); |
| 115 TestingSpellCheckProfile target; | 129 scoped_refptr<TestingSpellCheckProfile> target( |
| 116 target.SetHostToBeCreated(host.get()); | 130 new TestingSpellCheckProfile()); |
| 117 | 131 target->SetHostToBeCreated(host.get()); |
| 118 | 132 |
| 119 // At first, create the host. | 133 // At first, create the host. |
| 120 ResultType result1 = target.ReinitializeHost(false, true, "", NULL); | 134 ResultType result1 = target->ReinitializeHost(false, true, "", NULL); |
| 121 target.SpellCheckHostInitialized(0); | 135 target->SpellCheckHostInitialized(); |
| 122 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); | 136 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); |
| 123 EXPECT_TRUE(target.IsCreatedHostReady()); | 137 EXPECT_TRUE(target->IsCreatedHostReady()); |
| 124 | 138 |
| 125 // Then the host should be deleted if it's forced to be disabled. | 139 // Then the host should be deleted if it's forced to be disabled. |
| 126 ResultType result2 = target.ReinitializeHost(true, false, "", NULL); | 140 ResultType result2 = target->ReinitializeHost(true, false, "", NULL); |
| 127 target.SpellCheckHostInitialized(0); | 141 target->SpellCheckHostInitialized(); |
| 128 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_REMOVED_HOST); | 142 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_REMOVED_HOST); |
| 129 EXPECT_FALSE(target.IsCreatedHostReady()); | 143 EXPECT_FALSE(target->IsCreatedHostReady()); |
| 130 } | 144 } |
| 131 | 145 |
| 132 TEST_F(SpellCheckProfileTest, ReinitializeRecreate) { | 146 TEST_F(SpellCheckProfileTest, ReinitializeRecreate) { |
| 133 scoped_refptr<MockSpellCheckHost> host1(new MockSpellCheckHost()); | 147 scoped_refptr<MockSpellCheckHost> host1(new MockSpellCheckHost()); |
| 134 TestingSpellCheckProfile target; | 148 scoped_refptr<TestingSpellCheckProfile> target( |
| 135 target.SetHostToBeCreated(host1.get()); | 149 new TestingSpellCheckProfile()); |
| 150 target->SetHostToBeCreated(host1.get()); | |
| 136 | 151 |
| 137 // At first, create the host. | 152 // At first, create the host. |
| 138 ResultType result1 = target.ReinitializeHost(false, true, "", NULL); | 153 ResultType result1 = target->ReinitializeHost(false, true, "", NULL); |
| 139 target.SpellCheckHostInitialized(0); | 154 target->SpellCheckHostInitialized(); |
| 140 EXPECT_EQ(target.create_host_calls_, 1U); | 155 EXPECT_EQ(target->create_host_calls_, 1U); |
| 141 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); | 156 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); |
| 142 EXPECT_TRUE(target.IsCreatedHostReady()); | 157 EXPECT_TRUE(target->IsCreatedHostReady()); |
| 143 | 158 |
| 144 // Then the host should be re-created if it's forced to recreate. | 159 // Then the host should be re-created if it's forced to recreate. |
| 145 scoped_refptr<MockSpellCheckHost> host2(new MockSpellCheckHost()); | 160 scoped_refptr<MockSpellCheckHost> host2(new MockSpellCheckHost()); |
| 146 target.SetHostToBeCreated(host2.get()); | 161 target->SetHostToBeCreated(host2.get()); |
| 147 | 162 |
| 148 ResultType result2 = target.ReinitializeHost(true, true, "", NULL); | 163 ResultType result2 = target->ReinitializeHost(true, true, "", NULL); |
| 149 target.SpellCheckHostInitialized(0); | 164 target->SpellCheckHostInitialized(); |
| 150 EXPECT_EQ(target.create_host_calls_, 2U); | 165 EXPECT_EQ(target->create_host_calls_, 2U); |
| 151 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_CREATED_HOST); | 166 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_CREATED_HOST); |
| 152 EXPECT_TRUE(target.IsCreatedHostReady()); | 167 EXPECT_TRUE(target->IsCreatedHostReady()); |
| 153 } | 168 } |
| 154 | 169 |
| 155 TEST_F(SpellCheckProfileTest, SpellCheckHostInitializedWithCustomWords) { | 170 TEST_F(SpellCheckProfileTest, CustomDictionaryLoaded) { |
| 156 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); | 171 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); |
| 157 TestingSpellCheckProfile target; | 172 scoped_refptr<TestingSpellCheckProfile> target( |
| 158 target.SetHostToBeCreated(host.get()); | 173 new TestingSpellCheckProfile()); |
| 159 target.ReinitializeHost(false, true, "", NULL); | 174 target->SetHostToBeCreated(host.get()); |
| 175 target->ReinitializeHost(false, true, "", NULL); | |
| 176 | |
| 177 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words | |
| 178 (new SpellCheckProfile::CustomWordList()); | |
| 179 loaded_custom_words->push_back("foo"); | |
| 180 loaded_custom_words->push_back("bar"); | |
| 181 | |
| 182 SpellCheckProfile::CustomWordList expected(*loaded_custom_words); | |
| 183 target->CustomDictionaryLoaded(loaded_custom_words.release()); | |
| 184 EXPECT_EQ(target->GetCustomWords(), expected); | |
| 185 } | |
| 186 | |
| 187 TEST_F(SpellCheckProfileTest, SaveAndLoad) { | |
| 188 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); | |
| 189 scoped_refptr<TestingSpellCheckProfile> target( | |
| 190 new TestingSpellCheckProfile()); | |
| 191 target->SetHostToBeCreated(host.get()); | |
| 192 target->ReinitializeHost(false, true, "", NULL); | |
| 160 | 193 |
| 161 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words | 194 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words |
| 162 (new SpellCheckProfile::CustomWordList()); | 195 (new SpellCheckProfile::CustomWordList()); |
| 163 loaded_custom_words->push_back("foo"); | 196 target->SpellCheckHostInitialized(); |
| 164 loaded_custom_words->push_back("bar"); | 197 SpellCheckProfile::CustomWordList expected; |
| 165 SpellCheckProfile::CustomWordList expected(*loaded_custom_words); | 198 EXPECT_EQ(target->GetCustomWords(), expected); |
| 166 target.SpellCheckHostInitialized(loaded_custom_words.release()); | 199 target->CustomWordAddedLocally("foo"); |
| 167 EXPECT_EQ(target.GetCustomWords(), expected); | 200 expected.push_back("foo"); |
| 201 EXPECT_EQ(target->GetCustomWords(), expected); | |
| 202 target->CustomWordAddedLocally("bar"); | |
| 203 expected.push_back("bar"); | |
| 204 EXPECT_EQ(target->GetCustomWords(), expected); | |
| 205 | |
| 206 file_thread_.message_loop()->RunAllPending(); | |
| 207 | |
| 208 // The same dictionary should be loaded again. | |
| 209 target->ClearCustomWords(); | |
| 210 target->LoadCustomDictionary(); | |
| 211 file_thread_.message_loop()->RunAllPending(); | |
| 212 ui_thread_.message_loop()->RunAllPending(); | |
| 213 | |
| 214 EXPECT_EQ(target->GetCustomWords(), expected); | |
| 168 } | 215 } |
| 169 | |
| 170 TEST_F(SpellCheckProfileTest, CustomWordAddedLocally) { | |
| 171 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); | |
| 172 TestingSpellCheckProfile target; | |
| 173 target.SetHostToBeCreated(host.get()); | |
| 174 target.ReinitializeHost(false, true, "", NULL); | |
| 175 | |
| 176 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words | |
| 177 (new SpellCheckProfile::CustomWordList()); | |
| 178 target.SpellCheckHostInitialized(NULL); | |
| 179 SpellCheckProfile::CustomWordList expected; | |
| 180 EXPECT_EQ(target.GetCustomWords(), expected); | |
| 181 target.CustomWordAddedLocally("foo"); | |
| 182 expected.push_back("foo"); | |
| 183 EXPECT_EQ(target.GetCustomWords(), expected); | |
| 184 target.CustomWordAddedLocally("bar"); | |
| 185 expected.push_back("bar"); | |
| 186 EXPECT_EQ(target.GetCustomWords(), expected); | |
| 187 } | |
| OLD | NEW |