Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / test / nacl / nacl_browsertest_util.h
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.
4
5 #ifndef CHROME_TEST_NACL_NACL_BROWSERTEST_UTIL_H_
6 #define CHROME_TEST_NACL_NACL_BROWSERTEST_UTIL_H_
7
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"
12
13 // A helper base class that decodes structured automation messages of the form:
14 // {"type": type_name, ...}
15 class StructuredMessageHandler : public content::TestMessageHandler {
16  public:
17   MessageResponse HandleMessage(const std::string& json) override;
18
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;
28
29  protected:
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;
34
35   // Something went wrong while decoding the message.
36   MessageResponse InternalError(const std::string& reason) WARN_UNUSED_RESULT;
37 };
38
39 // A simple structured message handler for tests that load nexes.
40 class LoadTestMessageHandler : public StructuredMessageHandler {
41  public:
42   LoadTestMessageHandler();
43
44   void Log(const std::string& type, const std::string& message);
45
46   MessageResponse HandleStructuredMessage(const std::string& type,
47                                           base::DictionaryValue* msg) override;
48
49   bool test_passed() const {
50     return test_passed_;
51   }
52
53  private:
54   bool test_passed_;
55
56   DISALLOW_COPY_AND_ASSIGN(LoadTestMessageHandler);
57 };
58
59 class NaClBrowserTestBase : public InProcessBrowserTest {
60  public:
61   NaClBrowserTestBase();
62   ~NaClBrowserTestBase() override;
63
64   void SetUpCommandLine(base::CommandLine* command_line) override;
65
66   void SetUpOnMainThread() override;
67
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;
72
73   // Where are the files for this class of test located on disk?
74   virtual bool GetDocumentRoot(base::FilePath* document_root);
75
76   virtual bool IsAPnaclTest();
77
78   // Map a file relative to the variant directory to a URL served by the test
79   // web server.
80   GURL TestURL(const base::FilePath::StringType& url_fragment);
81
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);
86
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);
90
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
99   // relative URL.
100   void RunNaClIntegrationTest(const base::FilePath::StringType& url,
101                               bool full_url = false);
102
103  private:
104   bool StartTestServer();
105
106   scoped_ptr<net::SpawnedTestServer> test_server_;
107 };
108
109 class NaClBrowserTestNewlib : public NaClBrowserTestBase {
110  public:
111   base::FilePath::StringType Variant() override;
112 };
113
114 class NaClBrowserTestGLibc : public NaClBrowserTestBase {
115  public:
116   base::FilePath::StringType Variant() override;
117 };
118
119 class NaClBrowserTestPnacl : public NaClBrowserTestBase {
120  public:
121   base::FilePath::StringType Variant() override;
122
123   bool IsAPnaclTest() override;
124 };
125
126 class NaClBrowserTestPnaclNonSfi : public NaClBrowserTestBase {
127  public:
128   void SetUpCommandLine(base::CommandLine* command_line) override;
129   base::FilePath::StringType Variant() override;
130 };
131
132 class NaClBrowserTestNonSfiMode : public NaClBrowserTestBase {
133  public:
134   void SetUpCommandLine(base::CommandLine* command_line) override;
135   base::FilePath::StringType Variant() override;
136 };
137
138 // A NaCl browser test only using static files.
139 class NaClBrowserTestStatic : public NaClBrowserTestBase {
140  public:
141   base::FilePath::StringType Variant() override;
142   bool GetDocumentRoot(base::FilePath* document_root) override;
143 };
144
145 // A NaCl browser test that loads from an unpacked chrome extension.
146 // The directory of the unpacked extension files is determined by
147 // the tester's document root.
148 class NaClBrowserTestNewlibExtension : public NaClBrowserTestNewlib {
149  public:
150   void SetUpCommandLine(base::CommandLine* command_line) override;
151 };
152
153 class NaClBrowserTestGLibcExtension : public NaClBrowserTestGLibc {
154  public:
155   void SetUpCommandLine(base::CommandLine* command_line) override;
156 };
157
158 // PNaCl tests take a long time on windows debug builds
159 // and sometimes time out.  Disable until it is made faster:
160 // https://code.google.com/p/chromium/issues/detail?id=177555
161 #if (defined(OS_WIN) && !defined(NDEBUG))
162 #  define MAYBE_PNACL(test_name) DISABLED_##test_name
163 #else
164 #  define MAYBE_PNACL(test_name) test_name
165 #endif
166
167 // NaCl glibc tests are included for x86 only, as there is no glibc support
168 // for other architectures (ARM/MIPS).
169 #if defined(ARCH_CPU_X86_FAMILY)
170 #  define MAYBE_GLIBC(test_name) test_name
171 #else
172 #  define MAYBE_GLIBC(test_name) DISABLED_##test_name
173 #endif
174
175 // Sanitizers internally use some syscalls which non-SFI NaCl disallows.
176 #if defined(OS_LINUX) && !defined(ADDRESS_SANITIZER) && \
177     !defined(THREAD_SANITIZER) && !defined(MEMORY_SANITIZER) && \
178     !defined(LEAK_SANITIZER)
179 #  define MAYBE_NONSFI(test_case) test_case
180 #else
181 #  define MAYBE_NONSFI(test_case) DISABLED_##test_case
182 #endif
183
184 // Currently, translation from pexe to non-sfi nexe is supported only for
185 // x86-32 binary.
186 #if defined(OS_LINUX) && defined(ARCH_CPU_X86)
187 #  define MAYBE_PNACL_NONSFI(test_case) test_case
188 #else
189 #  define MAYBE_PNACL_NONSFI(test_case) DISABLED_##test_case
190 #endif
191
192 #define NACL_BROWSER_TEST_F(suite, name, body) \
193 IN_PROC_BROWSER_TEST_F(suite##Newlib, name) \
194 body \
195 IN_PROC_BROWSER_TEST_F(suite##GLibc, MAYBE_GLIBC(name)) \
196 body \
197 IN_PROC_BROWSER_TEST_F(suite##Pnacl, MAYBE_PNACL(name)) \
198 body
199
200 #endif  // CHROME_TEST_NACL_NACL_BROWSERTEST_UTIL_H_