OLD | NEW |
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/bind.h" | 10 #include "base/bind.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
12 #include "content/public/browser/notification_details.h" | 12 #include "content/public/browser/notification_details.h" |
13 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace chromeos { | 16 namespace chromeos { |
17 namespace input_method { | 17 namespace input_method { |
18 namespace { | 18 namespace { |
19 | 19 |
20 class MockObserver { | 20 class MockObserver { |
21 public: | 21 public: |
22 MockObserver() | 22 MockObserver() |
23 : state_(InputMethodManager::STATE_TERMINATING), | 23 : ui_session_(InputMethodManager::STATE_TERMINATING), |
24 update_state_count_(0) { } | 24 update_ui_session_count_(0) {} |
25 | 25 |
26 void SetState(InputMethodManager::State new_state) { | 26 void SetState(InputMethodManager::UISessionState new_ui_session) { |
27 ++update_state_count_; | 27 ++update_ui_session_count_; |
28 state_ = new_state; | 28 ui_session_ = new_ui_session; |
29 } | 29 } |
30 | 30 |
31 base::Callback<void(InputMethodManager::State new_state)> AsCallback() { | 31 base::Callback<void(InputMethodManager::UISessionState new_ui_session)> |
| 32 AsCallback() { |
32 return base::Bind(&MockObserver::SetState, base::Unretained(this)); | 33 return base::Bind(&MockObserver::SetState, base::Unretained(this)); |
33 } | 34 } |
34 | 35 |
35 int update_state_count() const { | 36 int update_ui_session_count() const { return update_ui_session_count_; } |
36 return update_state_count_; | |
37 } | |
38 | 37 |
39 InputMethodManager::State state() const { | 38 InputMethodManager::UISessionState ui_session() const { return ui_session_; } |
40 return state_; | |
41 } | |
42 | 39 |
43 private: | 40 private: |
44 InputMethodManager::State state_; | 41 InputMethodManager::UISessionState ui_session_; |
45 int update_state_count_; | 42 int update_ui_session_count_; |
46 | 43 |
47 DISALLOW_COPY_AND_ASSIGN(MockObserver); | 44 DISALLOW_COPY_AND_ASSIGN(MockObserver); |
48 }; | 45 }; |
49 | 46 |
50 } // anonymous namespace | 47 } // anonymous namespace |
51 | 48 |
52 TEST(BrowserStateMonitorLifetimeTest, TestConstruction) { | 49 TEST(BrowserStateMonitorLifetimeTest, TestConstruction) { |
53 MockObserver mock_observer; | 50 MockObserver mock_observer; |
54 BrowserStateMonitor monitor(mock_observer.AsCallback()); | 51 BrowserStateMonitor monitor(mock_observer.AsCallback()); |
55 | 52 |
56 // Check the initial state of the |mock_observer| and |monitor| objects. | 53 // Check the initial ui_session_ of the |mock_observer| and |monitor| objects. |
57 EXPECT_EQ(1, mock_observer.update_state_count()); | 54 EXPECT_EQ(1, mock_observer.update_ui_session_count()); |
58 EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, mock_observer.state()); | 55 EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, mock_observer.ui_session()); |
59 EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, monitor.state()); | 56 EXPECT_EQ(InputMethodManager::STATE_LOGIN_SCREEN, monitor.ui_session()); |
60 } | 57 } |
61 | 58 |
62 namespace { | 59 namespace { |
63 | 60 |
64 class BrowserStateMonitorTest : public testing::Test { | 61 class BrowserStateMonitorTest : public testing::Test { |
65 public: | 62 public: |
66 BrowserStateMonitorTest() | 63 BrowserStateMonitorTest() |
67 : monitor_(mock_observer_.AsCallback()) { | 64 : monitor_(mock_observer_.AsCallback()) { |
68 } | 65 } |
69 | 66 |
70 protected: | 67 protected: |
71 MockObserver mock_observer_; | 68 MockObserver mock_observer_; |
72 BrowserStateMonitor monitor_; | 69 BrowserStateMonitor monitor_; |
73 | 70 |
74 private: | 71 private: |
75 DISALLOW_COPY_AND_ASSIGN(BrowserStateMonitorTest); | 72 DISALLOW_COPY_AND_ASSIGN(BrowserStateMonitorTest); |
76 }; | 73 }; |
77 | 74 |
78 } // anonymous namespace | 75 } // anonymous namespace |
79 | 76 |
80 TEST_F(BrowserStateMonitorTest, TestObserveLoginUserChanged) { | 77 TEST_F(BrowserStateMonitorTest, TestObserveLoginUserChanged) { |
81 EXPECT_EQ(1, mock_observer_.update_state_count()); | 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()); |
82 monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, | 108 monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
83 content::NotificationService::AllSources(), | 109 content::NotificationService::AllSources(), |
84 content::NotificationService::NoDetails()); | 110 content::NotificationService::NoDetails()); |
85 | 111 |
86 // Check if the state of the |mock_observer_| as well as the |monitor| are | 112 // Check if the state of the |mock_observer_| as well as the |monitor| are |
87 // both changed. | 113 // both changed. |
88 EXPECT_EQ(2, mock_observer_.update_state_count()); | 114 EXPECT_EQ(2, mock_observer_.update_ui_session_count()); |
89 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, | 115 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, |
90 mock_observer_.state()); | 116 mock_observer_.ui_session()); |
91 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state()); | 117 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.ui_session()); |
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 | 118 |
121 monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED, | 119 monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED, |
122 content::NotificationService::AllSources(), | 120 content::NotificationService::AllSources(), |
123 content::NotificationService::NoDetails()); | 121 content::NotificationService::NoDetails()); |
124 | 122 |
125 // The second notification should be nop. | 123 // The second notification should be nop. |
126 EXPECT_EQ(2, mock_observer_.update_state_count()); | 124 EXPECT_EQ(2, mock_observer_.update_ui_session_count()); |
127 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, | 125 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, |
128 mock_observer_.state()); | 126 mock_observer_.ui_session()); |
129 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state()); | 127 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.ui_session()); |
130 } | 128 } |
131 | 129 |
132 TEST_F(BrowserStateMonitorTest, TestObserveScreenLockUnlock) { | 130 TEST_F(BrowserStateMonitorTest, TestObserveScreenLockUnlock) { |
133 EXPECT_EQ(1, mock_observer_.update_state_count()); | 131 EXPECT_EQ(1, mock_observer_.update_ui_session_count()); |
134 monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, | 132 monitor_.Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
135 content::NotificationService::AllSources(), | 133 content::NotificationService::AllSources(), |
136 content::NotificationService::NoDetails()); | 134 content::NotificationService::NoDetails()); |
137 EXPECT_EQ(2, mock_observer_.update_state_count()); | 135 EXPECT_EQ(2, mock_observer_.update_ui_session_count()); |
138 monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED, | 136 monitor_.Observe(chrome::NOTIFICATION_SESSION_STARTED, |
139 content::NotificationService::AllSources(), | 137 content::NotificationService::AllSources(), |
140 content::NotificationService::NoDetails()); | 138 content::NotificationService::NoDetails()); |
141 EXPECT_EQ(2, mock_observer_.update_state_count()); | 139 EXPECT_EQ(2, mock_observer_.update_ui_session_count()); |
142 bool locked = true; | 140 bool locked = true; |
143 monitor_.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, | 141 monitor_.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, |
144 content::NotificationService::AllSources(), | 142 content::NotificationService::AllSources(), |
145 content::Details<bool>(&locked)); | 143 content::Details<bool>(&locked)); |
146 EXPECT_EQ(3, mock_observer_.update_state_count()); | 144 EXPECT_EQ(3, mock_observer_.update_ui_session_count()); |
147 EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN, | 145 EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN, mock_observer_.ui_session()); |
148 mock_observer_.state()); | 146 EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN, monitor_.ui_session()); |
149 EXPECT_EQ(InputMethodManager::STATE_LOCK_SCREEN, monitor_.state()); | |
150 | 147 |
151 locked = false; | 148 locked = false; |
152 monitor_.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, | 149 monitor_.Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, |
153 content::NotificationService::AllSources(), | 150 content::NotificationService::AllSources(), |
154 content::Details<bool>(&locked)); | 151 content::Details<bool>(&locked)); |
155 EXPECT_EQ(4, mock_observer_.update_state_count()); | 152 EXPECT_EQ(4, mock_observer_.update_ui_session_count()); |
156 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, | 153 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, |
157 mock_observer_.state()); | 154 mock_observer_.ui_session()); |
158 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.state()); | 155 EXPECT_EQ(InputMethodManager::STATE_BROWSER_SCREEN, monitor_.ui_session()); |
159 } | 156 } |
160 | 157 |
161 TEST_F(BrowserStateMonitorTest, TestObserveAppTerminating) { | 158 TEST_F(BrowserStateMonitorTest, TestObserveAppTerminating) { |
162 EXPECT_EQ(1, mock_observer_.update_state_count()); | 159 EXPECT_EQ(1, mock_observer_.update_ui_session_count()); |
163 monitor_.Observe(chrome::NOTIFICATION_APP_TERMINATING, | 160 monitor_.Observe(chrome::NOTIFICATION_APP_TERMINATING, |
164 content::NotificationService::AllSources(), | 161 content::NotificationService::AllSources(), |
165 content::NotificationService::NoDetails()); | 162 content::NotificationService::NoDetails()); |
166 | 163 |
167 // Check if the state of the |mock_observer_| as well as the |monitor| are | 164 // Check if the state of the |mock_observer_| as well as the |monitor| are |
168 // both changed. | 165 // both changed. |
169 EXPECT_EQ(2, mock_observer_.update_state_count()); | 166 EXPECT_EQ(2, mock_observer_.update_ui_session_count()); |
170 EXPECT_EQ(InputMethodManager::STATE_TERMINATING, | 167 EXPECT_EQ(InputMethodManager::STATE_TERMINATING, mock_observer_.ui_session()); |
171 mock_observer_.state()); | 168 EXPECT_EQ(InputMethodManager::STATE_TERMINATING, monitor_.ui_session()); |
172 EXPECT_EQ(InputMethodManager::STATE_TERMINATING, monitor_.state()); | |
173 } | 169 } |
174 | 170 |
175 } // namespace input_method | 171 } // namespace input_method |
176 } // namespace chromeos | 172 } // namespace chromeos |
OLD | NEW |