- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / local_discovery / cloud_print_account_manager_unittest.cc
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.
4
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"
12
13 namespace local_discovery {
14
15 namespace {
16
17 const char kSampleResponse[] = "{"
18     "   \"success\": true,"
19     "   \"xsrf_token\": \"sample\","
20     "   \"request\" : { "
21     "     \"users\": [\"first@gmail.com\", \"second@gmail.com\"]"
22     "   } "
23     "}";
24
25 const char kSampleResponseFailure[] = "{"
26     "   \"success\": false,"
27     "}";
28
29 class MockCallback {
30  public:
31   MOCK_METHOD2(CloudPrintAccountsResolved, void(
32       const std::vector<std::string>& account,
33       const std::string& xsrf_token));
34 };
35
36 class CloudPrintAccountManagerTest : public testing::Test {
37  public:
38   CloudPrintAccountManagerTest()
39       : request_context_(
40             new net::TestURLRequestContextGetter(
41                 base::MessageLoopProxy::current())),
42         account_manager_(
43             request_context_.get(),
44             "https://www.google.com/cloudprint",
45             1,
46             base::Bind(
47                 &MockCallback::CloudPrintAccountsResolved,
48                 base::Unretained(&mock_callback_))) {
49   }
50
51   virtual ~CloudPrintAccountManagerTest() {
52   }
53
54  protected:
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_;
60 };
61
62 TEST_F(CloudPrintAccountManagerTest, Success) {
63   account_manager_.Start();
64   net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
65
66   net::HttpRequestHeaders headers;
67   std::string proxy;
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());
73
74   fetcher->SetResponseString(kSampleResponse);
75   fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
76                                             net::OK));
77   fetcher->set_response_code(200);
78
79   std::vector<std::string> expected_users;
80   expected_users.push_back("first@gmail.com");
81   expected_users.push_back("second@gmail.com");
82
83   EXPECT_CALL(mock_callback_,
84               CloudPrintAccountsResolved(expected_users, "sample"));
85   fetcher->delegate()->OnURLFetchComplete(fetcher);
86 }
87
88 TEST_F(CloudPrintAccountManagerTest, FailureJSON) {
89   account_manager_.Start();
90   net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
91
92   fetcher->SetResponseString(kSampleResponseFailure);
93   fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
94                                             net::OK));
95   fetcher->set_response_code(200);
96
97   EXPECT_CALL(mock_callback_,
98               CloudPrintAccountsResolved(std::vector<std::string>(), ""));
99   fetcher->delegate()->OnURLFetchComplete(fetcher);
100 }
101
102 }  // namespace
103
104 }  // namespace local_discovery