OLD | NEW |
(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 "chrome/browser/autofill/autofill_uitest_util.h" |
| 6 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
| 7 #include "chrome/browser/infobars/infobar_service.h" |
| 8 #include "chrome/browser/ui/browser.h" |
| 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 10 #include "components/autofill/core/browser/personal_data_manager.h" |
| 11 #include "components/autofill/core/browser/personal_data_manager_observer.h" |
| 12 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 13 #include "components/infobars/core/infobar.h" |
| 14 #include "components/infobars/core/infobar_manager.h" |
| 15 #include "content/public/test/test_utils.h" |
| 16 |
| 17 namespace autofill { |
| 18 |
| 19 // This class is used to wait for asynchronous updates to PersonalDataManager |
| 20 // to complete. |
| 21 class PdmChangeWaiter |
| 22 : public PersonalDataManagerObserver, |
| 23 public infobars::InfoBarManager::Observer { |
| 24 public: |
| 25 explicit PdmChangeWaiter(Browser* browser) |
| 26 : alerted_(false), |
| 27 has_run_message_loop_(false), |
| 28 browser_(browser), |
| 29 infobar_service_(InfoBarService::FromWebContents( |
| 30 browser_->tab_strip_model()->GetActiveWebContents())) { |
| 31 PersonalDataManagerFactory::GetForProfile(browser_->profile())-> |
| 32 AddObserver(this); |
| 33 infobar_service_->AddObserver(this); |
| 34 } |
| 35 |
| 36 ~PdmChangeWaiter() override { |
| 37 while (infobar_service_->infobar_count() > 0) { |
| 38 infobar_service_->RemoveInfoBar(infobar_service_->infobar_at(0)); |
| 39 } |
| 40 infobar_service_->RemoveObserver(this); |
| 41 } |
| 42 |
| 43 // PersonalDataManagerObserver: |
| 44 void OnPersonalDataChanged() override { |
| 45 if (has_run_message_loop_) { |
| 46 base::MessageLoopForUI::current()->Quit(); |
| 47 has_run_message_loop_ = false; |
| 48 } |
| 49 alerted_ = true; |
| 50 } |
| 51 |
| 52 void OnInsufficientFormData() override { OnPersonalDataChanged(); } |
| 53 |
| 54 |
| 55 void Wait() { |
| 56 if (!alerted_) { |
| 57 has_run_message_loop_ = true; |
| 58 content::RunMessageLoop(); |
| 59 } |
| 60 PersonalDataManagerFactory::GetForProfile(browser_->profile())-> |
| 61 RemoveObserver(this); |
| 62 } |
| 63 |
| 64 private: |
| 65 // infobars::InfoBarManager::Observer: |
| 66 void OnInfoBarAdded(infobars::InfoBar* infobar) override { |
| 67 infobar_service_->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate()-> |
| 68 Accept(); |
| 69 } |
| 70 |
| 71 bool alerted_; |
| 72 bool has_run_message_loop_; |
| 73 Browser* browser_; |
| 74 InfoBarService* infobar_service_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(PdmChangeWaiter); |
| 77 }; |
| 78 |
| 79 static PersonalDataManager* GetPersonalDataManager(Profile* profile) { |
| 80 return PersonalDataManagerFactory::GetForProfile(profile); |
| 81 } |
| 82 |
| 83 void AddTestProfile(Browser* browser, const AutofillProfile& profile) { |
| 84 PdmChangeWaiter observer(browser); |
| 85 GetPersonalDataManager(browser->profile())->AddProfile(profile); |
| 86 |
| 87 // AddProfile is asynchronous. Wait for it to finish before continuing the |
| 88 // tests. |
| 89 observer.Wait(); |
| 90 } |
| 91 |
| 92 void SetTestProfile(Browser* browser, const AutofillProfile& profile) { |
| 93 std::vector<AutofillProfile> profiles; |
| 94 profiles.push_back(profile); |
| 95 SetTestProfiles(browser, &profiles); |
| 96 } |
| 97 |
| 98 void SetTestProfiles(Browser* browser, std::vector<AutofillProfile>* profiles) { |
| 99 PdmChangeWaiter observer(browser); |
| 100 GetPersonalDataManager(browser->profile())->SetProfiles(profiles); |
| 101 observer.Wait(); |
| 102 } |
| 103 |
| 104 } // namespace autofill |
OLD | NEW |