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

Side by Side Diff: chrome/browser/chromeos/input_method/browser_state_monitor_unittest.cc

Issue 11466010: Decompose BrowserStateMonitor into two parts, simplifying unit tests and APIs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix gypi ordering. Created 8 years 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/input_method/browser_state_monitor.h" 5 #include "chrome/browser/chromeos/input_method/browser_state_monitor.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/prefs/public/pref_member.h" 10 #include "base/bind.h"
11 #include "chrome/browser/chromeos/input_method/mock_input_method_delegate.h"
12 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h"
13 #include "chrome/common/chrome_notification_types.h" 11 #include "chrome/common/chrome_notification_types.h"
14 #include "content/public/browser/notification_details.h" 12 #include "content/public/browser/notification_details.h"
15 #include "content/public/browser/notification_service.h" 13 #include "content/public/browser/notification_service.h"
16 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
17 15
18 namespace chromeos { 16 namespace chromeos {
19 namespace input_method { 17 namespace input_method {
20 namespace { 18 namespace {
21 19
22 class TestableBrowserStateMonitor : public BrowserStateMonitor { 20 class MockObserver {
23 public: 21 public:
24 using BrowserStateMonitor::InputMethodChanged; 22 MockObserver()
25 using BrowserStateMonitor::Observe; 23 : state_(InputMethodManager::STATE_TERMINATING),
24 update_state_count_(0) { }
26 25
27 TestableBrowserStateMonitor(InputMethodManager* manager, 26 void SetState(InputMethodManager::State new_state) {
28 InputMethodDelegate* delegate) 27 ++update_state_count_;
29 : BrowserStateMonitor(manager, delegate) { 28 state_ = new_state;
30 } 29 }
30
31 base::Callback<void(InputMethodManager::State new_state)> AsCallback() {
32 return base::Bind(&MockObserver::SetState, base::Unretained(this));
33 }
34
35 int update_state_count() const {
36 return update_state_count_;
37 }
38
39 InputMethodManager::State state() const {
40 return state_;
41 }
42
43 private:
44 InputMethodManager::State state_;
45 int update_state_count_;
46
47 DISALLOW_COPY_AND_ASSIGN(MockObserver);
31 }; 48 };
32 49
33 } // anonymous namespace 50 } // anonymous namespace
34 51
35 TEST(BrowserStateMonitorLifetimeTest, TestConstruction) { 52 TEST(BrowserStateMonitorLifetimeTest, TestConstruction) {
36 MockInputMethodManager mock_manager; 53 MockObserver mock_observer;
37 MockInputMethodDelegate mock_delegate; 54 BrowserStateMonitor monitor(mock_observer.AsCallback());
38 TestableBrowserStateMonitor monitor(&mock_manager, &mock_delegate);
39 55
40 // Check the initial state of the |mock_manager| and |monitor| objects. 56 // Check the initial state of the |mock_observer| and |monitor| objects.
41 EXPECT_EQ(1, mock_manager.add_observer_count_); 57 EXPECT_EQ(1, mock_observer.update_state_count());
42 EXPECT_EQ(1, mock_manager.set_state_count_); 58 EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, mock_observer.state());
43 EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, mock_manager.last_state_);
44 EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, monitor.state()); 59 EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, monitor.state());
45 } 60 }
46 61
47 TEST(BrowserStateMonitorLifetimeTest, TestDestruction) {
48 MockInputMethodManager mock_manager;
49 MockInputMethodDelegate mock_delegate;
50 {
51 TestableBrowserStateMonitor monitor(&mock_manager, &mock_delegate);
52 }
53 EXPECT_EQ(1, mock_manager.remove_observer_count_);
54 }
55
56 namespace { 62 namespace {
57 63
58 class BrowserStateMonitorTest : public testing::Test { 64 class BrowserStateMonitorTest : public testing::Test {
59 public: 65 public:
60 BrowserStateMonitorTest() 66 BrowserStateMonitorTest()
61 : monitor_(&mock_manager_, &mock_delegate_) { 67 : monitor_(mock_observer_.AsCallback()) {
62 } 68 }
63 69
64 protected: 70 protected:
65 MockInputMethodManager mock_manager_; 71 MockObserver mock_observer_;
66 MockInputMethodDelegate mock_delegate_; 72 BrowserStateMonitor monitor_;
67 TestableBrowserStateMonitor monitor_;
68 73
69 private: 74 private:
70 DISALLOW_COPY_AND_ASSIGN(BrowserStateMonitorTest); 75 DISALLOW_COPY_AND_ASSIGN(BrowserStateMonitorTest);
71 }; 76 };
72 77
73 } // anonymous namespace 78 } // anonymous namespace
74 79
75 TEST_F(BrowserStateMonitorTest, TestObserveLoginUserChanged) { 80 TEST_F(BrowserStateMonitorTest, TestObserveLoginUserChanged) {
76 EXPECT_EQ(1, mock_manager_.set_state_count_); 81 EXPECT_EQ(1, mock_observer_.update_state_count());
77 monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, 82 monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
78 content::NotificationService::AllSources(), 83 content::NotificationService::AllSources(),
79 content::NotificationService::NoDetails()); 84 content::NotificationService::NoDetails());
80 85
81 // Check if the state of the |mock_manager_| as well as the |monitor| are both 86 // Check if the state of the |mock_observer_| as well as the |monitor| are
82 // changed. 87 // both changed.
83 EXPECT_EQ(2, mock_manager_.set_state_count_); 88 EXPECT_EQ(2, mock_observer_.update_state_count());
84 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, 89 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
85 mock_manager_.last_state_); 90 mock_observer_.state());
86 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state()); 91 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
87 } 92 }
88 93
89 TEST_F(BrowserStateMonitorTest, TestObserveSessionStarted) { 94 TEST_F(BrowserStateMonitorTest, TestObserveSessionStarted) {
90 EXPECT_EQ(1, mock_manager_.set_state_count_); 95 EXPECT_EQ(1, mock_observer_.update_state_count());
91 monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED, 96 monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED,
92 content::NotificationService::AllSources(), 97 content::NotificationService::AllSources(),
93 content::NotificationService::NoDetails()); 98 content::NotificationService::NoDetails());
94 99
95 // Check if the state of the |mock_manager_| as well as the |monitor| are both 100 // Check if the state of the |mock_observer_| as well as the |monitor| are
96 // changed. 101 // both changed.
97 EXPECT_EQ(2, mock_manager_.set_state_count_); 102 EXPECT_EQ(2, mock_observer_.update_state_count());
98 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, 103 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
99 mock_manager_.last_state_); 104 mock_observer_.state());
100 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state()); 105 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
101 } 106 }
102 107
103 TEST_F(BrowserStateMonitorTest, TestObserveLoginUserChangedThenSessionStarted) { 108 TEST_F(BrowserStateMonitorTest, TestObserveLoginUserChangedThenSessionStarted) {
104 EXPECT_EQ(1, mock_manager_.set_state_count_); 109 EXPECT_EQ(1, mock_observer_.update_state_count());
105 monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, 110 monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
106 content::NotificationService::AllSources(), 111 content::NotificationService::AllSources(),
107 content::NotificationService::NoDetails()); 112 content::NotificationService::NoDetails());
108 113
109 // Check if the state of the |mock_manager_| as well as the |monitor| are both 114 // Check if the state of the |mock_observer_| as well as the |monitor| are
110 // changed. 115 // both changed.
111 EXPECT_EQ(2, mock_manager_.set_state_count_); 116 EXPECT_EQ(2, mock_observer_.update_state_count());
112 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, 117 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
113 mock_manager_.last_state_); 118 mock_observer_.state());
114 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state()); 119 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
115 120
116 monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED, 121 monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED,
117 content::NotificationService::AllSources(), 122 content::NotificationService::AllSources(),
118 content::NotificationService::NoDetails()); 123 content::NotificationService::NoDetails());
119 124
120 // The second notification should be nop. 125 // The second notification should be nop.
121 EXPECT_EQ(2, mock_manager_.set_state_count_); 126 EXPECT_EQ(2, mock_observer_.update_state_count());
122 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, 127 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
123 mock_manager_.last_state_); 128 mock_observer_.state());
124 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state()); 129 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
125 } 130 }
126 131
127 TEST_F(BrowserStateMonitorTest, TestObserveScreenLockUnlock) { 132 TEST_F(BrowserStateMonitorTest, TestObserveScreenLockUnlock) {
128 EXPECT_EQ(1, mock_manager_.set_state_count_); 133 EXPECT_EQ(1, mock_observer_.update_state_count());
129 monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, 134 monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
130 content::NotificationService::AllSources(), 135 content::NotificationService::AllSources(),
131 content::NotificationService::NoDetails()); 136 content::NotificationService::NoDetails());
132 EXPECT_EQ(2, mock_manager_.set_state_count_); 137 EXPECT_EQ(2, mock_observer_.update_state_count());
133 monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED, 138 monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED,
134 content::NotificationService::AllSources(), 139 content::NotificationService::AllSources(),
135 content::NotificationService::NoDetails()); 140 content::NotificationService::NoDetails());
136 EXPECT_EQ(2, mock_manager_.set_state_count_); 141 EXPECT_EQ(2, mock_observer_.update_state_count());
137 bool locked = true; 142 bool locked = true;
138 monitor_.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, 143 monitor_.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
139 content::NotificationService::AllSources(), 144 content::NotificationService::AllSources(),
140 content::Details<bool>(&locked)); 145 content::Details<bool>(&locked));
141 EXPECT_EQ(3, mock_manager_.set_state_count_); 146 EXPECT_EQ(3, mock_observer_.update_state_count());
142 EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN, 147 EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN,
143 mock_manager_.last_state_); 148 mock_observer_.state());
144 EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN, monitor_.state()); 149 EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN, monitor_.state());
145 150
146 // When the screen is locked, the monitor should ignore input method changes.
147 monitor_.InputMethodChanged(&mock_manager_, false);
148 EXPECT_EQ(0, mock_delegate_.update_system_input_method_count());
149 EXPECT_EQ(0, mock_delegate_.update_user_input_method_count());
150
151 locked = false; 151 locked = false;
152 monitor_.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, 152 monitor_.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
153 content::NotificationService::AllSources(), 153 content::NotificationService::AllSources(),
154 content::Details<bool>(&locked)); 154 content::Details<bool>(&locked));
155 EXPECT_EQ(4, mock_manager_.set_state_count_); 155 EXPECT_EQ(4, mock_observer_.update_state_count());
156 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, 156 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
157 mock_manager_.last_state_); 157 mock_observer_.state());
158 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state()); 158 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
159
160 monitor_.InputMethodChanged(&mock_manager_, false);
161 EXPECT_EQ(0, mock_delegate_.update_system_input_method_count());
162 EXPECT_EQ(1, mock_delegate_.update_user_input_method_count());
163 } 159 }
164 160
165 TEST_F(BrowserStateMonitorTest, TestObserveAppTerminating) { 161 TEST_F(BrowserStateMonitorTest, TestObserveAppTerminating) {
166 EXPECT_EQ(1, mock_manager_.set_state_count_); 162 EXPECT_EQ(1, mock_observer_.update_state_count());
167 monitor_.Observe(chrome::NOTIFICATION_APP_TERMINATING, 163 monitor_.Observe(chrome::NOTIFICATION_APP_TERMINATING,
168 content::NotificationService::AllSources(), 164 content::NotificationService::AllSources(),
169 content::NotificationService::NoDetails()); 165 content::NotificationService::NoDetails());
170 166
171 // Check if the state of the |mock_manager_| as well as the |monitor| are both 167 // Check if the state of the |mock_observer_| as well as the |monitor| are
172 // changed. 168 // both changed.
173 EXPECT_EQ(2, mock_manager_.set_state_count_); 169 EXPECT_EQ(2, mock_observer_.update_state_count());
174 EXPECT_EQ(InputMethodManager::STATE_TERMINATING, 170 EXPECT_EQ(InputMethodManager::STATE_TERMINATING,
175 mock_manager_.last_state_); 171 mock_observer_.state());
176 EXPECT_EQ(InputMethodManager::STATE_TERMINATING, monitor_.state()); 172 EXPECT_EQ(InputMethodManager::STATE_TERMINATING, monitor_.state());
177
178 // In the terminating state, the monitor should ignore input method changes.
179 monitor_.InputMethodChanged(&mock_manager_, false);
180 EXPECT_EQ(0, mock_delegate_.update_system_input_method_count());
181 EXPECT_EQ(0, mock_delegate_.update_user_input_method_count());
182 }
183
184 TEST_F(BrowserStateMonitorTest, TestUpdatePrefOnLoginScreen) {
185 EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, monitor_.state());
186 monitor_.InputMethodChanged(&mock_manager_, false);
187 EXPECT_EQ(1, mock_delegate_.update_system_input_method_count());
188 EXPECT_EQ(0, mock_delegate_.update_user_input_method_count());
189 monitor_.InputMethodChanged(&mock_manager_, false);
190 EXPECT_EQ(2, mock_delegate_.update_system_input_method_count());
191 EXPECT_EQ(0, mock_delegate_.update_user_input_method_count());
192 }
193
194 TEST_F(BrowserStateMonitorTest, TestUpdatePrefOnBrowserScreen) {
195 monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
196 content::NotificationService::AllSources(),
197 content::NotificationService::NoDetails());
198 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
199
200 monitor_.InputMethodChanged(&mock_manager_, false);
201 EXPECT_EQ(0, mock_delegate_.update_system_input_method_count());
202 EXPECT_EQ(1, mock_delegate_.update_user_input_method_count());
203 monitor_.InputMethodChanged(&mock_manager_, false);
204 EXPECT_EQ(0, mock_delegate_.update_system_input_method_count());
205 EXPECT_EQ(2, mock_delegate_.update_user_input_method_count());
206
207 monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED,
208 content::NotificationService::AllSources(),
209 content::NotificationService::NoDetails());
210 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
211
212 monitor_.InputMethodChanged(&mock_manager_, false);
213 EXPECT_EQ(0, mock_delegate_.update_system_input_method_count());
214 EXPECT_EQ(3, mock_delegate_.update_user_input_method_count());
215 monitor_.InputMethodChanged(&mock_manager_, false);
216 EXPECT_EQ(0, mock_delegate_.update_system_input_method_count());
217 EXPECT_EQ(4, mock_delegate_.update_user_input_method_count());
218 }
219
220 TEST_F(BrowserStateMonitorTest, TestUpdatePrefOnLoginScreenDetails) {
221 EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, monitor_.state());
222 std::string input_method_id = "xkb:us:dvorak:eng";
223 mock_manager_.SetCurrentInputMethodId(input_method_id);
224 monitor_.InputMethodChanged(&mock_manager_, false);
225 EXPECT_EQ(1, mock_delegate_.update_system_input_method_count());
226 EXPECT_EQ(0, mock_delegate_.update_user_input_method_count());
227 EXPECT_EQ(input_method_id, mock_delegate_.system_input_method());
228 input_method_id = "xkb:us:colemak:eng";
229 mock_manager_.SetCurrentInputMethodId(input_method_id);
230 monitor_.InputMethodChanged(&mock_manager_, false);
231 EXPECT_EQ(2, mock_delegate_.update_system_input_method_count());
232 EXPECT_EQ(0, mock_delegate_.update_user_input_method_count());
233 EXPECT_EQ(input_method_id, mock_delegate_.system_input_method());
234 }
235
236 TEST_F(BrowserStateMonitorTest, TestUpdatePrefOnBrowserScreenDetails) {
237 monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
238 content::NotificationService::AllSources(),
239 content::NotificationService::NoDetails());
240 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
241 monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED,
242 content::NotificationService::AllSources(),
243 content::NotificationService::NoDetails());
244 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
245
246 const std::string input_method_id = "xkb:us:dvorak:eng";
247 mock_manager_.SetCurrentInputMethodId(input_method_id);
248 monitor_.InputMethodChanged(&mock_manager_, false);
249 EXPECT_EQ(0, mock_delegate_.update_system_input_method_count());
250 EXPECT_EQ(1, mock_delegate_.update_user_input_method_count());
251 EXPECT_EQ(input_method_id, mock_delegate_.user_input_method());
252
253 monitor_.InputMethodChanged(&mock_manager_, false);
254 EXPECT_EQ(0, mock_delegate_.update_system_input_method_count());
255 EXPECT_EQ(2, mock_delegate_.update_user_input_method_count());
256 EXPECT_EQ(input_method_id, mock_delegate_.user_input_method());
257
258 const std::string input_method_id_2 = "xkb:us:colemak:eng";
259 mock_manager_.SetCurrentInputMethodId(input_method_id_2);
260 monitor_.InputMethodChanged(&mock_manager_, false);
261 EXPECT_EQ(0, mock_delegate_.update_system_input_method_count());
262 EXPECT_EQ(3, mock_delegate_.update_user_input_method_count());
263 EXPECT_EQ(input_method_id_2, mock_delegate_.user_input_method());
264
265 const std::string input_method_id_3 = "xkb:us::eng";
266 mock_manager_.SetCurrentInputMethodId(input_method_id_3);
267 monitor_.InputMethodChanged(&mock_manager_, false);
268 EXPECT_EQ(0, mock_delegate_.update_system_input_method_count());
269 EXPECT_EQ(4, mock_delegate_.update_user_input_method_count());
270 EXPECT_EQ(input_method_id_3, mock_delegate_.user_input_method());
271
272 monitor_.InputMethodChanged(&mock_manager_, false);
273 EXPECT_EQ(0, mock_delegate_.update_system_input_method_count());
274 EXPECT_EQ(5, mock_delegate_.update_user_input_method_count());
275 EXPECT_EQ(input_method_id_3, mock_delegate_.user_input_method());
276 } 173 }
277 174
278 } // namespace input_method 175 } // namespace input_method
279 } // namespace chromeos 176 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698