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.
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"
19 // This class is used to wait for asynchronous updates to PersonalDataManager
22 : public PersonalDataManagerObserver
,
23 public infobars::InfoBarManager::Observer
{
25 explicit PdmChangeWaiter(Browser
* browser
)
27 has_run_message_loop_(false),
29 infobar_service_(InfoBarService::FromWebContents(
30 browser_
->tab_strip_model()->GetActiveWebContents())) {
31 PersonalDataManagerFactory::GetForProfile(browser_
->profile())->
33 infobar_service_
->AddObserver(this);
36 ~PdmChangeWaiter() override
{
37 while (infobar_service_
->infobar_count() > 0) {
38 infobar_service_
->RemoveInfoBar(infobar_service_
->infobar_at(0));
40 infobar_service_
->RemoveObserver(this);
43 // PersonalDataManagerObserver:
44 void OnPersonalDataChanged() override
{
45 if (has_run_message_loop_
) {
46 base::MessageLoopForUI::current()->Quit();
47 has_run_message_loop_
= false;
52 void OnInsufficientFormData() override
{ OnPersonalDataChanged(); }
57 has_run_message_loop_
= true;
58 content::RunMessageLoop();
60 PersonalDataManagerFactory::GetForProfile(browser_
->profile())->
65 // infobars::InfoBarManager::Observer:
66 void OnInfoBarAdded(infobars::InfoBar
* infobar
) override
{
67 infobar_service_
->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate()->
72 bool has_run_message_loop_
;
74 InfoBarService
* infobar_service_
;
76 DISALLOW_COPY_AND_ASSIGN(PdmChangeWaiter
);
79 static PersonalDataManager
* GetPersonalDataManager(Profile
* profile
) {
80 return PersonalDataManagerFactory::GetForProfile(profile
);
83 void AddTestProfile(Browser
* browser
, const AutofillProfile
& profile
) {
84 PdmChangeWaiter
observer(browser
);
85 GetPersonalDataManager(browser
->profile())->AddProfile(profile
);
87 // AddProfile is asynchronous. Wait for it to finish before continuing the
92 void SetTestProfile(Browser
* browser
, const AutofillProfile
& profile
) {
93 std::vector
<AutofillProfile
> profiles
;
94 profiles
.push_back(profile
);
95 SetTestProfiles(browser
, &profiles
);
98 void SetTestProfiles(Browser
* browser
, std::vector
<AutofillProfile
>* profiles
) {
99 PdmChangeWaiter
observer(browser
);
100 GetPersonalDataManager(browser
->profile())->SetProfiles(profiles
);
104 } // namespace autofill