summaryrefslogtreecommitdiffstats
path: root/chrome/browser/services/gcm/gcm_profile_service_test_helper.cc
blob: 2379482220c6fab2a9d4ef7046dd852df28a6255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/services/gcm/gcm_profile_service_test_helper.h"

#include "base/bind.h"
#include "base/prefs/pref_service.h"
#include "base/run_loop.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/chrome_signin_client_factory.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"

namespace gcm {

Waiter::Waiter() {
}

Waiter::~Waiter() {
}

void Waiter::WaitUntilCompleted() {
  run_loop_.reset(new base::RunLoop);
  run_loop_->Run();
}

void Waiter::SignalCompleted() {
  if (run_loop_ && run_loop_->running())
    run_loop_->Quit();
}

void Waiter::PumpUILoop() {
  base::MessageLoop::current()->RunUntilIdle();
}

void Waiter::PumpIOLoop() {
  content::BrowserThread::PostTask(
      content::BrowserThread::IO,
      FROM_HERE,
      base::Bind(&Waiter::OnIOLoopPump, base::Unretained(this)));

  WaitUntilCompleted();
}

void Waiter::PumpIOLoopCompleted() {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));

  SignalCompleted();
}

void Waiter::OnIOLoopPump() {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));

  content::BrowserThread::PostTask(
      content::BrowserThread::IO,
      FROM_HERE,
      base::Bind(&Waiter::OnIOLoopPumpCompleted, base::Unretained(this)));
}

void Waiter::OnIOLoopPumpCompleted() {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));

  content::BrowserThread::PostTask(
      content::BrowserThread::UI,
      FROM_HERE,
      base::Bind(&Waiter::PumpIOLoopCompleted,
                 base::Unretained(this)));
}

// static
KeyedService* FakeSigninManager::Build(content::BrowserContext* context) {
  return new FakeSigninManager(static_cast<Profile*>(context));
}

FakeSigninManager::FakeSigninManager(Profile* profile)
#if defined(OS_CHROMEOS)
    : SigninManagerBase(
          ChromeSigninClientFactory::GetInstance()->GetForProfile(profile)),
#else
    : SigninManager(
          ChromeSigninClientFactory::GetInstance()->GetForProfile(profile),
          ProfileOAuth2TokenServiceFactory::GetForProfile(profile)),
#endif
      profile_(profile) {
  Initialize(NULL);
}

FakeSigninManager::~FakeSigninManager() {
}

void FakeSigninManager::SignIn(const std::string& username) {
  SetAuthenticatedUsername(username);
  FOR_EACH_OBSERVER(Observer,
                    observer_list_,
                    GoogleSigninSucceeded(username, std::string()));
}

void FakeSigninManager::SignOut() {
  std::string username = GetAuthenticatedUsername();
  clear_authenticated_username();
  profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername);
  FOR_EACH_OBSERVER(Observer, observer_list_, GoogleSignedOut(username));
}

FakeGCMClientFactory::FakeGCMClientFactory(
    GCMClientMock::LoadingDelay gcm_client_loading_delay)
    : gcm_client_loading_delay_(gcm_client_loading_delay) {
}

FakeGCMClientFactory::~FakeGCMClientFactory() {
}

scoped_ptr<GCMClient> FakeGCMClientFactory::BuildInstance() {
  return scoped_ptr<GCMClient>(new GCMClientMock(gcm_client_loading_delay_));
}

}  // namespace gcm