- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / input_method / browser_state_monitor_unittest.cc
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       : state_(InputMethodManager::STATE_TERMINATING),
24         update_state_count_(0) { }
25
26   void SetState(InputMethodManager::State new_state) {
27     ++update_state_count_;
28     state_ = new_state;
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);
48 };
49
50 }  // anonymous namespace
51
52 TEST(BrowserStateMonitorLifetimeTest, TestConstruction) {
53   MockObserver mock_observer;
54   BrowserStateMonitor monitor(mock_observer.AsCallback());
55
56   // Check the initial state of the |mock_observer| and |monitor| objects.
57   EXPECT_EQ(1, mock_observer.update_state_count());
58   EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, mock_observer.state());
59   EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, monitor.state());
60 }
61
62 namespace {
63
64 class BrowserStateMonitorTest :  public testing::Test {
65  public:
66   BrowserStateMonitorTest()
67       : monitor_(mock_observer_.AsCallback()) {
68   }
69
70  protected:
71   MockObserver mock_observer_;
72   BrowserStateMonitor monitor_;
73
74  private:
75   DISALLOW_COPY_AND_ASSIGN(BrowserStateMonitorTest);
76 };
77
78 }  // anonymous namespace
79
80 TEST_F(BrowserStateMonitorTest, TestObserveLoginUserChanged) {
81   EXPECT_EQ(1, mock_observer_.update_state_count());
82   monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
83                    content::NotificationService::AllSources(),
84                    content::NotificationService::NoDetails());
85
86   // Check if the state of the |mock_observer_| as well as the |monitor| are
87   // both changed.
88   EXPECT_EQ(2, mock_observer_.update_state_count());
89   EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
90             mock_observer_.state());
91   EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
92 }
93
94 TEST_F(BrowserStateMonitorTest, TestObserveSessionStarted) {
95   EXPECT_EQ(1, mock_observer_.update_state_count());
96   monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED,
97                    content::NotificationService::AllSources(),
98                    content::NotificationService::NoDetails());
99
100   // Check if the state of the |mock_observer_| as well as the |monitor| are
101   // both changed.
102   EXPECT_EQ(2, mock_observer_.update_state_count());
103   EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
104             mock_observer_.state());
105   EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
106 }
107
108 TEST_F(BrowserStateMonitorTest, TestObserveLoginUserChangedThenSessionStarted) {
109   EXPECT_EQ(1, mock_observer_.update_state_count());
110   monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
111                    content::NotificationService::AllSources(),
112                    content::NotificationService::NoDetails());
113
114   // Check if the state of the |mock_observer_| as well as the |monitor| are
115   // both changed.
116   EXPECT_EQ(2, mock_observer_.update_state_count());
117   EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
118             mock_observer_.state());
119   EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
120
121   monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED,
122                    content::NotificationService::AllSources(),
123                    content::NotificationService::NoDetails());
124
125   // The second notification should be nop.
126   EXPECT_EQ(2, mock_observer_.update_state_count());
127   EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
128             mock_observer_.state());
129   EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
130 }
131
132 TEST_F(BrowserStateMonitorTest, TestObserveScreenLockUnlock) {
133   EXPECT_EQ(1, mock_observer_.update_state_count());
134   monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
135                    content::NotificationService::AllSources(),
136                    content::NotificationService::NoDetails());
137   EXPECT_EQ(2, mock_observer_.update_state_count());
138   monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED,
139                    content::NotificationService::AllSources(),
140                    content::NotificationService::NoDetails());
141   EXPECT_EQ(2, mock_observer_.update_state_count());
142   bool locked = true;
143   monitor_.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
144                    content::NotificationService::AllSources(),
145                    content::Details<bool>(&locked));
146   EXPECT_EQ(3, mock_observer_.update_state_count());
147   EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN,
148             mock_observer_.state());
149   EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN, monitor_.state());
150
151   locked = false;
152   monitor_.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
153                    content::NotificationService::AllSources(),
154                    content::Details<bool>(&locked));
155   EXPECT_EQ(4, mock_observer_.update_state_count());
156   EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN,
157             mock_observer_.state());
158   EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state());
159 }
160
161 TEST_F(BrowserStateMonitorTest, TestObserveAppTerminating) {
162   EXPECT_EQ(1, mock_observer_.update_state_count());
163   monitor_.Observe(chrome::NOTIFICATION_APP_TERMINATING,
164                    content::NotificationService::AllSources(),
165                    content::NotificationService::NoDetails());
166
167   // Check if the state of the |mock_observer_| as well as the |monitor| are
168   // both changed.
169   EXPECT_EQ(2, mock_observer_.update_state_count());
170   EXPECT_EQ(InputMethodManager::STATE_TERMINATING,
171             mock_observer_.state());
172   EXPECT_EQ(InputMethodManager::STATE_TERMINATING, monitor_.state());
173 }
174
175 }  // namespace input_method
176 }  // namespace chromeos