| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/local_discovery/cloud_print_printer_list.h" |
| 6 |
| 5 #include <set> | 7 #include <set> |
| 6 | 8 |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 9 #include "chrome/browser/local_discovery/cloud_print_printer_list.h" | 10 #include "base/run_loop.h" |
| 10 #include "components/cloud_devices/common/cloud_devices_urls.h" | 11 #include "components/cloud_devices/common/cloud_devices_urls.h" |
| 11 #include "content/public/test/test_browser_thread.h" | 12 #include "content/public/test/test_browser_thread.h" |
| 12 #include "google_apis/gaia/fake_oauth2_token_service.h" | 13 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 13 #include "google_apis/gaia/google_service_auth_error.h" | |
| 14 #include "net/base/host_port_pair.h" | |
| 15 #include "net/base/net_errors.h" | |
| 16 #include "net/http/http_request_headers.h" | |
| 17 #include "net/http/http_status_code.h" | |
| 18 #include "net/url_request/test_url_fetcher_factory.h" | 14 #include "net/url_request/test_url_fetcher_factory.h" |
| 19 #include "net/url_request/url_fetcher_impl.h" | |
| 20 #include "net/url_request/url_request_status.h" | |
| 21 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "url/gurl.h" | |
| 25 | 18 |
| 26 using testing::Mock; | 19 using testing::Mock; |
| 27 using testing::NiceMock; | 20 using testing::NiceMock; |
| 28 using testing::StrictMock; | 21 using testing::StrictMock; |
| 29 | 22 |
| 30 namespace local_discovery { | 23 namespace local_discovery { |
| 31 | 24 |
| 32 namespace { | 25 namespace { |
| 33 | 26 |
| 34 const char kSampleSuccessResponseOAuth[] = "{" | 27 const char kSampleSuccessResponseOAuth[] = "{" |
| 35 " \"success\": true," | 28 " \"success\": true," |
| 36 " \"printers\": [" | 29 " \"printers\": [" |
| 37 " {\"id\" : \"someID\"," | 30 " {\"id\" : \"someID\"," |
| 38 " \"displayName\": \"someDisplayName\"," | 31 " \"displayName\": \"someDisplayName\"," |
| 39 " \"description\": \"someDescription\"}" | 32 " \"description\": \"someDescription\"}" |
| 40 " ]" | 33 " ]" |
| 41 "}"; | 34 "}"; |
| 42 | 35 |
| 43 class MockDelegate : public CloudPrintPrinterList::Delegate { | 36 class MockDelegate : public CloudPrintPrinterList::Delegate { |
| 44 public: | 37 public: |
| 45 MOCK_METHOD0(OnCloudPrintPrinterListUnavailable, void()); | 38 MOCK_METHOD0(OnCloudPrintPrinterListUnavailable, void()); |
| 46 MOCK_METHOD0(OnCloudPrintPrinterListReady, void()); | 39 MOCK_METHOD0(OnCloudPrintPrinterListReady, void()); |
| 47 }; | 40 }; |
| 48 | 41 |
| 49 class CloudPrintPrinterListTest : public testing::Test { | 42 class CloudPrintPrinterListTest : public testing::Test { |
| 50 public: | 43 public: |
| 51 CloudPrintPrinterListTest() | 44 CloudPrintPrinterListTest() |
| 52 : ui_thread_(content::BrowserThread::UI, &loop_), | 45 : ui_thread_(content::BrowserThread::UI, &loop_), |
| 53 request_context_(new net::TestURLRequestContextGetter( | 46 request_context_(new net::TestURLRequestContextGetter( |
| 54 base::MessageLoopProxy::current())) { | 47 base::MessageLoopProxy::current())), |
| 48 fetcher_factory_(NULL), |
| 49 printer_list_(request_context_.get(), &token_service_, "account_id", |
| 50 &delegate_) { |
| 51 } |
| 52 |
| 53 virtual void SetUp() OVERRIDE { |
| 55 ui_thread_.Stop(); // HACK: Fake being on the UI thread | 54 ui_thread_.Stop(); // HACK: Fake being on the UI thread |
| 56 token_service_.set_request_context(request_context_.get()); | 55 token_service_.set_request_context(request_context_.get()); |
| 57 token_service_.AddAccount("account_id"); | 56 token_service_.AddAccount("account_id"); |
| 58 printer_list_.reset( | |
| 59 new CloudPrintPrinterList(request_context_.get(), | |
| 60 &token_service_, | |
| 61 "account_id", | |
| 62 &delegate_)); | |
| 63 | |
| 64 fallback_fetcher_factory_.reset(new net::TestURLFetcherFactory()); | |
| 65 net::URLFetcherImpl::set_factory(NULL); | |
| 66 fetcher_factory_.reset(new net::FakeURLFetcherFactory( | |
| 67 fallback_fetcher_factory_.get())); | |
| 68 } | |
| 69 | |
| 70 virtual ~CloudPrintPrinterListTest() { | |
| 71 fetcher_factory_.reset(); | |
| 72 net::URLFetcherImpl::set_factory(fallback_fetcher_factory_.get()); | |
| 73 fallback_fetcher_factory_.reset(); | |
| 74 } | 57 } |
| 75 | 58 |
| 76 protected: | 59 protected: |
| 77 base::MessageLoopForUI loop_; | 60 base::MessageLoopForUI loop_; |
| 78 content::TestBrowserThread ui_thread_; | 61 content::TestBrowserThread ui_thread_; |
| 79 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 62 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 80 // Use a test factory as a fallback so we don't have to deal with OAuth2 | 63 net::FakeURLFetcherFactory fetcher_factory_; |
| 81 // requests. | |
| 82 scoped_ptr<net::TestURLFetcherFactory> fallback_fetcher_factory_; | |
| 83 scoped_ptr<net::FakeURLFetcherFactory> fetcher_factory_; | |
| 84 FakeOAuth2TokenService token_service_; | 64 FakeOAuth2TokenService token_service_; |
| 85 StrictMock<MockDelegate> delegate_; | 65 StrictMock<MockDelegate> delegate_; |
| 86 scoped_ptr<CloudPrintPrinterList> printer_list_; | 66 CloudPrintPrinterList printer_list_; |
| 87 }; | 67 }; |
| 88 | 68 |
| 89 TEST_F(CloudPrintPrinterListTest, SuccessOAuth2) { | 69 TEST_F(CloudPrintPrinterListTest, List) { |
| 90 fetcher_factory_->SetFakeResponse( | 70 fetcher_factory_.SetFakeResponse( |
| 91 cloud_devices::GetCloudPrintRelativeURL("search"), | 71 cloud_devices::GetCloudPrintRelativeURL("search"), |
| 92 kSampleSuccessResponseOAuth, | 72 kSampleSuccessResponseOAuth, |
| 93 net::HTTP_OK, | 73 net::HTTP_OK, |
| 94 net::URLRequestStatus::SUCCESS); | 74 net::URLRequestStatus::SUCCESS); |
| 95 | 75 |
| 96 GCDBaseApiFlow* cloudprint_flow = printer_list_->GetOAuth2ApiFlowForTests(); | 76 GCDBaseApiFlow* cloudprint_flow = printer_list_.GetOAuth2ApiFlowForTests(); |
| 97 | 77 |
| 98 printer_list_->Start(); | 78 printer_list_.Start(); |
| 99 | 79 |
| 100 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); | 80 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); |
| 101 | 81 |
| 102 EXPECT_CALL(delegate_, OnCloudPrintPrinterListReady()); | 82 EXPECT_CALL(delegate_, OnCloudPrintPrinterListReady()); |
| 83 EXPECT_CALL(delegate_, OnCloudPrintPrinterListUnavailable()).Times(0); |
| 103 | 84 |
| 104 base::MessageLoop::current()->RunUntilIdle(); | 85 base::RunLoop().RunUntilIdle(); |
| 105 | 86 |
| 106 Mock::VerifyAndClear(&delegate_); | 87 Mock::VerifyAndClear(&delegate_); |
| 107 | 88 |
| 108 std::set<std::string> ids_found; | 89 std::set<std::string> ids_found; |
| 109 std::set<std::string> ids_expected; | 90 std::set<std::string> ids_expected; |
| 110 | |
| 111 ids_expected.insert("someID"); | 91 ids_expected.insert("someID"); |
| 112 | 92 |
| 113 int length = 0; | 93 for (CloudPrintPrinterList::iterator i = printer_list_.begin(); |
| 114 for (CloudPrintPrinterList::iterator i = printer_list_->begin(); | 94 i != printer_list_.end(); i++) { |
| 115 i != printer_list_->end(); i++, length++) { | |
| 116 ids_found.insert(i->id); | 95 ids_found.insert(i->id); |
| 117 } | 96 } |
| 118 | 97 |
| 119 EXPECT_EQ(ids_expected, ids_found); | 98 EXPECT_EQ(ids_expected, ids_found); |
| 120 EXPECT_EQ(1, length); | |
| 121 | 99 |
| 122 const CloudPrintPrinterList::PrinterDetails* found = | 100 const CloudPrintPrinterList::PrinterDetails* found = |
| 123 printer_list_->GetDetailsFor("someID"); | 101 printer_list_.GetDetailsFor("someID"); |
| 124 ASSERT_TRUE(found != NULL); | 102 ASSERT_TRUE(found != NULL); |
| 125 EXPECT_EQ("someID", found->id); | 103 EXPECT_EQ("someID", found->id); |
| 126 EXPECT_EQ("someDisplayName", found->display_name); | 104 EXPECT_EQ("someDisplayName", found->display_name); |
| 127 EXPECT_EQ("someDescription", found->description); | 105 EXPECT_EQ("someDescription", found->description); |
| 128 } | 106 } |
| 129 | 107 |
| 130 } // namespace | 108 } // namespace |
| 131 | 109 |
| 132 } // namespace local_discovery | 110 } // namespace local_discovery |
| OLD | NEW |