cloud_print_printer_list_unittest.cc revision f2477e01787aa58f445919b809d89e252beef54f
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/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" 23 24using testing::Mock; 25using testing::NiceMock; 26using testing::StrictMock; 27 28namespace local_discovery { 29 30namespace { 31 32const char kSampleSuccessResponseOAuth[] = "{" 33 " \"success\": true," 34 " \"printers\": [" 35 " {\"id\" : \"someID\"," 36 " \"displayName\": \"someDisplayName\"," 37 " \"description\": \"someDescription\"}" 38 " ]" 39 "}"; 40 41class TestOAuth2TokenService : public OAuth2TokenService { 42 public: 43 explicit TestOAuth2TokenService(net::URLRequestContextGetter* request_context) 44 : request_context_(request_context) { 45 } 46 protected: 47 virtual std::string GetRefreshToken(const std::string& account_id) OVERRIDE { 48 return "SampleToken"; 49 } 50 51 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE { 52 return request_context_.get(); 53 } 54 55 private: 56 scoped_refptr<net::URLRequestContextGetter> request_context_; 57}; 58 59class MockDelegate : public CloudPrintPrinterList::Delegate { 60 public: 61 MOCK_METHOD0(OnCloudPrintPrinterListUnavailable, void()); 62 MOCK_METHOD0(OnCloudPrintPrinterListReady, void()); 63}; 64 65class 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 74 75 printer_list_.reset( 76 new CloudPrintPrinterList(request_context_.get(), 77 "http://SoMeUrL.com/cloudprint", 78 &token_service_, 79 "account_id", 80 &delegate_)); 81 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())); 86 } 87 88 virtual ~CloudPrintPrinterListTest() { 89 fetcher_factory_.reset(); 90 net::URLFetcherImpl::set_factory(fallback_fetcher_factory_.get()); 91 fallback_fetcher_factory_.reset(); 92 } 93 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_; 105}; 106 107TEST_F(CloudPrintPrinterListTest, SuccessOAuth2) { 108 fetcher_factory_->SetFakeResponse( 109 GURL("http://SoMeUrL.com/cloudprint/search"), 110 kSampleSuccessResponseOAuth, 111 net::HTTP_OK, 112 net::URLRequestStatus::SUCCESS); 113 114 CloudPrintBaseApiFlow* cloudprint_flow = 115 printer_list_->GetOAuth2ApiFlowForTests(); 116 117 printer_list_->Start(); 118 119 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); 120 121 EXPECT_CALL(delegate_, OnCloudPrintPrinterListReady()); 122 123 base::MessageLoop::current()->RunUntilIdle(); 124 125 Mock::VerifyAndClear(&delegate_); 126 127 std::set<std::string> ids_found; 128 std::set<std::string> ids_expected; 129 130 ids_expected.insert("someID"); 131 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); 136 } 137 138 EXPECT_EQ(ids_expected, ids_found); 139 EXPECT_EQ(1, length); 140 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); 147} 148 149} // namespace 150 151} // namespace local_discovery 152