OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 package org.chromium.chrome.browser.sync; | 5 package org.chromium.chrome.browser.sync; |
6 | 6 |
7 import android.test.suitebuilder.annotation.LargeTest; | 7 import android.test.suitebuilder.annotation.LargeTest; |
8 import android.util.Pair; | 8 import android.util.Pair; |
9 | 9 |
10 import org.chromium.base.test.util.Feature; | 10 import org.chromium.base.test.util.Feature; |
11 import org.chromium.chrome.test.util.browser.sync.SyncTestUtil; | 11 import org.chromium.chrome.test.util.browser.sync.SyncTestUtil; |
12 import org.chromium.content.browser.test.util.Criteria; | 12 import org.chromium.content.browser.test.util.Criteria; |
13 import org.chromium.content.browser.test.util.CriteriaHelper; | 13 import org.chromium.content.browser.test.util.CriteriaHelper; |
14 import org.chromium.sync.internal_api.pub.base.ModelType; | 14 import org.chromium.sync.internal_api.pub.base.ModelType; |
15 import org.chromium.sync.protocol.AutofillProfileSpecifics; | 15 import org.chromium.sync.protocol.AutofillProfileSpecifics; |
16 import org.chromium.sync.protocol.AutofillSpecifics; | |
17 import org.chromium.sync.protocol.EntitySpecifics; | 16 import org.chromium.sync.protocol.EntitySpecifics; |
18 import org.json.JSONException; | 17 import org.json.JSONException; |
19 import org.json.JSONObject; | 18 import org.json.JSONObject; |
20 | 19 |
21 import java.util.ArrayList; | 20 import java.util.ArrayList; |
22 import java.util.List; | 21 import java.util.List; |
23 | 22 |
24 /** | 23 /** |
25 * Test suite for the autofill sync data type. | 24 * Test suite for the autofill profile sync data type. |
26 */ | 25 */ |
27 public class AutofillTest extends SyncTestBase { | 26 public class AutofillTest extends SyncTestBase { |
28 private static final String TAG = "AutofillTest"; | 27 private static final String TAG = "AutofillTest"; |
29 | 28 |
30 private static final String AUTOFILL_TYPE = "Autofill"; | 29 private static final String AUTOFILL_TYPE = "Autofill Profiles"; |
| 30 |
| 31 private static final String GUID = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; |
| 32 private static final String ORIGIN = "https://www.chromium.org/"; |
31 | 33 |
32 private static final String STREET = "1600 Amphitheatre Pkwy"; | 34 private static final String STREET = "1600 Amphitheatre Pkwy"; |
33 private static final String CITY = "Mountain View"; | 35 private static final String CITY = "Mountain View"; |
34 private static final String STATE = "CA"; | 36 private static final String STATE = "CA"; |
35 private static final String ZIP = "94043"; | 37 private static final String ZIP = "94043"; |
36 | 38 |
37 // A container to store autofill information for data verification. | 39 // A container to store autofill profile information for data verification. |
38 private static class Autofill { | 40 private static class Autofill { |
39 public final String id; | 41 public final String id; |
40 public final String street; | 42 public final String street; |
41 public final String city; | 43 public final String city; |
42 public final String state; | 44 public final String state; |
43 public final String zip; | 45 public final String zip; |
44 | 46 |
45 public Autofill(String id, String street, String city, String state, Str
ing zip) { | 47 public Autofill(String id, String street, String city, String state, Str
ing zip) { |
46 this.id = id; | 48 this.id = id; |
47 this.street = street; | 49 this.street = street; |
48 this.city = city; | 50 this.city = city; |
49 this.state = state; | 51 this.state = state; |
50 this.zip = zip; | 52 this.zip = zip; |
51 } | 53 } |
52 } | 54 } |
53 | 55 |
54 @Override | 56 @Override |
55 protected void setUp() throws Exception { | 57 protected void setUp() throws Exception { |
56 super.setUp(); | 58 super.setUp(); |
57 setupTestAccountAndSignInToSync(CLIENT_ID); | 59 setupTestAccountAndSignInToSync(CLIENT_ID); |
58 // Make sure the initial state is clean. | 60 // Make sure the initial state is clean. |
59 assertClientAutofillCount(0); | 61 assertClientAutofillProfileCount(0); |
60 assertServerAutofillCountWithName(0, STREET); | 62 assertServerAutofillProfileCountWithName(0, STREET); |
61 } | 63 } |
62 | 64 |
63 // Test syncing a autofill from server to client. | 65 // Test syncing an autofill profile from server to client. |
64 @LargeTest | 66 @LargeTest |
65 @Feature({"Sync"}) | 67 @Feature({"Sync"}) |
66 public void testDownloadAutofill() throws Exception { | 68 public void testDownloadAutofill() throws Exception { |
67 addServerAutofillData(STREET, CITY, STATE, ZIP); | 69 addServerAutofillProfile(STREET, CITY, STATE, ZIP); |
68 assertServerAutofillCountWithName(1, STREET); | 70 assertServerAutofillProfileCountWithName(1, STREET); |
69 SyncTestUtil.triggerSyncAndWaitForCompletion(mContext); | 71 SyncTestUtil.triggerSyncAndWaitForCompletion(mContext); |
70 | 72 |
71 // Verify data synced to client. | 73 // Verify data synced to client. |
72 List<Autofill> autofills = getClientAutofillProfiles(); | 74 List<Autofill> autofills = getClientAutofillProfiles(); |
73 assertEquals("Only the injected autofill should exist on the client.", | 75 assertEquals("Only the injected autofill should exist on the client.", |
74 1, autofills.size()); | 76 1, autofills.size()); |
75 Autofill autofill = autofills.get(0); | 77 Autofill autofill = autofills.get(0); |
76 assertEquals("The wrong street was found for the address.", STREET, auto
fill.street); | 78 assertEquals("The wrong street was found for the address.", STREET, auto
fill.street); |
77 assertEquals("The wrong city was found for the autofill.", CITY, autofil
l.city); | 79 assertEquals("The wrong city was found for the autofill.", CITY, autofil
l.city); |
78 assertEquals("The wrong state was found for the autofill.", STATE, autof
ill.state); | 80 assertEquals("The wrong state was found for the autofill.", STATE, autof
ill.state); |
79 assertEquals("The wrong zip was found for the autofill.", ZIP, autofill.
zip); | 81 assertEquals("The wrong zip was found for the autofill.", ZIP, autofill.
zip); |
80 } | 82 } |
81 | 83 |
82 // Test syncing a autofill deletion from server to client. | 84 // Test syncing an autofill profile deletion from server to client. |
83 @LargeTest | 85 @LargeTest |
84 @Feature({"Sync"}) | 86 @Feature({"Sync"}) |
85 public void testDownloadDeletedAutofill() throws Exception { | 87 public void testDownloadDeletedAutofill() throws Exception { |
86 // Add the entity to test deleting. | 88 // Add the entity to test deleting. |
87 addServerAutofillData(STREET, CITY, STATE, ZIP); | 89 addServerAutofillProfile(STREET, CITY, STATE, ZIP); |
88 SyncTestUtil.triggerSyncAndWaitForCompletion(mContext); | 90 SyncTestUtil.triggerSyncAndWaitForCompletion(mContext); |
89 assertServerAutofillCountWithName(1, STREET); | 91 assertServerAutofillProfileCountWithName(1, STREET); |
90 assertClientAutofillCount(1); | 92 assertClientAutofillProfileCount(1); |
91 | 93 |
92 // Delete on server, sync, and verify deleted locally. | 94 // Delete on server, sync, and verify deleted locally. |
93 Autofill autofill = getClientAutofillProfiles().get(0); | 95 Autofill autofill = getClientAutofillProfiles().get(0); |
94 mFakeServerHelper.deleteEntity(autofill.id); | 96 mFakeServerHelper.deleteEntity(autofill.id); |
95 waitForServerAutofillCountWithName(0, STREET); | 97 waitForServerAutofillProfileCountWithName(0, STREET); |
96 SyncTestUtil.triggerSyncAndWaitForCompletion(mContext); | 98 SyncTestUtil.triggerSyncAndWaitForCompletion(mContext); |
97 waitForClientAutofillCount(0); | 99 waitForClientAutofillProfileCount(0); |
98 } | 100 } |
99 | 101 |
100 // Test that autofill entries don't get synced if the data type is disabled. | 102 // Test that autofill profiles don't get synced if the data type is disabled
. |
101 @LargeTest | 103 @LargeTest |
102 @Feature({"Sync"}) | 104 @Feature({"Sync"}) |
103 public void testDisabledNoDownloadAutofill() throws Exception { | 105 public void testDisabledNoDownloadAutofill() throws Exception { |
| 106 // The AUTOFILL type here controls both AUTOFILL and AUTOFILL_PROFILE. |
104 disableDataType(ModelType.AUTOFILL); | 107 disableDataType(ModelType.AUTOFILL); |
105 addServerAutofillData(STREET, CITY, STATE, ZIP); | 108 addServerAutofillProfile(STREET, CITY, STATE, ZIP); |
106 assertServerAutofillCountWithName(1, STREET); | 109 assertServerAutofillProfileCountWithName(1, STREET); |
107 SyncTestUtil.triggerSyncAndWaitForCompletion(mContext); | 110 SyncTestUtil.triggerSyncAndWaitForCompletion(mContext); |
108 assertClientAutofillCount(0); | 111 assertClientAutofillProfileCount(0); |
109 } | 112 } |
110 | 113 |
111 // TODO(maxbogue): Switch to using specifics.autofill_profile instead. | 114 private void addServerAutofillProfile(String street, String city, String sta
te, String zip) { |
112 private void addServerAutofillData(String street, String city, String state,
String zip) { | |
113 EntitySpecifics specifics = new EntitySpecifics(); | 115 EntitySpecifics specifics = new EntitySpecifics(); |
114 specifics.autofill = new AutofillSpecifics(); | |
115 AutofillProfileSpecifics profile = new AutofillProfileSpecifics(); | 116 AutofillProfileSpecifics profile = new AutofillProfileSpecifics(); |
| 117 profile.guid = GUID; |
| 118 profile.origin = ORIGIN; |
116 profile.addressHomeLine1 = street; | 119 profile.addressHomeLine1 = street; |
117 profile.addressHomeCity = city; | 120 profile.addressHomeCity = city; |
118 profile.addressHomeState = state; | 121 profile.addressHomeState = state; |
119 profile.addressHomeZip = zip; | 122 profile.addressHomeZip = zip; |
120 specifics.autofill.profile = profile; | 123 specifics.autofillProfile = profile; |
121 mFakeServerHelper.injectUniqueClientEntity(street /* name */, specifics)
; | 124 mFakeServerHelper.injectUniqueClientEntity(street /* name */, specifics)
; |
122 } | 125 } |
123 | 126 |
124 private List<Autofill> getClientAutofillProfiles() throws JSONException { | 127 private List<Autofill> getClientAutofillProfiles() throws JSONException { |
125 List<Pair<String, JSONObject>> entities = SyncTestUtil.getLocalData( | 128 List<Pair<String, JSONObject>> entities = SyncTestUtil.getLocalData( |
126 mContext, AUTOFILL_TYPE); | 129 mContext, AUTOFILL_TYPE); |
127 List<Autofill> autofills = new ArrayList<Autofill>(entities.size()); | 130 List<Autofill> autofills = new ArrayList<Autofill>(entities.size()); |
128 for (Pair<String, JSONObject> entity : entities) { | 131 for (Pair<String, JSONObject> entity : entities) { |
129 String id = entity.first; | 132 String id = entity.first; |
130 JSONObject profile = entity.second.getJSONObject("profile"); | 133 JSONObject profile = entity.second; |
131 String street = profile.getString("address_home_line1"); | 134 String street = profile.getString("address_home_line1"); |
132 String city = profile.getString("address_home_city"); | 135 String city = profile.getString("address_home_city"); |
133 String state = profile.getString("address_home_state"); | 136 String state = profile.getString("address_home_state"); |
134 String zip = profile.getString("address_home_zip"); | 137 String zip = profile.getString("address_home_zip"); |
135 autofills.add(new Autofill(id, street, city, state, zip)); | 138 autofills.add(new Autofill(id, street, city, state, zip)); |
136 } | 139 } |
137 return autofills; | 140 return autofills; |
138 } | 141 } |
139 | 142 |
140 private void assertClientAutofillCount(int count) throws JSONException { | 143 private void assertClientAutofillProfileCount(int count) throws JSONExceptio
n { |
141 assertEquals("There should be " + count + " local autofill entities.", | 144 assertEquals("There should be " + count + " local autofill profiles.", |
142 count, SyncTestUtil.getLocalData(mContext, AUTOFILL_TYPE).size()
); | 145 count, SyncTestUtil.getLocalData(mContext, AUTOFILL_TYPE).size()
); |
143 } | 146 } |
144 | 147 |
145 private void assertServerAutofillCountWithName(int count, String name) { | 148 private void assertServerAutofillProfileCountWithName(int count, String name
) { |
146 assertTrue("Expected " + count + " server autofills with name " + name +
".", | 149 assertTrue("Expected " + count + " server autofill profiles with name "
+ name + ".", |
147 mFakeServerHelper.verifyEntityCountByTypeAndName( | 150 mFakeServerHelper.verifyEntityCountByTypeAndName( |
148 count, ModelType.AUTOFILL, name)); | 151 count, ModelType.AUTOFILL_PROFILE, name)); |
149 } | 152 } |
150 | 153 |
151 private void waitForClientAutofillCount(final int count) throws InterruptedE
xception { | 154 private void waitForClientAutofillProfileCount(final int count) throws Inter
ruptedException { |
152 boolean success = CriteriaHelper.pollForCriteria(new Criteria() { | 155 boolean success = CriteriaHelper.pollForCriteria(new Criteria() { |
153 @Override | 156 @Override |
154 public boolean isSatisfied() { | 157 public boolean isSatisfied() { |
155 try { | 158 try { |
156 return SyncTestUtil.getLocalData(mContext, AUTOFILL_TYPE).si
ze() == count; | 159 return SyncTestUtil.getLocalData(mContext, AUTOFILL_TYPE).si
ze() == count; |
157 } catch (Exception e) { | 160 } catch (Exception e) { |
158 throw new RuntimeException(e); | 161 throw new RuntimeException(e); |
159 } | 162 } |
160 } | 163 } |
161 }, SyncTestUtil.UI_TIMEOUT_MS, SyncTestUtil.CHECK_INTERVAL_MS); | 164 }, SyncTestUtil.UI_TIMEOUT_MS, SyncTestUtil.CHECK_INTERVAL_MS); |
162 assertTrue("Expected " + count + " local autofill entities.", success); | 165 assertTrue("Expected " + count + " local autofill profiles.", success); |
163 } | 166 } |
164 | 167 |
165 private void waitForServerAutofillCountWithName(final int count, final Strin
g name) | 168 private void waitForServerAutofillProfileCountWithName(final int count, fina
l String name) |
166 throws InterruptedException { | 169 throws InterruptedException { |
167 boolean success = CriteriaHelper.pollForCriteria(new Criteria() { | 170 boolean success = CriteriaHelper.pollForCriteria(new Criteria() { |
168 @Override | 171 @Override |
169 public boolean isSatisfied() { | 172 public boolean isSatisfied() { |
170 try { | 173 try { |
171 return mFakeServerHelper.verifyEntityCountByTypeAndName( | 174 return mFakeServerHelper.verifyEntityCountByTypeAndName( |
172 count, ModelType.AUTOFILL, name); | 175 count, ModelType.AUTOFILL, name); |
173 } catch (Exception e) { | 176 } catch (Exception e) { |
174 throw new RuntimeException(e); | 177 throw new RuntimeException(e); |
175 } | 178 } |
176 } | 179 } |
177 }, SyncTestUtil.UI_TIMEOUT_MS, SyncTestUtil.CHECK_INTERVAL_MS); | 180 }, SyncTestUtil.UI_TIMEOUT_MS, SyncTestUtil.CHECK_INTERVAL_MS); |
178 assertTrue("Expected " + count + " server autofills with name " + name +
".", success); | 181 assertTrue("Expected " + count + " server autofill profiles with name "
+ name + ".", |
| 182 success); |
179 } | 183 } |
180 } | 184 } |
OLD | NEW |