blob: 4c69b95ab55dc100cc9d381589f7d40c584eedd5 [file] [log] [blame]
Andrew Top193dc3d2019-01-23 09:57:23 -08001// Copyright 2017 The Cobalt Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include <string>
16
17#include "starboard/android/shared/jni_env_ext.h"
18#include "starboard/configuration.h"
19
20#include "testing/gtest/include/gtest/gtest.h"
21
22namespace starboard {
23namespace android {
24namespace shared {
25namespace {
26
27// UTF-16, UTF-8, and Modified UTF-8 test strings, all "𐆖€£$"
28// 𐆖 U+10196 -> U16: D800 DD96 U8: F0 90 86 96 MU8: ED A0 80 ED B6 96
29// € U+020AC -> U16: 20AC U8: E2 82 AC MU8: E2 82 AC
30// £ U+000A3 -> U16: 00A3 U8: C2 A3 MU8: C2 A3
31// $ U+00024 -> U16: 0024 U8: 24 MU8: 24
32const char16_t kU16[] = u"\U00010196\u20AC\u00A3\u0024";
33const char kU8[] = "\xF0\x90\x86\x96\xE2\x82\xAC\xC2\xA3\x24";
34const char kMU8[] = "\xED\xA0\x80\xED\xB6\x96\xE2\x82\xAC\xC2\xA3\x24";
35
36// Subtract one from the array size to not count the null terminator.
37const int kU16Length = SB_ARRAY_SIZE(kU16) - 1;
38const int kU8Length = SB_ARRAY_SIZE(kU8) - 1;
39const int kMU8Length = SB_ARRAY_SIZE(kMU8) - 1;
40
41// Note: there is no test for getting the string back as modified UTF-8 since
42// on some Android devices GetStringUTFChars() may return standard UTF-8.
43// (e.g. Nexus Player returns modified UTF-8, but Shield returns standard UTF-8)
44// see: https://github.com/android-ndk/ndk/issues/283
45
46TEST(JniEnvExtTest, NewStringStandardUTF) {
47 JniEnvExt* env = JniEnvExt::Get();
48 jstring j_str = env->NewStringStandardUTFOrAbort(kU8);
49
50 EXPECT_EQ(kU16Length, env->GetStringLength(j_str));
51 const jchar* u16_chars = env->GetStringChars(j_str, NULL);
52 std::u16string u16_string(
53 reinterpret_cast<const char16_t*>(u16_chars), kU16Length);
54 EXPECT_EQ(std::u16string(kU16), u16_string);
55 env->ReleaseStringChars(j_str, u16_chars);
56}
57
58TEST(JniEnvExtTest, NewStringModifiedUTF) {
59 JniEnvExt* env = JniEnvExt::Get();
60 jstring j_str = env->NewStringUTF(kMU8);
61
62 EXPECT_EQ(kU16Length, env->GetStringLength(j_str));
63 const jchar* u16_chars = env->GetStringChars(j_str, NULL);
64 std::u16string u16_string(
65 reinterpret_cast<const char16_t*>(u16_chars), kU16Length);
66 EXPECT_EQ(std::u16string(kU16), u16_string);
67 env->ReleaseStringChars(j_str, u16_chars);
68}
69
70TEST(JniEnvExtTest, EmptyNewStringStandardUTF) {
71 JniEnvExt* env = JniEnvExt::Get();
72 jstring j_str = env->NewStringStandardUTFOrAbort("");
73
74 EXPECT_EQ(0, env->GetStringLength(j_str));
75}
76
77TEST(JniEnvExtTest, GetStringStandardUTF) {
78 JniEnvExt* env = JniEnvExt::Get();
79 jstring j_str =
80 env->NewString(reinterpret_cast<const jchar*>(kU16), kU16Length);
81
82 std::string str = env->GetStringStandardUTFOrAbort(j_str);
83 EXPECT_EQ(kU8Length, str.length());
84 EXPECT_EQ(std::string(kU8), str);
85 env->DeleteLocalRef(j_str);
86}
87
88TEST(JniEnvExtTest, EmptyGetStringStandardUTF) {
89 JniEnvExt* env = JniEnvExt::Get();
90 jchar empty[] = {};
91 jstring j_str = env->NewString(empty, 0);
92
93 std::string str = env->GetStringStandardUTFOrAbort(j_str);
94 EXPECT_EQ(0, str.length());
95 EXPECT_EQ(std::string(), str);
96 env->DeleteLocalRef(j_str);
97}
98
99} // namespace
100} // namespace shared
101} // namespace android
102} // namespace starboard