Return backed up TemplateURL on default search change
[chromium-blink-merge.git] / chrome / browser / spellchecker / spellcheck_profile_unittest.cc
blobfc4b53d388fbf756d76329d5e4f83f303de6b79b
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.
5 #include <vector>
7 #include "base/scoped_temp_dir.h"
8 #include "chrome/browser/spellchecker/spellcheck_host.h"
9 #include "chrome/browser/spellchecker/spellcheck_profile.h"
10 #include "content/test/test_browser_thread.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using content::BrowserThread;
16 namespace {
18 class MockSpellCheckHost : public SpellCheckHost {
19 public:
20 MOCK_METHOD0(UnsetProfile, void());
21 MOCK_METHOD1(InitForRenderer, void(content::RenderProcessHost* process));
22 MOCK_METHOD1(AddWord, void(const std::string& word));
23 MOCK_CONST_METHOD0(GetDictionaryFile, const base::PlatformFile&());
24 MOCK_CONST_METHOD0(GetCustomWords,
25 const SpellCheckProfile::CustomWordList&());
26 MOCK_CONST_METHOD0(GetLastAddedFile, const std::string&());
27 MOCK_CONST_METHOD0(GetLanguage, const std::string&());
28 MOCK_CONST_METHOD0(IsUsingPlatformChecker, bool());
29 MOCK_CONST_METHOD0(GetMetrics, SpellCheckHostMetrics*());
30 MOCK_CONST_METHOD0(IsReady, bool());
33 class TestingSpellCheckProfile : public SpellCheckProfile {
34 public:
35 explicit TestingSpellCheckProfile(const FilePath& profile_dir)
36 : SpellCheckProfile(profile_dir),
37 create_host_calls_(0) {
39 virtual SpellCheckHost* CreateHost(
40 SpellCheckProfileProvider* profile,
41 const std::string& language,
42 net::URLRequestContextGetter* request_context,
43 SpellCheckHostMetrics* metrics) {
44 create_host_calls_++;
45 return returning_from_create_.release();
48 virtual bool IsTesting() const {
49 return true;
52 bool IsCreatedHostReady() {
53 return !!GetHost();
56 void SetHostToBeCreated(MockSpellCheckHost* host) {
57 EXPECT_CALL(*host, UnsetProfile()).Times(1);
58 EXPECT_CALL(*host, IsReady()).WillRepeatedly(testing::Return(true));
59 returning_from_create_.reset(host);
62 size_t create_host_calls_;
63 scoped_ptr<SpellCheckHost> returning_from_create_;
66 typedef SpellCheckProfile::ReinitializeResult ResultType;
67 } // namespace
69 class SpellCheckProfileTest : public testing::Test {
70 protected:
71 SpellCheckProfileTest()
72 : file_thread_(BrowserThread::FILE) {
75 // SpellCheckHost will be deleted on FILE thread.
76 content::TestBrowserThread file_thread_;
79 TEST_F(SpellCheckProfileTest, ReinitializeEnabled) {
80 scoped_ptr<MockSpellCheckHost> host(new MockSpellCheckHost());
81 ScopedTempDir dir;
82 ASSERT_TRUE(dir.CreateUniqueTempDir());
83 TestingSpellCheckProfile target(dir.path());
84 target.SetHostToBeCreated(host.release());
86 // The first call should create host.
87 ResultType result1 = target.ReinitializeHost(false, true, "", NULL);
88 EXPECT_EQ(target.create_host_calls_, 1U);
89 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST);
91 // The second call should be ignored.
92 ResultType result2 = target.ReinitializeHost(false, true, "", NULL);
93 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING);
94 EXPECT_EQ(target.create_host_calls_, 1U);
96 // Host should become ready after the notification.
97 EXPECT_FALSE(target.IsCreatedHostReady());
98 target.SpellCheckHostInitialized(0);
99 EXPECT_TRUE(target.IsCreatedHostReady());
102 TEST_F(SpellCheckProfileTest, ReinitializeDisabled) {
103 scoped_ptr<MockSpellCheckHost> host(new MockSpellCheckHost());
104 ScopedTempDir dir;
105 ASSERT_TRUE(dir.CreateUniqueTempDir());
106 TestingSpellCheckProfile target(dir.path());
108 target.returning_from_create_.reset(host.release());
110 // If enabled is false, nothing should happen
111 ResultType result1 = target.ReinitializeHost(false, false, "", NULL);
112 EXPECT_EQ(target.create_host_calls_, 0U);
113 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_DID_NOTHING);
115 // Nothing should happen even if forced.
116 ResultType result2 = target.ReinitializeHost(true, false, "", NULL);
117 EXPECT_EQ(target.create_host_calls_, 0U);
118 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING);
121 TEST_F(SpellCheckProfileTest, ReinitializeRemove) {
122 scoped_ptr<MockSpellCheckHost> host(new MockSpellCheckHost());
123 ScopedTempDir dir;
124 ASSERT_TRUE(dir.CreateUniqueTempDir());
125 TestingSpellCheckProfile target(dir.path());
127 target.SetHostToBeCreated(host.release());
129 // At first, create the host.
130 ResultType result1 = target.ReinitializeHost(false, true, "", NULL);
131 target.SpellCheckHostInitialized(0);
132 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST);
133 EXPECT_TRUE(target.IsCreatedHostReady());
135 // Then the host should be deleted if it's forced to be disabled.
136 ResultType result2 = target.ReinitializeHost(true, false, "", NULL);
137 target.SpellCheckHostInitialized(0);
138 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_REMOVED_HOST);
139 EXPECT_FALSE(target.IsCreatedHostReady());
142 TEST_F(SpellCheckProfileTest, ReinitializeRecreate) {
143 scoped_ptr<MockSpellCheckHost> host1(new MockSpellCheckHost());
144 ScopedTempDir dir;
145 ASSERT_TRUE(dir.CreateUniqueTempDir());
146 TestingSpellCheckProfile target(dir.path());
148 target.SetHostToBeCreated(host1.release());
150 // At first, create the host.
151 ResultType result1 = target.ReinitializeHost(false, true, "", NULL);
152 target.SpellCheckHostInitialized(0);
153 EXPECT_EQ(target.create_host_calls_, 1U);
154 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST);
155 EXPECT_TRUE(target.IsCreatedHostReady());
157 // Then the host should be re-created if it's forced to recreate.
158 scoped_ptr<MockSpellCheckHost> host2(new MockSpellCheckHost());
159 target.SetHostToBeCreated(host2.release());
161 ResultType result2 = target.ReinitializeHost(true, true, "", NULL);
162 target.SpellCheckHostInitialized(0);
163 EXPECT_EQ(target.create_host_calls_, 2U);
164 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_CREATED_HOST);
165 EXPECT_TRUE(target.IsCreatedHostReady());
168 TEST_F(SpellCheckProfileTest, SpellCheckHostInitializedWithCustomWords) {
169 scoped_ptr<MockSpellCheckHost> host(new MockSpellCheckHost());
170 ScopedTempDir dir;
171 ASSERT_TRUE(dir.CreateUniqueTempDir());
172 TestingSpellCheckProfile target(dir.path());
174 target.SetHostToBeCreated(host.release());
175 target.ReinitializeHost(false, true, "", NULL);
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 SpellCheckProfile::CustomWordList expected(*loaded_custom_words);
182 target.SpellCheckHostInitialized(loaded_custom_words.release());
183 EXPECT_EQ(target.GetCustomWords(), expected);
186 TEST_F(SpellCheckProfileTest, CustomWordAddedLocally) {
187 scoped_ptr<MockSpellCheckHost> host(new MockSpellCheckHost());
188 ScopedTempDir dir;
189 ASSERT_TRUE(dir.CreateUniqueTempDir());
190 TestingSpellCheckProfile target(dir.path());
192 target.SetHostToBeCreated(host.release());
193 target.ReinitializeHost(false, true, "", NULL);
195 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words
196 (new SpellCheckProfile::CustomWordList());
197 target.SpellCheckHostInitialized(NULL);
198 SpellCheckProfile::CustomWordList expected;
199 EXPECT_EQ(target.GetCustomWords(), expected);
200 target.CustomWordAddedLocally("foo");
201 expected.push_back("foo");
202 EXPECT_EQ(target.GetCustomWords(), expected);
203 target.CustomWordAddedLocally("bar");
204 expected.push_back("bar");
205 EXPECT_EQ(target.GetCustomWords(), expected);
208 TEST_F(SpellCheckProfileTest, SaveAndLoad) {
209 scoped_ptr<MockSpellCheckHost> host(new MockSpellCheckHost());
210 ScopedTempDir dir;
211 ASSERT_TRUE(dir.CreateUniqueTempDir());
212 TestingSpellCheckProfile target(dir.path());
214 target.SetHostToBeCreated(host.release());
215 target.ReinitializeHost(false, true, "", NULL);
217 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words(
218 new SpellCheckProfile::CustomWordList());
219 target.LoadCustomDictionary(loaded_custom_words.get());
221 // The custom word list should be empty now.
222 SpellCheckProfile::CustomWordList expected;
223 EXPECT_EQ(*loaded_custom_words, expected);
225 target.WriteWordToCustomDictionary("foo");
226 expected.push_back("foo");
228 target.WriteWordToCustomDictionary("bar");
229 expected.push_back("bar");
231 // The custom word list should include written words.
232 target.LoadCustomDictionary(loaded_custom_words.get());
233 EXPECT_EQ(*loaded_custom_words, expected);
235 // Load in another instance of SpellCheckProfile.
236 // The result should be the same.
237 scoped_ptr<MockSpellCheckHost> host2(new MockSpellCheckHost());
238 TestingSpellCheckProfile target2(dir.path());
239 target2.SetHostToBeCreated(host2.release());
240 target2.ReinitializeHost(false, true, "", NULL);
241 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words2(
242 new SpellCheckProfile::CustomWordList());
243 target2.LoadCustomDictionary(loaded_custom_words2.get());
244 EXPECT_EQ(*loaded_custom_words2, expected);
247 TEST_F(SpellCheckProfileTest, MultiProfile) {
248 scoped_ptr<MockSpellCheckHost> host1(new MockSpellCheckHost());
249 scoped_ptr<MockSpellCheckHost> host2(new MockSpellCheckHost());
251 ScopedTempDir dir1;
252 ScopedTempDir dir2;
253 ASSERT_TRUE(dir1.CreateUniqueTempDir());
254 ASSERT_TRUE(dir2.CreateUniqueTempDir());
255 TestingSpellCheckProfile target1(dir1.path());
256 TestingSpellCheckProfile target2(dir2.path());
258 target1.SetHostToBeCreated(host1.release());
259 target1.ReinitializeHost(false, true, "", NULL);
260 target2.SetHostToBeCreated(host2.release());
261 target2.ReinitializeHost(false, true, "", NULL);
263 SpellCheckProfile::CustomWordList expected1;
264 SpellCheckProfile::CustomWordList expected2;
266 target1.WriteWordToCustomDictionary("foo");
267 target1.WriteWordToCustomDictionary("bar");
268 expected1.push_back("foo");
269 expected1.push_back("bar");
271 target2.WriteWordToCustomDictionary("hoge");
272 target2.WriteWordToCustomDictionary("fuga");
273 expected2.push_back("hoge");
274 expected2.push_back("fuga");
276 SpellCheckProfile::CustomWordList actual1;
277 target1.LoadCustomDictionary(&actual1);
278 EXPECT_EQ(actual1, expected1);
280 SpellCheckProfile::CustomWordList actual2;
281 target2.LoadCustomDictionary(&actual2);
282 EXPECT_EQ(actual2, expected2);