summaryrefslogtreecommitdiffstats
path: root/content/shell/android/browsertests_apk/content_browser_tests_android.cc
blob: 165f8042b9aee525c52d0d31103012c421f75e21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This class sets up the environment for running the content browser tests
// inside an android application.

#include <android/log.h>

#include "base/android/base_jni_registrar.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/strings/string_tokenizer.h"
#include "content/public/app/android_library_loader_hooks.h"
#include "content/public/app/content_main.h"
#include "content/shell/android/shell_jni_registrar.h"
#include "content/shell/shell_main_delegate.h"
#include "content/test/browser_test_message_pump_android.h"
#include "jni/ContentBrowserTestsActivity_jni.h"
#include "testing/android/native_test_util.h"

using testing::native_test_util::ArgsToArgv;
using testing::native_test_util::CreateFIFO;
using testing::native_test_util::ParseArgsFromCommandLineFile;
using testing::native_test_util::RedirectStream;
using testing::native_test_util::ScopedMainEntryLogger;

// The main function of the program to be wrapped as an apk.
extern int main(int argc, char** argv);

namespace {

// The test runner script writes the command line file in
// "/data/local/tmp".
static const char kCommandLineFilePath[] =
    "/data/local/tmp/content-browser-tests-command-line";

}  // namespace

namespace content {

static void RunTests(JNIEnv* env,
                     jobject obj,
                     jstring jfiles_dir,
                     jobject app_context) {

  // Command line basic initialization, will be fully initialized later.
  static const char* const kInitialArgv[] = { "ContentBrowserTestsActivity" };
  CommandLine::Init(arraysize(kInitialArgv), kInitialArgv);

  // Set the application context in base.
  base::android::ScopedJavaLocalRef<jobject> scoped_context(
      env, env->NewLocalRef(app_context));
  base::android::InitApplicationContext(scoped_context);
  base::android::RegisterJni(env);

  std::vector<std::string> args;
  ParseArgsFromCommandLineFile(kCommandLineFilePath, &args);

  std::vector<char*> argv;
  int argc = ArgsToArgv(args, &argv);

  // Fully initialize command line with arguments.
  CommandLine::ForCurrentProcess()->AppendArguments(
      CommandLine(argc, &argv[0]), false);

  // Create fifo and redirect stdout and stderr to it.
  FilePath files_dir(base::android::ConvertJavaStringToUTF8(env, jfiles_dir));
  FilePath fifo_path(files_dir.Append(FilePath("test.fifo")));
  CreateFIFO(fifo_path.value().c_str());
  RedirectStream(stdout, fifo_path.value().c_str(), "w");
  dup2(STDOUT_FILENO, STDERR_FILENO);

  ScopedMainEntryLogger scoped_main_entry_logger;
  main(argc, &argv[0]);
}
}  // namespace content

// This is called by the VM when the shared library is first loaded.
JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
  base::android::InitVM(vm);
  JNIEnv* env = base::android::AttachCurrentThread();

  if (!content::RegisterLibraryLoaderEntryHook(env))
    return -1;

  if (!content::android::RegisterShellJni(env))
    return -1;

  if (!content::BrowserTestMessagePumpAndroid::RegisterJni(env))
    return -1;

  if (!content::RegisterNativesImpl(env))
    return -1;

  content::SetContentMainDelegate(new content::ShellMainDelegate());
  return JNI_VERSION_1_4;
}