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_string.h"
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "jni/FakeServerHelper_jni.h"
14 #include "sync/internal_api/public/base/model_type.h"
15 #include "sync/internal_api/public/network_resources.h"
16 #include "sync/protocol/sync.pb.h"
17 #include "sync/test/fake_server/bookmark_entity_builder.h"
18 #include "sync/test/fake_server/entity_builder_factory.h"
19 #include "sync/test/fake_server/fake_server.h"
20 #include "sync/test/fake_server/fake_server_network_resources.h"
21 #include "sync/test/fake_server/fake_server_verifier.h"
22 #include "sync/test/fake_server/unique_client_entity.h"
23 #include "testing/gtest/include/gtest/gtest.h"
26 FakeServerHelperAndroid::FakeServerHelperAndroid(JNIEnv
* env
, jobject obj
) {
29 FakeServerHelperAndroid::~FakeServerHelperAndroid() { }
31 static jlong
Init(JNIEnv
* env
, jobject obj
) {
32 FakeServerHelperAndroid
* fake_server_android
=
33 new FakeServerHelperAndroid(env
, obj
);
34 return reinterpret_cast<intptr_t>(fake_server_android
);
37 jlong
FakeServerHelperAndroid::CreateFakeServer(JNIEnv
* env
, jobject obj
) {
38 fake_server::FakeServer
* fake_server
= new fake_server::FakeServer();
39 return reinterpret_cast<intptr_t>(fake_server
);
42 jlong
FakeServerHelperAndroid::CreateNetworkResources(JNIEnv
* env
,
45 fake_server::FakeServer
* fake_server_ptr
=
46 reinterpret_cast<fake_server::FakeServer
*>(fake_server
);
47 syncer::NetworkResources
* resources
=
48 new fake_server::FakeServerNetworkResources(fake_server_ptr
);
49 return reinterpret_cast<intptr_t>(resources
);
52 void FakeServerHelperAndroid::DeleteFakeServer(JNIEnv
* env
,
55 fake_server::FakeServer
* fake_server_ptr
=
56 reinterpret_cast<fake_server::FakeServer
*>(fake_server
);
57 delete fake_server_ptr
;
60 jboolean
FakeServerHelperAndroid::VerifyEntityCountByTypeAndName(
65 jstring model_type_string
,
67 syncer::ModelType model_type
;
68 if (!NotificationTypeToRealModelType(base::android::ConvertJavaStringToUTF8(
69 env
, model_type_string
), &model_type
)) {
70 LOG(WARNING
) << "Invalid ModelType string.";
73 fake_server::FakeServer
* fake_server_ptr
=
74 reinterpret_cast<fake_server::FakeServer
*>(fake_server
);
75 fake_server::FakeServerVerifier
fake_server_verifier(fake_server_ptr
);
76 testing::AssertionResult result
=
77 fake_server_verifier
.VerifyEntityCountByTypeAndName(
78 count
, model_type
, base::android::ConvertJavaStringToUTF8(env
, name
));
81 LOG(WARNING
) << result
.message();
86 void FakeServerHelperAndroid::InjectUniqueClientEntity(
91 jbyteArray serialized_entity_specifics
) {
92 fake_server::FakeServer
* fake_server_ptr
=
93 reinterpret_cast<fake_server::FakeServer
*>(fake_server
);
95 int specifics_bytes_length
= env
->GetArrayLength(serialized_entity_specifics
);
96 jbyte
* specifics_bytes
=
97 env
->GetByteArrayElements(serialized_entity_specifics
, NULL
);
98 std::string
specifics_string(reinterpret_cast<char *>(specifics_bytes
),
99 specifics_bytes_length
);
101 sync_pb::EntitySpecifics entity_specifics
;
102 if (!entity_specifics
.ParseFromString(specifics_string
))
103 NOTREACHED() << "Could not deserialize EntitySpecifics";
105 fake_server_ptr
->InjectEntity(
106 fake_server::UniqueClientEntity::CreateForInjection(
107 base::android::ConvertJavaStringToUTF8(env
, name
),
111 void FakeServerHelperAndroid::InjectBookmarkEntity(
118 fake_server::FakeServer
* fake_server_ptr
=
119 reinterpret_cast<fake_server::FakeServer
*>(fake_server
);
121 std::string url_as_string
= base::android::ConvertJavaStringToUTF8(env
, url
);
122 GURL gurl
= GURL(url_as_string
);
123 if (!gurl
.is_valid()) {
124 NOTREACHED() << "The given string (" << url_as_string
125 << ") is not a valid URL.";
128 fake_server::EntityBuilderFactory entity_builder_factory
;
129 fake_server::BookmarkEntityBuilder bookmark_builder
=
130 entity_builder_factory
.NewBookmarkEntityBuilder(
131 base::android::ConvertJavaStringToUTF8(env
, title
), gurl
);
132 bookmark_builder
.SetParentId(
133 base::android::ConvertJavaStringToUTF8(env
, parent_id
));
134 scoped_ptr
<fake_server::FakeServerEntity
> bookmark
= bookmark_builder
.Build();
135 fake_server_ptr
->InjectEntity(bookmark
.Pass());
138 base::android::ScopedJavaLocalRef
<jstring
>
139 FakeServerHelperAndroid::GetBookmarkBarFolderId(
143 fake_server::FakeServer
* fake_server_ptr
=
144 reinterpret_cast<fake_server::FakeServer
*>(fake_server
);
145 return base::android::ConvertUTF8ToJavaString(
146 env
, fake_server_ptr
->GetBookmarkBarFolderId());
150 bool FakeServerHelperAndroid::Register(JNIEnv
* env
) {
151 return RegisterNativesImpl(env
);