15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/chromeos/input_method/browser_state_monitor.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/basictypes.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind.h"
117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_details.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_service.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace chromeos {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace input_method {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class MockObserver {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MockObserver()
2303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      : ui_session_(InputMethodManager::STATE_TERMINATING),
2403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        update_ui_session_count_(0) {}
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  void SetState(InputMethodManager::UISessionState new_ui_session) {
2703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    ++update_ui_session_count_;
2803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    ui_session_ = new_ui_session;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  base::Callback<void(InputMethodManager::UISessionState new_ui_session)>
3203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  AsCallback() {
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return base::Bind(&MockObserver::SetState, base::Unretained(this));
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  int update_ui_session_count() const { return update_ui_session_count_; }
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  InputMethodManager::UISessionState ui_session() const { return ui_session_; }
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
4103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  InputMethodManager::UISessionState ui_session_;
4203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  int update_ui_session_count_;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MockObserver);
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // anonymous namespace
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST(BrowserStateMonitorLifetimeTest, TestConstruction) {
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MockObserver mock_observer;
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserStateMonitor monitor(mock_observer.AsCallback());
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Check the initial ui_session_ of the |mock_observer| and |monitor| objects.
5403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(1, mock_observer.update_ui_session_count());
5503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, mock_observer.ui_session());
5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, monitor.ui_session());
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class BrowserStateMonitorTest :  public testing::Test {
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserStateMonitorTest()
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      : monitor_(mock_observer_.AsCallback()) {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected:
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MockObserver mock_observer_;
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserStateMonitor monitor_;
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(BrowserStateMonitorTest);
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // anonymous namespace
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(BrowserStateMonitorTest, TestObserveLoginUserChanged) {
7803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(1, mock_observer_.update_ui_session_count());
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::AllSources(),
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::NoDetails());
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Check if the ui_session of the |mock_observer_| as well as the |monitor|
8403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // are
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // both changed.
8603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(2, mock_observer_.update_ui_session_count());
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
8803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)            mock_observer_.ui_session());
8903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.ui_session());
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(BrowserStateMonitorTest, TestObserveSessionStarted) {
9303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(1, mock_observer_.update_ui_session_count());
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED,
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::AllSources(),
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::NoDetails());
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check if the state of the |mock_observer_| as well as the |monitor| are
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // both changed.
10003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(2, mock_observer_.update_ui_session_count());
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
10203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)            mock_observer_.ui_session());
10303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.ui_session());
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(BrowserStateMonitorTest, TestObserveLoginUserChangedThenSessionStarted) {
10703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(1, mock_observer_.update_ui_session_count());
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::AllSources(),
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::NoDetails());
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check if the state of the |mock_observer_| as well as the |monitor| are
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // both changed.
11403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(2, mock_observer_.update_ui_session_count());
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
11603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)            mock_observer_.ui_session());
11703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.ui_session());
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED,
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::AllSources(),
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::NoDetails());
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The second notification should be nop.
12403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(2, mock_observer_.update_ui_session_count());
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
12603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)            mock_observer_.ui_session());
12703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.ui_session());
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(BrowserStateMonitorTest, TestObserveScreenLockUnlock) {
13103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(1, mock_observer_.update_ui_session_count());
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::AllSources(),
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::NoDetails());
13503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(2, mock_observer_.update_ui_session_count());
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED,
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::AllSources(),
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::NoDetails());
13903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(2, mock_observer_.update_ui_session_count());
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool locked = true;
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  monitor_.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::AllSources(),
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::Details<bool>(&locked));
14403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(3, mock_observer_.update_ui_session_count());
14503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN, mock_observer_.ui_session());
14603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN, monitor_.ui_session());
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  locked = false;
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  monitor_.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::AllSources(),
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::Details<bool>(&locked));
15203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(4, mock_observer_.update_ui_session_count());
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
15403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)            mock_observer_.ui_session());
15503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.ui_session());
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(BrowserStateMonitorTest, TestObserveAppTerminating) {
15903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(1, mock_observer_.update_ui_session_count());
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  monitor_.Observe(chrome::NOTIFICATION_APP_TERMINATING,
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::AllSources(),
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::NoDetails());
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check if the state of the |mock_observer_| as well as the |monitor| are
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // both changed.
16603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(2, mock_observer_.update_ui_session_count());
16703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_TERMINATING, mock_observer_.ui_session());
16803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_EQ(InputMethodManager::STATE_TERMINATING, monitor_.ui_session());
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace input_method
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace chromeos
173