OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This file defines a unit test for the profile's token service. | 5 // This file defines a unit test for the profile's token service. |
6 | 6 |
7 #include "chrome/browser/signin/token_service_unittest.h" | 7 #include "chrome/browser/signin/token_service_unittest.h" |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
14 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | 14 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
15 #include "chrome/browser/signin/fake_signin_manager.h" | 15 #include "chrome/browser/signin/fake_signin_manager.h" |
16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
17 #include "chrome/browser/signin/signin_manager_factory.h" | 17 #include "chrome/browser/signin/signin_manager_factory.h" |
18 #include "chrome/browser/signin/token_service_factory.h" | 18 #include "chrome/browser/signin/token_service_factory.h" |
19 #include "chrome/browser/webdata/token_web_data.h" | 19 #include "chrome/browser/webdata/token_web_data.h" |
20 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
21 #include "components/webdata/encryptor/encryptor.h" | 21 #include "components/webdata/encryptor/encryptor.h" |
22 #include "google_apis/gaia/gaia_constants.h" | 22 #include "google_apis/gaia/gaia_constants.h" |
23 #include "google_apis/gaia/mock_url_fetcher_factory.h" | 23 #include "google_apis/gaia/mock_url_fetcher_factory.h" |
24 #include "net/url_request/test_url_fetcher_factory.h" | 24 #include "net/url_request/test_url_fetcher_factory.h" |
25 | 25 |
26 TokenAvailableTracker::TokenAvailableTracker() {} | |
27 | |
28 TokenAvailableTracker::~TokenAvailableTracker() {} | |
29 | |
30 void TokenAvailableTracker::Observe( | |
31 int type, | |
32 const content::NotificationSource& source, | |
33 const content::NotificationDetails& details) { | |
34 TestNotificationTracker::Observe(type, source, details); | |
35 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { | |
36 content::Details<const TokenService::TokenAvailableDetails> full = details; | |
37 details_ = *full.ptr(); | |
38 } | |
39 } | |
40 | |
41 TokenFailedTracker::TokenFailedTracker() {} | |
42 | |
43 TokenFailedTracker::~TokenFailedTracker() {} | |
44 | |
45 void TokenFailedTracker::Observe(int type, | |
46 const content::NotificationSource& source, | |
47 const content::NotificationDetails& details) { | |
48 TestNotificationTracker::Observe(type, source, details); | |
49 if (type == chrome::NOTIFICATION_TOKEN_REQUEST_FAILED) { | |
50 content::Details<const TokenService::TokenRequestFailedDetails> full = | |
51 details; | |
52 details_ = *full.ptr(); | |
53 } | |
54 } | |
55 | |
56 TokenServiceTestHarness::TokenServiceTestHarness() | 26 TokenServiceTestHarness::TokenServiceTestHarness() |
57 : signin_manager_(NULL), service_(NULL) { | 27 : signin_manager_(NULL), service_(NULL) {} |
58 } | |
59 | 28 |
60 TokenServiceTestHarness::~TokenServiceTestHarness() {} | 29 TokenServiceTestHarness::~TokenServiceTestHarness() {} |
61 | 30 |
62 void TokenServiceTestHarness::SetUp() { | 31 void TokenServiceTestHarness::SetUp() { |
63 #if defined(OS_MACOSX) | 32 #if defined(OS_MACOSX) |
64 Encryptor::UseMockKeychain(true); | 33 Encryptor::UseMockKeychain(true); |
65 #endif | 34 #endif |
66 credentials_.sid = "sid"; | 35 credentials_.sid = "sid"; |
67 credentials_.lsid = "lsid"; | 36 credentials_.lsid = "lsid"; |
68 credentials_.token = "token"; | 37 credentials_.token = "token"; |
69 credentials_.data = "data"; | 38 credentials_.data = "data"; |
70 oauth_token_ = "oauth"; | 39 oauth_token_ = "oauth"; |
71 oauth_secret_ = "secret"; | 40 oauth_secret_ = "secret"; |
72 | 41 |
73 profile_ = CreateProfile().Pass(); | 42 profile_ = CreateProfile().Pass(); |
74 | 43 |
75 profile_->CreateWebDataService(); | 44 profile_->CreateWebDataService(); |
76 | 45 |
77 // Force the loading of the WebDataService. | 46 // Force the loading of the WebDataService. |
78 TokenWebData::FromBrowserContext(profile_.get()); | 47 TokenWebData::FromBrowserContext(profile_.get()); |
79 base::RunLoop().RunUntilIdle(); | 48 base::RunLoop().RunUntilIdle(); |
80 | 49 |
81 service_ = TokenServiceFactory::GetForProfile(profile_.get()); | 50 service_ = TokenServiceFactory::GetForProfile(profile_.get()); |
82 | 51 |
83 success_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_AVAILABLE, | |
84 content::Source<TokenService>(service_)); | |
85 failure_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, | |
86 content::Source<TokenService>(service_)); | |
87 | |
88 service()->Initialize("test", profile_.get()); | 52 service()->Initialize("test", profile_.get()); |
89 } | 53 } |
90 | 54 |
91 scoped_ptr<TestingProfile> TokenServiceTestHarness::CreateProfile() { | 55 scoped_ptr<TestingProfile> TokenServiceTestHarness::CreateProfile() { |
92 TestingProfile::Builder builder; | 56 TestingProfile::Builder builder; |
93 builder.AddTestingFactory( | 57 builder.AddTestingFactory( |
94 ProfileOAuth2TokenServiceFactory::GetInstance(), | 58 ProfileOAuth2TokenServiceFactory::GetInstance(), |
95 FakeProfileOAuth2TokenService::Build); | 59 FakeProfileOAuth2TokenService::Build); |
96 return builder.Build(); | 60 return builder.Build(); |
97 } | 61 } |
98 | 62 |
99 void TokenServiceTestHarness::CreateSigninManager(const std::string& username) { | 63 void TokenServiceTestHarness::CreateSigninManager(const std::string& username) { |
100 signin_manager_ = | 64 signin_manager_ = |
101 static_cast<FakeSigninManagerBase*>( | 65 static_cast<FakeSigninManagerBase*>( |
102 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 66 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
103 profile(), FakeSigninManagerBase::Build)); | 67 profile(), FakeSigninManagerBase::Build)); |
104 signin_manager_->Initialize(profile(), NULL); | 68 signin_manager_->Initialize(profile(), NULL); |
105 signin_manager_->SetAuthenticatedUsername(username); | 69 signin_manager_->SetAuthenticatedUsername(username); |
106 } | 70 } |
107 | 71 |
108 void TokenServiceTestHarness::TearDown() { | 72 void TokenServiceTestHarness::TearDown() { |
109 // You have to destroy the profile before the threads are shut down. | 73 // You have to destroy the profile before the threads are shut down. |
110 profile_.reset(); | 74 profile_.reset(); |
111 } | 75 } |
112 | 76 |
113 void TokenServiceTestHarness::UpdateCredentialsOnService() { | 77 void TokenServiceTestHarness::UpdateCredentialsOnService() { |
114 service()->UpdateCredentials(credentials_); | 78 service()->UpdateCredentials(credentials_); |
115 } | 79 } |
OLD | NEW |