Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - replace platform default implementation using nunicode fo…
Browse files Browse the repository at this point in the history
…r uppercasing an lowercasing with an Android specific String.java equivalent
  • Loading branch information
tobrun committed Aug 29, 2018
1 parent 4061e90 commit 98a3c0a
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.mapbox.mapboxsdk.testapp.string;

import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest;
import com.mapbox.mapboxsdk.testapp.activity.espresso.EspressoTestActivity;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;

/**
* Test verifying if String#toUpperCase and String#toLowerCase produces desired results
* <p>
* See core test in https://github.com/mapbox/mapbox-gl-native/blob/master/test/util/text_conversions.test.cpp
* </p>
*/
public class UppperLowerCaseTest extends BaseActivityTest {

@Override
protected Class getActivityClass() {
return EspressoTestActivity.class;
}

@Test
public void testToUpperCase() {
assertEquals("STREET", "strEEt".toUpperCase()); // EN
assertEquals("ROAD", "rOAd".toUpperCase()); // EN

assertEquals("STRASSE", "straße".toUpperCase()); // DE
assertEquals("MASSE", "maße".toUpperCase()); // DE
assertEquals("WEISSKOPFSEEADLER", "weißkopfseeadler".toUpperCase()); // DE

assertEquals("BÊNÇÃO", "bênção".toUpperCase()); // PT
assertEquals("AZƏRBAYCAN", "Azərbaycan".toUpperCase()); // AZ
assertEquals("ὈΔΥΣΣΕΎΣ", "Ὀδυσσεύς".toUpperCase()); // GR
}

@Test
public void testToLowerCase() {
assertEquals("street", "strEEt".toLowerCase()); // EN
assertEquals("road", "rOAd".toLowerCase()); // EN

assertEquals("straße", "Straße".toLowerCase()); // DE
assertEquals("strasse", "STRASSE".toLowerCase()); // DE
assertEquals("masse", "MASSE".toLowerCase()); // DE
assertEquals("weisskopfseeadler", "weiSSkopfseeadler".toLowerCase()); // DE

assertEquals("bênção", "BÊNÇÃO".toLowerCase()); // PT
assertEquals("azərbaycan", "AZƏRBAYCAN".toLowerCase()); //
}

}
2 changes: 1 addition & 1 deletion platform/android/core-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ platform/android/src/text/local_glyph_rasterizer.cpp
platform/android/src/text/local_glyph_rasterizer_jni.hpp
platform/android/src/logging_android.cpp
platform/android/src/thread.cpp
platform/default/string_stdlib.cpp
platform/android/src/string_util.cpp
platform/default/bidi.cpp
platform/default/thread_local.cpp
platform/default/unaccent.cpp
Expand Down
19 changes: 19 additions & 0 deletions platform/android/src/java/lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ namespace android {
namespace java {
namespace lang {

// String

jni::String String::toUpperCase(JNIEnv& env, jni::String value) {
static auto method = javaClass.GetMethod<jni::String ()>(env, "toUpperCase");
return value.Call(env, method);
}

jni::String String::toLowerCase(JNIEnv& env, jni::String value) {
static auto method = javaClass.GetMethod<jni::String ()>(env, "toLowerCase");
return value.Call(env, method);
}

void String::registerNative(jni::JNIEnv& env) {
// Lookup the class
javaClass = *jni::Class<jni::StringTag>::Find(env).NewGlobalRef(env).release();
}

jni::Class<jni::StringTag> String::javaClass;

// Float

jni::Object<Float> Float::valueOf(JNIEnv &env, jfloat value) {
Expand Down
11 changes: 11 additions & 0 deletions platform/android/src/java/lang.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ namespace android {
namespace java {
namespace lang {

class String : private mbgl::util::noncopyable {
public:
static jni::String toUpperCase(JNIEnv&, jni::String);

static jni::String toLowerCase(JNIEnv&, jni::String);

static jni::Class<jni::StringTag> javaClass;

static void registerNative(jni::JNIEnv&);
};

class Float : private mbgl::util::noncopyable {
public:
static constexpr auto Name() { return "java/lang/Float"; };
Expand Down
1 change: 1 addition & 0 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ void registerNatives(JavaVM *vm) {
java::lang::Boolean::registerNative(env);
java::lang::Double::registerNative(env);
java::lang::Long::registerNative(env);
java::lang::String::registerNative(env);

// GeoJSON
geojson::Feature::registerNative(env);
Expand Down
21 changes: 21 additions & 0 deletions platform/android/src/string_util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <mbgl/util/platform.hpp>
#include "java/lang.hpp"
#include "attach_env.hpp"

namespace mbgl {
namespace platform {

std::string uppercase(const std::string& str) {
auto env{ android::AttachEnv() };
auto jstring = jni::Make<jni::String>(*env, str.c_str());
return jni::Make<std::string>(*env, mbgl::android::java::lang::String::toUpperCase(*env, jstring));
}

std::string lowercase(const std::string& str) {
auto env{ android::AttachEnv() };
auto jstring = jni::Make<jni::String>(*env, str.c_str());
return jni::Make<std::string>(*env, mbgl::android::java::lang::String::toLowerCase(*env, jstring));
}

} // namespace platform
} // namespace mbgl

0 comments on commit 98a3c0a

Please sign in to comment.