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.
5 #include "chrome/browser/local_discovery/cloud_device_list.h"
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"
20 using testing::NiceMock
;
21 using testing::StrictMock
;
23 namespace local_discovery
{
27 const char kSampleSuccessResponseOAuth
[] = "{"
28 "\"kind\": \"clouddevices#devicesListResponse\","
30 " \"kind\": \"clouddevices#device\","
31 " \"id\": \"someID\","
32 " \"deviceKind\": \"someType\","
33 " \"creationTimeMs\": \"123\","
34 " \"systemName\": \"someDisplayName\","
35 " \"owner\": \"user@domain.com\","
36 " \"description\": \"someDescription\","
39 " \"connectionStatus\": \"offline\""
43 " \"supportedType\": \"xmpp\""
45 " \"personalizedInfo\": {"
46 " \"maxRole\": \"owner\""
49 class MockDelegate
: public CloudDeviceListDelegate
{
51 MOCK_METHOD0(OnDeviceListReady
, void());
52 MOCK_METHOD0(OnDeviceListUnavailable
, void());
55 class CloudDeviceListTest
: public testing::Test
{
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",
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");
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_
;
82 TEST_F(CloudDeviceListTest
, List
) {
83 fetcher_factory_
.SetFakeResponse(
84 cloud_devices::GetCloudDevicesRelativeURL("devices"),
85 kSampleSuccessResponseOAuth
,
87 net::URLRequestStatus::SUCCESS
);
89 GCDBaseApiFlow
* oauth2_api_flow
= device_list_
.GetOAuth2ApiFlowForTests();
93 oauth2_api_flow
->OnGetTokenSuccess(NULL
, "SomeToken", base::Time());
95 EXPECT_CALL(delegate_
, OnDeviceListReady());
97 base::RunLoop().RunUntilIdle();
99 Mock::VerifyAndClear(&delegate_
);
101 std::set
<std::string
> ids_found
;
102 std::set
<std::string
> ids_expected
;
103 ids_expected
.insert("someID");
105 for (CloudDeviceList::iterator i
= device_list_
.device_list().begin();
106 i
!= device_list_
.device_list().end(); i
++) {
107 ids_found
.insert(i
->id
);
110 ASSERT_EQ(ids_expected
, ids_found
);
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
);
120 } // namespace local_discovery