From ee154bcef127d0045b6678fd6625dd99ae64f30d Mon Sep 17 00:00:00 2001 From: maxbogue Date: Tue, 7 Jul 2015 15:18:33 -0700 Subject: [PATCH] [Sync] Test downloading a modified autofill entry on Android. BUG=480604 Review URL: https://codereview.chromium.org/1221333009 Cr-Commit-Position: refs/heads/master@{#337697} --- .../chromium/chrome/browser/sync/AutofillTest.java | 24 ++++++++++++++- .../chrome/browser/sync/FakeServerHelper.java | 22 +++++++++++++ .../android/fake_server_helper_android.cc | 36 ++++++++++++++++++---- .../android/fake_server_helper_android.h | 12 ++++++++ 4 files changed, 87 insertions(+), 7 deletions(-) diff --git a/chrome/android/sync_shell/javatests/src/org/chromium/chrome/browser/sync/AutofillTest.java b/chrome/android/sync_shell/javatests/src/org/chromium/chrome/browser/sync/AutofillTest.java index e3d5a66cd378..4f4b5ff0a1fd 100644 --- a/chrome/android/sync_shell/javatests/src/org/chromium/chrome/browser/sync/AutofillTest.java +++ b/chrome/android/sync_shell/javatests/src/org/chromium/chrome/browser/sync/AutofillTest.java @@ -33,6 +33,7 @@ public class AutofillTest extends SyncTestBase { private static final String STREET = "1600 Amphitheatre Pkwy"; private static final String CITY = "Mountain View"; + private static final String MODIFIED_CITY = "Sunnyvale"; private static final String STATE = "CA"; private static final String ZIP = "94043"; @@ -81,6 +82,25 @@ public class AutofillTest extends SyncTestBase { assertEquals("The wrong zip was found for the autofill.", ZIP, autofill.zip); } + // Test syncing an autofill profile modification from server to client. + @LargeTest + @Feature({"Sync"}) + public void testDownloadAutofillModification() throws Exception { + // Add the entity to test modifying. + EntitySpecifics specifics = addServerAutofillProfile(STREET, CITY, STATE, ZIP); + SyncTestUtil.triggerSyncAndWaitForCompletion(mContext); + assertServerAutofillProfileCountWithName(1, STREET); + assertClientAutofillProfileCount(1); + + // Modify on server, sync, and verify modification locally. + Autofill autofill = getClientAutofillProfiles().get(0); + specifics.autofillProfile.addressHomeCity = MODIFIED_CITY; + mFakeServerHelper.modifyEntitySpecifics(autofill.id, specifics); + SyncTestUtil.triggerSyncAndWaitForCompletion(mContext); + Autofill modifiedAutofill = getClientAutofillProfiles().get(0); + assertEquals("The city was not modified.", MODIFIED_CITY, modifiedAutofill.city); + } + // Test syncing an autofill profile deletion from server to client. @LargeTest @Feature({"Sync"}) @@ -111,7 +131,8 @@ public class AutofillTest extends SyncTestBase { assertClientAutofillProfileCount(0); } - private void addServerAutofillProfile(String street, String city, String state, String zip) { + private EntitySpecifics addServerAutofillProfile( + String street, String city, String state, String zip) { EntitySpecifics specifics = new EntitySpecifics(); AutofillProfileSpecifics profile = new AutofillProfileSpecifics(); profile.guid = GUID; @@ -122,6 +143,7 @@ public class AutofillTest extends SyncTestBase { profile.addressHomeZip = zip; specifics.autofillProfile = profile; mFakeServerHelper.injectUniqueClientEntity(street /* name */, specifics); + return specifics; } private List getClientAutofillProfiles() throws JSONException { diff --git a/chrome/android/sync_shell/javatests/src/org/chromium/chrome/browser/sync/FakeServerHelper.java b/chrome/android/sync_shell/javatests/src/org/chromium/chrome/browser/sync/FakeServerHelper.java index 841d0f45e6f7..40bacbee1d22 100644 --- a/chrome/android/sync_shell/javatests/src/org/chromium/chrome/browser/sync/FakeServerHelper.java +++ b/chrome/android/sync_shell/javatests/src/org/chromium/chrome/browser/sync/FakeServerHelper.java @@ -188,6 +188,26 @@ public class FakeServerHelper { } /** + * Modify the specifics of an entity on the fake Sync server. + * + * @param id the ID of the entity whose specifics to modify + * @param entitySpecifics the new specifics proto for the entity + */ + public void modifyEntitySpecifics(final String id, final EntitySpecifics entitySpecifics) { + checkFakeServerInitialized("useFakeServer must be called before data modification."); + ThreadUtils.runOnUiThreadBlockingNoException(new Callable() { + @Override + public Void call() { + // The protocol buffer is serialized as a byte array because it can be easily + // deserialized from this format in native code. + nativeModifyEntitySpecifics(mNativeFakeServerHelperAndroid, sNativeFakeServer, id, + MessageNano.toByteArray(entitySpecifics)); + return null; + } + }); + } + + /** * Injects a bookmark into the fake Sync server. * * @param title the title of the bookmark to inject @@ -285,6 +305,8 @@ public class FakeServerHelper { private native void nativeInjectUniqueClientEntity( long nativeFakeServerHelperAndroid, long nativeFakeServer, String name, byte[] serializedEntitySpecifics); + private native void nativeModifyEntitySpecifics(long nativeFakeServerHelperAndroid, + long nativeFakeServer, String id, byte[] serializedEntitySpecifics); private native void nativeInjectBookmarkEntity( long nativeFakeServerHelperAndroid, long nativeFakeServer, String title, String url, String parentId); diff --git a/sync/test/fake_server/android/fake_server_helper_android.cc b/sync/test/fake_server/android/fake_server_helper_android.cc index f27e3cd7bb22..e493545f1c68 100644 --- a/sync/test/fake_server/android/fake_server_helper_android.cc +++ b/sync/test/fake_server/android/fake_server_helper_android.cc @@ -118,20 +118,44 @@ void FakeServerHelperAndroid::InjectUniqueClientEntity( fake_server::FakeServer* fake_server_ptr = reinterpret_cast(fake_server); + sync_pb::EntitySpecifics entity_specifics; + DeserializeEntitySpecifics(env, serialized_entity_specifics, + entity_specifics); + + fake_server_ptr->InjectEntity( + fake_server::UniqueClientEntity::CreateForInjection( + base::android::ConvertJavaStringToUTF8(env, name), entity_specifics)); +} + +void FakeServerHelperAndroid::ModifyEntitySpecifics( + JNIEnv* env, + jobject obj, + jlong fake_server, + jstring id, + jbyteArray serialized_entity_specifics) { + fake_server::FakeServer* fake_server_ptr = + reinterpret_cast(fake_server); + + sync_pb::EntitySpecifics entity_specifics; + DeserializeEntitySpecifics(env, serialized_entity_specifics, + entity_specifics); + + fake_server_ptr->ModifyEntitySpecifics( + base::android::ConvertJavaStringToUTF8(env, id), entity_specifics); +} + +void FakeServerHelperAndroid::DeserializeEntitySpecifics( + JNIEnv* env, + jbyteArray serialized_entity_specifics, + sync_pb::EntitySpecifics& entity_specifics) { int specifics_bytes_length = env->GetArrayLength(serialized_entity_specifics); jbyte* specifics_bytes = env->GetByteArrayElements(serialized_entity_specifics, NULL); std::string specifics_string(reinterpret_cast(specifics_bytes), specifics_bytes_length); - sync_pb::EntitySpecifics entity_specifics; if (!entity_specifics.ParseFromString(specifics_string)) NOTREACHED() << "Could not deserialize EntitySpecifics"; - - fake_server_ptr->InjectEntity( - fake_server::UniqueClientEntity::CreateForInjection( - base::android::ConvertJavaStringToUTF8(env, name), - entity_specifics)); } void FakeServerHelperAndroid::InjectBookmarkEntity( diff --git a/sync/test/fake_server/android/fake_server_helper_android.h b/sync/test/fake_server/android/fake_server_helper_android.h index 5e8d363a01ad..fc74efeed873 100644 --- a/sync/test/fake_server/android/fake_server_helper_android.h +++ b/sync/test/fake_server/android/fake_server_helper_android.h @@ -55,6 +55,13 @@ class FakeServerHelperAndroid { jstring name, jbyteArray serialized_entity_specifics); + // Modifies the entity with |id| on |fake_server|. + void ModifyEntitySpecifics(JNIEnv* env, + jobject obj, + jlong fake_server, + jstring name, + jbyteArray serialized_entity_specifics); + // Injects a BookmarkEntity into |fake_server|. void InjectBookmarkEntity(JNIEnv* env, jobject obj, @@ -88,6 +95,11 @@ class FakeServerHelperAndroid { private: virtual ~FakeServerHelperAndroid(); + // Deserializes |serialized_entity_specifics| into |entity_specifics|. + void DeserializeEntitySpecifics(JNIEnv* env, + jbyteArray serialized_entity_specifics, + sync_pb::EntitySpecifics& entity_specifics); + // Creates a bookmark entity. scoped_ptr CreateBookmarkEntity( JNIEnv* env, -- 2.11.4.GIT