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: |
17 MOCK_METHOD0(UnsetProfile, void()); | 18 MOCK_METHOD0(UnsetProfile, void()); |
18 MOCK_METHOD1(InitForRenderer, void(RenderProcessHost* process)); | 19 MOCK_METHOD1(InitForRenderer, void(RenderProcessHost* process)); |
19 MOCK_METHOD1(AddWord, void(const std::string& word)); | 20 MOCK_METHOD1(AddWord, void(const std::string& word)); |
20 MOCK_CONST_METHOD0(GetDictionaryFile, const base::PlatformFile&()); | 21 MOCK_CONST_METHOD0(GetDictionaryFile, const base::PlatformFile&()); |
21 MOCK_CONST_METHOD0(GetCustomWords, | 22 MOCK_CONST_METHOD0(GetCustomWords, |
22 const SpellCheckProfile::CustomWordList&()); | 23 const SpellCheckProfile::CustomWordList&()); |
23 MOCK_CONST_METHOD0(GetLastAddedFile, const std::string&()); | 24 MOCK_CONST_METHOD0(GetLastAddedFile, const std::string&()); |
24 MOCK_CONST_METHOD0(GetLanguage, const std::string&()); | 25 MOCK_CONST_METHOD0(GetLanguage, const std::string&()); |
25 MOCK_CONST_METHOD0(IsUsingPlatformChecker, bool()); | 26 MOCK_CONST_METHOD0(IsUsingPlatformChecker, bool()); |
26 MOCK_CONST_METHOD0(GetMetrics, SpellCheckHostMetrics*()); | 27 MOCK_CONST_METHOD0(GetMetrics, SpellCheckHostMetrics*()); |
27 MOCK_CONST_METHOD0(IsReady, bool()); | 28 MOCK_CONST_METHOD0(IsReady, bool()); |
28 }; | 29 }; |
29 | 30 |
30 class TestingSpellCheckProfile : public SpellCheckProfile { | 31 class TestingSpellCheckProfile : public SpellCheckProfile { |
31 public: | 32 public: |
32 TestingSpellCheckProfile() | 33 explicit TestingSpellCheckProfile(const FilePath& profile_dir) |
33 : create_host_calls_(0) { | 34 : SpellCheckProfile(profile_dir), |
| 35 create_host_calls_(0) { |
34 } | 36 } |
35 | 37 |
36 virtual SpellCheckHost* CreateHost( | 38 virtual SpellCheckHost* CreateHost( |
37 SpellCheckProfileProvider* profile, | 39 SpellCheckProfileProvider* profile, |
38 const std::string& language, | 40 const std::string& language, |
39 net::URLRequestContextGetter* request_context, | 41 net::URLRequestContextGetter* request_context, |
40 SpellCheckHostMetrics* metrics) { | 42 SpellCheckHostMetrics* metrics) { |
41 create_host_calls_++; | 43 create_host_calls_++; |
42 return returning_from_create_.get(); | 44 return returning_from_create_.get(); |
43 } | 45 } |
(...skipping 24 matching lines...) Expand all Loading... |
68 SpellCheckProfileTest() | 70 SpellCheckProfileTest() |
69 : file_thread_(BrowserThread::FILE) { | 71 : file_thread_(BrowserThread::FILE) { |
70 } | 72 } |
71 | 73 |
72 // SpellCheckHost will be deleted on FILE thread. | 74 // SpellCheckHost will be deleted on FILE thread. |
73 BrowserThread file_thread_; | 75 BrowserThread file_thread_; |
74 }; | 76 }; |
75 | 77 |
76 TEST_F(SpellCheckProfileTest, ReinitializeEnabled) { | 78 TEST_F(SpellCheckProfileTest, ReinitializeEnabled) { |
77 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); | 79 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); |
78 TestingSpellCheckProfile target; | 80 ScopedTempDir dir; |
| 81 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 82 TestingSpellCheckProfile target(dir.path()); |
79 target.SetHostToBeCreated(host.get()); | 83 target.SetHostToBeCreated(host.get()); |
80 | 84 |
81 // The first call should create host. | 85 // The first call should create host. |
82 ResultType result1 = target.ReinitializeHost(false, true, "", NULL); | 86 ResultType result1 = target.ReinitializeHost(false, true, "", NULL); |
83 EXPECT_EQ(target.create_host_calls_, 1U); | 87 EXPECT_EQ(target.create_host_calls_, 1U); |
84 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); | 88 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); |
85 | 89 |
86 // The second call should be ignored. | 90 // The second call should be ignored. |
87 ResultType result2 = target.ReinitializeHost(false, true, "", NULL); | 91 ResultType result2 = target.ReinitializeHost(false, true, "", NULL); |
88 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING); | 92 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING); |
89 EXPECT_EQ(target.create_host_calls_, 1U); | 93 EXPECT_EQ(target.create_host_calls_, 1U); |
90 | 94 |
91 // Host should become ready after the notification. | 95 // Host should become ready after the notification. |
92 EXPECT_FALSE(target.IsCreatedHostReady()); | 96 EXPECT_FALSE(target.IsCreatedHostReady()); |
93 target.SpellCheckHostInitialized(0); | 97 target.SpellCheckHostInitialized(0); |
94 EXPECT_TRUE(target.IsCreatedHostReady()); | 98 EXPECT_TRUE(target.IsCreatedHostReady()); |
95 } | 99 } |
96 | 100 |
97 TEST_F(SpellCheckProfileTest, ReinitializeDisabled) { | 101 TEST_F(SpellCheckProfileTest, ReinitializeDisabled) { |
98 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); | 102 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); |
99 TestingSpellCheckProfile target; | 103 ScopedTempDir dir; |
| 104 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 105 TestingSpellCheckProfile target(dir.path()); |
| 106 |
100 target.returning_from_create_ = host.get(); | 107 target.returning_from_create_ = host.get(); |
101 | 108 |
102 // If enabled is false, nothing should happen | 109 // If enabled is false, nothing should happen |
103 ResultType result1 = target.ReinitializeHost(false, false, "", NULL); | 110 ResultType result1 = target.ReinitializeHost(false, false, "", NULL); |
104 EXPECT_EQ(target.create_host_calls_, 0U); | 111 EXPECT_EQ(target.create_host_calls_, 0U); |
105 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_DID_NOTHING); | 112 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_DID_NOTHING); |
106 | 113 |
107 // Nothing should happen even if forced. | 114 // Nothing should happen even if forced. |
108 ResultType result2 = target.ReinitializeHost(true, false, "", NULL); | 115 ResultType result2 = target.ReinitializeHost(true, false, "", NULL); |
109 EXPECT_EQ(target.create_host_calls_, 0U); | 116 EXPECT_EQ(target.create_host_calls_, 0U); |
110 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING); | 117 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING); |
111 } | 118 } |
112 | 119 |
113 TEST_F(SpellCheckProfileTest, ReinitializeRemove) { | 120 TEST_F(SpellCheckProfileTest, ReinitializeRemove) { |
114 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); | 121 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); |
115 TestingSpellCheckProfile target; | 122 ScopedTempDir dir; |
| 123 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 124 TestingSpellCheckProfile target(dir.path()); |
| 125 |
116 target.SetHostToBeCreated(host.get()); | 126 target.SetHostToBeCreated(host.get()); |
117 | 127 |
118 | |
119 // At first, create the host. | 128 // At first, create the host. |
120 ResultType result1 = target.ReinitializeHost(false, true, "", NULL); | 129 ResultType result1 = target.ReinitializeHost(false, true, "", NULL); |
121 target.SpellCheckHostInitialized(0); | 130 target.SpellCheckHostInitialized(0); |
122 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); | 131 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); |
123 EXPECT_TRUE(target.IsCreatedHostReady()); | 132 EXPECT_TRUE(target.IsCreatedHostReady()); |
124 | 133 |
125 // Then the host should be deleted if it's forced to be disabled. | 134 // Then the host should be deleted if it's forced to be disabled. |
126 ResultType result2 = target.ReinitializeHost(true, false, "", NULL); | 135 ResultType result2 = target.ReinitializeHost(true, false, "", NULL); |
127 target.SpellCheckHostInitialized(0); | 136 target.SpellCheckHostInitialized(0); |
128 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_REMOVED_HOST); | 137 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_REMOVED_HOST); |
129 EXPECT_FALSE(target.IsCreatedHostReady()); | 138 EXPECT_FALSE(target.IsCreatedHostReady()); |
130 } | 139 } |
131 | 140 |
132 TEST_F(SpellCheckProfileTest, ReinitializeRecreate) { | 141 TEST_F(SpellCheckProfileTest, ReinitializeRecreate) { |
133 scoped_refptr<MockSpellCheckHost> host1(new MockSpellCheckHost()); | 142 scoped_refptr<MockSpellCheckHost> host1(new MockSpellCheckHost()); |
134 TestingSpellCheckProfile target; | 143 ScopedTempDir dir; |
| 144 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 145 TestingSpellCheckProfile target(dir.path()); |
| 146 |
135 target.SetHostToBeCreated(host1.get()); | 147 target.SetHostToBeCreated(host1.get()); |
136 | 148 |
137 // At first, create the host. | 149 // At first, create the host. |
138 ResultType result1 = target.ReinitializeHost(false, true, "", NULL); | 150 ResultType result1 = target.ReinitializeHost(false, true, "", NULL); |
139 target.SpellCheckHostInitialized(0); | 151 target.SpellCheckHostInitialized(0); |
140 EXPECT_EQ(target.create_host_calls_, 1U); | 152 EXPECT_EQ(target.create_host_calls_, 1U); |
141 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); | 153 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); |
142 EXPECT_TRUE(target.IsCreatedHostReady()); | 154 EXPECT_TRUE(target.IsCreatedHostReady()); |
143 | 155 |
144 // Then the host should be re-created if it's forced to recreate. | 156 // Then the host should be re-created if it's forced to recreate. |
145 scoped_refptr<MockSpellCheckHost> host2(new MockSpellCheckHost()); | 157 scoped_refptr<MockSpellCheckHost> host2(new MockSpellCheckHost()); |
146 target.SetHostToBeCreated(host2.get()); | 158 target.SetHostToBeCreated(host2.get()); |
147 | 159 |
148 ResultType result2 = target.ReinitializeHost(true, true, "", NULL); | 160 ResultType result2 = target.ReinitializeHost(true, true, "", NULL); |
149 target.SpellCheckHostInitialized(0); | 161 target.SpellCheckHostInitialized(0); |
150 EXPECT_EQ(target.create_host_calls_, 2U); | 162 EXPECT_EQ(target.create_host_calls_, 2U); |
151 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_CREATED_HOST); | 163 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_CREATED_HOST); |
152 EXPECT_TRUE(target.IsCreatedHostReady()); | 164 EXPECT_TRUE(target.IsCreatedHostReady()); |
153 } | 165 } |
154 | 166 |
155 TEST_F(SpellCheckProfileTest, SpellCheckHostInitializedWithCustomWords) { | 167 TEST_F(SpellCheckProfileTest, SpellCheckHostInitializedWithCustomWords) { |
156 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); | 168 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); |
157 TestingSpellCheckProfile target; | 169 ScopedTempDir dir; |
| 170 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 171 TestingSpellCheckProfile target(dir.path()); |
| 172 |
158 target.SetHostToBeCreated(host.get()); | 173 target.SetHostToBeCreated(host.get()); |
159 target.ReinitializeHost(false, true, "", NULL); | 174 target.ReinitializeHost(false, true, "", NULL); |
160 | 175 |
161 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words | 176 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words |
162 (new SpellCheckProfile::CustomWordList()); | 177 (new SpellCheckProfile::CustomWordList()); |
163 loaded_custom_words->push_back("foo"); | 178 loaded_custom_words->push_back("foo"); |
164 loaded_custom_words->push_back("bar"); | 179 loaded_custom_words->push_back("bar"); |
165 SpellCheckProfile::CustomWordList expected(*loaded_custom_words); | 180 SpellCheckProfile::CustomWordList expected(*loaded_custom_words); |
166 target.SpellCheckHostInitialized(loaded_custom_words.release()); | 181 target.SpellCheckHostInitialized(loaded_custom_words.release()); |
167 EXPECT_EQ(target.GetCustomWords(), expected); | 182 EXPECT_EQ(target.GetCustomWords(), expected); |
168 } | 183 } |
169 | 184 |
170 TEST_F(SpellCheckProfileTest, CustomWordAddedLocally) { | 185 TEST_F(SpellCheckProfileTest, CustomWordAddedLocally) { |
171 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); | 186 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); |
172 TestingSpellCheckProfile target; | 187 ScopedTempDir dir; |
| 188 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 189 TestingSpellCheckProfile target(dir.path()); |
| 190 |
173 target.SetHostToBeCreated(host.get()); | 191 target.SetHostToBeCreated(host.get()); |
174 target.ReinitializeHost(false, true, "", NULL); | 192 target.ReinitializeHost(false, true, "", NULL); |
175 | 193 |
176 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words | 194 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words |
177 (new SpellCheckProfile::CustomWordList()); | 195 (new SpellCheckProfile::CustomWordList()); |
178 target.SpellCheckHostInitialized(NULL); | 196 target.SpellCheckHostInitialized(NULL); |
179 SpellCheckProfile::CustomWordList expected; | 197 SpellCheckProfile::CustomWordList expected; |
180 EXPECT_EQ(target.GetCustomWords(), expected); | 198 EXPECT_EQ(target.GetCustomWords(), expected); |
181 target.CustomWordAddedLocally("foo"); | 199 target.CustomWordAddedLocally("foo"); |
182 expected.push_back("foo"); | 200 expected.push_back("foo"); |
183 EXPECT_EQ(target.GetCustomWords(), expected); | 201 EXPECT_EQ(target.GetCustomWords(), expected); |
184 target.CustomWordAddedLocally("bar"); | 202 target.CustomWordAddedLocally("bar"); |
185 expected.push_back("bar"); | 203 expected.push_back("bar"); |
186 EXPECT_EQ(target.GetCustomWords(), expected); | 204 EXPECT_EQ(target.GetCustomWords(), expected); |
187 } | 205 } |
| 206 |
| 207 TEST_F(SpellCheckProfileTest, SaveAndLoad) { |
| 208 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); |
| 209 ScopedTempDir dir; |
| 210 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 211 TestingSpellCheckProfile target(dir.path()); |
| 212 |
| 213 target.SetHostToBeCreated(host.get()); |
| 214 target.ReinitializeHost(false, true, "", NULL); |
| 215 |
| 216 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words( |
| 217 new SpellCheckProfile::CustomWordList()); |
| 218 target.LoadCustomDictionary(loaded_custom_words.get()); |
| 219 |
| 220 // The custom word list should be empty now. |
| 221 SpellCheckProfile::CustomWordList expected; |
| 222 EXPECT_EQ(*loaded_custom_words, expected); |
| 223 |
| 224 target.WriteWordToCustomDictionary("foo"); |
| 225 expected.push_back("foo"); |
| 226 |
| 227 target.WriteWordToCustomDictionary("bar"); |
| 228 expected.push_back("bar"); |
| 229 |
| 230 // The custom word list should include written words. |
| 231 target.LoadCustomDictionary(loaded_custom_words.get()); |
| 232 EXPECT_EQ(*loaded_custom_words, expected); |
| 233 |
| 234 // Load in another instance of SpellCheckProfile. |
| 235 // The result should be the same. |
| 236 scoped_refptr<MockSpellCheckHost> host2(new MockSpellCheckHost()); |
| 237 TestingSpellCheckProfile target2(dir.path()); |
| 238 target2.SetHostToBeCreated(host2.get()); |
| 239 target2.ReinitializeHost(false, true, "", NULL); |
| 240 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words2( |
| 241 new SpellCheckProfile::CustomWordList()); |
| 242 target2.LoadCustomDictionary(loaded_custom_words2.get()); |
| 243 EXPECT_EQ(*loaded_custom_words2, expected); |
| 244 } |
| 245 |
| 246 TEST_F(SpellCheckProfileTest, MultiProfile) { |
| 247 scoped_refptr<MockSpellCheckHost> host1(new MockSpellCheckHost()); |
| 248 scoped_refptr<MockSpellCheckHost> host2(new MockSpellCheckHost()); |
| 249 |
| 250 ScopedTempDir dir1; |
| 251 ScopedTempDir dir2; |
| 252 ASSERT_TRUE(dir1.CreateUniqueTempDir()); |
| 253 ASSERT_TRUE(dir2.CreateUniqueTempDir()); |
| 254 TestingSpellCheckProfile target1(dir1.path()); |
| 255 TestingSpellCheckProfile target2(dir2.path()); |
| 256 |
| 257 target1.SetHostToBeCreated(host1.get()); |
| 258 target1.ReinitializeHost(false, true, "", NULL); |
| 259 target2.SetHostToBeCreated(host2.get()); |
| 260 target2.ReinitializeHost(false, true, "", NULL); |
| 261 |
| 262 SpellCheckProfile::CustomWordList expected1; |
| 263 SpellCheckProfile::CustomWordList expected2; |
| 264 |
| 265 target1.WriteWordToCustomDictionary("foo"); |
| 266 target1.WriteWordToCustomDictionary("bar"); |
| 267 expected1.push_back("foo"); |
| 268 expected1.push_back("bar"); |
| 269 |
| 270 target2.WriteWordToCustomDictionary("hoge"); |
| 271 target2.WriteWordToCustomDictionary("fuga"); |
| 272 expected2.push_back("hoge"); |
| 273 expected2.push_back("fuga"); |
| 274 |
| 275 SpellCheckProfile::CustomWordList actual1; |
| 276 target1.LoadCustomDictionary(&actual1); |
| 277 EXPECT_EQ(actual1, expected1); |
| 278 |
| 279 SpellCheckProfile::CustomWordList actual2; |
| 280 target2.LoadCustomDictionary(&actual2); |
| 281 EXPECT_EQ(actual2, expected2); |
| 282 } |
OLD | NEW |