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

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

Powered by Google App Engine
This is Rietveld 408576698