summaryrefslogtreecommitdiffstats
path: root/chrome/browser/local_discovery/cloud_print_account_manager_unittest.cc
blob: dcbc34e8b3c251e1ebd973e002f28ab79d433162 (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
// Copyright 2013 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 "base/message_loop/message_loop.h"
#include "chrome/browser/local_discovery/cloud_print_account_manager.h"
#include "net/http/http_request_headers.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace local_discovery {

namespace {

const char kSampleResponse[] = "{"
    "   \"success\": true,"
    "   \"xsrf_token\": \"sample\","
    "   \"request\" : { "
    "     \"users\": [\"first@gmail.com\", \"second@gmail.com\"]"
    "   } "
    "}";

const char kSampleResponseFailure[] = "{"
    "   \"success\": false,"
    "}";

class MockCallback {
 public:
  MOCK_METHOD2(CloudPrintAccountsResolved, void(
      const std::vector<std::string>& account,
      const std::string& xsrf_token));
};

class CloudPrintAccountManagerTest : public testing::Test {
 public:
  CloudPrintAccountManagerTest()
      : request_context_(
            new net::TestURLRequestContextGetter(
                base::MessageLoopProxy::current())),
        account_manager_(
            request_context_.get(),
            "https://www.google.com/cloudprint",
            1,
            base::Bind(
                &MockCallback::CloudPrintAccountsResolved,
                base::Unretained(&mock_callback_))) {
  }

  virtual ~CloudPrintAccountManagerTest() {
  }

 protected:
  base::MessageLoop message_loop_;
  scoped_refptr<net::TestURLRequestContextGetter> request_context_;
  net::TestURLFetcherFactory fetcher_factory_;
  MockCallback mock_callback_;
  CloudPrintAccountManager account_manager_;
};

TEST_F(CloudPrintAccountManagerTest, Success) {
  account_manager_.Start();
  net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);

  net::HttpRequestHeaders headers;
  std::string proxy;
  fetcher->GetExtraRequestHeaders(&headers);
  EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy));
  EXPECT_EQ("Chrome", proxy);
  EXPECT_EQ(GURL("https://www.google.com/cloudprint/list?proxy=none&user=1"),
            fetcher->GetOriginalURL());

  fetcher->SetResponseString(kSampleResponse);
  fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
                                            net::OK));
  fetcher->set_response_code(200);

  std::vector<std::string> expected_users;
  expected_users.push_back("first@gmail.com");
  expected_users.push_back("second@gmail.com");

  EXPECT_CALL(mock_callback_,
              CloudPrintAccountsResolved(expected_users, "sample"));
  fetcher->delegate()->OnURLFetchComplete(fetcher);
}

TEST_F(CloudPrintAccountManagerTest, FailureJSON) {
  account_manager_.Start();
  net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);

  fetcher->SetResponseString(kSampleResponseFailure);
  fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
                                            net::OK));
  fetcher->set_response_code(200);

  EXPECT_CALL(mock_callback_,
              CloudPrintAccountsResolved(std::vector<std::string>(), ""));
  fetcher->delegate()->OnURLFetchComplete(fetcher);
}

}  // namespace

}  // namespace local_discovery