xref: /aosp_15_r20/external/cronet/testing/android/native_test/main_runner.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <vector>
6 
7 #include "base/android/jni_array.h"
8 #include "base/check.h"
9 #include "testing/android/native_test/native_main_runner_jni/MainRunner_jni.h"
10 #include "testing/android/native_test/native_test_util.h"
11 
12 extern int main(int argc, char** argv);
13 
14 namespace testing {
15 namespace android {
16 
JNI_MainRunner_RunMain(JNIEnv * env,const base::android::JavaParamRef<jobjectArray> & command_line)17 static jint JNI_MainRunner_RunMain(
18     JNIEnv* env,
19     const base::android::JavaParamRef<jobjectArray>& command_line) {
20   // Guards against process being reused.
21   // In most cases, running main again will cause problems (static variables,
22   // singletons, lazy instances won't be in the same state as a clean run).
23   static bool alreadyRun = false;
24   CHECK(!alreadyRun);
25   alreadyRun = true;
26 
27   std::vector<std::string> cpp_command_line;
28   base::android::AppendJavaStringArrayToStringVector(env, command_line,
29                                                      &cpp_command_line);
30 
31   std::vector<char*> argv;
32   int argc = ArgsToArgv(cpp_command_line, &argv);
33   return main(argc, &argv[0]);
34 }
35 
36 }  // namespace android
37 }  // namespace testing
38