Commit 6fad8a5e authored by estade@chromium.org's avatar estade@chromium.org

Android: Get AutofillProfile summary labels from PersonalDataManagerAndroid

This will be used for the Android preferences UI

BUG=400888

Review URL: https://codereview.chromium.org/440283002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@287978 0039d316-1c4b-4281-b951-d872f2087c98
parent 91516900
......@@ -50,6 +50,7 @@ public class PersonalDataManager {
private String mCountry;
private String mPhoneNumber;
private String mEmailAddress;
private String mLabel;
private String mLanguageCode;
@CalledByNative("AutofillProfile")
......@@ -126,6 +127,10 @@ public class PersonalDataManager {
return mDependentLocality;
}
public String getLabel() {
return mLabel;
}
@CalledByNative("AutofillProfile")
public String getPostalCode() {
return mPostalCode;
......@@ -164,6 +169,10 @@ public class PersonalDataManager {
mGUID = guid;
}
public void setLabel(String label) {
mLabel = label;
}
public void setOrigin(String origin) {
mOrigin = origin;
}
......@@ -363,11 +372,17 @@ public class PersonalDataManager {
public List<AutofillProfile> getProfiles() {
ThreadUtils.assertOnUiThread();
String[] profileLabels = nativeGetProfileLabels(mPersonalDataManagerAndroid);
int profileCount = nativeGetProfileCount(mPersonalDataManagerAndroid);
List<AutofillProfile> profiles = new ArrayList<AutofillProfile>(profileCount);
for (int i = 0; i < profileCount; i++) {
profiles.add(nativeGetProfileByIndex(mPersonalDataManagerAndroid, i));
AutofillProfile profile = nativeGetProfileByIndex(mPersonalDataManagerAndroid, i);
profile.setLabel(profileLabels[i]);
profiles.add(profile);
}
return profiles;
}
......@@ -435,6 +450,7 @@ public class PersonalDataManager {
private native long nativeInit();
private native int nativeGetProfileCount(long nativePersonalDataManagerAndroid);
private native String[] nativeGetProfileLabels(long nativePersonalDataManagerAndroid);
private native AutofillProfile nativeGetProfileByIndex(long nativePersonalDataManagerAndroid,
int index);
private native AutofillProfile nativeGetProfileByGUID(long nativePersonalDataManagerAndroid,
......
......@@ -9,6 +9,7 @@ import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile;
import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard;
import org.chromium.chrome.browser.autofill.PersonalDataManager.PersonalDataManagerObserver;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
......@@ -32,6 +33,15 @@ public class AutofillTestHelper {
});
}
List<AutofillProfile> getProfiles() throws ExecutionException {
return ThreadUtils.runOnUiThreadBlocking(new Callable<List<AutofillProfile> >() {
@Override
public List<AutofillProfile> call() {
return PersonalDataManager.getInstance().getProfiles();
}
});
}
int getNumberOfProfiles() throws ExecutionException {
return ThreadUtils.runOnUiThreadBlocking(new Callable<Integer>() {
@Override
......
......@@ -11,6 +11,7 @@ import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile;
import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard;
import org.chromium.chrome.shell.ChromeShellTestBase;
import java.util.List;
import java.util.concurrent.ExecutionException;
/**
......@@ -220,4 +221,48 @@ public class PersonalDataManagerTest extends ChromeShellTestBase {
AutofillProfile storedProfile2 = mHelper.getProfile(profileGuid2);
assertEquals(streetAddress2, storedProfile2.getStreetAddress());
}
@SmallTest
@Feature({"Autofill"})
public void testLabels() throws InterruptedException, ExecutionException {
AutofillProfile profile1 = new AutofillProfile(
"" /* guid */, "https://www.example.com" /* origin */,
"John Major", "Acme Inc.",
"123 Main", "California", "Los Angeles", "",
"90210", "",
"US", "555 123-4567", "jm@example.com", "");
// An almost identical profile.
AutofillProfile profile2 = new AutofillProfile(
"" /* guid */, "https://www.example.com" /* origin */,
"John Major", "Acme Inc.",
"123 Main", "California", "Los Angeles", "",
"90210", "",
"US", "555 123-4567", "jm-work@example.com", "");
// A different profile.
AutofillProfile profile3 = new AutofillProfile(
"" /* guid */, "https://www.example.com" /* origin */,
"Jasper Lundgren", "",
"1500 Second Ave", "California", "Hollywood", "",
"90068", "",
"US", "555 123-9876", "jasperl@example.com", "");
// A profile where a lot of stuff is missing.
AutofillProfile profile4 = new AutofillProfile(
"" /* guid */, "https://www.example.com" /* origin */,
"Joe Sergeant", "",
"", "Texas", "Fort Worth", "",
"", "",
"US", "", "", "");
mHelper.setProfile(profile1);
mHelper.setProfile(profile2);
mHelper.setProfile(profile3);
mHelper.setProfile(profile4);
List<AutofillProfile> profiles = mHelper.getProfiles();
assertEquals(4, profiles.size());
assertEquals("123 Main, Los Angeles, jm@example.com", profiles.get(0).getLabel());
assertEquals("123 Main, Los Angeles, jm-work@example.com", profiles.get(1).getLabel());
assertEquals("1500 Second Ave, Hollywood", profiles.get(2).getLabel());
assertEquals("Fort Worth, Texas", profiles.get(3).getLabel());
}
}
......@@ -4,6 +4,7 @@
#include "chrome/browser/autofill/android/personal_data_manager_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/format_macros.h"
#include "base/prefs/pref_service.h"
......@@ -286,6 +287,21 @@ ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetCreditCard(
return ConvertUTF8ToJavaString(env, card.guid());
}
ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileLabels(
JNIEnv* env,
jobject unused_obj) {
std::vector<base::string16> labels;
AutofillProfile::CreateInferredLabels(
personal_data_manager_->GetProfiles(),
NULL,
NAME_FULL,
2,
g_browser_process->GetApplicationLocale(),
&labels);
return base::android::ToJavaArrayOfStrings(env, labels);
}
void PersonalDataManagerAndroid::RemoveByGUID(JNIEnv* env,
jobject unused_obj,
jstring jguid) {
......
......@@ -75,6 +75,12 @@ class PersonalDataManagerAndroid : public PersonalDataManagerObserver {
jobject unused_obj,
jobject jcard);
// Gets the labels for all known profiles. These labels are useful for
// distinguishing the profiles from one another.
base::android::ScopedJavaLocalRef<jobjectArray> GetProfileLabels(
JNIEnv* env,
jobject unused_obj);
// Removes the profile or credit card represented by |jguid|.
void RemoveByGUID(JNIEnv* env, jobject unused_obj, jstring jguid);
......
......@@ -71,12 +71,16 @@ void GetFieldsForDistinguishingProfiles(
COMPANY_NAME,
};
std::vector<ServerFieldType> default_fields;
if (!suggested_fields) {
DCHECK_EQ(excluded_field, UNKNOWN_TYPE);
distinguishing_fields->assign(
default_fields.assign(
kDefaultDistinguishingFields,
kDefaultDistinguishingFields + arraysize(kDefaultDistinguishingFields));
return;
if (excluded_field == UNKNOWN_TYPE) {
distinguishing_fields->swap(default_fields);
return;
}
suggested_fields = &default_fields;
}
// Keep track of which fields we've seen so that we avoid duplicate entries.
......
......@@ -446,6 +446,12 @@ TEST(AutofillProfileTest, CreateInferredLabels) {
NAME_FULL, 1, "en-US", &labels);
EXPECT_EQ(base::string16(ASCIIToUTF16("666 Erebus St.")), labels[0]);
EXPECT_EQ(base::string16(ASCIIToUTF16("123 Letha Shore.")), labels[1]);
// No suggested fields, but non-unknown excluded field.
AutofillProfile::CreateInferredLabels(profiles.get(), NULL,
NAME_FULL, 1, "en-US", &labels);
EXPECT_EQ(base::string16(ASCIIToUTF16("666 Erebus St.")), labels[0]);
EXPECT_EQ(base::string16(ASCIIToUTF16("123 Letha Shore.")), labels[1]);
}
// Test that we fall back to using the full name if there are no other
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment