[Android] Add enhanced bookmarks grid/list view experiment
[chromium-blink-merge.git] / base / android / jni_generator / sample_for_tests.cc
blob3c5ca028906c847e1b1793744854c0e48600811a
1 // Copyright (c) 2012 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 "base/android/jni_generator/sample_for_tests.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h"
10 // Generated file for JNI bindings from C++ to Java @CalledByNative methods.
11 // Only to be included in one .cc file.
12 // Name is based on the java file name: *.java -> jni/*_jni.h
13 #include "jni/SampleForTests_jni.h" // Generated by JNI.
15 using base::android::AttachCurrentThread;
16 using base::android::ScopedJavaLocalRef;
18 namespace base {
19 namespace android {
21 jdouble CPPClass::InnerClass::MethodOtherP0(JNIEnv* env, jobject obj) {
22 return 0.0;
25 CPPClass::CPPClass() {
28 CPPClass::~CPPClass() {
31 // static
32 bool CPPClass::RegisterJNI(JNIEnv* env) {
33 return RegisterNativesImpl(env); // Generated in SampleForTests_jni.h
36 void CPPClass::Destroy(JNIEnv* env, jobject obj) {
37 delete this;
40 jint CPPClass::Method(JNIEnv* env, jobject obj) {
41 return 0;
44 void CPPClass::AddStructB(JNIEnv* env, jobject obj, jobject structb) {
45 long key = Java_InnerStructB_getKey(env, structb);
46 std::string value = ConvertJavaStringToUTF8(
47 env, Java_InnerStructB_getValue(env, structb).obj());
48 map_[key] = value;
51 void CPPClass::IterateAndDoSomethingWithStructB(JNIEnv* env, jobject obj) {
52 // Iterate over the elements and do something with them.
53 for (std::map<long, std::string>::const_iterator it = map_.begin();
54 it != map_.end(); ++it) {
55 long key = it->first;
56 std::string value = it->second;
58 map_.clear();
61 base::android::ScopedJavaLocalRef<jstring> CPPClass::ReturnAString(
62 JNIEnv* env, jobject obj) {
63 base::android::ScopedJavaLocalRef<jstring> ret = ConvertUTF8ToJavaString(
64 env, "test");
65 return ret;
68 // Static free functions declared and called directly from java.
69 static jlong Init(JNIEnv* env, jobject obj, jstring param) {
70 return 0;
73 static jdouble GetDoubleFunction(JNIEnv*, jobject) {
74 return 0;
77 static jfloat GetFloatFunction(JNIEnv*, jclass) {
78 return 0;
81 static void SetNonPODDatatype(JNIEnv*, jobject, jobject) {
84 static jobject GetNonPODDatatype(JNIEnv*, jobject) {
85 return NULL;
88 static jint InnerFunction(JNIEnv*, jclass) {
89 return 0;
92 } // namespace android
93 } // namespace base
95 int main() {
96 // On a regular application, you'd call AttachCurrentThread(). This sample is
97 // not yet linking with all the libraries.
98 JNIEnv* env = /* AttachCurrentThread() */ NULL;
100 // This is how you call a java static method from C++.
101 bool foo = base::android::Java_SampleForTests_staticJavaMethod(env);
103 // This is how you call a java method from C++. Note that you must have
104 // obtained the jobject somehow.
105 jobject my_java_object = NULL;
106 int bar = base::android::Java_SampleForTests_javaMethod(
107 env, my_java_object, 1, 2);
109 for (int i = 0; i < 10; ++i) {
110 // Creates a "struct" that will then be used by the java side.
111 ScopedJavaLocalRef<jobject> struct_a =
112 base::android::Java_InnerStructA_create(
113 env, 0, 1,
114 base::android::ConvertUTF8ToJavaString(env, "test").obj());
115 base::android::Java_SampleForTests_addStructA(
116 env, my_java_object, struct_a.obj());
118 base::android::Java_SampleForTests_iterateAndDoSomething(env, my_java_object);
119 return 0;