1 // Copyright 2013 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 package org.chromium.chrome.browser.autofill;
7 import org.chromium.base.ThreadUtils;
8 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile;
9 import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard;
10 import org.chromium.chrome.browser.autofill.PersonalDataManager.PersonalDataManagerObserver;
12 import java.util.List;
13 import java.util.concurrent.Callable;
14 import java.util.concurrent.ExecutionException;
17 * Helper class for testing AutofillProfiles.
19 public class AutofillTestHelper {
21 private Object mObserverNotified;
23 public AutofillTestHelper() {
24 registerDataObserver();
27 AutofillProfile getProfile(final String guid) throws ExecutionException {
28 return ThreadUtils.runOnUiThreadBlocking(new Callable<AutofillProfile>() {
30 public AutofillProfile call() {
31 return PersonalDataManager.getInstance().getProfile(guid);
36 List<AutofillProfile> getProfiles() throws ExecutionException {
37 return ThreadUtils.runOnUiThreadBlocking(new Callable<List<AutofillProfile> >() {
39 public List<AutofillProfile> call() {
40 return PersonalDataManager.getInstance().getProfiles();
45 int getNumberOfProfiles() throws ExecutionException {
46 return ThreadUtils.runOnUiThreadBlocking(new Callable<Integer>() {
48 public Integer call() {
49 return PersonalDataManager.getInstance().getProfiles().size();
54 String setProfile(final AutofillProfile profile) throws InterruptedException,
56 String guid = ThreadUtils.runOnUiThreadBlocking(new Callable<String>() {
58 public String call() {
59 return PersonalDataManager.getInstance().setProfile(profile);
66 void deleteProfile(final String guid) throws InterruptedException {
67 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
70 PersonalDataManager.getInstance().deleteProfile(guid);
76 CreditCard getCreditCard(final String guid) throws ExecutionException {
77 return ThreadUtils.runOnUiThreadBlocking(new Callable<CreditCard>() {
79 public CreditCard call() {
80 return PersonalDataManager.getInstance().getCreditCard(guid);
85 int getNumberOfCreditCards() throws ExecutionException {
86 return ThreadUtils.runOnUiThreadBlocking(new Callable<Integer>() {
88 public Integer call() {
89 return PersonalDataManager.getInstance().getCreditCards().size();
94 String setCreditCard(final CreditCard card) throws InterruptedException, ExecutionException {
95 String guid = ThreadUtils.runOnUiThreadBlocking(new Callable<String>() {
97 public String call() {
98 return PersonalDataManager.getInstance().setCreditCard(card);
101 waitForDataChanged();
105 void deleteCreditCard(final String guid) throws InterruptedException {
106 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
109 PersonalDataManager.getInstance().deleteCreditCard(guid);
112 waitForDataChanged();
115 private void registerDataObserver() {
116 mObserverNotified = new Object();
117 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
120 PersonalDataManager.getInstance().registerDataObserver(
121 new PersonalDataManagerObserver() {
123 public void onPersonalDataChanged() {
124 synchronized (mObserverNotified) {
125 mObserverNotified.notifyAll();
134 public void waitForDataChanged() throws InterruptedException {
135 synchronized (mObserverNotified) {
136 mObserverNotified.wait(3000);