Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(822)

Side by Side Diff: chrome/browser/local_discovery/privetv3_setup_flow_unittest.cc

Issue 383023002: Partial setup flow with unittest. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Wed 07/16/2014 12:37:42.65 Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/privetv3_setup_flow.h"
6
7 #include "base/json/json_reader.h"
8 #include "chrome/browser/local_discovery/gcd_api_flow.h"
9 #include "net/http/http_response_headers.h"
10 #include "net/url_request/test_url_fetcher_factory.h"
11 #include "net/url_request/url_request_test_util.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace local_discovery {
16
17 namespace {
18
19 using testing::HasSubstr;
20 using testing::Invoke;
21 using testing::Return;
22 using testing::SaveArg;
23 using testing::StrictMock;
24 using testing::WithArgs;
25 using testing::_;
26
27 const char kServiceName[] = "test_service";
28
29 const char kRegistrationTicketResponse[] =
30 "{"
31 "\"kind\": \"clouddevices#registrationTicket\","
32 "\"id\": \"test_ticket\""
33 "}";
34
35 class MockPrivetHTTPClient : public PrivetHTTPClient {
36 public:
37 MockPrivetHTTPClient() {
38 request_context_ =
39 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
40 }
41
42 MOCK_METHOD0(GetName, const std::string&());
43 MOCK_METHOD1(
44 CreateInfoOperationPtr,
45 PrivetJSONOperation*(const PrivetJSONOperation::ResultCallback&));
46 MOCK_METHOD1(RefreshPrivetToken,
47 void(const PrivetURLFetcher::TokenCallback&));
48
49 virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
50 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE {
51 return make_scoped_ptr(CreateInfoOperationPtr(callback));
52 }
53
54 virtual scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
55 const GURL& url,
56 net::URLFetcher::RequestType request_type,
57 PrivetURLFetcher::Delegate* delegate) OVERRIDE {
58 return make_scoped_ptr(
59 new PrivetURLFetcher(url, request_type, request_context_, delegate));
60 }
61
62 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
63 };
64
65 class MockDelegate : public PrivetV3SetupFlow::Delegate {
66 public:
67 MockDelegate() : privet_client_ptr_(NULL) {}
68
69 class MockGCDApiFlow : public GCDApiFlow {
70 public:
71 explicit MockGCDApiFlow(MockDelegate* delegate) : delegate_(delegate) {}
72
73 void Start(scoped_ptr<Request> request) OVERRIDE {
74 ASSERT_FALSE(delegate_->gcd_request_);
75 delegate_->gcd_request_ = request.Pass();
76 delegate_->ReplyWithToken();
77 }
78
79 private:
80 MockDelegate* delegate_;
81 };
82
83 MOCK_METHOD1(GetWiFiCredentials, void(const CredentialsCallback&));
84 MOCK_METHOD1(SwitchToSetupWiFi, void(const ResultCallback&));
85 virtual void CreatePrivetV3Client(
86 const std::string& service_name,
87 const PrivetClientCallback& callback) OVERRIDE {
88 scoped_ptr<MockPrivetHTTPClient> privet_client(new MockPrivetHTTPClient());
89 privet_client_ptr_ = privet_client.get();
90 callback.Run(privet_client.PassAs<PrivetHTTPClient>());
91 }
92 MOCK_METHOD2(ConfirmSecurityCode,
93 void(const std::string&, const ResultCallback&));
94 MOCK_METHOD1(RestoreWifi, void(const ResultCallback&));
95 MOCK_METHOD0(OnSetupDone, void());
96 MOCK_METHOD0(OnSetupError, void());
97
98 virtual scoped_ptr<GCDApiFlow> CreateApiFlow() OVERRIDE {
99 scoped_ptr<MockGCDApiFlow> mock_gcd(new MockGCDApiFlow(this));
100 return mock_gcd.PassAs<GCDApiFlow>();
101 }
102
103 void ReplyWithToken() {
104 scoped_ptr<base::Value> value(base::JSONReader::Read(gcd_server_response_));
105 const base::DictionaryValue* dictionary = NULL;
106 value->GetAsDictionary(&dictionary);
107 gcd_request_->OnGCDAPIFlowComplete(*dictionary);
108 }
109
110 void ConfirmCode(const ResultCallback& confirm_callback) {
111 confirm_callback.Run(true);
112 }
113
114 std::string gcd_server_response_;
115 scoped_ptr<GCDApiFlow::Request> gcd_request_;
116 MockPrivetHTTPClient* privet_client_ptr_;
117 };
118
119 class PrivetV3SetupFlowTest : public testing::Test {
120 public:
121 PrivetV3SetupFlowTest() : setup_(&delegate_) {}
122
123 virtual ~PrivetV3SetupFlowTest() {}
124
125 protected:
126 virtual void SetUp() OVERRIDE {
127 EXPECT_CALL(delegate_, GetWiFiCredentials(_)).Times(0);
128 EXPECT_CALL(delegate_, SwitchToSetupWiFi(_)).Times(0);
129 EXPECT_CALL(delegate_, ConfirmSecurityCode(_, _)).Times(0);
130 EXPECT_CALL(delegate_, RestoreWifi(_)).Times(0);
131 EXPECT_CALL(delegate_, OnSetupDone()).Times(0);
132 EXPECT_CALL(delegate_, OnSetupError()).Times(0);
133 }
134
135 void SimulateFetch(int response_code, const std::string& response) {
136 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
137 ASSERT_TRUE(fetcher);
138 EXPECT_THAT(fetcher->GetOriginalURL().spec(),
139 testing::HasSubstr("/privet/v3/setup/start"));
140 fetcher->set_response_code(response_code);
141 scoped_refptr<net::HttpResponseHeaders> response_headers(
142 new net::HttpResponseHeaders(""));
143 response_headers->AddHeader("Content-Type: application/json");
144 fetcher->set_response_headers(response_headers);
145 fetcher->SetResponseString(response);
146 fetcher->delegate()->OnURLFetchComplete(fetcher);
147 }
148
149 base::MessageLoop loop_;
150 net::TestURLFetcherFactory url_fetcher_factory_;
151
152 StrictMock<MockDelegate> delegate_;
153 PrivetV3SetupFlow setup_;
154 };
155
156 TEST_F(PrivetV3SetupFlowTest, InvalidTicket) {
157 EXPECT_CALL(delegate_, OnSetupError()).Times(1);
158 delegate_.gcd_server_response_ = "{}";
159 setup_.Register(kServiceName);
160 }
161
162 TEST_F(PrivetV3SetupFlowTest, InvalidDeviceResponce) {
163 EXPECT_CALL(delegate_, OnSetupError()).Times(1);
164 EXPECT_CALL(delegate_, ConfirmSecurityCode(_, _)).Times(1).WillOnce(
165 WithArgs<1>(Invoke(&delegate_, &MockDelegate::ConfirmCode)));
166 delegate_.gcd_server_response_ = kRegistrationTicketResponse;
167 setup_.Register(kServiceName);
168 SimulateFetch(0, "{}");
169 }
170
171 TEST_F(PrivetV3SetupFlowTest, Success) {
172 EXPECT_CALL(delegate_, OnSetupDone()).Times(1);
173 EXPECT_CALL(delegate_, ConfirmSecurityCode(_, _)).Times(1).WillOnce(
174 WithArgs<1>(Invoke(&delegate_, &MockDelegate::ConfirmCode)));
175 delegate_.gcd_server_response_ = kRegistrationTicketResponse;
176 setup_.Register(kServiceName);
177 SimulateFetch(200, "{}");
178 }
179
180 } // namespace
181
182 } // namespace local_discovery
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698