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