Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / local_discovery / cloud_print_printer_list_unittest.cc
blob7ab0eb9c23e07e8bc1e22fee4f94ba8d44a7a38d
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 <set>
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/http/http_status_code.h"
16 #include "net/url_request/test_url_fetcher_factory.h"
17 #include "net/url_request/url_fetcher_impl.h"
18 #include "net/url_request/url_request_status.h"
19 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "url/gurl.h"
24 using testing::Mock;
25 using testing::NiceMock;
26 using testing::StrictMock;
28 namespace local_discovery {
30 namespace {
32 const char kSampleSuccessResponseOAuth[] = "{"
33 " \"success\": true,"
34 " \"printers\": ["
35 " {\"id\" : \"someID\","
36 " \"displayName\": \"someDisplayName\","
37 " \"description\": \"someDescription\"}"
38 " ]"
39 "}";
41 class TestOAuth2TokenService : public OAuth2TokenService {
42 public:
43 explicit TestOAuth2TokenService(net::URLRequestContextGetter* request_context)
44 : request_context_(request_context) {
46 protected:
47 virtual std::string GetRefreshToken(const std::string& account_id) OVERRIDE {
48 return "SampleToken";
51 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE {
52 return request_context_.get();
55 private:
56 scoped_refptr<net::URLRequestContextGetter> request_context_;
59 class MockDelegate : public CloudPrintPrinterList::Delegate {
60 public:
61 MOCK_METHOD0(OnCloudPrintPrinterListUnavailable, void());
62 MOCK_METHOD0(OnCloudPrintPrinterListReady, void());
65 class CloudPrintPrinterListTest : public testing::Test {
66 public:
67 CloudPrintPrinterListTest()
68 : ui_thread_(content::BrowserThread::UI,
69 &loop_),
70 request_context_(new net::TestURLRequestContextGetter(
71 base::MessageLoopProxy::current())),
72 token_service_(request_context_.get()) {
73 ui_thread_.Stop(); // HACK: Fake being on the UI thread
75 printer_list_.reset(
76 new CloudPrintPrinterList(request_context_.get(),
77 "http://SoMeUrL.com/cloudprint",
78 &token_service_,
79 "account_id",
80 &delegate_));
82 fallback_fetcher_factory_.reset(new net::TestURLFetcherFactory());
83 net::URLFetcherImpl::set_factory(NULL);
84 fetcher_factory_.reset(new net::FakeURLFetcherFactory(
85 fallback_fetcher_factory_.get()));
88 virtual ~CloudPrintPrinterListTest() {
89 fetcher_factory_.reset();
90 net::URLFetcherImpl::set_factory(fallback_fetcher_factory_.get());
91 fallback_fetcher_factory_.reset();
94 protected:
95 base::MessageLoopForUI loop_;
96 content::TestBrowserThread ui_thread_;
97 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
98 // Use a test factory as a fallback so we don't have to deal with OAuth2
99 // requests.
100 scoped_ptr<net::TestURLFetcherFactory> fallback_fetcher_factory_;
101 scoped_ptr<net::FakeURLFetcherFactory> fetcher_factory_;
102 TestOAuth2TokenService token_service_;
103 StrictMock<MockDelegate> delegate_;
104 scoped_ptr<CloudPrintPrinterList> printer_list_;
107 TEST_F(CloudPrintPrinterListTest, SuccessOAuth2) {
108 fetcher_factory_->SetFakeResponse(
109 GURL("http://SoMeUrL.com/cloudprint/search"),
110 kSampleSuccessResponseOAuth,
111 net::HTTP_OK,
112 net::URLRequestStatus::SUCCESS);
114 CloudPrintBaseApiFlow* cloudprint_flow =
115 printer_list_->GetOAuth2ApiFlowForTests();
117 printer_list_->Start();
119 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
121 EXPECT_CALL(delegate_, OnCloudPrintPrinterListReady());
123 base::MessageLoop::current()->RunUntilIdle();
125 Mock::VerifyAndClear(&delegate_);
127 std::set<std::string> ids_found;
128 std::set<std::string> ids_expected;
130 ids_expected.insert("someID");
132 int length = 0;
133 for (CloudPrintPrinterList::iterator i = printer_list_->begin();
134 i != printer_list_->end(); i++, length++) {
135 ids_found.insert(i->id);
138 EXPECT_EQ(ids_expected, ids_found);
139 EXPECT_EQ(1, length);
141 const CloudPrintPrinterList::PrinterDetails* found =
142 printer_list_->GetDetailsFor("someID");
143 ASSERT_TRUE(found != NULL);
144 EXPECT_EQ("someID", found->id);
145 EXPECT_EQ("someDisplayName", found->display_name);
146 EXPECT_EQ("someDescription", found->description);
149 } // namespace
151 } // namespace local_discovery