summaryrefslogtreecommitdiffstats
path: root/chrome/browser/local_discovery/privet_confirm_api_flow_unittest.cc
blob: 19f828a037cac5177b66652475549f1a039f82c5 (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
// Copyright 2013 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 "chrome/browser/local_discovery/privet_confirm_api_flow.h"

#include <set>

#include "base/json/json_reader.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::StrictMock;
using testing::_;

namespace local_discovery {

namespace {

const char kSampleConfirmResponse[] = "{"
    "   \"success\": true"
    "}";

const char kFailedConfirmResponse[] = "{"
    "   \"success\": false"
    "}";

TEST(PrivetConfirmApiFlowTest, Params) {
  PrivetConfirmApiCallFlow confirmation(
      "123", PrivetConfirmApiCallFlow::ResponseCallback());
  EXPECT_EQ(GURL("https://www.google.com/cloudprint/confirm?token=123"),
            confirmation.GetURL());
  EXPECT_EQ("https://www.googleapis.com/auth/cloudprint",
            confirmation.GetOAuthScope());
  EXPECT_EQ(net::URLFetcher::GET, confirmation.GetRequestType());
  EXPECT_FALSE(confirmation.GetExtraRequestHeaders().empty());
}

class MockDelegate {
 public:
  MOCK_METHOD1(Callback, void(GCDApiFlow::Status));
};

TEST(PrivetConfirmApiFlowTest, Parsing) {
  StrictMock<MockDelegate> delegate;
  PrivetConfirmApiCallFlow confirmation(
      "123", base::Bind(&MockDelegate::Callback, base::Unretained(&delegate)));
  EXPECT_CALL(delegate, Callback(GCDApiFlow::SUCCESS)).Times(1);

  scoped_ptr<base::Value> value(base::JSONReader::Read(kSampleConfirmResponse));
  const base::DictionaryValue* dictionary = NULL;
  ASSERT_TRUE(value->GetAsDictionary(&dictionary));
  confirmation.OnGCDAPIFlowComplete(*dictionary);

  EXPECT_CALL(delegate, Callback(GCDApiFlow::ERROR_FROM_SERVER)).Times(1);

  value.reset(base::JSONReader::Read(kFailedConfirmResponse));
  ASSERT_TRUE(value->GetAsDictionary(&dictionary));
  confirmation.OnGCDAPIFlowComplete(*dictionary);
}

}  // namespace

}  // namespace local_discovery