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"
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"
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
,
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
,
57 fake_server::FakeServer
* fake_server_ptr
=
58 reinterpret_cast<fake_server::FakeServer
*>(fake_server
);
59 delete fake_server_ptr
;
62 jboolean
FakeServerHelperAndroid::VerifyEntityCountByTypeAndName(
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
));
78 LOG(WARNING
) << result
.message();
83 jboolean
FakeServerHelperAndroid::VerifySessions(
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
);
103 LOG(WARNING
) << result
.message();
108 base::android::ScopedJavaLocalRef
<jobjectArray
>
109 FakeServerHelperAndroid::GetSyncEntitiesByModelType(JNIEnv
* env
,
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
) {
124 entities
[i
].SerializeToString(&s
);
125 entity_strings
.push_back(s
);
127 return base::android::ToJavaArrayOfByteArray(env
, entity_strings
);
130 void FakeServerHelperAndroid::InjectUniqueClientEntity(
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
,
143 fake_server_ptr
->InjectEntity(
144 fake_server::UniqueClientEntity::CreateForInjection(
145 base::android::ConvertJavaStringToUTF8(env
, name
), entity_specifics
));
148 void FakeServerHelperAndroid::ModifyEntitySpecifics(
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
,
161 fake_server_ptr
->ModifyEntitySpecifics(
162 base::android::ConvertJavaStringToUTF8(env
, id
), entity_specifics
);
165 void FakeServerHelperAndroid::DeserializeEntitySpecifics(
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(
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
,
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
,
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
),
229 void FakeServerHelperAndroid::ModifyBookmarkFolderEntity(JNIEnv
* env
,
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
),
253 scoped_ptr
<fake_server::FakeServerEntity
>
254 FakeServerHelperAndroid::CreateBookmarkEntity(JNIEnv
* env
,
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(
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(
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
));
298 bool FakeServerHelperAndroid::Register(JNIEnv
* env
) {
299 return RegisterNativesImpl(env
);