Fix initial hiding and centering cursor on ChromeOS
[chromium-blink-merge.git] / chromeos / login / auth / mock_url_fetchers.cc
blob744631f6bd7484f5a4fd64bee00f3e3268e96d3b
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.
5 #include "chromeos/login/auth/mock_url_fetchers.h"
7 #include <errno.h>
9 #include "base/bind.h"
10 #include "base/strings/stringprintf.h"
11 #include "net/http/http_status_code.h"
12 #include "net/url_request/url_fetcher.h"
13 #include "net/url_request/url_fetcher_delegate.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace chromeos {
18 ExpectCanceledFetcher::ExpectCanceledFetcher(
19 bool success,
20 const GURL& url,
21 const std::string& results,
22 net::URLFetcher::RequestType request_type,
23 net::URLFetcherDelegate* d)
24 : net::TestURLFetcher(0, url, d), weak_factory_(this) {
27 ExpectCanceledFetcher::~ExpectCanceledFetcher() {
30 void ExpectCanceledFetcher::Start() {
31 base::MessageLoop::current()->PostDelayedTask(
32 FROM_HERE,
33 base::Bind(&ExpectCanceledFetcher::CompleteFetch,
34 weak_factory_.GetWeakPtr()),
35 base::TimeDelta::FromMilliseconds(100));
38 void ExpectCanceledFetcher::CompleteFetch() {
39 ADD_FAILURE() << "Fetch completed in ExpectCanceledFetcher!";
40 base::MessageLoop::current()->Quit(); // Allow exiting even if we mess up.
43 GotCanceledFetcher::GotCanceledFetcher(
44 bool success,
45 const GURL& url,
46 const std::string& results,
47 net::URLFetcher::RequestType request_type,
48 net::URLFetcherDelegate* d)
49 : net::TestURLFetcher(0, url, d) {
50 set_url(url);
51 set_status(net::URLRequestStatus(net::URLRequestStatus::CANCELED, 0));
52 set_response_code(net::HTTP_FORBIDDEN);
55 GotCanceledFetcher::~GotCanceledFetcher() {
58 void GotCanceledFetcher::Start() {
59 delegate()->OnURLFetchComplete(this);
62 SuccessFetcher::SuccessFetcher(bool success,
63 const GURL& url,
64 const std::string& results,
65 net::URLFetcher::RequestType request_type,
66 net::URLFetcherDelegate* d)
67 : net::TestURLFetcher(0, url, d) {
68 set_url(url);
69 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
70 set_response_code(net::HTTP_OK);
73 SuccessFetcher::~SuccessFetcher() {
76 void SuccessFetcher::Start() {
77 delegate()->OnURLFetchComplete(this);
80 FailFetcher::FailFetcher(bool success,
81 const GURL& url,
82 const std::string& results,
83 net::URLFetcher::RequestType request_type,
84 net::URLFetcherDelegate* d)
85 : net::TestURLFetcher(0, url, d) {
86 set_url(url);
87 set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, ECONNRESET));
88 set_response_code(net::HTTP_OK);
91 FailFetcher::~FailFetcher() {
94 void FailFetcher::Start() {
95 delegate()->OnURLFetchComplete(this);
98 // static
99 const char CaptchaFetcher::kCaptchaToken[] = "token";
100 // static
101 const char CaptchaFetcher::kCaptchaUrlBase[] = "http://accounts.google.com/";
102 // static
103 const char CaptchaFetcher::kCaptchaUrlFragment[] = "fragment";
104 // static
105 const char CaptchaFetcher::kUnlockUrl[] = "http://what.ever";
107 CaptchaFetcher::CaptchaFetcher(bool success,
108 const GURL& url,
109 const std::string& results,
110 net::URLFetcher::RequestType request_type,
111 net::URLFetcherDelegate* d)
112 : net::TestURLFetcher(0, url, d) {
113 set_url(url);
114 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
115 set_response_code(net::HTTP_FORBIDDEN);
116 SetResponseString(base::StringPrintf(
117 "Error=%s\n"
118 "Url=%s\n"
119 "CaptchaUrl=%s\n"
120 "CaptchaToken=%s\n",
121 "CaptchaRequired",
122 kUnlockUrl,
123 kCaptchaUrlFragment,
124 kCaptchaToken));
127 CaptchaFetcher::~CaptchaFetcher() {
130 // static
131 std::string CaptchaFetcher::GetCaptchaToken() {
132 return kCaptchaToken;
135 // static
136 std::string CaptchaFetcher::GetCaptchaUrl() {
137 return std::string(kCaptchaUrlBase).append(kCaptchaUrlFragment);
140 // static
141 std::string CaptchaFetcher::GetUnlockUrl() {
142 return kUnlockUrl;
145 void CaptchaFetcher::Start() {
146 delegate()->OnURLFetchComplete(this);
149 HostedFetcher::HostedFetcher(bool success,
150 const GURL& url,
151 const std::string& results,
152 net::URLFetcher::RequestType request_type,
153 net::URLFetcherDelegate* d)
154 : net::TestURLFetcher(0, url, d) {
155 set_url(url);
156 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
157 set_response_code(net::HTTP_OK);
160 HostedFetcher::~HostedFetcher() {
163 void HostedFetcher::Start() {
164 VLOG(1) << upload_data();
165 if (upload_data().find("HOSTED") == std::string::npos) {
166 VLOG(1) << "HostedFetcher failing request";
167 set_response_code(net::HTTP_FORBIDDEN);
168 SetResponseString("Error=BadAuthentication");
170 delegate()->OnURLFetchComplete(this);
173 } // namespace chromeos