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

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: 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
« no previous file with comments | « chrome/browser/local_discovery/privetv3_setup_flow.cc ('k') | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
47 virtual void RefreshPrivetToken(
48 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE {
49 callback.Run("x-privet-token");
50 }
51
52 virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation(
53 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE {
54 return make_scoped_ptr(CreateInfoOperationPtr(callback));
55 }
56
57 virtual scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
58 const GURL& url,
59 net::URLFetcher::RequestType request_type,
60 PrivetURLFetcher::Delegate* delegate) OVERRIDE {
61 return make_scoped_ptr(
62 new PrivetURLFetcher(url, request_type, request_context_, delegate));
63 }
64
65 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
66 };
67
68 class MockDelegate : public PrivetV3SetupFlow::Delegate {
69 public:
70 MockDelegate() : privet_client_ptr_(NULL) {}
71
72 class MockGCDApiFlow : public GCDApiFlow {
73 public:
74 explicit MockGCDApiFlow(MockDelegate* delegate) : delegate_(delegate) {}
75
76 virtual void Start(scoped_ptr<Request> request) OVERRIDE {
77 ASSERT_FALSE(delegate_->gcd_request_);
78 delegate_->gcd_request_ = request.Pass();
79 delegate_->ReplyWithToken();
80 }
81
82 private:
83 MockDelegate* delegate_;
84 };
85
86 MOCK_METHOD1(GetWiFiCredentials, void(const CredentialsCallback&));
87 MOCK_METHOD1(SwitchToSetupWiFi, void(const ResultCallback&));
88 virtual void CreatePrivetV3Client(
89 const std::string& service_name,
90 const PrivetClientCallback& callback) OVERRIDE {
91 scoped_ptr<MockPrivetHTTPClient> privet_client(new MockPrivetHTTPClient());
92 privet_client_ptr_ = privet_client.get();
93 callback.Run(privet_client.PassAs<PrivetHTTPClient>());
94 }
95 MOCK_METHOD2(ConfirmSecurityCode,
96 void(const std::string&, const ResultCallback&));
97 MOCK_METHOD1(RestoreWifi, void(const ResultCallback&));
98 MOCK_METHOD0(OnSetupDone, void());
99 MOCK_METHOD0(OnSetupError, void());
100
101 virtual scoped_ptr<GCDApiFlow> CreateApiFlow() OVERRIDE {
102 scoped_ptr<MockGCDApiFlow> mock_gcd(new MockGCDApiFlow(this));
103 return mock_gcd.PassAs<GCDApiFlow>();
104 }
105
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);
111 }
112
113 void ConfirmCode(const ResultCallback& confirm_callback) {
114 confirm_callback.Run(true);
115 }
116
117 std::string gcd_server_response_;
118 scoped_ptr<GCDApiFlow::Request> gcd_request_;
119 MockPrivetHTTPClient* privet_client_ptr_;
120 };
121
122 class PrivetV3SetupFlowTest : public testing::Test {
123 public:
124 PrivetV3SetupFlowTest() : setup_(&delegate_) {}
125
126 virtual ~PrivetV3SetupFlowTest() {}
127
128 protected:
129 virtual void SetUp() OVERRIDE {
130 EXPECT_CALL(delegate_, GetWiFiCredentials(_)).Times(0);
131 EXPECT_CALL(delegate_, SwitchToSetupWiFi(_)).Times(0);
132 EXPECT_CALL(delegate_, ConfirmSecurityCode(_, _)).Times(0);
133 EXPECT_CALL(delegate_, RestoreWifi(_)).Times(0);
134 EXPECT_CALL(delegate_, OnSetupDone()).Times(0);
135 EXPECT_CALL(delegate_, OnSetupError()).Times(0);
136 }
137
138 void SimulateFetch(int response_code, const std::string& response) {
139 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
140 ASSERT_TRUE(fetcher);
141 EXPECT_THAT(fetcher->GetOriginalURL().spec(),
142 testing::HasSubstr("/privet/v3/setup/start"));
143 fetcher->set_response_code(response_code);
144 scoped_refptr<net::HttpResponseHeaders> response_headers(
145 new net::HttpResponseHeaders(""));
146 response_headers->AddHeader("Content-Type: application/json");
147 fetcher->set_response_headers(response_headers);
148 fetcher->SetResponseString(response);
149 fetcher->delegate()->OnURLFetchComplete(fetcher);
150 }
151
152 base::MessageLoop loop_;
153 net::TestURLFetcherFactory url_fetcher_factory_;
154
155 StrictMock<MockDelegate> delegate_;
156 PrivetV3SetupFlow setup_;
157 };
158
159 TEST_F(PrivetV3SetupFlowTest, InvalidTicket) {
160 EXPECT_CALL(delegate_, OnSetupError()).Times(1);
161 delegate_.gcd_server_response_ = "{}";
162 setup_.Register(kServiceName);
163 }
164
165 TEST_F(PrivetV3SetupFlowTest, InvalidDeviceResponce) {
166 EXPECT_CALL(delegate_, OnSetupError()).Times(1);
167 EXPECT_CALL(delegate_, ConfirmSecurityCode(_, _)).Times(1).WillOnce(
168 WithArgs<1>(Invoke(&delegate_, &MockDelegate::ConfirmCode)));
169 delegate_.gcd_server_response_ = kRegistrationTicketResponse;
170 setup_.Register(kServiceName);
171 SimulateFetch(0, "{}");
172 }
173
174 TEST_F(PrivetV3SetupFlowTest, Success) {
175 EXPECT_CALL(delegate_, OnSetupDone()).Times(1);
176 EXPECT_CALL(delegate_, ConfirmSecurityCode(_, _)).Times(1).WillOnce(
177 WithArgs<1>(Invoke(&delegate_, &MockDelegate::ConfirmCode)));
178 delegate_.gcd_server_response_ = kRegistrationTicketResponse;
179 setup_.Register(kServiceName);
180 SimulateFetch(200, "{}");
181 }
182
183 } // namespace
184
185 } // namespace local_discovery
OLDNEW
« no previous file with comments | « chrome/browser/local_discovery/privetv3_setup_flow.cc ('k') | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698