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