Home | History | Annotate | Download | only in local_discovery
      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 "chrome/browser/local_discovery/gcd_registration_ticket_request.h"
      6 
      7 #include "base/json/json_reader.h"
      8 #include "testing/gmock/include/gmock/gmock.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 using testing::StrictMock;
     12 
     13 namespace local_discovery {
     14 
     15 namespace {
     16 
     17 const char kSampleResponse[] =
     18     "{"
     19     "\"kind\": \"clouddevices#registrationTicket\","
     20     "\"id\": \"SampleTicketID\","
     21     "\"deviceId\": \"SampleDeviceID\""
     22     "}";
     23 
     24 const char kErrorResponse[] =
     25     "{"
     26     "\"kind\": \"clouddevices#error\""
     27     "}";
     28 
     29 TEST(GCDRegistrationTicketRequestTest, Params) {
     30   GCDRegistrationTicketRequest::ResponseCallback null_callback;
     31   GCDRegistrationTicketRequest request(null_callback);
     32 
     33   EXPECT_EQ(
     34       GURL("https://www.googleapis.com/clouddevices/v1/registrationTickets"),
     35       request.GetURL());
     36   EXPECT_EQ("https://www.googleapis.com/auth/clouddevices",
     37             request.GetOAuthScope());
     38   EXPECT_EQ(net::URLFetcher::POST, request.GetRequestType());
     39   EXPECT_TRUE(request.GetExtraRequestHeaders().empty());
     40 }
     41 
     42 class MockDelegate {
     43  public:
     44   MOCK_METHOD2(Callback,
     45                void(const std::string& ticket_id,
     46                     const std::string& device_id));
     47 };
     48 
     49 TEST(GCDRegistrationTicketRequestTest, Parsing) {
     50   StrictMock<MockDelegate> delegate;
     51   GCDRegistrationTicketRequest request(
     52       base::Bind(&MockDelegate::Callback, base::Unretained(&delegate)));
     53 
     54   EXPECT_CALL(delegate, Callback("SampleTicketID", "SampleDeviceID"));
     55 
     56   scoped_ptr<base::Value> value(base::JSONReader::Read(kSampleResponse));
     57   const base::DictionaryValue* dictionary = NULL;
     58   ASSERT_TRUE(value->GetAsDictionary(&dictionary));
     59   request.OnGCDAPIFlowComplete(*dictionary);
     60 
     61   EXPECT_CALL(delegate, Callback("", ""));
     62 
     63   value.reset(base::JSONReader::Read(kErrorResponse));
     64   ASSERT_TRUE(value->GetAsDictionary(&dictionary));
     65   request.OnGCDAPIFlowComplete(*dictionary);
     66 }
     67 
     68 }  // namespace
     69 
     70 }  // namespace local_discovery
     71