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.
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"
25 using testing::NiceMock
;
26 using testing::StrictMock
;
28 namespace local_discovery
{
32 const char kSampleSuccessResponseOAuth
[] = "{"
35 " {\"id\" : \"someID\","
36 " \"displayName\": \"someDisplayName\","
37 " \"description\": \"someDescription\"}"
41 class TestOAuth2TokenService
: public OAuth2TokenService
{
43 explicit TestOAuth2TokenService(net::URLRequestContextGetter
* request_context
)
44 : request_context_(request_context
) {
47 virtual std::string
GetRefreshToken(const std::string
& account_id
) OVERRIDE
{
51 virtual net::URLRequestContextGetter
* GetRequestContext() OVERRIDE
{
52 return request_context_
.get();
56 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
59 class MockDelegate
: public CloudPrintPrinterList::Delegate
{
61 MOCK_METHOD0(OnCloudPrintPrinterListUnavailable
, void());
62 MOCK_METHOD0(OnCloudPrintPrinterListReady
, void());
65 class CloudPrintPrinterListTest
: public testing::Test
{
67 CloudPrintPrinterListTest()
68 : ui_thread_(content::BrowserThread::UI
,
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
76 new CloudPrintPrinterList(request_context_
.get(),
77 "http://SoMeUrL.com/cloudprint",
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();
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
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
,
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");
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
);
151 } // namespace local_discovery