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.
4
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"
13
14 namespace api = extensions::api;
15
16 namespace {
17
18 const char kCloudPrintResponse[] =
19 "{"
20 " \"success\": true,"
21 " \"printers\": ["
22 " {\"id\" : \"someCloudPrintID\","
23 " \"displayName\": \"someCloudPrintDisplayName\","
24 " \"description\": \"someCloudPrintDescription\"}"
25 " ]"
26 "}";
27
28 const char kGCDResponse[] =
29 "{"
30 "\"kind\": \"clouddevices#devicesListResponse\","
31 "\"devices\": [{"
32 " \"kind\": \"clouddevices#device\","
33 " \"id\": \"someGCDID\","
34 " \"deviceKind\": \"someType\","
35 " \"creationTimeMs\": \"123\","
36 " \"systemName\": \"someGCDDisplayName\","
37 " \"owner\": \"user@domain.com\","
38 " \"description\": \"someGCDDescription\","
39 " \"state\": {"
40 " \"base\": {"
41 " \"connectionStatus\": \"offline\""
42 " }"
43 " },"
44 " \"channel\": {"
45 " \"supportedType\": \"xmpp\""
46 " },"
47 " \"personalizedInfo\": {"
48 " \"maxRole\": \"owner\""
49 " }}]}";
50
51 // Sentinel value to signify the request should fail.
52 const char kResponseValueFailure[] = "FAILURE";
53
54 class FakeGCDApiFlowFactory
55 : public extensions::GcdPrivateAPI::GCDApiFlowFactoryForTests {
56 public:
FakeGCDApiFlowFactory()57 FakeGCDApiFlowFactory() {
58 extensions::GcdPrivateAPI::SetGCDApiFlowFactoryForTests(this);
59 }
60
~FakeGCDApiFlowFactory()61 virtual ~FakeGCDApiFlowFactory() {
62 extensions::GcdPrivateAPI::SetGCDApiFlowFactoryForTests(NULL);
63 }
64
CreateGCDApiFlow()65 virtual scoped_ptr<local_discovery::GCDApiFlow> CreateGCDApiFlow() OVERRIDE {
66 return scoped_ptr<local_discovery::GCDApiFlow>(new FakeGCDApiFlow(this));
67 }
68
SetResponse(const GURL & url,const std::string & response)69 void SetResponse(const GURL& url, const std::string& response) {
70 responses_[url] = response;
71 }
72
73 private:
74 class FakeGCDApiFlow : public local_discovery::GCDApiFlow {
75 public:
FakeGCDApiFlow(FakeGCDApiFlowFactory * factory)76 explicit FakeGCDApiFlow(FakeGCDApiFlowFactory* factory)
77 : factory_(factory) {}
78
~FakeGCDApiFlow()79 virtual ~FakeGCDApiFlow() {}
80
Start(scoped_ptr<Request> request)81 virtual void Start(scoped_ptr<Request> request) OVERRIDE {
82 std::string response_str = factory_->responses_[request->GetURL()];
83
84 if (response_str == kResponseValueFailure) {
85 request->OnGCDAPIFlowError(
86 local_discovery::GCDApiFlow::ERROR_MALFORMED_RESPONSE);
87 return;
88 }
89
90 scoped_ptr<base::Value> response(base::JSONReader::Read(response_str));
91 ASSERT_TRUE(response);
92
93 base::DictionaryValue* response_dict;
94 ASSERT_TRUE(response->GetAsDictionary(&response_dict));
95
96 request->OnGCDAPIFlowComplete(*response_dict);
97 }
98
99 private:
100 FakeGCDApiFlowFactory* factory_;
101 };
102
103 std::map<GURL /*request url*/, std::string /*response json*/> responses_;
104 };
105
106 class GcdPrivateAPITest : public ExtensionApiTest {
107 public:
GcdPrivateAPITest()108 GcdPrivateAPITest() {}
109
110 protected:
111 FakeGCDApiFlowFactory api_flow_factory_;
112 };
113
IN_PROC_BROWSER_TEST_F(GcdPrivateAPITest,GetCloudList)114 IN_PROC_BROWSER_TEST_F(GcdPrivateAPITest, GetCloudList) {
115 api_flow_factory_.SetResponse(
116 GURL("https://www.google.com/cloudprint/search"), kCloudPrintResponse);
117
118 api_flow_factory_.SetResponse(
119 GURL("https://www.googleapis.com/clouddevices/v1/devices"), kGCDResponse);
120
121 EXPECT_TRUE(RunExtensionSubtest("gcd_private/api", "get_cloud_list.html"));
122 }
123
124 } // namespace
125