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

Side by Side Diff: chrome/browser/ui/desktop_ios_promotion/sms_service_unittest.cc

Issue 2646823002: D2MIOS: Add plumbing to query verified number and send SMS from growth server. (Closed)
Patch Set: Add unit tests Created 3 years, 10 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
OLDNEW
(Empty)
1 // Copyright 2017 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/ui/desktop_ios_promotion/sms_service.h"
6
7 #include "base/mac/bind_objc_block.h"
8 #include "base/run_loop.h"
9 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
10 #include "components/signin/core/browser/fake_signin_manager.h"
11 #include "components/signin/core/browser/test_signin_client.h"
12 #include "net/http/http_status_code.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace DesktopIOSPromotion {
msramek 2017/01/27 16:38:58 Ditto here.
justincohen 2017/01/27 23:06:46 Done.
17
18 namespace {
19
20 // A testing web history service that does extra checks and creates a
21 // TestRequest instead of a normal request.
22 class TestingSMSService : public SMSService {
23 public:
24 explicit TestingSMSService(
25 ProfileOAuth2TokenService* token_service,
26 SigninManagerBase* signin_manager,
27 const scoped_refptr<net::URLRequestContextGetter>& request_context)
28 : SMSService(token_service, signin_manager, request_context) {}
29
30 ~TestingSMSService() override {}
31
32 SMSService::Request* CreateRequest(
33 const GURL& url,
34 const CompletionCallback& callback) override;
35
36 void SetNextRequestResponseData(int response_code,
37 const std::string& response_body) {
38 next_response_code_ = response_code;
39 next_response_body_ = response_body;
40 }
41
42 private:
43 int next_response_code_;
44 std::string next_response_body_;
45
46 DISALLOW_COPY_AND_ASSIGN(TestingSMSService);
47 };
48
49 // A testing request class that allows expected values to be filled in.
50 class TestRequest : public SMSService::Request {
51 public:
52 TestRequest(const SMSService::CompletionCallback& callback,
53 int response_code,
54 const std::string& response_body)
55 : callback_(callback),
56 response_code_(response_code),
57 response_body_(response_body),
58 is_pending_(false) {}
59
60 ~TestRequest() override {}
61
62 // history::Request overrides
63 bool IsPending() override { return is_pending_; }
64 int GetResponseCode() override { return response_code_; }
65 const std::string& GetResponseBody() override { return response_body_; }
66 void SetPostData(const std::string& post_data) override {
67 post_data_ = post_data;
68 }
69 void SetPostDataAndType(const std::string& post_data,
70 const std::string& mime_type) override {
71 SetPostData(post_data);
72 }
73 void Start() override {
74 is_pending_ = true;
75 base::ThreadTaskRunnerHandle::Get()->PostTask(
76 FROM_HERE,
77 base::Bind(&TestRequest::MimicReturnFromFetch, base::Unretained(this)));
78 }
79
80 void MimicReturnFromFetch() {
81 callback_.Run(this, response_code_ == net::HTTP_OK);
82 }
83
84 private:
85 GURL url_;
86 SMSService::CompletionCallback callback_;
87 int response_code_;
88 std::string response_body_;
89 std::string post_data_;
90 bool is_pending_;
91
92 DISALLOW_COPY_AND_ASSIGN(TestRequest);
93 };
94
95 SMSService::Request* TestingSMSService::CreateRequest(
96 const GURL& url,
97 const CompletionCallback& callback) {
98 SMSService::Request* request =
99 new TestRequest(callback, next_response_code_, next_response_body_);
100 return request;
101 }
102
103 } // namespace
104
105 // A test class used for testing the SMSService class.
106 // In order for SMSService to be valid, we must have a valid
107 // ProfileSyncService. Using the ProfileSyncServiceMock class allows to
108 // assign specific return values as needed to make sure the web history
109 // service is available.
110 class SMSServiceTest : public testing::Test {
111 public:
112 SMSServiceTest()
113 : signin_client_(nullptr),
114 signin_manager_(&signin_client_, &account_tracker_),
115 url_request_context_(new net::TestURLRequestContextGetter(
116 base::ThreadTaskRunnerHandle::Get())),
117 sms_service_(&token_service_, &signin_manager_, url_request_context_) {}
118
119 ~SMSServiceTest() override {}
120
121 void TearDown() override {
122 base::RunLoop run_loop;
123 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
124 run_loop.QuitClosure());
125 run_loop.Run();
126 }
127
128 TestingSMSService* sms_service() { return &sms_service_; }
129
130 void SetNextRequestResponseData(int response_code,
131 const std::string& response_body) {
132 sms_service_.SetNextRequestResponseData(response_code, response_body);
133 }
134
135 private:
136 base::MessageLoop message_loop_;
137 FakeProfileOAuth2TokenService token_service_;
138 AccountTrackerService account_tracker_;
139 TestSigninClient signin_client_;
140 FakeSigninManagerBase signin_manager_;
141 scoped_refptr<net::URLRequestContextGetter> url_request_context_;
142 TestingSMSService sms_service_;
143
144 DISALLOW_COPY_AND_ASSIGN(SMSServiceTest);
145 };
146
147 TEST_F(SMSServiceTest, VerifyJsonData) {
148 // Test that properly formatted response with good response code returns true
149 // as expected.
150 std::string query_phone_valid =
151 "{\n\"phoneNumber\": "
152 " [\n"
153 " {\"phoneNumber\": \"1\"},"
154 " {\"phoneNumber\": \"2\"}"
155 " ]\n"
156 "}";
157 sms_service()->SetNextRequestResponseData(net::HTTP_OK, query_phone_valid);
158 sms_service()->QueryPhoneNumber(
159 base::BindBlock(^(DesktopIOSPromotion::SMSService::Request* request,
msramek 2017/01/27 16:38:58 base::BindBlock is .mm code though, this doesn't l
justincohen 2017/01/27 23:06:46 Done.
160 bool success, const std::string& number) {
161 EXPECT_TRUE(success);
162 EXPECT_EQ(number, "1");
163 }));
164 std::string send_sms_valid = "{\"phoneNumber\": \"2\"}";
165 sms_service()->SetNextRequestResponseData(net::HTTP_OK, send_sms_valid);
166 std::string promo_id = "";
167 sms_service()->SendSMS(
168 promo_id,
169 base::BindBlock(^(DesktopIOSPromotion::SMSService::Request* request,
170 bool success, const std::string& number) {
171 EXPECT_TRUE(success);
172 EXPECT_EQ(number, "2");
173 }));
174
175 // Test that improperly formatted response returns no number.
176 std::string query_phone_invalid =
177 "{\n\"phoneNumber\": "
178 " [\n"
179 " ]\n"
180 "}";
181 sms_service()->SetNextRequestResponseData(net::HTTP_OK, query_phone_invalid);
182 sms_service()->QueryPhoneNumber(
183 base::BindBlock(^(DesktopIOSPromotion::SMSService::Request* request,
184 bool success, const std::string& number) {
185 EXPECT_FALSE(success);
186 EXPECT_EQ(number, "");
187 }));
188 std::string send_sms_invalid = "{}";
189 sms_service()->SetNextRequestResponseData(net::HTTP_OK, send_sms_invalid);
190 sms_service()->SendSMS(
191 promo_id,
192 base::BindBlock(^(DesktopIOSPromotion::SMSService::Request* request,
193 bool success, const std::string& number) {
194 EXPECT_FALSE(success);
195 EXPECT_EQ(number, "");
196 }));
197 }
198
199 } // namespace DesktopIOSPromotion
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698