| OLD | NEW |
| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/prefs/pref_registry_simple.h" | 7 #include "base/prefs/pref_registry_simple.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/prefs/testing_pref_service.h" | 9 #include "base/prefs/testing_pref_service.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 using base::ASCIIToUTF16; | 26 using base::ASCIIToUTF16; |
| 27 | 27 |
| 28 namespace password_manager { | 28 namespace password_manager { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 class TestPasswordManagerDriver : public StubPasswordManagerDriver { | 32 class TestPasswordManagerDriver : public StubPasswordManagerDriver { |
| 33 public: | 33 public: |
| 34 TestPasswordManagerDriver(PasswordManagerClient* client) | 34 TestPasswordManagerDriver(PasswordManagerClient* client) |
| 35 : password_manager_(client), | 35 : password_manager_(client), |
| 36 password_generation_manager_(client), | 36 password_generation_manager_(client, this), |
| 37 password_autofill_manager_(client, NULL), | 37 password_autofill_manager_(client, this, NULL) {} |
| 38 is_off_the_record_(false) {} | |
| 39 ~TestPasswordManagerDriver() override {} | 38 ~TestPasswordManagerDriver() override {} |
| 40 | 39 |
| 41 // PasswordManagerDriver implementation. | 40 // PasswordManagerDriver implementation. |
| 42 bool IsOffTheRecord() override { return is_off_the_record_; } | |
| 43 PasswordGenerationManager* GetPasswordGenerationManager() override { | 41 PasswordGenerationManager* GetPasswordGenerationManager() override { |
| 44 return &password_generation_manager_; | 42 return &password_generation_manager_; |
| 45 } | 43 } |
| 46 PasswordManager* GetPasswordManager() override { return &password_manager_; } | 44 PasswordManager* GetPasswordManager() override { return &password_manager_; } |
| 47 PasswordAutofillManager* GetPasswordAutofillManager() override { | 45 PasswordAutofillManager* GetPasswordAutofillManager() override { |
| 48 return &password_autofill_manager_; | 46 return &password_autofill_manager_; |
| 49 } | 47 } |
| 50 void AccountCreationFormsFound( | 48 void AccountCreationFormsFound( |
| 51 const std::vector<autofill::FormData>& forms) override { | 49 const std::vector<autofill::FormData>& forms) override { |
| 52 found_account_creation_forms_.insert( | 50 found_account_creation_forms_.insert( |
| 53 found_account_creation_forms_.begin(), forms.begin(), forms.end()); | 51 found_account_creation_forms_.begin(), forms.begin(), forms.end()); |
| 54 } | 52 } |
| 55 | 53 |
| 56 const std::vector<autofill::FormData>& GetFoundAccountCreationForms() { | 54 const std::vector<autofill::FormData>& GetFoundAccountCreationForms() { |
| 57 return found_account_creation_forms_; | 55 return found_account_creation_forms_; |
| 58 } | 56 } |
| 59 void set_is_off_the_record(bool is_off_the_record) { | |
| 60 is_off_the_record_ = is_off_the_record; | |
| 61 } | |
| 62 | 57 |
| 63 private: | 58 private: |
| 64 PasswordManager password_manager_; | 59 PasswordManager password_manager_; |
| 65 PasswordGenerationManager password_generation_manager_; | 60 PasswordGenerationManager password_generation_manager_; |
| 66 PasswordAutofillManager password_autofill_manager_; | 61 PasswordAutofillManager password_autofill_manager_; |
| 67 std::vector<autofill::FormData> found_account_creation_forms_; | 62 std::vector<autofill::FormData> found_account_creation_forms_; |
| 68 bool is_off_the_record_; | |
| 69 }; | 63 }; |
| 70 | 64 |
| 71 class TestPasswordManagerClient : public StubPasswordManagerClient { | 65 class TestPasswordManagerClient : public StubPasswordManagerClient { |
| 72 public: | 66 public: |
| 73 TestPasswordManagerClient(scoped_ptr<PrefService> prefs) | 67 TestPasswordManagerClient(scoped_ptr<PrefService> prefs) |
| 74 : prefs_(prefs.Pass()), | 68 : prefs_(prefs.Pass()), |
| 75 store_(new TestPasswordStore), | 69 store_(new TestPasswordStore), |
| 76 driver_(this), | 70 driver_(this), |
| 77 is_sync_enabled_(false) {} | 71 is_sync_enabled_(false), |
| 72 is_off_the_record_(false) {} |
| 78 | 73 |
| 79 ~TestPasswordManagerClient() override { | 74 ~TestPasswordManagerClient() override { |
| 80 store_->Shutdown(); | 75 store_->Shutdown(); |
| 81 } | 76 } |
| 82 | 77 |
| 78 bool IsOffTheRecord() override { return is_off_the_record_; } |
| 83 PasswordStore* GetPasswordStore() override { return store_.get(); } | 79 PasswordStore* GetPasswordStore() override { return store_.get(); } |
| 84 PrefService* GetPrefs() override { return prefs_.get(); } | 80 PrefService* GetPrefs() override { return prefs_.get(); } |
| 85 PasswordManagerDriver* GetDriver() override { return &driver_; } | |
| 86 bool IsPasswordSyncEnabled(CustomPassphraseState state) override { | 81 bool IsPasswordSyncEnabled(CustomPassphraseState state) override { |
| 87 return is_sync_enabled_; | 82 return is_sync_enabled_; |
| 88 } | 83 } |
| 89 | 84 |
| 90 void set_is_password_sync_enabled(bool enabled) { | 85 void set_is_password_sync_enabled(bool enabled) { |
| 91 is_sync_enabled_ = enabled; | 86 is_sync_enabled_ = enabled; |
| 92 } | 87 } |
| 93 | 88 |
| 89 TestPasswordManagerDriver* test_driver() { return &driver_; } |
| 90 void set_is_off_the_record(bool is_otr) { is_off_the_record_ = is_otr; } |
| 91 |
| 94 private: | 92 private: |
| 95 scoped_ptr<PrefService> prefs_; | 93 scoped_ptr<PrefService> prefs_; |
| 96 scoped_refptr<TestPasswordStore> store_; | 94 scoped_refptr<TestPasswordStore> store_; |
| 97 TestPasswordManagerDriver driver_; | 95 TestPasswordManagerDriver driver_; |
| 98 bool is_sync_enabled_; | 96 bool is_sync_enabled_; |
| 97 bool is_off_the_record_; |
| 99 }; | 98 }; |
| 100 | 99 |
| 101 // Unlike the base AutofillMetrics, exposes copy and assignment constructors, | 100 // Unlike the base AutofillMetrics, exposes copy and assignment constructors, |
| 102 // which are handy for briefer test code. The AutofillMetrics class is | 101 // which are handy for briefer test code. The AutofillMetrics class is |
| 103 // stateless, so this is safe. | 102 // stateless, so this is safe. |
| 104 class TestAutofillMetrics : public autofill::AutofillMetrics { | 103 class TestAutofillMetrics : public autofill::AutofillMetrics { |
| 105 public: | 104 public: |
| 106 TestAutofillMetrics() {} | 105 TestAutofillMetrics() {} |
| 107 ~TestAutofillMetrics() override {} | 106 ~TestAutofillMetrics() override {} |
| 108 }; | 107 }; |
| 109 | 108 |
| 110 } // anonymous namespace | 109 } // anonymous namespace |
| 111 | 110 |
| 112 class PasswordGenerationManagerTest : public testing::Test { | 111 class PasswordGenerationManagerTest : public testing::Test { |
| 113 protected: | 112 protected: |
| 114 void SetUp() override { | 113 void SetUp() override { |
| 115 // Construct a PrefService and register all necessary prefs before handing | 114 // Construct a PrefService and register all necessary prefs before handing |
| 116 // it off to |client_|, as the initialization flow of |client_| will | 115 // it off to |client_|, as the initialization flow of |client_| will |
| 117 // indirectly cause those prefs to be immediately accessed. | 116 // indirectly cause those prefs to be immediately accessed. |
| 118 scoped_ptr<TestingPrefServiceSimple> prefs(new TestingPrefServiceSimple()); | 117 scoped_ptr<TestingPrefServiceSimple> prefs(new TestingPrefServiceSimple()); |
| 119 prefs->registry()->RegisterBooleanPref(prefs::kPasswordManagerSavingEnabled, | 118 prefs->registry()->RegisterBooleanPref(prefs::kPasswordManagerSavingEnabled, |
| 120 true); | 119 true); |
| 121 client_.reset(new TestPasswordManagerClient(prefs.Pass())); | 120 client_.reset(new TestPasswordManagerClient(prefs.Pass())); |
| 122 } | 121 } |
| 123 | 122 |
| 124 void TearDown() override { client_.reset(); } | 123 void TearDown() override { client_.reset(); } |
| 125 | 124 |
| 126 PasswordGenerationManager* GetGenerationManager() { | 125 PasswordGenerationManager* GetGenerationManager() { |
| 127 return client_->GetDriver()->GetPasswordGenerationManager(); | 126 return client_->test_driver()->GetPasswordGenerationManager(); |
| 128 } | 127 } |
| 129 | 128 |
| 130 TestPasswordManagerDriver* GetTestDriver() { | 129 TestPasswordManagerDriver* GetTestDriver() { return client_->test_driver(); } |
| 131 return static_cast<TestPasswordManagerDriver*>(client_->GetDriver()); | |
| 132 } | |
| 133 | 130 |
| 134 bool IsGenerationEnabled() { | 131 bool IsGenerationEnabled() { |
| 135 return GetGenerationManager()->IsGenerationEnabled(); | 132 return GetGenerationManager()->IsGenerationEnabled(); |
| 136 } | 133 } |
| 137 | 134 |
| 138 void DetectAccountCreationForms( | 135 void DetectAccountCreationForms( |
| 139 const std::vector<autofill::FormStructure*>& forms) { | 136 const std::vector<autofill::FormStructure*>& forms) { |
| 140 GetGenerationManager()->DetectAccountCreationForms(forms); | 137 GetGenerationManager()->DetectAccountCreationForms(forms); |
| 141 } | 138 } |
| 142 | 139 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 DetectAccountCreationForms(forms); | 207 DetectAccountCreationForms(forms); |
| 211 EXPECT_EQ(1u, GetTestDriver()->GetFoundAccountCreationForms().size()); | 208 EXPECT_EQ(1u, GetTestDriver()->GetFoundAccountCreationForms().size()); |
| 212 EXPECT_EQ(GURL("http://accounts.yahoo.com/"), | 209 EXPECT_EQ(GURL("http://accounts.yahoo.com/"), |
| 213 GetTestDriver()->GetFoundAccountCreationForms()[0].origin); | 210 GetTestDriver()->GetFoundAccountCreationForms()[0].origin); |
| 214 } | 211 } |
| 215 | 212 |
| 216 TEST_F(PasswordGenerationManagerTest, UpdatePasswordSyncStateIncognito) { | 213 TEST_F(PasswordGenerationManagerTest, UpdatePasswordSyncStateIncognito) { |
| 217 // Disable password manager by going incognito. Even though password | 214 // Disable password manager by going incognito. Even though password |
| 218 // syncing is enabled, generation should still | 215 // syncing is enabled, generation should still |
| 219 // be disabled. | 216 // be disabled. |
| 220 GetTestDriver()->set_is_off_the_record(true); | 217 client_->set_is_off_the_record(true); |
| 221 PrefService* prefs = client_->GetPrefs(); | 218 PrefService* prefs = client_->GetPrefs(); |
| 222 prefs->SetBoolean(prefs::kPasswordManagerSavingEnabled, true); | 219 prefs->SetBoolean(prefs::kPasswordManagerSavingEnabled, true); |
| 223 client_->set_is_password_sync_enabled(true); | 220 client_->set_is_password_sync_enabled(true); |
| 224 | 221 |
| 225 EXPECT_FALSE(IsGenerationEnabled()); | 222 EXPECT_FALSE(IsGenerationEnabled()); |
| 226 } | 223 } |
| 227 | 224 |
| 228 } // namespace password_manager | 225 } // namespace password_manager |
| OLD | NEW |