summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/gcd_private/gcd_private_apitest.cc
blob: 7e6ae8b22a484fccddbf1893bc3ca240a84c6888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/json/json_reader.h"
#include "chrome/browser/extensions/api/gcd_private/gcd_private_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/common/extensions/api/mdns.h"
#include "extensions/common/switches.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace api = extensions::api;

namespace {

const char kCloudPrintResponse[] =
    "{"
    "   \"success\": true,"
    "   \"printers\": ["
    "     {\"id\" : \"someCloudPrintID\","
    "      \"displayName\": \"someCloudPrintDisplayName\","
    "      \"description\": \"someCloudPrintDescription\"}"
    "    ]"
    "}";

const char kGCDResponse[] =
    "{"
    "\"kind\": \"clouddevices#devicesListResponse\","
    "\"devices\": [{"
    "  \"kind\": \"clouddevices#device\","
    "  \"id\": \"someGCDID\","
    "  \"deviceKind\": \"someType\","
    "  \"creationTimeMs\": \"123\","
    "  \"systemName\": \"someGCDDisplayName\","
    "  \"owner\": \"user@domain.com\","
    "  \"description\": \"someGCDDescription\","
    "  \"state\": {"
    "  \"base\": {"
    "    \"connectionStatus\": \"offline\""
    "   }"
    "  },"
    "  \"channel\": {"
    "  \"supportedType\": \"xmpp\""
    "  },"
    "  \"personalizedInfo\": {"
    "   \"maxRole\": \"owner\""
    "  }}]}";

// Sentinel value to signify the request should fail.
const char kResponseValueFailure[] = "FAILURE";

class FakeGCDApiFlowFactory
    : public extensions::GcdPrivateAPI::GCDApiFlowFactoryForTests {
 public:
  FakeGCDApiFlowFactory() {
    extensions::GcdPrivateAPI::SetGCDApiFlowFactoryForTests(this);
  }

  virtual ~FakeGCDApiFlowFactory() {
    extensions::GcdPrivateAPI::SetGCDApiFlowFactoryForTests(NULL);
  }

  virtual scoped_ptr<local_discovery::GCDApiFlow> CreateGCDApiFlow() OVERRIDE {
    return scoped_ptr<local_discovery::GCDApiFlow>(new FakeGCDApiFlow(this));
  }

  void SetResponse(const GURL& url, const std::string& response) {
    responses_[url] = response;
  }

 private:
  class FakeGCDApiFlow : public local_discovery::GCDApiFlow {
   public:
    explicit FakeGCDApiFlow(FakeGCDApiFlowFactory* factory)
        : factory_(factory) {}

    virtual ~FakeGCDApiFlow() {}

    virtual void Start(scoped_ptr<Request> request) OVERRIDE {
      std::string response_str = factory_->responses_[request->GetURL()];

      if (response_str == kResponseValueFailure) {
        request->OnGCDAPIFlowError(
            local_discovery::GCDApiFlow::ERROR_MALFORMED_RESPONSE);
        return;
      }

      scoped_ptr<base::Value> response(base::JSONReader::Read(response_str));
      ASSERT_TRUE(response);

      base::DictionaryValue* response_dict;
      ASSERT_TRUE(response->GetAsDictionary(&response_dict));

      request->OnGCDAPIFlowComplete(*response_dict);
    }

   private:
    FakeGCDApiFlowFactory* factory_;
  };

  std::map<GURL /*request url*/, std::string /*response json*/> responses_;
};

class GcdPrivateAPITest : public ExtensionApiTest {
 public:
  GcdPrivateAPITest() {}

 protected:
  FakeGCDApiFlowFactory api_flow_factory_;
};

IN_PROC_BROWSER_TEST_F(GcdPrivateAPITest, GetCloudList) {
  api_flow_factory_.SetResponse(
      GURL("https://www.google.com/cloudprint/search"), kCloudPrintResponse);

  api_flow_factory_.SetResponse(
      GURL("https://www.googleapis.com/clouddevices/v1/devices"), kGCDResponse);

  EXPECT_TRUE(RunExtensionSubtest("gcd_private/api", "get_cloud_list.html"));
}

}  // namespace