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