Chromium Code Reviews| 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 scoped_ptr<DeviceTokenFetcher::ObserverRegistrar> registrar( |
| 148 NotificationType::DEVICE_TOKEN_AVAILABLE, | 146 new DeviceTokenFetcher::ObserverRegistrar(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.reset(NULL); |
|
danno
2010/11/23 12:10:14
Aternitavely, you can add a RemoveObserver method
Jakob Kummerow (corp)
2010/11/23 12:25:26
That's the alternative, yes. It would mean more co
| |
| 162 temp_user_data_dir_.path()); | 161 fetcher_ = NewTestFetcher(temp_user_data_dir_.path()); |
| 162 registrar.reset(new DeviceTokenFetcher::ObserverRegistrar(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(fetcher_); | |
| 175 registrar.AddObserver(&observer); | |
| 176 EXPECT_CALL(observer, OnTokenSuccess()).Times(0); | |
| 177 EXPECT_CALL(observer, OnTokenError()).Times(1); | |
| 173 backend_->AllShouldFail(); | 178 backend_->AllShouldFail(); |
| 174 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 179 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
| 175 SimulateSuccessfulLoginAndRunPending(); | 180 SimulateSuccessfulLoginAndRunPending(); |
| 176 ASSERT_FALSE(fetcher_->IsTokenPending()); | 181 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 177 const std::string token(fetcher_->GetDeviceToken()); | 182 const std::string token(fetcher_->GetDeviceToken()); |
| 178 EXPECT_EQ("", token); | 183 EXPECT_EQ("", token); |
| 179 } | 184 } |
| 180 | 185 |
| 181 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) { | 186 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) { |
| 187 FilePath token_path; | |
| 188 GetDeviceTokenPath(fetcher_, &token_path); | |
| 189 file_util::WriteFile(token_path, "foo", 3); | |
| 190 ASSERT_TRUE(file_util::PathExists(token_path)); | |
| 182 backend_->UnmanagedDevice(); | 191 backend_->UnmanagedDevice(); |
| 183 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 192 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
| 184 SimulateSuccessfulLoginAndRunPending(); | 193 SimulateSuccessfulLoginAndRunPending(); |
| 185 ASSERT_FALSE(fetcher_->IsTokenPending()); | 194 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 186 ASSERT_EQ("", fetcher_->GetDeviceToken()); | 195 ASSERT_EQ("", fetcher_->GetDeviceToken()); |
| 187 ASSERT_EQ("", device_id(fetcher_)); | 196 ASSERT_EQ("", device_id(fetcher_)); |
| 188 FilePath token_path; | |
| 189 GetDeviceTokenPath(fetcher_, &token_path); | |
| 190 ASSERT_FALSE(file_util::PathExists(token_path)); | 197 ASSERT_FALSE(file_util::PathExists(token_path)); |
| 191 } | 198 } |
| 192 | 199 |
| 193 } // namespace policy | 200 } // namespace policy |
| OLD | NEW |