OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "chrome/browser/local_discovery/cloud_device_list.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "components/cloud_devices/common/cloud_devices_urls.h" |
| 12 #include "content/public/test/test_browser_thread.h" |
| 13 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 14 #include "net/url_request/test_url_fetcher_factory.h" |
| 15 #include "net/url_request/url_request_test_util.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 using testing::Mock; |
| 20 using testing::NiceMock; |
| 21 using testing::StrictMock; |
| 22 |
| 23 namespace local_discovery { |
| 24 |
| 25 namespace { |
| 26 |
| 27 const char kSampleSuccessResponseOAuth[] = "{" |
| 28 "\"kind\": \"clouddevices#devicesListResponse\"," |
| 29 "\"devices\": [{" |
| 30 " \"kind\": \"clouddevices#device\"," |
| 31 " \"id\": \"someID\"," |
| 32 " \"deviceKind\": \"someType\"," |
| 33 " \"creationTimeMs\": \"123\"," |
| 34 " \"systemName\": \"someDisplayName\"," |
| 35 " \"owner\": \"user@domain.com\"," |
| 36 " \"description\": \"someDescription\"," |
| 37 " \"state\": {" |
| 38 " \"base\": {" |
| 39 " \"connectionStatus\": \"offline\"" |
| 40 " }" |
| 41 " }," |
| 42 " \"channel\": {" |
| 43 " \"supportedType\": \"xmpp\"" |
| 44 " }," |
| 45 " \"personalizedInfo\": {" |
| 46 " \"maxRole\": \"owner\"" |
| 47 " }}]}"; |
| 48 |
| 49 class MockDelegate : public CloudDeviceListDelegate { |
| 50 public: |
| 51 MOCK_METHOD0(OnDeviceListReady, void()); |
| 52 MOCK_METHOD0(OnDeviceListUnavailable, void()); |
| 53 }; |
| 54 |
| 55 class CloudDeviceListTest : public testing::Test { |
| 56 public: |
| 57 CloudDeviceListTest() |
| 58 : ui_thread_(content::BrowserThread::UI, &loop_), |
| 59 request_context_(new net::TestURLRequestContextGetter( |
| 60 base::MessageLoopProxy::current())), |
| 61 fetcher_factory_(NULL), |
| 62 device_list_(request_context_.get(), &token_service_, "account_id", |
| 63 &delegate_) { |
| 64 } |
| 65 |
| 66 virtual void SetUp() OVERRIDE { |
| 67 ui_thread_.Stop(); // HACK: Fake being on the UI thread |
| 68 token_service_.set_request_context(request_context_.get()); |
| 69 token_service_.AddAccount("account_id"); |
| 70 } |
| 71 |
| 72 protected: |
| 73 base::MessageLoopForUI loop_; |
| 74 content::TestBrowserThread ui_thread_; |
| 75 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 76 net::FakeURLFetcherFactory fetcher_factory_; |
| 77 FakeOAuth2TokenService token_service_; |
| 78 StrictMock<MockDelegate> delegate_; |
| 79 CloudDeviceList device_list_; |
| 80 }; |
| 81 |
| 82 TEST_F(CloudDeviceListTest, List) { |
| 83 fetcher_factory_.SetFakeResponse( |
| 84 cloud_devices::GetCloudDevicesRelativeURL("devices"), |
| 85 kSampleSuccessResponseOAuth, |
| 86 net::HTTP_OK, |
| 87 net::URLRequestStatus::SUCCESS); |
| 88 |
| 89 GCDBaseApiFlow* oauth2_api_flow = device_list_.GetOAuth2ApiFlowForTests(); |
| 90 |
| 91 device_list_.Start(); |
| 92 |
| 93 oauth2_api_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); |
| 94 |
| 95 EXPECT_CALL(delegate_, OnDeviceListReady()); |
| 96 |
| 97 base::RunLoop().RunUntilIdle(); |
| 98 |
| 99 Mock::VerifyAndClear(&delegate_); |
| 100 |
| 101 std::set<std::string> ids_found; |
| 102 std::set<std::string> ids_expected; |
| 103 ids_expected.insert("someID"); |
| 104 |
| 105 for (CloudDeviceList::iterator i = device_list_.device_list().begin(); |
| 106 i != device_list_.device_list().end(); i++) { |
| 107 ids_found.insert(i->id); |
| 108 } |
| 109 |
| 110 ASSERT_EQ(ids_expected, ids_found); |
| 111 |
| 112 EXPECT_EQ("someID", device_list_.device_list()[0].id); |
| 113 EXPECT_EQ("someDisplayName", device_list_.device_list()[0].display_name); |
| 114 EXPECT_EQ("someDescription", device_list_.device_list()[0].description); |
| 115 EXPECT_EQ("someType", device_list_.device_list()[0].type); |
| 116 } |
| 117 |
| 118 } // namespace |
| 119 |
| 120 } // namespace local_discovery |
OLD | NEW |