Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / mojo / apps / js / test / run_apps_js_tests.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
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 "base/file_util.h"
6 #include "base/files/file_path.h"
7 #include "base/path_service.h"
8 #include "gin/modules/console.h"
9 #include "gin/modules/module_registry.h"
10 #include "gin/modules/timer.h"
11 #include "gin/test/file_runner.h"
12 #include "gin/test/gtest.h"
13 #include "mojo/apps/js/bindings/monotonic_clock.h"
14 #include "mojo/apps/js/bindings/threading.h"
15 #include "mojo/bindings/js/core.h"
16 #include "mojo/common/test/test_utils.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace mojo {
20 namespace js {
21 namespace {
22
23 class TestRunnerDelegate : public gin::FileRunnerDelegate {
24  public:
25   TestRunnerDelegate() {
26     AddBuiltinModule(gin::Console::kModuleName, gin::Console::GetModule);
27     AddBuiltinModule(Core::kModuleName, Core::GetModule);
28     AddBuiltinModule(gin::TimerModule::kName, gin::TimerModule::GetModule);
29     AddBuiltinModule(apps::MonotonicClock::kModuleName,
30                      apps::MonotonicClock::GetModule);
31     AddBuiltinModule(apps::Threading::kModuleName, apps::Threading::GetModule);
32   }
33
34  private:
35   DISALLOW_COPY_AND_ASSIGN(TestRunnerDelegate);
36 };
37
38 void RunTest(std::string test, bool run_until_idle) {
39   base::FilePath path;
40   PathService::Get(base::DIR_SOURCE_ROOT, &path);
41   path = path.AppendASCII("mojo")
42              .AppendASCII("apps")
43              .AppendASCII("js")
44              .AppendASCII("bindings")
45              .AppendASCII(test);
46   TestRunnerDelegate delegate;
47   gin::RunTestFromFile(path, &delegate, run_until_idle);
48 }
49
50 // TODO(abarth): Should we autogenerate these stubs from GYP?
51
52 TEST(JSTest, sample_test) {
53   // TODO(yzshen): Remove this check once isolated tests are supported on the
54   // Chromium waterfall. (http://crbug.com/351214)
55   const base::FilePath test_file_path(
56       test::GetFilePathForJSResource(
57           "mojo/public/interfaces/bindings/tests/sample_service.mojom"));
58   if (!base::PathExists(test_file_path)) {
59     LOG(WARNING) << "Mojom binding files don't exist. Skipping the test.";
60     return;
61   }
62
63   RunTest("sample_service_unittests.js", true);
64 }
65
66 TEST(JSTest, connection) {
67   // TODO(yzshen): Remove this check once isolated tests are supported on the
68   // Chromium waterfall. (http://crbug.com/351214)
69   const base::FilePath test_file_path(
70       test::GetFilePathForJSResource(
71           "mojo/public/interfaces/bindings/tests/sample_service.mojom"));
72   if (!base::PathExists(test_file_path)) {
73     LOG(WARNING) << "Mojom binding files don't exist. Skipping the test.";
74     return;
75   }
76
77   RunTest("connection_unittests.js", false);
78 }
79
80 TEST(JSTest, monotonic_clock) {
81   RunTest("monotonic_clock_unittests.js", false);
82 }
83
84 }  // namespace
85 }  // namespace js
86 }  // namespace mojo