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

Side by Side Diff: chrome/browser/extensions/api/passwords_private/passwords_private_apitest.cc

Issue 1142693003: Implement the chrome.passwordsPrivate API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: stevenjb comment. Created 5 years, 6 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 2015 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 <sstream>
6
7 #include "base/command_line.h"
8 #include "base/memory/linked_ptr.h"
9 #include "base/observer_list_threadsafe.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "chrome/browser/extensions/api/passwords_private/passwords_private_dele gate.h"
13 #include "chrome/browser/extensions/api/passwords_private/passwords_private_dele gate_factory.h"
14 #include "chrome/browser/extensions/extension_apitest.h"
15 #include "chrome/common/extensions/api/passwords_private.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "content/public/test/test_utils.h"
19 #include "extensions/common/switches.h"
20
21 namespace extensions {
22
23 namespace {
24
25 static const size_t kNumMocks = 3;
26 static const int kNumCharactersInPassword = 10;
27 static const char kPlaintextPassword[] = "plaintext";
28
29 linked_ptr<api::passwords_private::PasswordUiEntry> CreateEntry(size_t num) {
30 api::passwords_private::PasswordUiEntry* entry =
31 new api::passwords_private::PasswordUiEntry();
32 std::stringstream ss;
33 ss << "http://test" << num << ".com";
34 entry->login_pair.origin_url = ss.str();
35 ss.clear();
36 ss << "testName" << num;
37 entry->login_pair.username = ss.str();
38 entry->num_characters_in_password = kNumCharactersInPassword;
39 return make_linked_ptr(entry);
40 }
41
42 std::string CreateException(size_t num) {
43 std::stringstream ss;
44 ss << "http://exception" << num << ".com";
45 return ss.str();
46 }
47
48 // A test PasswordsPrivateDelegate implementation which uses mock data.
49 // TestDelegate starts out with kNumMocks mocks of each type (saved password
50 // and password exception) and removes one mock each time RemoveSavedPassword()
51 // or RemovePasswordException() is called.
52 class TestDelegate : public PasswordsPrivateDelegate {
53 public:
54 TestDelegate() : observers_(new base::ObserverListThreadSafe<Observer>()) {
55 // Create mock data.
56 for (size_t i = 0; i < kNumMocks; i++) {
57 current_entries_.push_back(CreateEntry(i));;
58 current_exceptions_.push_back(CreateException(i));
59 }
60 }
61 ~TestDelegate() override {}
62
63 void AddObserver(Observer* observer) override {
64 observers_->AddObserver(observer);
65 SendSavedPasswordsList();
66 SendPasswordExceptionsList();
67 }
68
69 void RemoveObserver(Observer* observer) override {
70 observers_->RemoveObserver(observer);
71 }
72
73 void RemoveSavedPassword(
74 const std::string& origin_url, const std::string& username) override {
75 if (!current_entries_.size())
76 return;
77
78 // Since this is just mock data, remove the first entry regardless of
79 // the data contained.
80 current_entries_.erase(current_entries_.begin());
81 SendSavedPasswordsList();
82 }
83
84 void RemovePasswordException(const std::string& exception_url) override {
85 if (!current_exceptions_.size())
86 return;
87
88 // Since this is just mock data, remove the first entry regardless of
89 // the data contained.
90 current_exceptions_.erase(current_exceptions_.begin());
91 SendPasswordExceptionsList();
92 }
93
94 void RequestShowPassword(
95 const std::string& origin_url,
96 const std::string& username,
97 const content::RenderViewHost* render_view_host) override {
98 // Return a mocked password value.
99 std::string plaintext_password(kPlaintextPassword);
100 observers_->Notify(
101 FROM_HERE,
102 &Observer::OnPlaintextPasswordFetched,
103 origin_url,
104 username,
105 plaintext_password);
106 }
107
108 private:
109 void SendSavedPasswordsList() {
110 observers_->Notify(
111 FROM_HERE,
112 &Observer::OnSavedPasswordsListChanged,
113 current_entries_);
114 }
115
116 void SendPasswordExceptionsList() {
117 observers_->Notify(
118 FROM_HERE,
119 &Observer::OnPasswordExceptionsListChanged,
120 current_exceptions_);
121 }
122
123 // The current list of entries/exceptions. Cached here so that when new
124 // observers are added, this delegate can send the current lists without
125 // having to request them from |password_manager_presenter_| again.
126 std::vector<linked_ptr<api::passwords_private::PasswordUiEntry>>
127 current_entries_;
128 std::vector<std::string> current_exceptions_;
129
130 // The observers.
131 scoped_refptr<base::ObserverListThreadSafe<Observer>> observers_;
132 };
133
134 class PasswordsPrivateApiTest : public ExtensionApiTest {
135 public:
136 PasswordsPrivateApiTest() {
137 if (!s_test_delegate_) {
138 s_test_delegate_ = new TestDelegate();
139 }
140 }
141 ~PasswordsPrivateApiTest() override {}
142
143 static scoped_ptr<KeyedService> GetPasswordsPrivateDelegate(
144 content::BrowserContext* profile) {
145 CHECK(s_test_delegate_);
146 return make_scoped_ptr(s_test_delegate_);
147 }
148
149 void SetUpCommandLine(base::CommandLine* command_line) override {
150 ExtensionApiTest::SetUpCommandLine(command_line);
151 }
152
153 void SetUp() override {
154 ExtensionApiTest::SetUp();
155 }
156
157 void SetUpOnMainThread() override {
158 ExtensionApiTest::SetUpOnMainThread();
159 PasswordsPrivateDelegateFactory::GetInstance()->SetTestingFactory(
160 profile(), &PasswordsPrivateApiTest::GetPasswordsPrivateDelegate);
161 content::RunAllPendingInMessageLoop();
162 }
163
164 protected:
165 bool RunPasswordsSubtest(const std::string& subtest) {
166 return RunExtensionSubtest("passwords_private",
167 "main.html?" + subtest,
168 kFlagLoadAsComponent);
169 }
170
171 private:
172 static TestDelegate* s_test_delegate_;
173
174 DISALLOW_COPY_AND_ASSIGN(PasswordsPrivateApiTest);
175 };
176
177 // static
178 TestDelegate* PasswordsPrivateApiTest::s_test_delegate_ = nullptr;
179
180 } // namespace
181
182 IN_PROC_BROWSER_TEST_F(PasswordsPrivateApiTest, CanPasswordAccountBeManaged) {
183 EXPECT_TRUE(RunPasswordsSubtest("canPasswordAccountBeManaged")) << message_;
184 }
185
186 IN_PROC_BROWSER_TEST_F(PasswordsPrivateApiTest, RemoveSavedPassword) {
187 EXPECT_TRUE(RunPasswordsSubtest("removeSavedPassword")) << message_;
188 }
189
190 IN_PROC_BROWSER_TEST_F(PasswordsPrivateApiTest, RemovePasswordException) {
191 EXPECT_TRUE(RunPasswordsSubtest("removePasswordException")) << message_;
192 }
193
194 IN_PROC_BROWSER_TEST_F(PasswordsPrivateApiTest, RequestPlaintextPassword) {
195 EXPECT_TRUE(RunPasswordsSubtest("requestPlaintextPassword")) << message_;
196 }
197
198 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698