OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
10 #include "chrome/browser/browser_thread.h" | 10 #include "chrome/browser/browser_thread.h" |
11 #include "chrome/browser/net/gaia/token_service.h" | 11 #include "chrome/browser/net/gaia/token_service.h" |
12 #include "chrome/browser/policy/device_token_fetcher.h" | 12 #include "chrome/browser/policy/device_token_fetcher.h" |
13 #include "chrome/browser/policy/mock_device_management_backend.h" | 13 #include "chrome/browser/policy/mock_device_management_backend.h" |
14 #include "chrome/common/net/gaia/gaia_constants.h" | 14 #include "chrome/common/net/gaia/gaia_constants.h" |
15 #include "chrome/common/notification_service.h" | 15 #include "chrome/common/notification_service.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace policy { | 19 namespace policy { |
20 | 20 |
21 const char kTestToken[] = "device_token_fetcher_test_auth_token"; | 21 const char kTestToken[] = "device_token_fetcher_test_auth_token"; |
22 | 22 |
23 using testing::_; | 23 using testing::_; |
24 using testing::Mock; | 24 using testing::Mock; |
25 | 25 |
26 class MockTokenAvailableObserver : public NotificationObserver { | 26 class MockTokenAvailableObserver : public DeviceTokenFetcher::Observer { |
27 public: | 27 public: |
28 MockTokenAvailableObserver() {} | 28 MockTokenAvailableObserver() {} |
29 virtual ~MockTokenAvailableObserver() {} | 29 virtual ~MockTokenAvailableObserver() {} |
30 | 30 |
31 MOCK_METHOD3(Observe, void( | 31 MOCK_METHOD0(OnTokenSuccess, void()); |
32 NotificationType type, | 32 MOCK_METHOD0(OnTokenError, void()); |
33 const NotificationSource& source, | 33 MOCK_METHOD0(OnNotManaged, void()); |
34 const NotificationDetails& details)); | |
35 | 34 |
36 private: | 35 private: |
37 DISALLOW_COPY_AND_ASSIGN(MockTokenAvailableObserver); | 36 DISALLOW_COPY_AND_ASSIGN(MockTokenAvailableObserver); |
38 }; | 37 }; |
39 | 38 |
40 class DeviceTokenFetcherTest : public testing::Test { | 39 class DeviceTokenFetcherTest : public testing::Test { |
41 protected: | 40 protected: |
42 DeviceTokenFetcherTest() | 41 DeviceTokenFetcherTest() |
43 : ui_thread_(BrowserThread::UI, &loop_), | 42 : ui_thread_(BrowserThread::UI, &loop_), |
44 file_thread_(BrowserThread::FILE, &loop_) { | 43 file_thread_(BrowserThread::FILE, &loop_) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 EXPECT_NE("", token); | 134 EXPECT_NE("", token); |
136 | 135 |
137 SimulateSuccessfulLoginAndRunPending(); | 136 SimulateSuccessfulLoginAndRunPending(); |
138 ASSERT_FALSE(fetcher_->IsTokenPending()); | 137 ASSERT_FALSE(fetcher_->IsTokenPending()); |
139 const std::string token2(fetcher_->GetDeviceToken()); | 138 const std::string token2(fetcher_->GetDeviceToken()); |
140 EXPECT_NE("", token2); | 139 EXPECT_NE("", token2); |
141 EXPECT_EQ(token, token2); | 140 EXPECT_EQ(token, token2); |
142 } | 141 } |
143 | 142 |
144 TEST_F(DeviceTokenFetcherTest, FetchBetweenBrowserLaunchAndNotify) { | 143 TEST_F(DeviceTokenFetcherTest, FetchBetweenBrowserLaunchAndNotify) { |
145 NotificationRegistrar registrar; | |
146 MockTokenAvailableObserver observer; | 144 MockTokenAvailableObserver observer; |
147 registrar.Add(&observer, | 145 DeviceTokenFetcher::ObserverRegistrar registrar; |
148 NotificationType::DEVICE_TOKEN_AVAILABLE, | 146 registrar.Init(fetcher_); |
149 NotificationService::AllSources()); | 147 registrar.AddObserver(&observer); |
150 EXPECT_CALL(observer, Observe(_, _, _)).Times(1); | 148 EXPECT_CALL(observer, OnTokenSuccess()).Times(1); |
| 149 EXPECT_CALL(observer, OnTokenError()).Times(0); |
151 backend_->AllShouldSucceed(); | 150 backend_->AllShouldSucceed(); |
152 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 151 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
153 SimulateSuccessfulLoginAndRunPending(); | 152 SimulateSuccessfulLoginAndRunPending(); |
154 ASSERT_FALSE(fetcher_->IsTokenPending()); | 153 ASSERT_FALSE(fetcher_->IsTokenPending()); |
155 const std::string token(fetcher_->GetDeviceToken()); | 154 const std::string token(fetcher_->GetDeviceToken()); |
156 EXPECT_NE("", token); | 155 EXPECT_NE("", token); |
157 Mock::VerifyAndClearExpectations(&observer); | 156 Mock::VerifyAndClearExpectations(&observer); |
158 | 157 |
159 // Swap out the fetchers, including copying the device management token on | 158 // Swap out the fetchers, including copying the device management token on |
160 // disk to where the new fetcher expects it. | 159 // disk to where the new fetcher expects it. |
161 fetcher_ = NewTestFetcher( | 160 registrar.RemoveAll(); |
162 temp_user_data_dir_.path()); | 161 fetcher_ = NewTestFetcher(temp_user_data_dir_.path()); |
| 162 registrar.Init(fetcher_); |
163 fetcher_->StartFetching(); | 163 fetcher_->StartFetching(); |
164 ASSERT_TRUE(fetcher_->IsTokenPending()); | 164 ASSERT_TRUE(fetcher_->IsTokenPending()); |
165 loop_.RunAllPending(); | 165 loop_.RunAllPending(); |
166 ASSERT_FALSE(fetcher_->IsTokenPending()); | 166 ASSERT_FALSE(fetcher_->IsTokenPending()); |
167 const std::string token2(fetcher_->GetDeviceToken()); | 167 const std::string token2(fetcher_->GetDeviceToken()); |
168 EXPECT_NE("", token2); | 168 EXPECT_NE("", token2); |
169 EXPECT_EQ(token, token2); | 169 EXPECT_EQ(token, token2); |
170 } | 170 } |
171 | 171 |
172 TEST_F(DeviceTokenFetcherTest, FailedServerRequest) { | 172 TEST_F(DeviceTokenFetcherTest, FailedServerRequest) { |
| 173 MockTokenAvailableObserver observer; |
| 174 DeviceTokenFetcher::ObserverRegistrar registrar; |
| 175 registrar.Init(fetcher_); |
| 176 registrar.AddObserver(&observer); |
| 177 EXPECT_CALL(observer, OnTokenSuccess()).Times(0); |
| 178 EXPECT_CALL(observer, OnTokenError()).Times(1); |
173 backend_->AllShouldFail(); | 179 backend_->AllShouldFail(); |
174 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 180 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
175 SimulateSuccessfulLoginAndRunPending(); | 181 SimulateSuccessfulLoginAndRunPending(); |
176 ASSERT_FALSE(fetcher_->IsTokenPending()); | 182 ASSERT_FALSE(fetcher_->IsTokenPending()); |
177 const std::string token(fetcher_->GetDeviceToken()); | 183 const std::string token(fetcher_->GetDeviceToken()); |
178 EXPECT_EQ("", token); | 184 EXPECT_EQ("", token); |
179 } | 185 } |
180 | 186 |
181 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) { | 187 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) { |
| 188 FilePath token_path; |
| 189 GetDeviceTokenPath(fetcher_, &token_path); |
| 190 file_util::WriteFile(token_path, "foo", 3); |
| 191 ASSERT_TRUE(file_util::PathExists(token_path)); |
182 backend_->UnmanagedDevice(); | 192 backend_->UnmanagedDevice(); |
183 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 193 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
184 SimulateSuccessfulLoginAndRunPending(); | 194 SimulateSuccessfulLoginAndRunPending(); |
185 ASSERT_FALSE(fetcher_->IsTokenPending()); | 195 ASSERT_FALSE(fetcher_->IsTokenPending()); |
186 ASSERT_EQ("", fetcher_->GetDeviceToken()); | 196 ASSERT_EQ("", fetcher_->GetDeviceToken()); |
187 ASSERT_EQ("", device_id(fetcher_)); | 197 ASSERT_EQ("", device_id(fetcher_)); |
188 FilePath token_path; | |
189 GetDeviceTokenPath(fetcher_, &token_path); | |
190 ASSERT_FALSE(file_util::PathExists(token_path)); | 198 ASSERT_FALSE(file_util::PathExists(token_path)); |
191 } | 199 } |
192 | 200 |
193 } // namespace policy | 201 } // namespace policy |
OLD | NEW |