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. 4 5 #include "chrome/browser/chromeos/input_method/browser_state_monitor.h" 6 7 #include <string> 8 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" 15 16 namespace chromeos { 17 namespace input_method { 18 namespace { 19 20 class MockObserver { 21 public: 22 MockObserver() 23 : ui_session_(InputMethodManager::STATE_TERMINATING), 24 update_ui_session_count_(0) {} 25 26 void SetState(InputMethodManager::UISessionState new_ui_session) { 27 ++update_ui_session_count_; 28 ui_session_ = new_ui_session; 29 } 30 31 base::Callback<void(InputMethodManager::UISessionState new_ui_session)> 32 AsCallback() { 33 return base::Bind(&MockObserver::SetState, base::Unretained(this)); 34 } 35 36 int update_ui_session_count() const { return update_ui_session_count_; } 37 38 InputMethodManager::UISessionState ui_session() const { return ui_session_; } 39 40 private: 41 InputMethodManager::UISessionState ui_session_; 42 int update_ui_session_count_; 43 44 DISALLOW_COPY_AND_ASSIGN(MockObserver); 45 }; 46 47 } // anonymous namespace 48 49 TEST(BrowserStateMonitorLifetimeTest, TestConstruction) { 50 MockObserver mock_observer; 51 BrowserStateMonitor monitor(mock_observer.AsCallback()); 52 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()); 57 } 58 59 namespace { 60 61 class BrowserStateMonitorTest : public testing::Test { 62 public: 63 BrowserStateMonitorTest() 64 : monitor_(mock_observer_.AsCallback()) { 65 } 66 67 protected: 68 MockObserver mock_observer_; 69 BrowserStateMonitor monitor_; 70 71 private: 72 DISALLOW_COPY_AND_ASSIGN(BrowserStateMonitorTest); 73 }; 74 75 } // anonymous namespace 76 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()); 82 83 // Check if the ui_session of the |mock_observer_| as well as the |monitor| 84 // are 85 // both changed. 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()); 90 } 91 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()); 97 98 // Check if the state of the |mock_observer_| as well as the |monitor| are 99 // both changed. 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()); 104 } 105 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()); 111 112 // Check if the state of the |mock_observer_| as well as the |monitor| are 113 // both changed. 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()); 118 119 monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED, 120 content::NotificationService::AllSources(), 121 content::NotificationService::NoDetails()); 122 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()); 128 } 129 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()); 140 bool locked = true; 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()); 147 148 locked = false; 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()); 156 } 157 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()); 163 164 // Check if the state of the |mock_observer_| as well as the |monitor| are 165 // both changed. 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()); 169 } 170 171 } // namespace input_method 172 } // namespace chromeos 173