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, | |
34 const NotificationDetails& details)); | |
35 | 33 |
36 private: | 34 private: |
37 DISALLOW_COPY_AND_ASSIGN(MockTokenAvailableObserver); | 35 DISALLOW_COPY_AND_ASSIGN(MockTokenAvailableObserver); |
38 }; | 36 }; |
39 | 37 |
40 class DeviceTokenFetcherTest : public testing::Test { | 38 class DeviceTokenFetcherTest : public testing::Test { |
41 protected: | 39 protected: |
42 DeviceTokenFetcherTest() | 40 DeviceTokenFetcherTest() |
43 : ui_thread_(BrowserThread::UI, &loop_), | 41 : ui_thread_(BrowserThread::UI, &loop_), |
44 file_thread_(BrowserThread::FILE, &loop_) { | 42 file_thread_(BrowserThread::FILE, &loop_) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 EXPECT_NE("", token); | 133 EXPECT_NE("", token); |
136 | 134 |
137 SimulateSuccessfulLoginAndRunPending(); | 135 SimulateSuccessfulLoginAndRunPending(); |
138 ASSERT_FALSE(fetcher_->IsTokenPending()); | 136 ASSERT_FALSE(fetcher_->IsTokenPending()); |
139 const std::string token2(fetcher_->GetDeviceToken()); | 137 const std::string token2(fetcher_->GetDeviceToken()); |
140 EXPECT_NE("", token2); | 138 EXPECT_NE("", token2); |
141 EXPECT_EQ(token, token2); | 139 EXPECT_EQ(token, token2); |
142 } | 140 } |
143 | 141 |
144 TEST_F(DeviceTokenFetcherTest, FetchBetweenBrowserLaunchAndNotify) { | 142 TEST_F(DeviceTokenFetcherTest, FetchBetweenBrowserLaunchAndNotify) { |
145 NotificationRegistrar registrar; | |
146 MockTokenAvailableObserver observer; | 143 MockTokenAvailableObserver observer; |
147 registrar.Add(&observer, | 144 fetcher_->AddObserver(&observer); |
danno
2010/11/22 13:47:50
Who removes the observer from the fetcher?
Jakob Kummerow (corp)
2010/11/22 16:56:08
Done.
| |
148 NotificationType::DEVICE_TOKEN_AVAILABLE, | 145 EXPECT_CALL(observer, OnTokenSuccess()).Times(1); |
149 NotificationService::AllSources()); | 146 EXPECT_CALL(observer, OnTokenError()).Times(0); |
150 EXPECT_CALL(observer, Observe(_, _, _)).Times(1); | |
151 backend_->AllShouldSucceed(); | 147 backend_->AllShouldSucceed(); |
152 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 148 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
153 SimulateSuccessfulLoginAndRunPending(); | 149 SimulateSuccessfulLoginAndRunPending(); |
154 ASSERT_FALSE(fetcher_->IsTokenPending()); | 150 ASSERT_FALSE(fetcher_->IsTokenPending()); |
155 const std::string token(fetcher_->GetDeviceToken()); | 151 const std::string token(fetcher_->GetDeviceToken()); |
156 EXPECT_NE("", token); | 152 EXPECT_NE("", token); |
157 Mock::VerifyAndClearExpectations(&observer); | 153 Mock::VerifyAndClearExpectations(&observer); |
158 | 154 |
159 // Swap out the fetchers, including copying the device management token on | 155 // Swap out the fetchers, including copying the device management token on |
160 // disk to where the new fetcher expects it. | 156 // disk to where the new fetcher expects it. |
161 fetcher_ = NewTestFetcher( | 157 fetcher_ = NewTestFetcher( |
162 temp_user_data_dir_.path()); | 158 temp_user_data_dir_.path()); |
163 fetcher_->StartFetching(); | 159 fetcher_->StartFetching(); |
164 ASSERT_TRUE(fetcher_->IsTokenPending()); | 160 ASSERT_TRUE(fetcher_->IsTokenPending()); |
165 loop_.RunAllPending(); | 161 loop_.RunAllPending(); |
166 ASSERT_FALSE(fetcher_->IsTokenPending()); | 162 ASSERT_FALSE(fetcher_->IsTokenPending()); |
167 const std::string token2(fetcher_->GetDeviceToken()); | 163 const std::string token2(fetcher_->GetDeviceToken()); |
168 EXPECT_NE("", token2); | 164 EXPECT_NE("", token2); |
169 EXPECT_EQ(token, token2); | 165 EXPECT_EQ(token, token2); |
170 } | 166 } |
171 | 167 |
172 TEST_F(DeviceTokenFetcherTest, FailedServerRequest) { | 168 TEST_F(DeviceTokenFetcherTest, FailedServerRequest) { |
173 backend_->AllShouldFail(); | 169 MockTokenAvailableObserver observer; |
170 fetcher_->AddObserver(&observer); | |
danno
2010/11/22 13:47:50
Who removes the observer from the fetcher?
Jakob Kummerow (corp)
2010/11/22 16:56:08
Done.
| |
171 EXPECT_CALL(observer, OnTokenSuccess()).Times(0); | |
172 EXPECT_CALL(observer, OnTokenError()).Times(1); | |
173 backend_->AllShouldFail(); | |
danno
2010/11/22 13:47:50
indentation above should be 2
Jakob Kummerow (corp)
2010/11/22 16:56:08
Done.
(Of course -- copy-paste artifact.)
| |
174 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 174 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
175 SimulateSuccessfulLoginAndRunPending(); | 175 SimulateSuccessfulLoginAndRunPending(); |
176 ASSERT_FALSE(fetcher_->IsTokenPending()); | 176 ASSERT_FALSE(fetcher_->IsTokenPending()); |
177 const std::string token(fetcher_->GetDeviceToken()); | 177 const std::string token(fetcher_->GetDeviceToken()); |
178 EXPECT_EQ("", token); | 178 EXPECT_EQ("", token); |
179 } | 179 } |
180 | 180 |
181 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) { | 181 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) { |
182 FilePath token_path; | |
183 GetDeviceTokenPath(fetcher_, &token_path); | |
184 file_util::WriteFile(token_path, "foo", 3); | |
185 ASSERT_TRUE(file_util::PathExists(token_path)); | |
182 backend_->UnmanagedDevice(); | 186 backend_->UnmanagedDevice(); |
183 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 187 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
184 SimulateSuccessfulLoginAndRunPending(); | 188 SimulateSuccessfulLoginAndRunPending(); |
185 ASSERT_FALSE(fetcher_->IsTokenPending()); | 189 ASSERT_FALSE(fetcher_->IsTokenPending()); |
186 ASSERT_EQ("", fetcher_->GetDeviceToken()); | 190 ASSERT_EQ("", fetcher_->GetDeviceToken()); |
187 ASSERT_EQ("", device_id(fetcher_)); | 191 ASSERT_EQ("", device_id(fetcher_)); |
188 FilePath token_path; | |
189 GetDeviceTokenPath(fetcher_, &token_path); | |
190 ASSERT_FALSE(file_util::PathExists(token_path)); | 192 ASSERT_FALSE(file_util::PathExists(token_path)); |
191 } | 193 } |
192 | 194 |
193 } // namespace policy | 195 } // namespace policy |
OLD | NEW |