| 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/notification_service.h" | 15 #include "chrome/common/notification_service.h" |
| 15 #include "chrome/test/device_management_test_util.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 namespace { |
| 22 const char kTestToken[] = "device_token_fetcher_test_auth_token"; |
| 23 } |
| 24 |
| 21 using testing::_; | 25 using testing::_; |
| 22 using testing::Mock; | 26 using testing::Mock; |
| 23 | 27 |
| 24 class MockTokenAvailableObserver : public NotificationObserver { | 28 class MockTokenAvailableObserver : public NotificationObserver { |
| 25 public: | 29 public: |
| 26 MockTokenAvailableObserver() {} | 30 MockTokenAvailableObserver() {} |
| 27 virtual ~MockTokenAvailableObserver() {} | 31 virtual ~MockTokenAvailableObserver() {} |
| 28 | 32 |
| 29 MOCK_METHOD3(Observe, void( | 33 MOCK_METHOD3(Observe, void( |
| 30 NotificationType type, | 34 NotificationType type, |
| 31 const NotificationSource& source, | 35 const NotificationSource& source, |
| 32 const NotificationDetails& details)); | 36 const NotificationDetails& details)); |
| 33 | 37 |
| 34 private: | 38 private: |
| 35 DISALLOW_COPY_AND_ASSIGN(MockTokenAvailableObserver); | 39 DISALLOW_COPY_AND_ASSIGN(MockTokenAvailableObserver); |
| 36 }; | 40 }; |
| 37 | 41 |
| 38 class DeviceTokenFetcherTest : public testing::Test { | 42 class DeviceTokenFetcherTest : public testing::Test { |
| 39 protected: | 43 protected: |
| 40 DeviceTokenFetcherTest() | 44 DeviceTokenFetcherTest() |
| 41 : ui_thread_(BrowserThread::UI, &loop_), | 45 : ui_thread_(BrowserThread::UI, &loop_), |
| 42 file_thread_(BrowserThread::FILE, &loop_) { | 46 file_thread_(BrowserThread::FILE, &loop_) { |
| 43 EXPECT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); | 47 EXPECT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); |
| 44 fetcher_ = NewTestFetcher(temp_user_data_dir_.path()); | 48 fetcher_ = NewTestFetcher(temp_user_data_dir_.path()); |
| 45 fetcher_->StartFetching(); | 49 fetcher_->StartFetching(); |
| 46 } | 50 } |
| 47 | 51 |
| 48 virtual void TearDown() { | 52 virtual void TearDown() { |
| 53 backend_.reset(); |
| 54 token_service_.reset(); |
| 49 loop_.RunAllPending(); | 55 loop_.RunAllPending(); |
| 50 } | 56 } |
| 51 | 57 |
| 52 void SimulateSuccessfulLoginAndRunPending() { | 58 void SimulateSuccessfulLoginAndRunPending() { |
| 53 loop_.RunAllPending(); | 59 loop_.RunAllPending(); |
| 54 SimulateSuccessfulLogin(); | 60 token_service_->IssueAuthTokenForTest( |
| 61 GaiaConstants::kDeviceManagementService, kTestToken); |
| 55 loop_.RunAllPending(); | 62 loop_.RunAllPending(); |
| 56 } | 63 } |
| 57 | 64 |
| 58 DeviceTokenFetcher* NewTestFetcher(const FilePath& token_dir) { | 65 DeviceTokenFetcher* NewTestFetcher(const FilePath& token_dir) { |
| 66 token_service_.reset(new TokenService); |
| 59 backend_.reset(new MockDeviceManagementBackend()); | 67 backend_.reset(new MockDeviceManagementBackend()); |
| 60 return new DeviceTokenFetcher( | 68 return new DeviceTokenFetcher( |
| 61 backend_.get(), | 69 backend_.get(), |
| 70 token_service_.get(), |
| 62 token_dir.Append(FILE_PATH_LITERAL("test-token-file.txt"))); | 71 token_dir.Append(FILE_PATH_LITERAL("test-token-file.txt"))); |
| 63 } | 72 } |
| 64 | 73 |
| 65 static void GetDeviceTokenPath(const DeviceTokenFetcher* fetcher, | 74 static void GetDeviceTokenPath(const DeviceTokenFetcher* fetcher, |
| 66 FilePath* path) { | 75 FilePath* path) { |
| 67 fetcher->GetDeviceTokenPath(path); | 76 fetcher->GetDeviceTokenPath(path); |
| 68 } | 77 } |
| 69 | 78 |
| 70 const std::string& device_id(const DeviceTokenFetcher* fetcher) { | 79 const std::string& device_id(const DeviceTokenFetcher* fetcher) { |
| 71 return fetcher->device_id_; | 80 return fetcher->device_id_; |
| 72 } | 81 } |
| 73 | 82 |
| 74 MessageLoop loop_; | 83 MessageLoop loop_; |
| 84 scoped_ptr<TokenService> token_service_; |
| 75 scoped_ptr<MockDeviceManagementBackend> backend_; | 85 scoped_ptr<MockDeviceManagementBackend> backend_; |
| 76 ScopedTempDir temp_user_data_dir_; | 86 ScopedTempDir temp_user_data_dir_; |
| 77 scoped_refptr<DeviceTokenFetcher> fetcher_; | 87 scoped_refptr<DeviceTokenFetcher> fetcher_; |
| 78 | 88 |
| 79 private: | 89 private: |
| 80 BrowserThread ui_thread_; | 90 BrowserThread ui_thread_; |
| 81 BrowserThread file_thread_; | 91 BrowserThread file_thread_; |
| 82 }; | 92 }; |
| 83 | 93 |
| 84 TEST_F(DeviceTokenFetcherTest, IsPending) { | 94 TEST_F(DeviceTokenFetcherTest, IsPending) { |
| 85 ASSERT_TRUE(fetcher_->IsTokenPending()); | 95 ASSERT_TRUE(fetcher_->IsTokenPending()); |
| 86 backend_->AllShouldSucceed(); | 96 backend_->AllShouldSucceed(); |
| 87 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 97 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
| 88 SimulateSuccessfulLoginAndRunPending(); | 98 SimulateSuccessfulLoginAndRunPending(); |
| 89 ASSERT_FALSE(fetcher_->IsTokenPending()); | 99 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 90 } | 100 } |
| 91 | 101 |
| 92 TEST_F(DeviceTokenFetcherTest, StoreAndLoad) { | 102 TEST_F(DeviceTokenFetcherTest, StoreAndLoad) { |
| 93 backend_->AllShouldSucceed(); | 103 backend_->AllShouldSucceed(); |
| 94 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 104 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
| 95 SimulateSuccessfulLoginAndRunPending(); | 105 SimulateSuccessfulLoginAndRunPending(); |
| 96 ASSERT_FALSE(fetcher_->IsTokenPending()); | 106 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 97 std::string device_token = fetcher_->GetDeviceToken(); | 107 std::string device_token = fetcher_->GetDeviceToken(); |
| 98 std::string device_id = fetcher_->GetDeviceID(); | 108 std::string device_id = fetcher_->GetDeviceID(); |
| 99 ASSERT_NE("", device_id); | 109 ASSERT_NE("", device_id); |
| 100 | 110 |
| 101 FilePath token_path; | 111 FilePath token_path; |
| 102 GetDeviceTokenPath(fetcher_, &token_path); | 112 GetDeviceTokenPath(fetcher_, &token_path); |
| 103 scoped_refptr<DeviceTokenFetcher> fetcher2( | 113 scoped_refptr<DeviceTokenFetcher> fetcher2( |
| 104 new DeviceTokenFetcher(backend_.get(), token_path)); | 114 new DeviceTokenFetcher(backend_.get(), token_service_.get(), token_path)); |
| 105 fetcher2->StartFetching(); | 115 fetcher2->StartFetching(); |
| 106 loop_.RunAllPending(); | 116 loop_.RunAllPending(); |
| 107 ASSERT_EQ(device_id, fetcher2->GetDeviceID()); | 117 ASSERT_EQ(device_id, fetcher2->GetDeviceID()); |
| 108 ASSERT_EQ(device_token, fetcher2->GetDeviceToken()); | 118 ASSERT_EQ(device_token, fetcher2->GetDeviceToken()); |
| 109 } | 119 } |
| 110 | 120 |
| 111 TEST_F(DeviceTokenFetcherTest, SimpleFetchSingleLogin) { | 121 TEST_F(DeviceTokenFetcherTest, SimpleFetchSingleLogin) { |
| 112 backend_->AllShouldSucceed(); | 122 backend_->AllShouldSucceed(); |
| 113 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 123 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
| 114 SimulateSuccessfulLoginAndRunPending(); | 124 SimulateSuccessfulLoginAndRunPending(); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 SimulateSuccessfulLoginAndRunPending(); | 186 SimulateSuccessfulLoginAndRunPending(); |
| 177 ASSERT_FALSE(fetcher_->IsTokenPending()); | 187 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 178 ASSERT_EQ("", fetcher_->GetDeviceToken()); | 188 ASSERT_EQ("", fetcher_->GetDeviceToken()); |
| 179 ASSERT_EQ("", device_id(fetcher_)); | 189 ASSERT_EQ("", device_id(fetcher_)); |
| 180 FilePath token_path; | 190 FilePath token_path; |
| 181 GetDeviceTokenPath(fetcher_, &token_path); | 191 GetDeviceTokenPath(fetcher_, &token_path); |
| 182 ASSERT_FALSE(file_util::PathExists(token_path)); | 192 ASSERT_FALSE(file_util::PathExists(token_path)); |
| 183 } | 193 } |
| 184 | 194 |
| 185 } // namespace policy | 195 } // namespace policy |
| OLD | NEW |