cloud_print_printer_list_unittest.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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.
4
5#include "chrome/browser/local_discovery/cloud_print_printer_list.h"
6
7#include <set>
8
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"
13
14using testing::Mock;
15using testing::SaveArg;
16using testing::StrictMock;
17using testing::_;
18
19namespace local_discovery {
20
21namespace {
22
23const char kSampleSuccessResponseOAuth[] = "{"
24    "   \"success\": true,"
25    "   \"printers\": ["
26    "     {\"id\" : \"someID\","
27    "      \"displayName\": \"someDisplayName\","
28    "      \"description\": \"someDescription\"}"
29    "    ]"
30    "}";
31
32class MockDelegate : public CloudDeviceListDelegate {
33 public:
34  MOCK_METHOD1(OnDeviceListReady, void(const DeviceList&));
35  MOCK_METHOD0(OnDeviceListUnavailable, void());
36};
37
38TEST(CloudPrintPrinterListTest, Params) {
39  CloudPrintPrinterList device_list(NULL);
40  EXPECT_EQ(GURL("https://www.google.com/cloudprint/search"),
41            device_list.GetURL());
42  EXPECT_EQ("https://www.googleapis.com/auth/cloudprint",
43            device_list.GetOAuthScope());
44  EXPECT_EQ(net::URLFetcher::GET, device_list.GetRequestType());
45  EXPECT_FALSE(device_list.GetExtraRequestHeaders().empty());
46}
47
48TEST(CloudPrintPrinterListTest, Parsing) {
49  StrictMock<MockDelegate> delegate;
50  CloudPrintPrinterList device_list(&delegate);
51  CloudDeviceListDelegate::DeviceList devices;
52  EXPECT_CALL(delegate, OnDeviceListReady(_)).WillOnce(SaveArg<0>(&devices));
53
54  scoped_ptr<base::Value> value(
55      base::JSONReader::Read(kSampleSuccessResponseOAuth));
56  const base::DictionaryValue* dictionary = NULL;
57  ASSERT_TRUE(value->GetAsDictionary(&dictionary));
58  device_list.OnGCDAPIFlowComplete(*dictionary);
59
60  Mock::VerifyAndClear(&delegate);
61
62  std::set<std::string> ids_found;
63  std::set<std::string> ids_expected;
64  ids_expected.insert("someID");
65
66  for (size_t i = 0; i != devices.size(); ++i) {
67    ids_found.insert(devices[i].id);
68  }
69
70  ASSERT_EQ(ids_expected, ids_found);
71
72  EXPECT_EQ("someID", devices[0].id);
73  EXPECT_EQ("someDisplayName", devices[0].display_name);
74  EXPECT_EQ("someDescription", devices[0].description);
75  EXPECT_EQ("printer", devices[0].type);
76}
77
78}  // namespace
79
80}  // namespace local_discovery
81