cloud_print_printer_list_unittest.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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 <set>
6
7#include "base/bind.h"
8#include "base/message_loop/message_loop.h"
9#include "chrome/browser/local_discovery/cloud_print_printer_list.h"
10#include "content/public/test/test_browser_thread.h"
11#include "google_apis/gaia/google_service_auth_error.h"
12#include "net/base/host_port_pair.h"
13#include "net/base/net_errors.h"
14#include "net/http/http_request_headers.h"
15#include "net/url_request/test_url_fetcher_factory.h"
16#include "net/url_request/url_fetcher_impl.h"
17#include "net/url_request/url_request_test_util.h"
18#include "testing/gmock/include/gmock/gmock.h"
19#include "testing/gtest/include/gtest/gtest.h"
20#include "url/gurl.h"
21
22using testing::Mock;
23using testing::NiceMock;
24using testing::StrictMock;
25
26namespace local_discovery {
27
28namespace {
29
30const char kSampleSuccessResponseOAuth[] = "{"
31    "   \"success\": true,"
32    "   \"printers\": ["
33    "     {\"id\" : \"someID\","
34    "      \"displayName\": \"someDisplayName\","
35    "      \"description\": \"someDescription\"}"
36    "    ]"
37    "}";
38
39class TestOAuth2TokenService : public OAuth2TokenService {
40 public:
41  explicit TestOAuth2TokenService(net::URLRequestContextGetter* request_context)
42      : request_context_(request_context) {
43  }
44 protected:
45  virtual std::string GetRefreshToken(const std::string& account_id) OVERRIDE {
46    return "SampleToken";
47  }
48
49  virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE {
50    return request_context_.get();
51  }
52
53 private:
54  scoped_refptr<net::URLRequestContextGetter> request_context_;
55};
56
57class MockDelegate : public CloudPrintPrinterList::Delegate {
58 public:
59  MOCK_METHOD0(OnCloudPrintPrinterListUnavailable, void());
60  MOCK_METHOD0(OnCloudPrintPrinterListReady, void());
61};
62
63class CloudPrintPrinterListTest : public testing::Test {
64 public:
65  CloudPrintPrinterListTest()
66      : ui_thread_(content::BrowserThread::UI,
67                   &loop_),
68        request_context_(new net::TestURLRequestContextGetter(
69            base::MessageLoopProxy::current())),
70        token_service_(request_context_.get()) {
71    ui_thread_.Stop();  // HACK: Fake being on the UI thread
72
73    printer_list_.reset(
74        new CloudPrintPrinterList(request_context_.get(),
75                                  "http://SoMeUrL.com/cloudprint",
76                                  &token_service_,
77                                  "account_id",
78                                  &delegate_));
79
80    fallback_fetcher_factory_.reset(new net::TestURLFetcherFactory());
81    net::URLFetcherImpl::set_factory(NULL);
82    fetcher_factory_.reset(new net::FakeURLFetcherFactory(
83        fallback_fetcher_factory_.get()));
84  }
85
86  virtual ~CloudPrintPrinterListTest() {
87    fetcher_factory_.reset();
88    net::URLFetcherImpl::set_factory(fallback_fetcher_factory_.get());
89    fallback_fetcher_factory_.reset();
90  }
91
92 protected:
93  base::MessageLoopForUI loop_;
94  content::TestBrowserThread ui_thread_;
95  scoped_refptr<net::TestURLRequestContextGetter> request_context_;
96  // Use a test factory as a fallback so we don't have to deal with OAuth2
97  // requests.
98  scoped_ptr<net::TestURLFetcherFactory> fallback_fetcher_factory_;
99  scoped_ptr<net::FakeURLFetcherFactory> fetcher_factory_;
100  TestOAuth2TokenService token_service_;
101  StrictMock<MockDelegate> delegate_;
102  scoped_ptr<CloudPrintPrinterList> printer_list_;
103};
104
105TEST_F(CloudPrintPrinterListTest, SuccessOAuth2) {
106  fetcher_factory_->SetFakeResponse(
107      GURL("http://SoMeUrL.com/cloudprint/search"),
108      kSampleSuccessResponseOAuth,
109      true);
110
111  CloudPrintBaseApiFlow* cloudprint_flow =
112      printer_list_->GetOAuth2ApiFlowForTests();
113
114  printer_list_->Start();
115
116  cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
117
118  EXPECT_CALL(delegate_, OnCloudPrintPrinterListReady());
119
120  base::MessageLoop::current()->RunUntilIdle();
121
122  Mock::VerifyAndClear(&delegate_);
123
124  std::set<std::string> ids_found;
125  std::set<std::string> ids_expected;
126
127  ids_expected.insert("someID");
128
129  int length = 0;
130  for (CloudPrintPrinterList::iterator i = printer_list_->begin();
131       i != printer_list_->end(); i++, length++) {
132    ids_found.insert(i->id);
133  }
134
135  EXPECT_EQ(ids_expected, ids_found);
136  EXPECT_EQ(1, length);
137
138  const CloudPrintPrinterList::PrinterDetails* found =
139      printer_list_->GetDetailsFor("someID");
140  ASSERT_TRUE(found != NULL);
141  EXPECT_EQ("someID", found->id);
142  EXPECT_EQ("someDisplayName", found->display_name);
143  EXPECT_EQ("someDescription", found->description);
144}
145
146}  // namespace
147
148}  // namespace local_discovery
149