Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: chrome/browser/policy/device_token_fetcher_unittest.cc

Issue 4121003: Implement device token fetcher (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: final feedback Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/file_util.h"
6 #include "base/message_loop.h"
7 #include "base/scoped_temp_dir.h"
8 #include "chrome/browser/browser_thread.h"
9 #include "chrome/browser/net/gaia/token_service.h"
10 #include "chrome/browser/policy/device_token_fetcher.h"
11 #include "chrome/browser/policy/mock_device_management_backend.h"
12 #include "chrome/common/net/gaia/gaia_constants.h"
13 #include "chrome/common/notification_details.h"
14 #include "chrome/common/notification_observer.h"
15 #include "chrome/common/notification_service.h"
16 #include "chrome/common/notification_source.h"
17 #include "chrome/common/notification_type.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace policy {
22
23 using testing::_;
24 using testing::Mock;
25
26 class MockDeviceTokenPathProvider
27 : public StoredDeviceTokenPathProvider {
28 public:
29 MockDeviceTokenPathProvider() {
30 EXPECT_TRUE(temp_user_data_dir_.CreateUniqueTempDir());
31 }
32
33 virtual ~MockDeviceTokenPathProvider() {}
34
35 virtual bool GetPath(FilePath* path) const {
36 if (!file_util::PathExists(temp_user_data_dir_.path()))
37 return false;
38 *path = temp_user_data_dir_.path().Append(
39 FILE_PATH_LITERAL("temp_token_file"));
40 return true;
41 }
42
43 private:
44 ScopedTempDir temp_user_data_dir_;
45
46 DISALLOW_COPY_AND_ASSIGN(MockDeviceTokenPathProvider);
47 };
48
49 class MockTokenAvailableObserver : public NotificationObserver {
50 public:
51 MockTokenAvailableObserver() {}
52 virtual ~MockTokenAvailableObserver() {}
53
54 MOCK_METHOD3(Observe, void(
55 NotificationType type,
56 const NotificationSource& source,
57 const NotificationDetails& details));
58
59 private:
60 DISALLOW_COPY_AND_ASSIGN(MockTokenAvailableObserver);
61 };
62
63 class DeviceTokenFetcherTest : public testing::Test {
64 protected:
65 DeviceTokenFetcherTest() {
66 backend_ = new MockDeviceManagementBackend();
67 path_provider_ = new MockDeviceTokenPathProvider();
68 fetcher_.reset(new DeviceTokenFetcher(backend_, path_provider_));
69 fetcher_->StartFetching();
70 }
71
72 virtual void SetUp() {
73 ui_thread_.reset(new BrowserThread(BrowserThread::UI, &loop_));
74 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &loop_));
75 }
76
77 virtual void TearDown() {
78 loop_.RunAllPending();
79 }
80
81 void SimulateSuccessfulLogin() {
82 const std::string service(GaiaConstants::kDeviceManagementService);
83 const std::string auth_token(kFakeGaiaAuthToken);
84 const Source<TokenService> source(NULL);
85 TokenService::TokenAvailableDetails details(service, auth_token);
86 NotificationService::current()->Notify(
87 NotificationType::TOKEN_AVAILABLE,
88 source,
89 Details<const TokenService::TokenAvailableDetails>(&details));
90 loop_.RunAllPending();
91 }
92
93 MockDeviceManagementBackend* backend_; // weak
94 MockDeviceTokenPathProvider* path_provider_; // weak
95 scoped_ptr<DeviceTokenFetcher> fetcher_;
96
97 private:
98 MessageLoop loop_;
99 scoped_ptr<BrowserThread> ui_thread_;
100 scoped_ptr<BrowserThread> file_thread_;
101
102 static const char kFakeGaiaAuthToken[];
103 };
104
105 const char DeviceTokenFetcherTest::kFakeGaiaAuthToken[] = "0123456789abcdef";
106
107 TEST_F(DeviceTokenFetcherTest, IsPending) {
108 ASSERT_TRUE(fetcher_->IsTokenPending());
109 SimulateSuccessfulLogin();
110 ASSERT_FALSE(fetcher_->IsTokenPending());
111 }
112
113 TEST_F(DeviceTokenFetcherTest, SimpleFetchSingleLogin) {
114 SimulateSuccessfulLogin();
115 ASSERT_FALSE(fetcher_->IsTokenPending());
116 ASSERT_TRUE(fetcher_->IsTokenValid());
117 const std::string token(fetcher_->GetDeviceToken());
118 EXPECT_NE("", token);
119 }
120
121 TEST_F(DeviceTokenFetcherTest, SimpleFetchDoubleLogin) {
122 SimulateSuccessfulLogin();
123 ASSERT_FALSE(fetcher_->IsTokenPending());
124 const std::string token(fetcher_->GetDeviceToken());
125 EXPECT_NE("", token);
126
127 SimulateSuccessfulLogin();
128 ASSERT_FALSE(fetcher_->IsTokenPending());
129 const std::string token2(fetcher_->GetDeviceToken());
130 EXPECT_NE("", token2);
131 EXPECT_EQ(token, token2);
132 }
133
134 TEST_F(DeviceTokenFetcherTest, FetchBetweenBrowserLaunchAndNotify) {
135 NotificationRegistrar registrar;
136 MockTokenAvailableObserver observer;
137 registrar.Add(&observer,
138 NotificationType::DEVICE_TOKEN_AVAILABLE,
139 NotificationService::AllSources());
140 EXPECT_CALL(observer, Observe(_, _, _)).Times(1);
141
142 SimulateSuccessfulLogin();
143 ASSERT_FALSE(fetcher_->IsTokenPending());
144 const std::string token(fetcher_->GetDeviceToken());
145 EXPECT_NE("", token);
146
147 Mock::VerifyAndClearExpectations(&observer);
148 EXPECT_CALL(observer, Observe(_, _, _)).Times(1);
149
150 // Swap out the fetchers, including copying the device management token on
151 // disk to where the new fetcher expects it.
152 backend_ = new MockDeviceManagementBackend();
153 FilePath old_path;
154 ASSERT_TRUE(path_provider_->GetPath(&old_path));
155 MockDeviceTokenPathProvider* new_provider =
156 new MockDeviceTokenPathProvider();
157 FilePath new_path;
158 ASSERT_TRUE(new_provider->GetPath(&new_path));
159 ASSERT_TRUE(file_util::Move(old_path, new_path));
160 path_provider_ = new_provider;
161 fetcher_.reset(new DeviceTokenFetcher(backend_, path_provider_));
162
163 fetcher_->StartFetching();
164 ASSERT_FALSE(fetcher_->IsTokenPending());
165 const std::string token2(fetcher_->GetDeviceToken());
166 EXPECT_NE("", token2);
167 EXPECT_EQ(token, token2);
168 }
169
170 TEST_F(DeviceTokenFetcherTest, FailedServerRequest) {
171 backend_->SetFailure(true);
172 SimulateSuccessfulLogin();
173 ASSERT_FALSE(fetcher_->IsTokenPending());
174 const std::string token(fetcher_->GetDeviceToken());
175 EXPECT_EQ("", token);
176 }
177
178 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/device_token_fetcher.cc ('k') | chrome/browser/policy/mock_device_management_backend.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698