Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1003)

Side by Side Diff: chrome/browser/spellchecker/spellcheck_profile_unittest.cc

Issue 7919003: Remove refptr usages from SpellCheckHost (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Fix pointer returns. Update tests. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/spellchecker/spellcheck_host.h" 7 #include "chrome/browser/spellchecker/spellcheck_host.h"
8 #include "chrome/browser/spellchecker/spellcheck_profile.h" 8 #include "chrome/browser/spellchecker/spellcheck_profile.h"
9 #include "content/browser/browser_thread.h" 9 #include "content/browser/browser_thread.h"
10 #include "testing/gmock/include/gmock/gmock.h" 10 #include "testing/gmock/include/gmock/gmock.h"
(...skipping 21 matching lines...) Expand all
32 TestingSpellCheckProfile() 32 TestingSpellCheckProfile()
33 : create_host_calls_(0) { 33 : create_host_calls_(0) {
34 } 34 }
35 35
36 virtual SpellCheckHost* CreateHost( 36 virtual SpellCheckHost* CreateHost(
37 SpellCheckProfileProvider* profile, 37 SpellCheckProfileProvider* profile,
38 const std::string& language, 38 const std::string& language,
39 net::URLRequestContextGetter* request_context, 39 net::URLRequestContextGetter* request_context,
40 SpellCheckHostMetrics* metrics) { 40 SpellCheckHostMetrics* metrics) {
41 create_host_calls_++; 41 create_host_calls_++;
42 returning_from_create_.reset(
43 SpellCheckHost::Create(profile, language, request_context, metrics));
42 return returning_from_create_.get(); 44 return returning_from_create_.get();
43 } 45 }
44 46
45 virtual bool IsTesting() const { 47 virtual bool IsTesting() const {
46 return true; 48 return true;
47 } 49 }
48 50
49 bool IsCreatedHostReady() { 51 bool IsCreatedHostReady() {
50 return GetHost() == returning_from_create_.get(); 52 return GetHost() == returning_from_create_.get();
51 } 53 }
52 54
53 void SetHostToBeCreated(MockSpellCheckHost* host) { 55 void SetHostToBeCreated(MockSpellCheckHost* host) {
54 EXPECT_CALL(*host, UnsetProfile()).Times(1); 56 EXPECT_CALL(*host, UnsetProfile()).Times(1);
55 EXPECT_CALL(*host, IsReady()).WillRepeatedly(testing::Return(true)); 57 EXPECT_CALL(*host, IsReady()).WillRepeatedly(testing::Return(true));
56 returning_from_create_ = host; 58 returning_from_create_.reset(host);
57 } 59 }
58 60
59 size_t create_host_calls_; 61 size_t create_host_calls_;
60 scoped_refptr<SpellCheckHost> returning_from_create_; 62 scoped_ptr<SpellCheckHost> returning_from_create_;
61 }; 63 };
62 64
63 typedef SpellCheckProfile::ReinitializeResult ResultType; 65 typedef SpellCheckProfile::ReinitializeResult ResultType;
64 } // namespace 66 } // namespace
65 67
66 class SpellCheckProfileTest : public testing::Test { 68 class SpellCheckProfileTest : public testing::Test {
67 protected: 69 protected:
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_ptr<MockSpellCheckHost> host(new MockSpellCheckHost());
78 TestingSpellCheckProfile target; 80 TestingSpellCheckProfile target;
79 target.SetHostToBeCreated(host.get()); 81 target.SetHostToBeCreated(host.get());
80 82
81 // The first call should create host. 83 // The first call should create host.
82 ResultType result1 = target.ReinitializeHost(false, true, "", NULL); 84 ResultType result1 = target.ReinitializeHost(false, true, "", NULL);
83 EXPECT_EQ(target.create_host_calls_, 1U); 85 EXPECT_EQ(target.create_host_calls_, 1U);
84 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); 86 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST);
85 87
86 // The second call should be ignored. 88 // The second call should be ignored.
87 ResultType result2 = target.ReinitializeHost(false, true, "", NULL); 89 ResultType result2 = target.ReinitializeHost(false, true, "", NULL);
88 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING); 90 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING);
89 EXPECT_EQ(target.create_host_calls_, 1U); 91 EXPECT_EQ(target.create_host_calls_, 1U);
90 92
91 // Host should become ready after the notification. 93 // Host should become ready after the notification.
92 EXPECT_FALSE(target.IsCreatedHostReady()); 94 EXPECT_FALSE(target.IsCreatedHostReady());
93 target.SpellCheckHostInitialized(0); 95 target.SpellCheckHostInitialized(0);
94 EXPECT_TRUE(target.IsCreatedHostReady()); 96 EXPECT_TRUE(target.IsCreatedHostReady());
95 } 97 }
96 98
97 TEST_F(SpellCheckProfileTest, ReinitializeDisabled) { 99 TEST_F(SpellCheckProfileTest, ReinitializeDisabled) {
98 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); 100 scoped_ptr<MockSpellCheckHost> host(new MockSpellCheckHost());
99 TestingSpellCheckProfile target; 101 TestingSpellCheckProfile target;
100 target.returning_from_create_ = host.get(); 102 target.returning_from_create_.reset(host.get());
101 103
102 // If enabled is false, nothing should happen 104 // If enabled is false, nothing should happen
103 ResultType result1 = target.ReinitializeHost(false, false, "", NULL); 105 ResultType result1 = target.ReinitializeHost(false, false, "", NULL);
104 EXPECT_EQ(target.create_host_calls_, 0U); 106 EXPECT_EQ(target.create_host_calls_, 0U);
105 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_DID_NOTHING); 107 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_DID_NOTHING);
106 108
107 // Nothing should happen even if forced. 109 // Nothing should happen even if forced.
108 ResultType result2 = target.ReinitializeHost(true, false, "", NULL); 110 ResultType result2 = target.ReinitializeHost(true, false, "", NULL);
109 EXPECT_EQ(target.create_host_calls_, 0U); 111 EXPECT_EQ(target.create_host_calls_, 0U);
110 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING); 112 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_DID_NOTHING);
111 } 113 }
112 114
113 TEST_F(SpellCheckProfileTest, ReinitializeRemove) { 115 TEST_F(SpellCheckProfileTest, ReinitializeRemove) {
114 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); 116 scoped_ptr<MockSpellCheckHost> host(new MockSpellCheckHost());
115 TestingSpellCheckProfile target; 117 TestingSpellCheckProfile target;
116 target.SetHostToBeCreated(host.get()); 118 target.SetHostToBeCreated(host.get());
117 119
118 120
119 // At first, create the host. 121 // At first, create the host.
120 ResultType result1 = target.ReinitializeHost(false, true, "", NULL); 122 ResultType result1 = target.ReinitializeHost(false, true, "", NULL);
121 target.SpellCheckHostInitialized(0); 123 target.SpellCheckHostInitialized(0);
122 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); 124 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST);
123 EXPECT_TRUE(target.IsCreatedHostReady()); 125 EXPECT_TRUE(target.IsCreatedHostReady());
124 126
125 // Then the host should be deleted if it's forced to be disabled. 127 // Then the host should be deleted if it's forced to be disabled.
126 ResultType result2 = target.ReinitializeHost(true, false, "", NULL); 128 ResultType result2 = target.ReinitializeHost(true, false, "", NULL);
127 target.SpellCheckHostInitialized(0); 129 target.SpellCheckHostInitialized(0);
128 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_REMOVED_HOST); 130 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_REMOVED_HOST);
129 EXPECT_FALSE(target.IsCreatedHostReady()); 131 EXPECT_FALSE(target.IsCreatedHostReady());
130 } 132 }
131 133
132 TEST_F(SpellCheckProfileTest, ReinitializeRecreate) { 134 TEST_F(SpellCheckProfileTest, ReinitializeRecreate) {
133 scoped_refptr<MockSpellCheckHost> host1(new MockSpellCheckHost()); 135 scoped_ptr<MockSpellCheckHost> host1(new MockSpellCheckHost());
134 TestingSpellCheckProfile target; 136 TestingSpellCheckProfile target;
135 target.SetHostToBeCreated(host1.get()); 137 target.SetHostToBeCreated(host1.get());
136 138
137 // At first, create the host. 139 // At first, create the host.
138 ResultType result1 = target.ReinitializeHost(false, true, "", NULL); 140 ResultType result1 = target.ReinitializeHost(false, true, "", NULL);
139 target.SpellCheckHostInitialized(0); 141 target.SpellCheckHostInitialized(0);
140 EXPECT_EQ(target.create_host_calls_, 1U); 142 EXPECT_EQ(target.create_host_calls_, 1U);
141 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST); 143 EXPECT_EQ(result1, SpellCheckProfile::REINITIALIZE_CREATED_HOST);
142 EXPECT_TRUE(target.IsCreatedHostReady()); 144 EXPECT_TRUE(target.IsCreatedHostReady());
143 145
144 // Then the host should be re-created if it's forced to recreate. 146 // Then the host should be re-created if it's forced to recreate.
145 scoped_refptr<MockSpellCheckHost> host2(new MockSpellCheckHost()); 147 scoped_ptr<MockSpellCheckHost> host2(new MockSpellCheckHost());
146 target.SetHostToBeCreated(host2.get()); 148 target.SetHostToBeCreated(host2.get());
147 149
148 ResultType result2 = target.ReinitializeHost(true, true, "", NULL); 150 ResultType result2 = target.ReinitializeHost(true, true, "", NULL);
149 target.SpellCheckHostInitialized(0); 151 target.SpellCheckHostInitialized(0);
150 EXPECT_EQ(target.create_host_calls_, 2U); 152 EXPECT_EQ(target.create_host_calls_, 2U);
151 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_CREATED_HOST); 153 EXPECT_EQ(result2, SpellCheckProfile::REINITIALIZE_CREATED_HOST);
152 EXPECT_TRUE(target.IsCreatedHostReady()); 154 EXPECT_TRUE(target.IsCreatedHostReady());
153 } 155 }
154 156
155 TEST_F(SpellCheckProfileTest, SpellCheckHostInitializedWithCustomWords) { 157 TEST_F(SpellCheckProfileTest, SpellCheckHostInitializedWithCustomWords) {
156 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); 158 scoped_ptr<MockSpellCheckHost> host(new MockSpellCheckHost());
157 TestingSpellCheckProfile target; 159 TestingSpellCheckProfile target;
158 target.SetHostToBeCreated(host.get()); 160 target.SetHostToBeCreated(host.get());
159 target.ReinitializeHost(false, true, "", NULL); 161 target.ReinitializeHost(false, true, "", NULL);
160 162
161 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words 163 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words
162 (new SpellCheckProfile::CustomWordList()); 164 (new SpellCheckProfile::CustomWordList());
163 loaded_custom_words->push_back("foo"); 165 loaded_custom_words->push_back("foo");
164 loaded_custom_words->push_back("bar"); 166 loaded_custom_words->push_back("bar");
165 SpellCheckProfile::CustomWordList expected(*loaded_custom_words); 167 SpellCheckProfile::CustomWordList expected(*loaded_custom_words);
166 target.SpellCheckHostInitialized(loaded_custom_words.release()); 168 target.SpellCheckHostInitialized(loaded_custom_words.release());
167 EXPECT_EQ(target.GetCustomWords(), expected); 169 EXPECT_EQ(target.GetCustomWords(), expected);
168 } 170 }
169 171
170 TEST_F(SpellCheckProfileTest, CustomWordAddedLocally) { 172 TEST_F(SpellCheckProfileTest, CustomWordAddedLocally) {
171 scoped_refptr<MockSpellCheckHost> host(new MockSpellCheckHost()); 173 scoped_ptr<MockSpellCheckHost> host(new MockSpellCheckHost());
172 TestingSpellCheckProfile target; 174 TestingSpellCheckProfile target;
173 target.SetHostToBeCreated(host.get()); 175 target.SetHostToBeCreated(host.get());
174 target.ReinitializeHost(false, true, "", NULL); 176 target.ReinitializeHost(false, true, "", NULL);
175 177
176 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words 178 scoped_ptr<SpellCheckProfile::CustomWordList> loaded_custom_words
177 (new SpellCheckProfile::CustomWordList()); 179 (new SpellCheckProfile::CustomWordList());
178 target.SpellCheckHostInitialized(NULL); 180 target.SpellCheckHostInitialized(NULL);
179 SpellCheckProfile::CustomWordList expected; 181 SpellCheckProfile::CustomWordList expected;
180 EXPECT_EQ(target.GetCustomWords(), expected); 182 EXPECT_EQ(target.GetCustomWords(), expected);
181 target.CustomWordAddedLocally("foo"); 183 target.CustomWordAddedLocally("foo");
182 expected.push_back("foo"); 184 expected.push_back("foo");
183 EXPECT_EQ(target.GetCustomWords(), expected); 185 EXPECT_EQ(target.GetCustomWords(), expected);
184 target.CustomWordAddedLocally("bar"); 186 target.CustomWordAddedLocally("bar");
185 expected.push_back("bar"); 187 expected.push_back("bar");
186 EXPECT_EQ(target.GetCustomWords(), expected); 188 EXPECT_EQ(target.GetCustomWords(), expected);
187 } 189 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698