OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "chrome/browser/services/gcm/gcm_profile_service_test_helper.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/prefs/pref_service.h" | |
9 #include "base/run_loop.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/signin/chrome_signin_client_factory.h" | |
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
13 #include "chrome/common/pref_names.h" | |
14 #include "content/public/browser/browser_thread.h" | |
15 | |
16 namespace gcm { | |
17 | |
18 Waiter::Waiter() { | |
19 } | |
20 | |
21 Waiter::~Waiter() { | |
22 } | |
23 | |
24 void Waiter::WaitUntilCompleted() { | |
25 run_loop_.reset(new base::RunLoop); | |
26 run_loop_->Run(); | |
27 } | |
28 | |
29 void Waiter::SignalCompleted() { | |
30 if (run_loop_ && run_loop_->running()) | |
31 run_loop_->Quit(); | |
32 } | |
33 | |
34 void Waiter::PumpUILoop() { | |
35 base::MessageLoop::current()->RunUntilIdle(); | |
36 } | |
37 | |
38 void Waiter::PumpIOLoop() { | |
39 content::BrowserThread::PostTask( | |
40 content::BrowserThread::IO, | |
41 FROM_HERE, | |
42 base::Bind(&Waiter::OnIOLoopPump, base::Unretained(this))); | |
43 | |
44 WaitUntilCompleted(); | |
45 } | |
46 | |
47 void Waiter::PumpIOLoopCompleted() { | |
48 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
49 | |
50 SignalCompleted(); | |
51 } | |
52 | |
53 void Waiter::OnIOLoopPump() { | |
54 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
55 | |
56 content::BrowserThread::PostTask( | |
57 content::BrowserThread::IO, | |
58 FROM_HERE, | |
59 base::Bind(&Waiter::OnIOLoopPumpCompleted, base::Unretained(this))); | |
60 } | |
61 | |
62 void Waiter::OnIOLoopPumpCompleted() { | |
63 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
64 | |
65 content::BrowserThread::PostTask( | |
66 content::BrowserThread::UI, | |
67 FROM_HERE, | |
68 base::Bind(&Waiter::PumpIOLoopCompleted, | |
69 base::Unretained(this))); | |
70 } | |
71 | |
72 // static | |
73 KeyedService* FakeSigninManager::Build(content::BrowserContext* context) { | |
74 return new FakeSigninManager(static_cast<Profile*>(context)); | |
75 } | |
76 | |
77 FakeSigninManager::FakeSigninManager(Profile* profile) | |
78 #if defined(OS_CHROMEOS) | |
79 : SigninManagerBase( | |
80 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile)), | |
81 #else | |
82 : SigninManager( | |
83 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile), | |
84 ProfileOAuth2TokenServiceFactory::GetForProfile(profile)), | |
85 #endif | |
86 profile_(profile) { | |
87 Initialize(NULL); | |
88 } | |
89 | |
90 FakeSigninManager::~FakeSigninManager() { | |
91 } | |
92 | |
93 void FakeSigninManager::SignIn(const std::string& username) { | |
94 SetAuthenticatedUsername(username); | |
95 FOR_EACH_OBSERVER(Observer, | |
96 observer_list_, | |
97 GoogleSigninSucceeded(username, std::string())); | |
98 } | |
99 | |
100 void FakeSigninManager::SignOut() { | |
101 std::string username = GetAuthenticatedUsername(); | |
102 clear_authenticated_username(); | |
103 profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); | |
104 FOR_EACH_OBSERVER(Observer, observer_list_, GoogleSignedOut(username)); | |
105 } | |
106 | |
107 FakeGCMClientFactory::FakeGCMClientFactory( | |
108 GCMClientMock::LoadingDelay gcm_client_loading_delay) | |
109 : gcm_client_loading_delay_(gcm_client_loading_delay) { | |
110 } | |
111 | |
112 FakeGCMClientFactory::~FakeGCMClientFactory() { | |
113 } | |
114 | |
115 scoped_ptr<GCMClient> FakeGCMClientFactory::BuildInstance() { | |
116 return scoped_ptr<GCMClient>(new GCMClientMock(gcm_client_loading_delay_)); | |
117 } | |
118 | |
119 } // namespace gcm | |
OLD | NEW |