1 // Copyright (c) 2012 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.
5 #ifndef CHROME_TEST_NACL_NACL_BROWSERTEST_UTIL_H_
6 #define CHROME_TEST_NACL_NACL_BROWSERTEST_UTIL_H_
8 #include "base/files/file_path.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/test/base/in_process_browser_test.h"
11 #include "content/public/test/javascript_test_observer.h"
13 // A helper base class that decodes structured automation messages of the form:
14 // {"type": type_name, ...}
15 class StructuredMessageHandler
: public content::TestMessageHandler
{
17 MessageResponse
HandleMessage(const std::string
& json
) override
;
19 // This method provides a higher-level interface for handling JSON messages
20 // from the DOM automation controler. Instead of handling a string
21 // containing a JSON-encoded object, this specialization of TestMessageHandler
22 // decodes the string into a dictionary. This makes it easier to send and
23 // receive structured messages. It is assumed the dictionary will always have
24 // a "type" field that indicates the nature of message.
25 virtual MessageResponse
HandleStructuredMessage(
26 const std::string
& type
,
27 base::DictionaryValue
* msg
) = 0;
30 // The structured message is missing an expected field.
31 MessageResponse
MissingField(
32 const std::string
& type
,
33 const std::string
& field
) WARN_UNUSED_RESULT
;
35 // Something went wrong while decoding the message.
36 MessageResponse
InternalError(const std::string
& reason
) WARN_UNUSED_RESULT
;
39 // A simple structured message handler for tests that load nexes.
40 class LoadTestMessageHandler
: public StructuredMessageHandler
{
42 LoadTestMessageHandler();
44 void Log(const std::string
& type
, const std::string
& message
);
46 MessageResponse
HandleStructuredMessage(const std::string
& type
,
47 base::DictionaryValue
* msg
) override
;
49 bool test_passed() const {
56 DISALLOW_COPY_AND_ASSIGN(LoadTestMessageHandler
);
59 class NaClBrowserTestBase
: public InProcessBrowserTest
{
61 NaClBrowserTestBase();
62 ~NaClBrowserTestBase() override
;
64 void SetUpCommandLine(base::CommandLine
* command_line
) override
;
66 void SetUpOnMainThread() override
;
68 // What variant are we running - newlib, glibc, pnacl, etc?
69 // This is used to compute what directory we're pulling data from, but it can
70 // also be used to affect the behavior of the test.
71 virtual base::FilePath::StringType
Variant() = 0;
73 // Where are the files for this class of test located on disk?
74 virtual bool GetDocumentRoot(base::FilePath
* document_root
);
76 virtual bool IsAPnaclTest();
78 // Map a file relative to the variant directory to a URL served by the test
80 GURL
TestURL(const base::FilePath::StringType
& url_fragment
);
82 // Load a URL and listen to automation events with a given handler.
83 // Returns true if the test glue function correctly. (The handler should
84 // seperately indicate if the test failed.)
85 bool RunJavascriptTest(const GURL
& url
, content::TestMessageHandler
* handler
);
87 // Run a simple test that checks that a nexe loads correctly. Useful for
88 // setting up other tests, such as checking that UMA data was logged.
89 void RunLoadTest(const base::FilePath::StringType
& test_file
);
91 // Run a test that was originally written to use NaCl's integration testing
92 // jig. These tests were originally driven by NaCl's SCons build in the
93 // nacl_integration test stage on the Chrome waterfall. Changes in the
94 // boundaries between the Chrome and NaCl repos have resulted in many of
95 // these tests having a stronger affinity with the Chrome repo. This method
96 // provides a compatibility layer to simplify turning nacl_integration tests
97 // into browser tests.
98 // |full_url| is true if the full URL is given, otherwise it is a
100 void RunNaClIntegrationTest(const base::FilePath::StringType
& url
,
101 bool full_url
= false);
104 bool StartTestServer();
106 scoped_ptr
<net::SpawnedTestServer
> test_server_
;
109 class NaClBrowserTestNewlib
: public NaClBrowserTestBase
{
111 base::FilePath::StringType
Variant() override
;
114 class NaClBrowserTestGLibc
: public NaClBrowserTestBase
{
116 base::FilePath::StringType
Variant() override
;
119 class NaClBrowserTestPnacl
: public NaClBrowserTestBase
{
121 base::FilePath::StringType
Variant() override
;
123 bool IsAPnaclTest() override
;
126 // TODO(jvoung): We can remove this and test the Subzero translator
127 // with NaClBrowserTestPnacl once Subzero is automatically chosen
128 // (not behind a flag).
129 class NaClBrowserTestPnaclSubzero
: public NaClBrowserTestPnacl
{
131 void SetUpCommandLine(base::CommandLine
* command_line
) override
;
134 class NaClBrowserTestPnaclNonSfi
: public NaClBrowserTestBase
{
136 void SetUpCommandLine(base::CommandLine
* command_line
) override
;
137 base::FilePath::StringType
Variant() override
;
140 // "Transitional" here means that this uses nacl_helper_nonsfi which has
141 // nacl_helper feature for Non-SFI mode, but statically linked to newlib
142 // instead of using host glibc. It is still under development.
143 // TODO(hidehiko): Switch NonSfi tests to use nacl_helper_nonsfi, when
144 // it is launched officially.
145 class NaClBrowserTestPnaclTransitionalNonSfi
146 : public NaClBrowserTestPnaclNonSfi
{
148 void SetUpCommandLine(base::CommandLine
* command_line
) override
;
151 class NaClBrowserTestNonSfiMode
: public NaClBrowserTestBase
{
153 void SetUpCommandLine(base::CommandLine
* command_line
) override
;
154 base::FilePath::StringType
Variant() override
;
157 // TODO(hidehiko): Switch NonSfi tests to use nacl_helper_nonsfi, when
158 // it is launched officially. See NaClBrowserTestPnaclTransitionalNonSfi
160 class NaClBrowserTestTransitionalNonSfi
: public NaClBrowserTestNonSfiMode
{
162 void SetUpCommandLine(base::CommandLine
* command_line
) override
;
165 // A NaCl browser test only using static files.
166 class NaClBrowserTestStatic
: public NaClBrowserTestBase
{
168 base::FilePath::StringType
Variant() override
;
169 bool GetDocumentRoot(base::FilePath
* document_root
) override
;
172 // A NaCl browser test that loads from an unpacked chrome extension.
173 // The directory of the unpacked extension files is determined by
174 // the tester's document root.
175 class NaClBrowserTestNewlibExtension
: public NaClBrowserTestNewlib
{
177 void SetUpCommandLine(base::CommandLine
* command_line
) override
;
180 class NaClBrowserTestGLibcExtension
: public NaClBrowserTestGLibc
{
182 void SetUpCommandLine(base::CommandLine
* command_line
) override
;
185 // PNaCl tests take a long time on windows debug builds
186 // and sometimes time out. Disable until it is made faster:
187 // https://code.google.com/p/chromium/issues/detail?id=177555
188 #if (defined(OS_WIN) && !defined(NDEBUG))
189 # define MAYBE_PNACL(test_name) DISABLED_##test_name
191 # define MAYBE_PNACL(test_name) test_name
194 // NaCl glibc tests are included for x86 only, as there is no glibc support
195 // for other architectures (ARM/MIPS).
196 #if defined(ARCH_CPU_X86_FAMILY)
197 # define MAYBE_GLIBC(test_name) test_name
199 # define MAYBE_GLIBC(test_name) DISABLED_##test_name
202 // Sanitizers internally use some syscalls which non-SFI NaCl disallows.
203 #if defined(OS_LINUX) && !defined(ADDRESS_SANITIZER) && \
204 !defined(THREAD_SANITIZER) && !defined(MEMORY_SANITIZER) && \
205 !defined(LEAK_SANITIZER)
206 # define MAYBE_NONSFI(test_case) test_case
208 # define MAYBE_NONSFI(test_case) DISABLED_##test_case
211 // Currently, we only support it on x86-32 or ARM architecture.
212 // TODO(hidehiko,mazda): Enable this on x86-64, too, when it is supported.
213 #if defined(OS_LINUX) && !defined(ADDRESS_SANITIZER) && \
214 !defined(THREAD_SANITIZER) && !defined(MEMORY_SANITIZER) && \
215 !defined(LEAK_SANITIZER) && \
216 (defined(ARCH_CPU_X86) || defined(ARCH_CPU_ARMEL))
217 # define MAYBE_TRANSITIONAL_NONSFI(test_case) test_case
219 # define MAYBE_TRANSITIONAL_NONSFI(test_case) DISABLED_##test_case
222 // Currently, translation from pexe to non-sfi nexe is supported only for
223 // x86-32 or ARM binary.
224 #if defined(OS_LINUX) && (defined(ARCH_CPU_X86) || defined(ARCH_CPU_ARMEL))
225 # define MAYBE_PNACL_NONSFI(test_case) test_case
227 # define MAYBE_PNACL_NONSFI(test_case) DISABLED_##test_case
230 // Similar to MAYBE_TRANSITIONAL_NONSFI, this is available only on x86-32,
231 // x86-64 or ARM linux.
232 #if defined(OS_LINUX) && \
233 (defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARMEL))
234 # define MAYBE_PNACL_TRANSITIONAL_NONSFI(test_case) test_case
236 # define MAYBE_PNACL_TRANSITIONAL_NONSFI(test_case) DISABLED_##test_case
240 #define NACL_BROWSER_TEST_F(suite, name, body) \
241 IN_PROC_BROWSER_TEST_F(suite##Newlib, name) \
243 IN_PROC_BROWSER_TEST_F(suite##GLibc, MAYBE_GLIBC(name)) \
245 IN_PROC_BROWSER_TEST_F(suite##Pnacl, MAYBE_PNACL(name)) \
248 #endif // CHROME_TEST_NACL_NACL_BROWSERTEST_UTIL_H_