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/json/json_reader.h"
10 #include "chrome/browser/local_discovery/cloud_device_list_delegate.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
15 using testing::SaveArg
;
16 using testing::StrictMock
;
19 namespace local_discovery
{
23 const char kSampleSuccessResponseOAuth
[] =
25 "\"kind\": \"clouddevices#devicesListResponse\","
27 " \"kind\": \"clouddevices#device\","
28 " \"id\": \"someID\","
29 " \"deviceKind\": \"someType\","
30 " \"creationTimeMs\": \"123\","
31 " \"systemName\": \"someDisplayName\","
32 " \"owner\": \"user@domain.com\","
33 " \"description\": \"someDescription\","
36 " \"connectionStatus\": \"offline\""
40 " \"supportedType\": \"xmpp\""
42 " \"personalizedInfo\": {"
43 " \"maxRole\": \"owner\""
46 class MockDelegate
: public CloudDeviceListDelegate
{
48 MOCK_METHOD1(OnDeviceListReady
, void(const DeviceList
&));
49 MOCK_METHOD0(OnDeviceListUnavailable
, void());
52 TEST(CloudDeviceListTest
, Params
) {
53 CloudDeviceList
device_list(NULL
);
54 EXPECT_EQ(GURL("https://www.googleapis.com/clouddevices/v1/devices"),
55 device_list
.GetURL());
56 EXPECT_EQ("https://www.googleapis.com/auth/clouddevices",
57 device_list
.GetOAuthScope());
58 EXPECT_EQ(net::URLFetcher::GET
, device_list
.GetRequestType());
59 EXPECT_TRUE(device_list
.GetExtraRequestHeaders().empty());
62 TEST(CloudDeviceListTest
, Parsing
) {
63 StrictMock
<MockDelegate
> delegate
;
64 CloudDeviceList
device_list(&delegate
);
65 CloudDeviceListDelegate::DeviceList devices
;
66 EXPECT_CALL(delegate
, OnDeviceListReady(_
)).WillOnce(SaveArg
<0>(&devices
));
68 scoped_ptr
<base::Value
> value(
69 base::JSONReader::DeprecatedRead(kSampleSuccessResponseOAuth
));
70 const base::DictionaryValue
* dictionary
= NULL
;
71 ASSERT_TRUE(value
->GetAsDictionary(&dictionary
));
72 device_list
.OnGCDAPIFlowComplete(*dictionary
);
74 Mock::VerifyAndClear(&delegate
);
76 std::set
<std::string
> ids_found
;
77 std::set
<std::string
> ids_expected
;
78 ids_expected
.insert("someID");
80 for (size_t i
= 0; i
!= devices
.size(); ++i
) {
81 ids_found
.insert(devices
[i
].id
);
84 ASSERT_EQ(ids_expected
, ids_found
);
86 EXPECT_EQ("someID", devices
[0].id
);
87 EXPECT_EQ("someDisplayName", devices
[0].display_name
);
88 EXPECT_EQ("someDescription", devices
[0].description
);
89 EXPECT_EQ("someType", devices
[0].type
);
94 } // namespace local_discovery