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 "base/command_line.h"
6 #include "base/json/json_reader.h"
7 #include "chrome/browser/extensions/api/gcd_private/gcd_private_api.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/common/extensions/api/mdns.h"
11 #include "extensions/common/switches.h"
12 #include "testing/gmock/include/gmock/gmock.h"
14 namespace api = extensions::api;
18 const char kCloudPrintResponse[] =
22 " {\"id\" : \"someCloudPrintID\","
23 " \"displayName\": \"someCloudPrintDisplayName\","
24 " \"description\": \"someCloudPrintDescription\"}"
28 const char kGCDResponse[] =
30 "\"kind\": \"clouddevices#devicesListResponse\","
32 " \"kind\": \"clouddevices#device\","
33 " \"id\": \"someGCDID\","
34 " \"deviceKind\": \"someType\","
35 " \"creationTimeMs\": \"123\","
36 " \"systemName\": \"someGCDDisplayName\","
37 " \"owner\": \"user@domain.com\","
38 " \"description\": \"someGCDDescription\","
41 " \"connectionStatus\": \"offline\""
45 " \"supportedType\": \"xmpp\""
47 " \"personalizedInfo\": {"
48 " \"maxRole\": \"owner\""
51 // Sentinel value to signify the request should fail.
52 const char kResponseValueFailure[] = "FAILURE";
54 class FakeGCDApiFlowFactory
55 : public extensions::GcdPrivateAPI::GCDApiFlowFactoryForTests {
57 FakeGCDApiFlowFactory() {
58 extensions::GcdPrivateAPI::SetGCDApiFlowFactoryForTests(this);
61 virtual ~FakeGCDApiFlowFactory() {
62 extensions::GcdPrivateAPI::SetGCDApiFlowFactoryForTests(NULL);
65 virtual scoped_ptr<local_discovery::GCDApiFlow> CreateGCDApiFlow() OVERRIDE {
66 return scoped_ptr<local_discovery::GCDApiFlow>(new FakeGCDApiFlow(this));
69 void SetResponse(const GURL& url, const std::string& response) {
70 responses_[url] = response;
74 class FakeGCDApiFlow : public local_discovery::GCDApiFlow {
76 explicit FakeGCDApiFlow(FakeGCDApiFlowFactory* factory)
77 : factory_(factory) {}
79 virtual ~FakeGCDApiFlow() {}
81 virtual void Start(scoped_ptr<Request> request) OVERRIDE {
82 std::string response_str = factory_->responses_[request->GetURL()];
84 if (response_str == kResponseValueFailure) {
85 request->OnGCDAPIFlowError(
86 local_discovery::GCDApiFlow::ERROR_MALFORMED_RESPONSE);
90 scoped_ptr<base::Value> response(base::JSONReader::Read(response_str));
91 ASSERT_TRUE(response);
93 base::DictionaryValue* response_dict;
94 ASSERT_TRUE(response->GetAsDictionary(&response_dict));
96 request->OnGCDAPIFlowComplete(*response_dict);
100 FakeGCDApiFlowFactory* factory_;
103 std::map<GURL /*request url*/, std::string /*response json*/> responses_;
106 class GcdPrivateAPITest : public ExtensionApiTest {
108 GcdPrivateAPITest() {}
111 FakeGCDApiFlowFactory api_flow_factory_;
114 IN_PROC_BROWSER_TEST_F(GcdPrivateAPITest, GetCloudList) {
115 api_flow_factory_.SetResponse(
116 GURL("https://www.google.com/cloudprint/search"), kCloudPrintResponse);
118 api_flow_factory_.SetResponse(
119 GURL("https://www.googleapis.com/clouddevices/v1/devices"), kGCDResponse);
121 EXPECT_TRUE(RunExtensionSubtest("gcd_private/api", "get_cloud_list.html"));