| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/linked_ptr.h" | 11 #include "base/memory/linked_ptr.h" |
| 12 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/extensions/api/passwords_private/passwords_private_dele
gate.h" | 15 #include "chrome/browser/extensions/api/passwords_private/passwords_private_dele
gate.h" |
| 16 #include "chrome/browser/extensions/api/passwords_private/passwords_private_dele
gate_factory.h" | 16 #include "chrome/browser/extensions/api/passwords_private/passwords_private_dele
gate_factory.h" |
| 17 #include "chrome/browser/extensions/api/passwords_private/passwords_private_even
t_router.h" |
| 18 #include "chrome/browser/extensions/api/passwords_private/passwords_private_even
t_router_factory.h" |
| 17 #include "chrome/browser/extensions/extension_apitest.h" | 19 #include "chrome/browser/extensions/extension_apitest.h" |
| 18 #include "chrome/common/extensions/api/passwords_private.h" | 20 #include "chrome/common/extensions/api/passwords_private.h" |
| 19 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
| 20 #include "components/keyed_service/core/keyed_service.h" | 22 #include "components/keyed_service/core/keyed_service.h" |
| 21 #include "content/public/test/test_utils.h" | 23 #include "content/public/test/test_utils.h" |
| 22 #include "extensions/common/switches.h" | 24 #include "extensions/common/switches.h" |
| 23 | 25 |
| 24 namespace extensions { | 26 namespace extensions { |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 46 ss << "http://exception" << num << ".com"; | 48 ss << "http://exception" << num << ".com"; |
| 47 return ss.str(); | 49 return ss.str(); |
| 48 } | 50 } |
| 49 | 51 |
| 50 // A test PasswordsPrivateDelegate implementation which uses mock data. | 52 // A test PasswordsPrivateDelegate implementation which uses mock data. |
| 51 // TestDelegate starts out with kNumMocks mocks of each type (saved password | 53 // TestDelegate starts out with kNumMocks mocks of each type (saved password |
| 52 // and password exception) and removes one mock each time RemoveSavedPassword() | 54 // and password exception) and removes one mock each time RemoveSavedPassword() |
| 53 // or RemovePasswordException() is called. | 55 // or RemovePasswordException() is called. |
| 54 class TestDelegate : public PasswordsPrivateDelegate { | 56 class TestDelegate : public PasswordsPrivateDelegate { |
| 55 public: | 57 public: |
| 56 TestDelegate() { | 58 TestDelegate() : profile_(nullptr) { |
| 57 // Create mock data. | 59 // Create mock data. |
| 58 for (size_t i = 0; i < kNumMocks; i++) { | 60 for (size_t i = 0; i < kNumMocks; i++) { |
| 59 current_entries_.push_back(CreateEntry(i)); | 61 current_entries_.push_back(CreateEntry(i)); |
| 60 current_exceptions_.push_back(CreateException(i)); | 62 current_exceptions_.push_back(CreateException(i)); |
| 61 } | 63 } |
| 62 } | 64 } |
| 63 ~TestDelegate() override {} | 65 ~TestDelegate() override {} |
| 64 | 66 |
| 65 void AddObserver(Observer* observer) override { | 67 void SendSavedPasswordsList() override { |
| 66 observers_.AddObserver(observer); | 68 PasswordsPrivateEventRouter* router = |
| 67 SendSavedPasswordsList(); | 69 PasswordsPrivateEventRouterFactory::GetForProfile(profile_); |
| 68 SendPasswordExceptionsList(); | 70 if (router) |
| 71 router->OnSavedPasswordsListChanged(current_entries_); |
| 69 } | 72 } |
| 70 | 73 |
| 71 void RemoveObserver(Observer* observer) override { | 74 void SendPasswordExceptionsList() override { |
| 72 observers_.RemoveObserver(observer); | 75 PasswordsPrivateEventRouter* router = |
| 76 PasswordsPrivateEventRouterFactory::GetForProfile(profile_); |
| 77 if (router) |
| 78 router->OnPasswordExceptionsListChanged(current_exceptions_); |
| 73 } | 79 } |
| 74 | 80 |
| 75 void RemoveSavedPassword( | 81 void RemoveSavedPassword( |
| 76 const std::string& origin_url, const std::string& username) override { | 82 const std::string& origin_url, const std::string& username) override { |
| 77 if (!current_entries_.size()) | 83 if (!current_entries_.size()) |
| 78 return; | 84 return; |
| 79 | 85 |
| 80 // Since this is just mock data, remove the first entry regardless of | 86 // Since this is just mock data, remove the first entry regardless of |
| 81 // the data contained. | 87 // the data contained. |
| 82 current_entries_.erase(current_entries_.begin()); | 88 current_entries_.erase(current_entries_.begin()); |
| 83 SendSavedPasswordsList(); | 89 SendSavedPasswordsList(); |
| 84 } | 90 } |
| 85 | 91 |
| 86 void RemovePasswordException(const std::string& exception_url) override { | 92 void RemovePasswordException(const std::string& exception_url) override { |
| 87 if (!current_exceptions_.size()) | 93 if (!current_exceptions_.size()) |
| 88 return; | 94 return; |
| 89 | 95 |
| 90 // Since this is just mock data, remove the first entry regardless of | 96 // Since this is just mock data, remove the first entry regardless of |
| 91 // the data contained. | 97 // the data contained. |
| 92 current_exceptions_.erase(current_exceptions_.begin()); | 98 current_exceptions_.erase(current_exceptions_.begin()); |
| 93 SendPasswordExceptionsList(); | 99 SendPasswordExceptionsList(); |
| 94 } | 100 } |
| 95 | 101 |
| 96 void RequestShowPassword(const std::string& origin_url, | 102 void RequestShowPassword(const std::string& origin_url, |
| 97 const std::string& username, | 103 const std::string& username, |
| 98 content::WebContents* web_contents) override { | 104 content::WebContents* web_contents) override { |
| 99 // Return a mocked password value. | 105 // Return a mocked password value. |
| 100 std::string plaintext_password(kPlaintextPassword); | 106 std::string plaintext_password(kPlaintextPassword); |
| 101 FOR_EACH_OBSERVER( | 107 PasswordsPrivateEventRouter* router = |
| 102 Observer, observers_, | 108 PasswordsPrivateEventRouterFactory::GetForProfile(profile_); |
| 103 OnPlaintextPasswordFetched(origin_url, username, plaintext_password)); | 109 if (router) { |
| 110 router->OnPlaintextPasswordFetched(origin_url, username, |
| 111 plaintext_password); |
| 112 } |
| 104 } | 113 } |
| 105 | 114 |
| 115 void SetProfile(Profile* profile) { profile_ = profile; } |
| 116 |
| 106 private: | 117 private: |
| 107 void SendSavedPasswordsList() { | |
| 108 FOR_EACH_OBSERVER(Observer, observers_, | |
| 109 OnSavedPasswordsListChanged(current_entries_)); | |
| 110 } | |
| 111 | |
| 112 void SendPasswordExceptionsList() { | |
| 113 FOR_EACH_OBSERVER(Observer, observers_, | |
| 114 OnPasswordExceptionsListChanged(current_exceptions_)); | |
| 115 } | |
| 116 | |
| 117 // The current list of entries/exceptions. Cached here so that when new | 118 // The current list of entries/exceptions. Cached here so that when new |
| 118 // observers are added, this delegate can send the current lists without | 119 // observers are added, this delegate can send the current lists without |
| 119 // having to request them from |password_manager_presenter_| again. | 120 // having to request them from |password_manager_presenter_| again. |
| 120 std::vector<api::passwords_private::PasswordUiEntry> current_entries_; | 121 std::vector<api::passwords_private::PasswordUiEntry> current_entries_; |
| 121 std::vector<std::string> current_exceptions_; | 122 std::vector<std::string> current_exceptions_; |
| 122 | 123 Profile* profile_; |
| 123 // The observers. | |
| 124 base::ObserverList<Observer> observers_; | |
| 125 }; | 124 }; |
| 126 | 125 |
| 127 class PasswordsPrivateApiTest : public ExtensionApiTest { | 126 class PasswordsPrivateApiTest : public ExtensionApiTest { |
| 128 public: | 127 public: |
| 129 PasswordsPrivateApiTest() { | 128 PasswordsPrivateApiTest() { |
| 130 if (!s_test_delegate_) { | 129 if (!s_test_delegate_) { |
| 131 s_test_delegate_ = new TestDelegate(); | 130 s_test_delegate_ = new TestDelegate(); |
| 132 } | 131 } |
| 133 } | 132 } |
| 134 ~PasswordsPrivateApiTest() override {} | 133 ~PasswordsPrivateApiTest() override {} |
| 135 | 134 |
| 136 static scoped_ptr<KeyedService> GetPasswordsPrivateDelegate( | 135 static scoped_ptr<KeyedService> GetPasswordsPrivateDelegate( |
| 137 content::BrowserContext* profile) { | 136 content::BrowserContext* profile) { |
| 138 CHECK(s_test_delegate_); | 137 CHECK(s_test_delegate_); |
| 139 return make_scoped_ptr(s_test_delegate_); | 138 return make_scoped_ptr(s_test_delegate_); |
| 140 } | 139 } |
| 141 | 140 |
| 142 void SetUpCommandLine(base::CommandLine* command_line) override { | 141 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 143 ExtensionApiTest::SetUpCommandLine(command_line); | 142 ExtensionApiTest::SetUpCommandLine(command_line); |
| 144 } | 143 } |
| 145 | 144 |
| 146 void SetUp() override { | 145 void SetUp() override { |
| 147 ExtensionApiTest::SetUp(); | 146 ExtensionApiTest::SetUp(); |
| 148 } | 147 } |
| 149 | 148 |
| 150 void SetUpOnMainThread() override { | 149 void SetUpOnMainThread() override { |
| 151 ExtensionApiTest::SetUpOnMainThread(); | 150 ExtensionApiTest::SetUpOnMainThread(); |
| 152 PasswordsPrivateDelegateFactory::GetInstance()->SetTestingFactory( | 151 PasswordsPrivateDelegateFactory::GetInstance()->SetTestingFactory( |
| 153 profile(), &PasswordsPrivateApiTest::GetPasswordsPrivateDelegate); | 152 profile(), &PasswordsPrivateApiTest::GetPasswordsPrivateDelegate); |
| 153 s_test_delegate_->SetProfile(profile()); |
| 154 content::RunAllPendingInMessageLoop(); | 154 content::RunAllPendingInMessageLoop(); |
| 155 } | 155 } |
| 156 | 156 |
| 157 protected: | 157 protected: |
| 158 bool RunPasswordsSubtest(const std::string& subtest) { | 158 bool RunPasswordsSubtest(const std::string& subtest) { |
| 159 return RunExtensionSubtest("passwords_private", | 159 return RunExtensionSubtest("passwords_private", |
| 160 "main.html?" + subtest, | 160 "main.html?" + subtest, |
| 161 kFlagLoadAsComponent); | 161 kFlagLoadAsComponent); |
| 162 } | 162 } |
| 163 | 163 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 178 | 178 |
| 179 IN_PROC_BROWSER_TEST_F(PasswordsPrivateApiTest, RemovePasswordException) { | 179 IN_PROC_BROWSER_TEST_F(PasswordsPrivateApiTest, RemovePasswordException) { |
| 180 EXPECT_TRUE(RunPasswordsSubtest("removePasswordException")) << message_; | 180 EXPECT_TRUE(RunPasswordsSubtest("removePasswordException")) << message_; |
| 181 } | 181 } |
| 182 | 182 |
| 183 IN_PROC_BROWSER_TEST_F(PasswordsPrivateApiTest, RequestPlaintextPassword) { | 183 IN_PROC_BROWSER_TEST_F(PasswordsPrivateApiTest, RequestPlaintextPassword) { |
| 184 EXPECT_TRUE(RunPasswordsSubtest("requestPlaintextPassword")) << message_; | 184 EXPECT_TRUE(RunPasswordsSubtest("requestPlaintextPassword")) << message_; |
| 185 } | 185 } |
| 186 | 186 |
| 187 } // namespace extensions | 187 } // namespace extensions |
| OLD | NEW |