Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / sync / test / fake_server / android / fake_server_helper_android.cc
blob42542951bf7840e6a938de5e7978192f18ef56c3
1 // Copyright 2014 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 "sync/test/fake_server/android/fake_server_helper_android.h"
7 #include <jni.h>
9 #include "base/android/jni_array.h"
10 #include "base/android/jni_string.h"
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "jni/FakeServerHelper_jni.h"
15 #include "sync/internal_api/public/base/model_type.h"
16 #include "sync/internal_api/public/network_resources.h"
17 #include "sync/protocol/sync.pb.h"
18 #include "sync/test/fake_server/bookmark_entity_builder.h"
19 #include "sync/test/fake_server/entity_builder_factory.h"
20 #include "sync/test/fake_server/fake_server.h"
21 #include "sync/test/fake_server/fake_server_network_resources.h"
22 #include "sync/test/fake_server/fake_server_verifier.h"
23 #include "sync/test/fake_server/tombstone_entity.h"
24 #include "sync/test/fake_server/unique_client_entity.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "url/gurl.h"
28 FakeServerHelperAndroid::FakeServerHelperAndroid(JNIEnv* env, jobject obj) {
31 FakeServerHelperAndroid::~FakeServerHelperAndroid() { }
33 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) {
34 FakeServerHelperAndroid* fake_server_android =
35 new FakeServerHelperAndroid(env, obj);
36 return reinterpret_cast<intptr_t>(fake_server_android);
39 jlong FakeServerHelperAndroid::CreateFakeServer(JNIEnv* env, jobject obj) {
40 fake_server::FakeServer* fake_server = new fake_server::FakeServer();
41 return reinterpret_cast<intptr_t>(fake_server);
44 jlong FakeServerHelperAndroid::CreateNetworkResources(JNIEnv* env,
45 jobject obj,
46 jlong fake_server) {
47 fake_server::FakeServer* fake_server_ptr =
48 reinterpret_cast<fake_server::FakeServer*>(fake_server);
49 syncer::NetworkResources* resources =
50 new fake_server::FakeServerNetworkResources(fake_server_ptr->AsWeakPtr());
51 return reinterpret_cast<intptr_t>(resources);
54 void FakeServerHelperAndroid::DeleteFakeServer(JNIEnv* env,
55 jobject obj,
56 jlong fake_server) {
57 fake_server::FakeServer* fake_server_ptr =
58 reinterpret_cast<fake_server::FakeServer*>(fake_server);
59 delete fake_server_ptr;
62 jboolean FakeServerHelperAndroid::VerifyEntityCountByTypeAndName(
63 JNIEnv* env,
64 jobject obj,
65 jlong fake_server,
66 jlong count,
67 jint model_type_int,
68 jstring name) {
69 syncer::ModelType model_type = static_cast<syncer::ModelType>(model_type_int);
70 fake_server::FakeServer* fake_server_ptr =
71 reinterpret_cast<fake_server::FakeServer*>(fake_server);
72 fake_server::FakeServerVerifier fake_server_verifier(fake_server_ptr);
73 testing::AssertionResult result =
74 fake_server_verifier.VerifyEntityCountByTypeAndName(
75 count, model_type, base::android::ConvertJavaStringToUTF8(env, name));
77 if (!result)
78 LOG(WARNING) << result.message();
80 return result;
83 jboolean FakeServerHelperAndroid::VerifySessions(
84 JNIEnv* env,
85 jobject obj,
86 jlong fake_server,
87 jobjectArray url_array) {
88 std::multiset<std::string> tab_urls;
89 for (int i = 0; i < env->GetArrayLength(url_array); i++) {
90 jstring s = (jstring) env->GetObjectArrayElement(url_array, i);
91 tab_urls.insert(base::android::ConvertJavaStringToUTF8(env, s));
93 fake_server::SessionsHierarchy expected_sessions;
94 expected_sessions.AddWindow(tab_urls);
96 fake_server::FakeServer* fake_server_ptr =
97 reinterpret_cast<fake_server::FakeServer*>(fake_server);
98 fake_server::FakeServerVerifier fake_server_verifier(fake_server_ptr);
99 testing::AssertionResult result =
100 fake_server_verifier.VerifySessions(expected_sessions);
102 if (!result)
103 LOG(WARNING) << result.message();
105 return result;
108 base::android::ScopedJavaLocalRef<jobjectArray>
109 FakeServerHelperAndroid::GetSyncEntitiesByModelType(JNIEnv* env,
110 jobject obj,
111 jlong fake_server,
112 jint model_type_int) {
113 fake_server::FakeServer* fake_server_ptr =
114 reinterpret_cast<fake_server::FakeServer*>(fake_server);
116 syncer::ModelType model_type = static_cast<syncer::ModelType>(model_type_int);
118 std::vector<sync_pb::SyncEntity> entities =
119 fake_server_ptr->GetSyncEntitiesByModelType(model_type);
121 std::vector<std::string> entity_strings;
122 for (size_t i = 0; i < entities.size(); ++i) {
123 std::string s;
124 entities[i].SerializeToString(&s);
125 entity_strings.push_back(s);
127 return base::android::ToJavaArrayOfByteArray(env, entity_strings);
130 void FakeServerHelperAndroid::InjectUniqueClientEntity(
131 JNIEnv* env,
132 jobject obj,
133 jlong fake_server,
134 jstring name,
135 jbyteArray serialized_entity_specifics) {
136 fake_server::FakeServer* fake_server_ptr =
137 reinterpret_cast<fake_server::FakeServer*>(fake_server);
139 sync_pb::EntitySpecifics entity_specifics;
140 DeserializeEntitySpecifics(env, serialized_entity_specifics,
141 entity_specifics);
143 fake_server_ptr->InjectEntity(
144 fake_server::UniqueClientEntity::CreateForInjection(
145 base::android::ConvertJavaStringToUTF8(env, name), entity_specifics));
148 void FakeServerHelperAndroid::ModifyEntitySpecifics(
149 JNIEnv* env,
150 jobject obj,
151 jlong fake_server,
152 jstring id,
153 jbyteArray serialized_entity_specifics) {
154 fake_server::FakeServer* fake_server_ptr =
155 reinterpret_cast<fake_server::FakeServer*>(fake_server);
157 sync_pb::EntitySpecifics entity_specifics;
158 DeserializeEntitySpecifics(env, serialized_entity_specifics,
159 entity_specifics);
161 fake_server_ptr->ModifyEntitySpecifics(
162 base::android::ConvertJavaStringToUTF8(env, id), entity_specifics);
165 void FakeServerHelperAndroid::DeserializeEntitySpecifics(
166 JNIEnv* env,
167 jbyteArray serialized_entity_specifics,
168 sync_pb::EntitySpecifics& entity_specifics) {
169 int specifics_bytes_length = env->GetArrayLength(serialized_entity_specifics);
170 jbyte* specifics_bytes =
171 env->GetByteArrayElements(serialized_entity_specifics, NULL);
172 std::string specifics_string(reinterpret_cast<char *>(specifics_bytes),
173 specifics_bytes_length);
175 if (!entity_specifics.ParseFromString(specifics_string))
176 NOTREACHED() << "Could not deserialize EntitySpecifics";
179 void FakeServerHelperAndroid::InjectBookmarkEntity(
180 JNIEnv* env,
181 jobject obj,
182 jlong fake_server,
183 jstring title,
184 jstring url,
185 jstring parent_id) {
186 fake_server::FakeServer* fake_server_ptr =
187 reinterpret_cast<fake_server::FakeServer*>(fake_server);
188 fake_server_ptr->InjectEntity(
189 CreateBookmarkEntity(env, title, url, parent_id));
192 void FakeServerHelperAndroid::InjectBookmarkFolderEntity(JNIEnv* env,
193 jobject obj,
194 jlong fake_server,
195 jstring title,
196 jstring parent_id) {
197 fake_server::FakeServer* fake_server_ptr =
198 reinterpret_cast<fake_server::FakeServer*>(fake_server);
200 fake_server::EntityBuilderFactory entity_builder_factory;
201 fake_server::BookmarkEntityBuilder bookmark_builder =
202 entity_builder_factory.NewBookmarkEntityBuilder(
203 base::android::ConvertJavaStringToUTF8(env, title));
204 bookmark_builder.SetParentId(
205 base::android::ConvertJavaStringToUTF8(env, parent_id));
207 fake_server_ptr->InjectEntity(bookmark_builder.BuildFolder());
210 void FakeServerHelperAndroid::ModifyBookmarkEntity(JNIEnv* env,
211 jobject obj,
212 jlong fake_server,
213 jstring entity_id,
214 jstring title,
215 jstring url,
216 jstring parent_id) {
217 fake_server::FakeServer* fake_server_ptr =
218 reinterpret_cast<fake_server::FakeServer*>(fake_server);
219 scoped_ptr<fake_server::FakeServerEntity> bookmark =
220 CreateBookmarkEntity(env, title, url, parent_id);
221 sync_pb::SyncEntity proto;
222 bookmark->SerializeAsProto(&proto);
223 fake_server_ptr->ModifyBookmarkEntity(
224 base::android::ConvertJavaStringToUTF8(env, entity_id),
225 base::android::ConvertJavaStringToUTF8(env, parent_id),
226 proto.specifics());
229 void FakeServerHelperAndroid::ModifyBookmarkFolderEntity(JNIEnv* env,
230 jobject obj,
231 jlong fake_server,
232 jstring entity_id,
233 jstring title,
234 jstring parent_id) {
235 fake_server::FakeServer* fake_server_ptr =
236 reinterpret_cast<fake_server::FakeServer*>(fake_server);
238 fake_server::EntityBuilderFactory entity_builder_factory;
239 fake_server::BookmarkEntityBuilder bookmark_builder =
240 entity_builder_factory.NewBookmarkEntityBuilder(
241 base::android::ConvertJavaStringToUTF8(env, title));
242 bookmark_builder.SetParentId(
243 base::android::ConvertJavaStringToUTF8(env, parent_id));
245 sync_pb::SyncEntity proto;
246 bookmark_builder.BuildFolder()->SerializeAsProto(&proto);
247 fake_server_ptr->ModifyBookmarkEntity(
248 base::android::ConvertJavaStringToUTF8(env, entity_id),
249 base::android::ConvertJavaStringToUTF8(env, parent_id),
250 proto.specifics());
253 scoped_ptr<fake_server::FakeServerEntity>
254 FakeServerHelperAndroid::CreateBookmarkEntity(JNIEnv* env,
255 jstring title,
256 jstring url,
257 jstring parent_id) {
258 std::string url_as_string = base::android::ConvertJavaStringToUTF8(env, url);
259 GURL gurl = GURL(url_as_string);
260 if (!gurl.is_valid()) {
261 NOTREACHED() << "The given string (" << url_as_string
262 << ") is not a valid URL.";
265 fake_server::EntityBuilderFactory entity_builder_factory;
266 fake_server::BookmarkEntityBuilder bookmark_builder =
267 entity_builder_factory.NewBookmarkEntityBuilder(
268 base::android::ConvertJavaStringToUTF8(env, title));
269 bookmark_builder.SetParentId(
270 base::android::ConvertJavaStringToUTF8(env, parent_id));
271 return bookmark_builder.BuildBookmark(gurl);
274 base::android::ScopedJavaLocalRef<jstring>
275 FakeServerHelperAndroid::GetBookmarkBarFolderId(
276 JNIEnv* env,
277 jobject obj,
278 jlong fake_server) {
279 fake_server::FakeServer* fake_server_ptr =
280 reinterpret_cast<fake_server::FakeServer*>(fake_server);
281 return base::android::ConvertUTF8ToJavaString(
282 env, fake_server_ptr->GetBookmarkBarFolderId());
285 void FakeServerHelperAndroid::DeleteEntity(
286 JNIEnv* env,
287 jobject obj,
288 jlong fake_server,
289 jstring id) {
290 fake_server::FakeServer* fake_server_ptr =
291 reinterpret_cast<fake_server::FakeServer*>(fake_server);
292 std::string native_id = base::android::ConvertJavaStringToUTF8(env, id);
293 fake_server_ptr->InjectEntity(
294 fake_server::TombstoneEntity::Create(native_id));
297 // static
298 bool FakeServerHelperAndroid::Register(JNIEnv* env) {
299 return RegisterNativesImpl(env);