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

Side by Side Diff: components/password_manager/content/renderer/credential_manager_client_browsertest.cc

Issue 533493004: Credential Manager: Refactor password_manager::CredentialManagerClient. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Explicit. Created 6 years, 3 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/password_manager/content/common/credential_manager_messages .h" 5 #include "components/password_manager/content/common/credential_manager_messages .h"
6 #include "components/password_manager/content/renderer/credential_manager_client .h" 6 #include "components/password_manager/content/renderer/credential_manager_client .h"
7 #include "components/password_manager/content/renderer/test_credential_manager_c lient.h" 7 #include "content/public/test/render_view_test.h"
8 #include "content/public/test/mock_render_thread.h"
9 #include "content/test/blink_test_environment.h"
10 #include "ipc/ipc_test_sink.h" 8 #include "ipc/ipc_test_sink.h"
11 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/WebKit/public/platform/WebCredential.h" 10 #include "third_party/WebKit/public/platform/WebCredential.h"
13 #include "third_party/WebKit/public/platform/WebCredentialManagerClient.h" 11 #include "third_party/WebKit/public/platform/WebCredentialManagerClient.h"
14 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h" 12 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h"
15 13
16 namespace password_manager { 14 namespace password_manager {
17 15
18 namespace { 16 namespace {
19 17
20 class CredentialManagerClientTest : public testing::Test { 18 class CredentialManagerClientTest : public content::RenderViewTest {
21 public: 19 public:
22 CredentialManagerClientTest() 20 CredentialManagerClientTest()
23 : callback_errored_(false), 21 : callback_errored_(false), callback_succeeded_(false) {}
24 callback_succeeded_(false) {
25 blink::WebString string = blink::WebString::fromUTF8("");
26 GURL url("https://example.com/image");
27 credential_.reset(new blink::WebCredential(string, string, url));
28 }
29 virtual ~CredentialManagerClientTest() {} 22 virtual ~CredentialManagerClientTest() {}
30 23
31 static void SetUpTestCase() { content::SetUpBlinkTestEnvironment(); } 24 virtual void SetUp() OVERRIDE {
25 content::RenderViewTest::SetUp();
26 credential_.reset(new blink::WebCredential("", "", GURL()));
27 client_.reset(new CredentialManagerClient(view_));
28 }
32 29
33 static void TearDownTestCase() { content::TearDownBlinkTestEnvironment(); } 30 virtual void TearDown() OVERRIDE {
31 credential_.reset();
32 content::RenderViewTest::TearDown();
33 }
34 34
35 IPC::TestSink& sink() { return render_thread_.sink(); } 35 IPC::TestSink& sink() { return render_thread_->sink(); }
36 36
37 blink::WebCredential* credential() { return credential_.get(); } 37 blink::WebCredential* credential() { return credential_.get(); }
38 38
39 // The browser's response to any of the messages the client sends must contain 39 // The browser's response to any of the messages the client sends must contain
40 // a request ID so that the client knows which request is being serviced. This 40 // a request ID so that the client knows which request is being serviced. This
41 // method grabs the ID from an outgoing |message_id| message, and sets the 41 // method grabs the ID from an outgoing |message_id| message, and sets the
42 // |request_id| param to its value. If no request ID can be found, the method 42 // |request_id| param to its value. If no request ID can be found, the method
43 // returns false, and the |request_id| is set to -1. 43 // returns false, and the |request_id| is set to -1.
44 // 44 //
45 // Clears any pending messages upon return. 45 // Clears any pending messages upon return.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 sink().ClearMessages(); 84 sink().ClearMessages();
85 return request_id != -1; 85 return request_id != -1;
86 } 86 }
87 87
88 bool callback_errored() const { return callback_errored_; } 88 bool callback_errored() const { return callback_errored_; }
89 void set_callback_errored(bool state) { callback_errored_ = state; } 89 void set_callback_errored(bool state) { callback_errored_ = state; }
90 bool callback_succeeded() const { return callback_succeeded_; } 90 bool callback_succeeded() const { return callback_succeeded_; }
91 void set_callback_succeeded(bool state) { callback_succeeded_ = state; } 91 void set_callback_succeeded(bool state) { callback_succeeded_ = state; }
92 92
93 protected: 93 protected:
94 content::MockRenderThread render_thread_; 94 scoped_ptr<CredentialManagerClient> client_;
95 TestCredentialManagerClient client_;
96 95
97 // True if a message's callback's 'onSuccess'/'onError' methods were called, 96 // True if a message's callback's 'onSuccess'/'onError' methods were called,
98 // false otherwise. We put these on the test object rather than on the 97 // false otherwise. We put these on the test object rather than on the
99 // Test*Callbacks objects because ownership of those objects passes into the 98 // Test*Callbacks objects because ownership of those objects passes into the
100 // client, which destroys the callbacks after calling them to resolve the 99 // client, which destroys the callbacks after calling them to resolve the
101 // pending Blink-side Promise. 100 // pending Blink-side Promise.
102 bool callback_errored_; 101 bool callback_errored_;
103 bool callback_succeeded_; 102 bool callback_succeeded_;
104 103
105 scoped_ptr<blink::WebCredential> credential_; 104 scoped_ptr<blink::WebCredential> credential_;
106 }; 105 };
107 106
108 class TestNotificationCallbacks 107 class TestNotificationCallbacks
109 : public blink::WebCredentialManagerClient::NotificationCallbacks { 108 : public blink::WebCredentialManagerClient::NotificationCallbacks {
110 public: 109 public:
111 explicit TestNotificationCallbacks(CredentialManagerClientTest* test) 110 explicit TestNotificationCallbacks(CredentialManagerClientTest* test)
112 : test_(test) { 111 : test_(test) {}
113 }
114 112
115 virtual ~TestNotificationCallbacks() {} 113 virtual ~TestNotificationCallbacks() {}
116 114
117 virtual void onSuccess() OVERRIDE { test_->set_callback_succeeded(true); } 115 virtual void onSuccess() OVERRIDE { test_->set_callback_succeeded(true); }
118 116
119 virtual void onError(blink::WebCredentialManagerError* reason) OVERRIDE { 117 virtual void onError(blink::WebCredentialManagerError* reason) OVERRIDE {
120 test_->set_callback_errored(true); 118 test_->set_callback_errored(true);
121 } 119 }
122 120
123 private: 121 private:
124 CredentialManagerClientTest* test_; 122 CredentialManagerClientTest* test_;
125 }; 123 };
126 124
127 class TestRequestCallbacks 125 class TestRequestCallbacks
128 : public blink::WebCredentialManagerClient::RequestCallbacks { 126 : public blink::WebCredentialManagerClient::RequestCallbacks {
129 public: 127 public:
130 explicit TestRequestCallbacks(CredentialManagerClientTest* test) 128 explicit TestRequestCallbacks(CredentialManagerClientTest* test)
131 : test_(test) { 129 : test_(test) {}
132 }
133 130
134 virtual ~TestRequestCallbacks() {} 131 virtual ~TestRequestCallbacks() {}
135 132
136 virtual void onSuccess(blink::WebCredential*) OVERRIDE { 133 virtual void onSuccess(blink::WebCredential*) OVERRIDE {
137 test_->set_callback_succeeded(true); 134 test_->set_callback_succeeded(true);
138 } 135 }
139 136
140 virtual void onError(blink::WebCredentialManagerError* reason) OVERRIDE { 137 virtual void onError(blink::WebCredentialManagerError* reason) OVERRIDE {
141 test_->set_callback_errored(true); 138 test_->set_callback_errored(true);
142 } 139 }
143 140
144 private: 141 private:
145 CredentialManagerClientTest* test_; 142 CredentialManagerClientTest* test_;
146 }; 143 };
147 144
148 } // namespace 145 } // namespace
149 146
150 TEST_F(CredentialManagerClientTest, SendNotifyFailedSignIn) { 147 TEST_F(CredentialManagerClientTest, SendNotifyFailedSignIn) {
151 int request_id; 148 int request_id;
152 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID, 149 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID,
153 request_id)); 150 request_id));
154 151
155 scoped_ptr<TestNotificationCallbacks> callbacks( 152 scoped_ptr<TestNotificationCallbacks> callbacks(
156 new TestNotificationCallbacks(this)); 153 new TestNotificationCallbacks(this));
157 client_.dispatchFailedSignIn(*credential(), callbacks.release()); 154 client_->dispatchFailedSignIn(*credential(), callbacks.release());
158 155
159 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID, 156 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID,
160 request_id)); 157 request_id));
161 158
162 client_.OnAcknowledgeFailedSignIn(request_id); 159 client_->OnAcknowledgeFailedSignIn(request_id);
163 EXPECT_TRUE(callback_succeeded()); 160 EXPECT_TRUE(callback_succeeded());
164 EXPECT_FALSE(callback_errored()); 161 EXPECT_FALSE(callback_errored());
165 } 162 }
166 163
167 TEST_F(CredentialManagerClientTest, SendNotifySignedIn) { 164 TEST_F(CredentialManagerClientTest, SendNotifySignedIn) {
168 int request_id; 165 int request_id;
169 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID, 166 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID,
170 request_id)); 167 request_id));
171 168
172 scoped_ptr<TestNotificationCallbacks> callbacks( 169 scoped_ptr<TestNotificationCallbacks> callbacks(
173 new TestNotificationCallbacks(this)); 170 new TestNotificationCallbacks(this));
174 client_.dispatchSignedIn(*credential(), callbacks.release()); 171 client_->dispatchSignedIn(*credential(), callbacks.release());
175 172
176 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID, 173 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID,
177 request_id)); 174 request_id));
178 175
179 client_.OnAcknowledgeSignedIn(request_id); 176 client_->OnAcknowledgeSignedIn(request_id);
180 EXPECT_TRUE(callback_succeeded()); 177 EXPECT_TRUE(callback_succeeded());
181 EXPECT_FALSE(callback_errored()); 178 EXPECT_FALSE(callback_errored());
182 } 179 }
183 180
184 TEST_F(CredentialManagerClientTest, SendNotifySignedOut) { 181 TEST_F(CredentialManagerClientTest, SendNotifySignedOut) {
185 int request_id; 182 int request_id;
186 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID, 183 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID,
187 request_id)); 184 request_id));
188 185
189 scoped_ptr<TestNotificationCallbacks> callbacks( 186 scoped_ptr<TestNotificationCallbacks> callbacks(
190 new TestNotificationCallbacks(this)); 187 new TestNotificationCallbacks(this));
191 client_.dispatchSignedOut(callbacks.release()); 188 client_->dispatchSignedOut(callbacks.release());
192 189
193 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID, 190 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID,
194 request_id)); 191 request_id));
195 192
196 client_.OnAcknowledgeSignedOut(request_id); 193 client_->OnAcknowledgeSignedOut(request_id);
197 EXPECT_TRUE(callback_succeeded()); 194 EXPECT_TRUE(callback_succeeded());
198 EXPECT_FALSE(callback_errored()); 195 EXPECT_FALSE(callback_errored());
199 } 196 }
200 197
201 TEST_F(CredentialManagerClientTest, SendRequestCredential) { 198 TEST_F(CredentialManagerClientTest, SendRequestCredential) {
202 int request_id; 199 int request_id;
203 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, 200 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
204 request_id)); 201 request_id));
205 202
206 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this)); 203 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this));
207 std::vector<GURL> federations; 204 std::vector<GURL> federations;
208 client_.dispatchRequest(false, federations, callbacks.release()); 205 client_->dispatchRequest(false, federations, callbacks.release());
209 206
210 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, 207 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
211 request_id)); 208 request_id));
212 209
213 CredentialInfo info; 210 CredentialInfo info;
214 client_.OnSendCredential(request_id, info); 211 client_->OnSendCredential(request_id, info);
215 EXPECT_TRUE(callback_succeeded()); 212 EXPECT_TRUE(callback_succeeded());
216 EXPECT_FALSE(callback_errored()); 213 EXPECT_FALSE(callback_errored());
217 } 214 }
218 215
219 } // namespace password_manager 216 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698