Commit 147adcfa by Jamie Madill Committed by Commit Bot

Android: Call getExternalStorageDirectory natively.

This changes the call to base.test.util.UrlUtils to a native OS method. We may need to update this when Chromium starts running tests on "R". At that point we'll need a non-base mechanism to pass the right folder to ANGLE. This could be via env vars, debug properties, command-line arguments, or #defines. The prior implementation is left as commented-out code as a reminder to fix later. Also updates WARN() to std::cerr because WARN() was not showing up when testing. Bug: chromium:1097957 Change-Id: I4a84ea007341dbe7fe2184eac3aae0ddca44cc9c Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2818240 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarYuly Novikov <ynovikov@chromium.org>
parent 5902753b
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "util/android/AndroidWindow.h" #include "util/android/AndroidWindow.h"
#include <pthread.h> #include <pthread.h>
#include <iostream>
#include "common/debug.h" #include "common/debug.h"
#include "util/android/third_party/android_native_app_glue.h" #include "util/android/third_party/android_native_app_glue.h"
...@@ -43,7 +44,7 @@ int SetScreenOrientation(struct android_app *app, int orientation) ...@@ -43,7 +44,7 @@ int SetScreenOrientation(struct android_app *app, int orientation)
JNIEnv *jni = GetJniEnv(); JNIEnv *jni = GetJniEnv();
if (!jni) if (!jni)
{ {
WARN() << "Failed to get JNI env for screen rotation"; std::cerr << "Failed to get JNI env for screen rotation";
return JNI_ERR; return JNI_ERR;
} }
...@@ -196,83 +197,131 @@ std::string AndroidWindow::GetExternalStorageDirectory() ...@@ -196,83 +197,131 @@ std::string AndroidWindow::GetExternalStorageDirectory()
JNIEnv *jni = GetJniEnv(); JNIEnv *jni = GetJniEnv();
if (!jni) if (!jni)
{ {
WARN() << "GetExternalStorageDirectory:: Failed to get JNI env"; std::cerr << "GetExternalStorageDirectory:: Failed to get JNI env";
return ""; return "";
} }
// https://stackoverflow.com/questions/12841240/android-pass-parameter-to-native-activity jclass classEnvironment = jni->FindClass("android/os/Environment");
jclass clazz = jni->GetObjectClass(sApp->activity->clazz); if (classEnvironment == 0)
if (clazz == 0)
{ {
WARN() << "GetExternalStorageDirectory: Bad activity"; std::cerr << "GetExternalStorageDirectory: Failed to find Environment";
return ""; return "";
} }
jmethodID giid = jni->GetMethodID(clazz, "getIntent", "()Landroid/content/Intent;"); // public static File getExternalStorageDirectory ()
if (giid == 0) jmethodID methodIDgetExternalStorageDirectory =
jni->GetStaticMethodID(classEnvironment, "getExternalStorageDirectory", "()Ljava/io/File;");
if (methodIDgetExternalStorageDirectory == 0)
{ {
WARN() << "GetExternalStorageDirectory: Could not find getIntent"; std::cerr << "GetExternalStorageDirectory: Failed to get static method";
return ""; return "";
} }
jobject intent = jni->CallObjectMethod(sApp->activity->clazz, giid); jobject objectFile =
if (intent == 0) jni->CallStaticObjectMethod(classEnvironment, methodIDgetExternalStorageDirectory);
{ jthrowable exception = jni->ExceptionOccurred();
WARN() << "GetExternalStorageDirectory: Error calling getIntent"; if (exception != 0)
return "";
}
jclass icl = jni->GetObjectClass(intent);
if (icl == 0)
{
WARN() << "GetExternalStorageDirectory: Error getting getIntent class";
return "";
}
jmethodID gseid =
jni->GetMethodID(icl, "getStringExtra", "(Ljava/lang/String;)Ljava/lang/String;");
if (gseid == 0)
{ {
WARN() << "GetExternalStorageDirectory: Could not find getStringExtra"; jni->ExceptionDescribe();
jni->ExceptionClear();
std::cerr << "GetExternalStorageDirectory: Failed because of exception";
return ""; return "";
} }
jstring stringPath = static_cast<jstring>(jni->CallObjectMethod( // Call method on File object to retrieve String object.
intent, gseid, jni->NewStringUTF("org.chromium.base.test.util.UrlUtils.RootDirectory"))); jclass classFile = jni->GetObjectClass(objectFile);
if (stringPath != 0) if (classEnvironment == 0)
{
const char *path = jni->GetStringUTFChars(stringPath, nullptr);
return std::string(path) + "/chromium_tests_root";
}
jclass environment = jni->FindClass("org/chromium/base/test/util/UrlUtils");
if (environment == 0)
{ {
WARN() << "GetExternalStorageDirectory: Failed to find Environment"; std::cerr << "GetExternalStorageDirectory: Failed to find object class";
return ""; return "";
} }
jmethodID getDir = jmethodID methodIDgetAbsolutePath =
jni->GetStaticMethodID(environment, "getIsolatedTestRoot", "()Ljava/lang/String;"); jni->GetMethodID(classFile, "getAbsolutePath", "()Ljava/lang/String;");
if (getDir == 0) if (methodIDgetAbsolutePath == 0)
{ {
WARN() << "GetExternalStorageDirectory: Failed to get static method"; std::cerr << "GetExternalStorageDirectory: Failed to get method ID";
return ""; return "";
} }
stringPath = static_cast<jstring>(jni->CallStaticObjectMethod(environment, getDir)); jstring stringPath =
static_cast<jstring>(jni->CallObjectMethod(objectFile, methodIDgetAbsolutePath));
jthrowable exception = jni->ExceptionOccurred();
// TODO(jmadill): Find how to pass the root test directory to ANGLE. http://crbug.com/1097957
// // https://stackoverflow.com/questions/12841240/android-pass-parameter-to-native-activity
// jclass clazz = jni->GetObjectClass(sApp->activity->clazz);
// if (clazz == 0)
// {
// std::cerr << "GetExternalStorageDirectory: Bad activity";
// return "";
// }
// jmethodID giid = jni->GetMethodID(clazz, "getIntent", "()Landroid/content/Intent;");
// if (giid == 0)
// {
// std::cerr << "GetExternalStorageDirectory: Could not find getIntent";
// return "";
// }
// jobject intent = jni->CallObjectMethod(sApp->activity->clazz, giid);
// if (intent == 0)
// {
// std::cerr << "GetExternalStorageDirectory: Error calling getIntent";
// return "";
// }
// jclass icl = jni->GetObjectClass(intent);
// if (icl == 0)
// {
// std::cerr << "GetExternalStorageDirectory: Error getting getIntent class";
// return "";
// }
// jmethodID gseid =
// jni->GetMethodID(icl, "getStringExtra", "(Ljava/lang/String;)Ljava/lang/String;");
// if (gseid == 0)
// {
// std::cerr << "GetExternalStorageDirectory: Could not find getStringExtra";
// return "";
// }
// jstring stringPath = static_cast<jstring>(jni->CallObjectMethod(
// intent, gseid, jni->NewStringUTF("org.chromium.base.test.util.UrlUtils.RootDirectory")));
// if (stringPath != 0)
// {
// const char *path = jni->GetStringUTFChars(stringPath, nullptr);
// return std::string(path) + "/chromium_tests_root";
// }
// jclass environment = jni->FindClass("org/chromium/base/test/util/UrlUtils");
// if (environment == 0)
// {
// std::cerr << "GetExternalStorageDirectory: Failed to find Environment";
// return "";
// }
// jmethodID getDir =
// jni->GetStaticMethodID(environment, "getIsolatedTestRoot", "()Ljava/lang/String;");
// if (getDir == 0)
// {
// std::cerr << "GetExternalStorageDirectory: Failed to get static method";
// return "";
// }
// stringPath = static_cast<jstring>(jni->CallStaticObjectMethod(environment, getDir));
exception = jni->ExceptionOccurred();
if (exception != 0) if (exception != 0)
{ {
jni->ExceptionDescribe(); jni->ExceptionDescribe();
jni->ExceptionClear(); jni->ExceptionClear();
WARN() << "GetExternalStorageDirectory: Failed because of exception"; std::cerr << "GetExternalStorageDirectory: Failed because of exception";
return ""; return "";
} }
const char *path = jni->GetStringUTFChars(stringPath, nullptr); const char *path = jni->GetStringUTFChars(stringPath, nullptr);
return std::string(path); return std::string(path) + "/chromium_tests_root";
} }
// static // static
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment