7f74379d94966222395035d35032dfb630828491
[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   virtual 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   virtual MessageResponse HandleStructuredMessage(
47       const std::string& type,
48       base::DictionaryValue* msg) OVERRIDE;
49
50   bool test_passed() const {
51     return test_passed_;
52   }
53
54  private:
55   bool test_passed_;
56
57   DISALLOW_COPY_AND_ASSIGN(LoadTestMessageHandler);
58 };
59
60 class NaClBrowserTestBase : public InProcessBrowserTest {
61  public:
62   NaClBrowserTestBase();
63   virtual ~NaClBrowserTestBase();
64
65   virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE;
66
67   virtual void SetUpOnMainThread() OVERRIDE;
68
69   // What variant are we running - newlib, glibc, pnacl, etc?
70   // This is used to compute what directory we're pulling data from, but it can
71   // also be used to affect the behavior of the test.
72   virtual base::FilePath::StringType Variant() = 0;
73
74   // Where are the files for this class of test located on disk?
75   virtual bool GetDocumentRoot(base::FilePath* document_root);
76
77   virtual bool IsAPnaclTest();
78
79   virtual bool IsPnaclDisabled();
80
81   // Map a file relative to the variant directory to a URL served by the test
82   // web server.
83   GURL TestURL(const base::FilePath::StringType& url_fragment);
84
85   // Load a URL and listen to automation events with a given handler.
86   // Returns true if the test glue function correctly.  (The handler should
87   // seperately indicate if the test failed.)
88   bool RunJavascriptTest(const GURL& url, content::TestMessageHandler* handler);
89
90   // Run a simple test that checks that a nexe loads correctly.  Useful for
91   // setting up other tests, such as checking that UMA data was logged.
92   void RunLoadTest(const base::FilePath::StringType& test_file);
93
94   // Run a test that was originally written to use NaCl's integration testing
95   // jig. These tests were originally driven by NaCl's SCons build in the
96   // nacl_integration test stage on the Chrome waterfall. Changes in the
97   // boundaries between the Chrome and NaCl repos have resulted in many of
98   // these tests having a stronger affinity with the Chrome repo. This method
99   // provides a compatibility layer to simplify turning nacl_integration tests
100   // into browser tests.
101   // |full_url| is true if the full URL is given, otherwise it is a
102   // relative URL.
103   void RunNaClIntegrationTest(const base::FilePath::StringType& url,
104                               bool full_url = false);
105
106  private:
107   bool StartTestServer();
108
109   scoped_ptr<net::SpawnedTestServer> test_server_;
110 };
111
112 class NaClBrowserTestNewlib : public NaClBrowserTestBase {
113  public:
114   virtual base::FilePath::StringType Variant() OVERRIDE;
115 };
116
117 class NaClBrowserTestGLibc : public NaClBrowserTestBase {
118  public:
119   virtual base::FilePath::StringType Variant() OVERRIDE;
120 };
121
122 class NaClBrowserTestPnacl : public NaClBrowserTestBase {
123  public:
124   virtual base::FilePath::StringType Variant() OVERRIDE;
125
126   virtual bool IsAPnaclTest() OVERRIDE;
127 };
128
129 class NaClBrowserTestPnaclNonSfi : public NaClBrowserTestBase {
130  public:
131   virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE;
132   virtual base::FilePath::StringType Variant() OVERRIDE;
133 };
134
135 // Class used to test that when --disable-pnacl is specified the PNaCl mime
136 // type is not available.
137 class NaClBrowserTestPnaclDisabled : public NaClBrowserTestBase {
138  public:
139   virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE;
140
141   virtual base::FilePath::StringType Variant() OVERRIDE;
142
143   virtual bool IsAPnaclTest() OVERRIDE;
144
145   virtual bool IsPnaclDisabled() OVERRIDE;
146 };
147
148 class NaClBrowserTestNonSfiMode : public NaClBrowserTestBase {
149  public:
150   virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE;
151   virtual base::FilePath::StringType Variant() OVERRIDE;
152 };
153
154 // A NaCl browser test only using static files.
155 class NaClBrowserTestStatic : public NaClBrowserTestBase {
156  public:
157   virtual base::FilePath::StringType Variant() OVERRIDE;
158   virtual bool GetDocumentRoot(base::FilePath* document_root) OVERRIDE;
159 };
160
161 // A NaCl browser test that loads from an unpacked chrome extension.
162 // The directory of the unpacked extension files is determined by
163 // the tester's document root.
164 class NaClBrowserTestNewlibExtension : public NaClBrowserTestNewlib {
165  public:
166   virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE;
167 };
168
169 // PNaCl tests take a long time on windows debug builds
170 // and sometimes time out.  Disable until it is made faster:
171 // https://code.google.com/p/chromium/issues/detail?id=177555
172 #if (defined(OS_WIN) && !defined(NDEBUG))
173 #  define MAYBE_PNACL(test_name) DISABLED_##test_name
174 #else
175 #  define MAYBE_PNACL(test_name) test_name
176 #endif
177
178 // NaCl glibc tests are included for x86 only, as there is no glibc support
179 // for other architectures (ARM/MIPS).
180 #if defined(ARCH_CPU_X86_FAMILY)
181 #  define MAYBE_GLIBC(test_name) test_name
182 #else
183 #  define MAYBE_GLIBC(test_name) DISABLED_##test_name
184 #endif
185
186 // ASan does not work with libc-free context, so disable the test.
187 #if defined(OS_LINUX) && !defined(ADDRESS_SANITIZER)
188 #  define MAYBE_NONSFI(test_case) test_case
189 #else
190 #  define MAYBE_NONSFI(test_case) DISABLED_##test_case
191 #endif
192
193 // Currently, translation from pexe to non-sfi nexe is supported only for
194 // x86-32 binary.
195 #if defined(OS_LINUX) && defined(ARCH_CPU_X86)
196 #  define MAYBE_PNACL_NONSFI(test_case) test_case
197 #else
198 #  define MAYBE_PNACL_NONSFI(test_case) DISABLED_##test_case
199 #endif
200
201 #define NACL_BROWSER_TEST_F(suite, name, body) \
202 IN_PROC_BROWSER_TEST_F(suite##Newlib, name) \
203 body \
204 IN_PROC_BROWSER_TEST_F(suite##GLibc, MAYBE_GLIBC(name)) \
205 body \
206 IN_PROC_BROWSER_TEST_F(suite##Pnacl, MAYBE_PNACL(name)) \
207 body
208
209 #endif  // CHROME_TEST_NACL_NACL_BROWSERTEST_UTIL_H_