Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / local_discovery / privetv3_setup_flow_unittest.cc
blob9b6ae67ff866ea3ea2cea23c3b11132751e7c160
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 "chrome/browser/local_discovery/privetv3_setup_flow.h"
7 #include "base/json/json_reader.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/local_discovery/gcd_api_flow.h"
10 #include "net/http/http_response_headers.h"
11 #include "net/url_request/test_url_fetcher_factory.h"
12 #include "net/url_request/url_request_test_util.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace local_discovery {
18 namespace {
20 using testing::HasSubstr;
21 using testing::Invoke;
22 using testing::Return;
23 using testing::SaveArg;
24 using testing::StrictMock;
25 using testing::WithArgs;
26 using testing::_;
28 const char kServiceName[] = "test_service";
30 const char kRegistrationTicketResponse[] =
31 "{"
32 "\"kind\": \"clouddevices#registrationTicket\","
33 "\"id\": \"test_ticket\","
34 "\"deviceId\": \"test_id\""
35 "}";
37 class MockPrivetHTTPClient : public PrivetHTTPClient {
38 public:
39 MockPrivetHTTPClient() {
40 request_context_ =
41 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
44 MOCK_METHOD0(GetName, const std::string&());
45 MOCK_METHOD1(
46 CreateInfoOperationPtr,
47 PrivetJSONOperation*(const PrivetJSONOperation::ResultCallback&));
49 virtual void RefreshPrivetToken(
50 const PrivetURLFetcher::TokenCallback& callback) override {
51 callback.Run("x-privet-token");
54 virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
55 const PrivetJSONOperation::ResultCallback& callback) override {
56 return make_scoped_ptr(CreateInfoOperationPtr(callback));
59 virtual scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
60 const GURL& url,
61 net::URLFetcher::RequestType request_type,
62 PrivetURLFetcher::Delegate* delegate) override {
63 return make_scoped_ptr(new PrivetURLFetcher(
64 url, request_type, request_context_.get(), delegate));
67 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
70 class MockDelegate : public PrivetV3SetupFlow::Delegate {
71 public:
72 MockDelegate() : privet_client_ptr_(NULL) {}
74 class MockGCDApiFlow : public GCDApiFlow {
75 public:
76 explicit MockGCDApiFlow(MockDelegate* delegate) : delegate_(delegate) {}
78 void Start(scoped_ptr<Request> request) override {
79 ASSERT_FALSE(delegate_->gcd_request_);
80 delegate_->gcd_request_ = request.Pass();
81 delegate_->ReplyWithToken();
84 private:
85 MockDelegate* delegate_;
88 MOCK_METHOD1(GetWiFiCredentials, void(const CredentialsCallback&));
89 MOCK_METHOD1(SwitchToSetupWiFi, void(const ResultCallback&));
90 virtual void CreatePrivetV3Client(
91 const std::string& service_name,
92 const PrivetClientCallback& callback) override {
93 scoped_ptr<MockPrivetHTTPClient> privet_client(new MockPrivetHTTPClient());
94 privet_client_ptr_ = privet_client.get();
95 callback.Run(privet_client.Pass());
97 MOCK_METHOD1(ConfirmSecurityCode, void(const ResultCallback&));
98 MOCK_METHOD1(RestoreWifi, void(const ResultCallback&));
99 MOCK_METHOD0(OnSetupDone, void());
100 MOCK_METHOD0(OnSetupError, void());
102 virtual scoped_ptr<GCDApiFlow> CreateApiFlow() override {
103 return make_scoped_ptr(new MockGCDApiFlow(this));
106 void ReplyWithToken() {
107 scoped_ptr<base::Value> value(base::JSONReader::Read(gcd_server_response_));
108 const base::DictionaryValue* dictionary = NULL;
109 value->GetAsDictionary(&dictionary);
110 gcd_request_->OnGCDAPIFlowComplete(*dictionary);
113 std::string gcd_server_response_;
114 scoped_ptr<GCDApiFlow::Request> gcd_request_;
115 MockPrivetHTTPClient* privet_client_ptr_;
116 base::Closure quit_closure_;
119 class PrivetV3SetupFlowTest : public testing::Test {
120 public:
121 PrivetV3SetupFlowTest() : setup_(&delegate_) {}
123 virtual ~PrivetV3SetupFlowTest() {}
125 void ConfirmCode(const MockDelegate::ResultCallback& confirm_callback) {
126 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
127 confirm_callback.Run(true);
130 protected:
131 virtual void SetUp() override {
132 quit_closure_ = run_loop_.QuitClosure();
133 EXPECT_CALL(delegate_, GetWiFiCredentials(_)).Times(0);
134 EXPECT_CALL(delegate_, SwitchToSetupWiFi(_)).Times(0);
135 EXPECT_CALL(delegate_, ConfirmSecurityCode(_)).Times(0);
136 EXPECT_CALL(delegate_, RestoreWifi(_)).Times(0);
137 EXPECT_CALL(delegate_, OnSetupDone()).Times(0);
138 EXPECT_CALL(delegate_, OnSetupError()).Times(0);
141 void SimulateFetch(int response_code, const std::string& response) {
142 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
143 ASSERT_TRUE(fetcher);
144 EXPECT_THAT(fetcher->GetOriginalURL().spec(),
145 testing::HasSubstr("/privet/v3/setup/start"));
146 fetcher->set_response_code(response_code);
147 scoped_refptr<net::HttpResponseHeaders> response_headers(
148 new net::HttpResponseHeaders(""));
149 response_headers->AddHeader("Content-Type: application/json");
150 fetcher->set_response_headers(response_headers);
151 fetcher->SetResponseString(response);
152 fetcher->delegate()->OnURLFetchComplete(fetcher);
155 net::TestURLFetcherFactory url_fetcher_factory_;
156 StrictMock<MockDelegate> delegate_;
157 PrivetV3SetupFlow setup_;
158 base::MessageLoop loop_;
159 base::RunLoop run_loop_;
160 base::Closure quit_closure_;
163 TEST_F(PrivetV3SetupFlowTest, InvalidTicket) {
164 EXPECT_CALL(delegate_, OnSetupError()).Times(1);
165 delegate_.gcd_server_response_ = "{}";
166 setup_.Register(kServiceName);
169 TEST_F(PrivetV3SetupFlowTest, InvalidDeviceResponse) {
170 EXPECT_CALL(delegate_, OnSetupError()).Times(1);
171 EXPECT_CALL(delegate_, ConfirmSecurityCode(_))
172 .Times(1)
173 .WillOnce(WithArgs<0>(Invoke(this, &PrivetV3SetupFlowTest::ConfirmCode)));
174 delegate_.gcd_server_response_ = kRegistrationTicketResponse;
175 setup_.Register(kServiceName);
176 run_loop_.Run();
177 SimulateFetch(0, "{}");
180 TEST_F(PrivetV3SetupFlowTest, Success) {
181 EXPECT_CALL(delegate_, OnSetupDone()).Times(1);
182 EXPECT_CALL(delegate_, ConfirmSecurityCode(_))
183 .Times(1)
184 .WillOnce(WithArgs<0>(Invoke(this, &PrivetV3SetupFlowTest::ConfirmCode)));
185 delegate_.gcd_server_response_ = kRegistrationTicketResponse;
186 setup_.Register(kServiceName);
187 run_loop_.Run();
188 SimulateFetch(200, "{}");
191 } // namespace
193 } // namespace local_discovery