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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/input_method/browser_state_monitor.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "content/public/browser/notification_details.h"
13 #include "content/public/browser/notification_service.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 namespace input_method
{
23 : ui_session_(InputMethodManager::STATE_TERMINATING
),
24 update_ui_session_count_(0) {}
26 void SetState(InputMethodManager::UISessionState new_ui_session
) {
27 ++update_ui_session_count_
;
28 ui_session_
= new_ui_session
;
31 base::Callback
<void(InputMethodManager::UISessionState new_ui_session
)>
33 return base::Bind(&MockObserver::SetState
, base::Unretained(this));
36 int update_ui_session_count() const { return update_ui_session_count_
; }
38 InputMethodManager::UISessionState
ui_session() const { return ui_session_
; }
41 InputMethodManager::UISessionState ui_session_
;
42 int update_ui_session_count_
;
44 DISALLOW_COPY_AND_ASSIGN(MockObserver
);
47 } // anonymous namespace
49 TEST(BrowserStateMonitorLifetimeTest
, TestConstruction
) {
50 MockObserver mock_observer
;
51 BrowserStateMonitor
monitor(mock_observer
.AsCallback());
53 // Check the initial ui_session_ of the |mock_observer| and |monitor| objects.
54 EXPECT_EQ(1, mock_observer
.update_ui_session_count());
55 EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN
, mock_observer
.ui_session());
56 EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN
, monitor
.ui_session());
61 class BrowserStateMonitorTest
: public testing::Test
{
63 BrowserStateMonitorTest()
64 : monitor_(mock_observer_
.AsCallback()) {
68 MockObserver mock_observer_
;
69 BrowserStateMonitor monitor_
;
72 DISALLOW_COPY_AND_ASSIGN(BrowserStateMonitorTest
);
75 } // anonymous namespace
77 TEST_F(BrowserStateMonitorTest
, TestObserveLoginUserChanged
) {
78 EXPECT_EQ(1, mock_observer_
.update_ui_session_count());
79 monitor_
.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED
,
80 content::NotificationService::AllSources(),
81 content::NotificationService::NoDetails());
83 // Check if the ui_session of the |mock_observer_| as well as the |monitor|
86 EXPECT_EQ(2, mock_observer_
.update_ui_session_count());
87 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN
,
88 mock_observer_
.ui_session());
89 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN
, monitor_
.ui_session());
92 TEST_F(BrowserStateMonitorTest
, TestObserveSessionStarted
) {
93 EXPECT_EQ(1, mock_observer_
.update_ui_session_count());
94 monitor_
.Observe(chrome::NOTIFICATION_SESSION_STARTED
,
95 content::NotificationService::AllSources(),
96 content::NotificationService::NoDetails());
98 // Check if the state of the |mock_observer_| as well as the |monitor| are
100 EXPECT_EQ(2, mock_observer_
.update_ui_session_count());
101 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN
,
102 mock_observer_
.ui_session());
103 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN
, monitor_
.ui_session());
106 TEST_F(BrowserStateMonitorTest
, TestObserveLoginUserChangedThenSessionStarted
) {
107 EXPECT_EQ(1, mock_observer_
.update_ui_session_count());
108 monitor_
.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED
,
109 content::NotificationService::AllSources(),
110 content::NotificationService::NoDetails());
112 // Check if the state of the |mock_observer_| as well as the |monitor| are
114 EXPECT_EQ(2, mock_observer_
.update_ui_session_count());
115 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN
,
116 mock_observer_
.ui_session());
117 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN
, monitor_
.ui_session());
119 monitor_
.Observe(chrome::NOTIFICATION_SESSION_STARTED
,
120 content::NotificationService::AllSources(),
121 content::NotificationService::NoDetails());
123 // The second notification should be nop.
124 EXPECT_EQ(2, mock_observer_
.update_ui_session_count());
125 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN
,
126 mock_observer_
.ui_session());
127 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN
, monitor_
.ui_session());
130 TEST_F(BrowserStateMonitorTest
, TestObserveScreenLockUnlock
) {
131 EXPECT_EQ(1, mock_observer_
.update_ui_session_count());
132 monitor_
.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED
,
133 content::NotificationService::AllSources(),
134 content::NotificationService::NoDetails());
135 EXPECT_EQ(2, mock_observer_
.update_ui_session_count());
136 monitor_
.Observe(chrome::NOTIFICATION_SESSION_STARTED
,
137 content::NotificationService::AllSources(),
138 content::NotificationService::NoDetails());
139 EXPECT_EQ(2, mock_observer_
.update_ui_session_count());
141 monitor_
.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED
,
142 content::NotificationService::AllSources(),
143 content::Details
<bool>(&locked
));
144 EXPECT_EQ(3, mock_observer_
.update_ui_session_count());
145 EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN
, mock_observer_
.ui_session());
146 EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN
, monitor_
.ui_session());
149 monitor_
.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED
,
150 content::NotificationService::AllSources(),
151 content::Details
<bool>(&locked
));
152 EXPECT_EQ(4, mock_observer_
.update_ui_session_count());
153 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN
,
154 mock_observer_
.ui_session());
155 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN
, monitor_
.ui_session());
158 TEST_F(BrowserStateMonitorTest
, TestObserveAppTerminating
) {
159 EXPECT_EQ(1, mock_observer_
.update_ui_session_count());
160 monitor_
.Observe(chrome::NOTIFICATION_APP_TERMINATING
,
161 content::NotificationService::AllSources(),
162 content::NotificationService::NoDetails());
164 // Check if the state of the |mock_observer_| as well as the |monitor| are
166 EXPECT_EQ(2, mock_observer_
.update_ui_session_count());
167 EXPECT_EQ(InputMethodManager::STATE_TERMINATING
, mock_observer_
.ui_session());
168 EXPECT_EQ(InputMethodManager::STATE_TERMINATING
, monitor_
.ui_session());
171 } // namespace input_method
172 } // namespace chromeos