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

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

Powered by Google App Engine
This is Rietveld 408576698