1 // Copyright 2013 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 "base/message_loop/message_loop.h"
6 #include "chrome/browser/local_discovery/cloud_print_account_manager.h"
7 #include "net/http/http_request_headers.h"
8 #include "net/url_request/test_url_fetcher_factory.h"
9 #include "net/url_request/url_request_test_util.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace local_discovery
{
17 const char kSampleResponse
[] = "{"
19 " \"xsrf_token\": \"sample\","
21 " \"users\": [\"first@gmail.com\", \"second@gmail.com\"]"
25 const char kSampleResponseFailure
[] = "{"
26 " \"success\": false,"
31 MOCK_METHOD2(CloudPrintAccountsResolved
, void(
32 const std::vector
<std::string
>& account
,
33 const std::string
& xsrf_token
));
36 class CloudPrintAccountManagerTest
: public testing::Test
{
38 CloudPrintAccountManagerTest()
40 new net::TestURLRequestContextGetter(
41 base::MessageLoopProxy::current())),
43 request_context_
.get(),
44 "https://www.google.com/cloudprint",
47 &MockCallback::CloudPrintAccountsResolved
,
48 base::Unretained(&mock_callback_
))) {
51 virtual ~CloudPrintAccountManagerTest() {
55 base::MessageLoop message_loop_
;
56 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_
;
57 net::TestURLFetcherFactory fetcher_factory_
;
58 MockCallback mock_callback_
;
59 CloudPrintAccountManager account_manager_
;
62 TEST_F(CloudPrintAccountManagerTest
, Success
) {
63 account_manager_
.Start();
64 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
66 net::HttpRequestHeaders headers
;
68 fetcher
->GetExtraRequestHeaders(&headers
);
69 EXPECT_TRUE(headers
.GetHeader("X-Cloudprint-Proxy", &proxy
));
70 EXPECT_EQ("Chrome", proxy
);
71 EXPECT_EQ(GURL("https://www.google.com/cloudprint/list?proxy=none&user=1"),
72 fetcher
->GetOriginalURL());
74 fetcher
->SetResponseString(kSampleResponse
);
75 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
77 fetcher
->set_response_code(200);
79 std::vector
<std::string
> expected_users
;
80 expected_users
.push_back("first@gmail.com");
81 expected_users
.push_back("second@gmail.com");
83 EXPECT_CALL(mock_callback_
,
84 CloudPrintAccountsResolved(expected_users
, "sample"));
85 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
88 TEST_F(CloudPrintAccountManagerTest
, FailureJSON
) {
89 account_manager_
.Start();
90 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
92 fetcher
->SetResponseString(kSampleResponseFailure
);
93 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
95 fetcher
->set_response_code(200);
97 EXPECT_CALL(mock_callback_
,
98 CloudPrintAccountsResolved(std::vector
<std::string
>(), ""));
99 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
104 } // namespace local_discovery