• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 PPAPI_TESTS_TESTING_INSTANCE_H_
6 #define PPAPI_TESTS_TESTING_INSTANCE_H_
7 
8 #include <string>
9 
10 #include "ppapi/utility/completion_callback_factory.h"
11 
12 #if defined(__native_client__)
13 #include "ppapi/cpp/instance.h"
14 #else
15 #include "ppapi/cpp/private/instance_private.h"
16 #endif
17 
18 // Windows defines 'PostMessage', so we have to undef it.
19 #ifdef PostMessage
20 #undef PostMessage
21 #endif
22 
23 class TestCase;
24 
25 // How signaling works:
26 //
27 // We want to signal to the Chrome browser test harness
28 // (chrome/test/ppapi/ppapi_browsertest.cc) that we're making progress and when
29 // we're done. This is done using the DOM controlller. The browser test waits
30 // for a message from it. We don't want to have a big wait for all tests in a
31 // TestCase since they can take a while and it might timeout.  So we send it
32 // pings between each test to tell it that we're still running tests and aren't
33 // stuck.
34 //
35 // If the value of the message is "..." then that tells the test runner that
36 // the test is progressing. It then waits for the next message until it either
37 // times out or the value is something other than "...". In this case, the value
38 // will be either "PASS" or "FAIL [optional message]" corresponding to the
39 // outcome of the entire test case. Timeout will be treated just like a failure
40 // of the entire test case and the test will be terminated.
41 //
42 // In trusted builds, we use InstancePrivate and allow tests that use
43 // synchronous scripting. NaCl does not support synchronous scripting.
44 class TestingInstance : public
45 #if defined(__native_client__)
46 pp::Instance {
47 #else
48 pp::InstancePrivate {
49 #endif
50  public:
51   explicit TestingInstance(PP_Instance instance);
52   virtual ~TestingInstance();
53 
54   // pp::Instance override.
55   virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]);
56   virtual void DidChangeView(const pp::View& view);
57   virtual bool HandleInputEvent(const pp::InputEvent& event);
58 
59 #if !(defined __native_client__)
60   virtual pp::Var GetInstanceObject();
61 #endif
62 
63   // Outputs the information from one test run, using the format
64   //   <test_name> [PASS|FAIL <error_message>]
65   //
66   // You should generally use one of the RUN_TEST* macros in test_case.h
67   // instead.
68   //
69   // If error_message is empty, we say the test passed and emit PASS. If
70   // error_message is nonempty, the test failed with that message as the error
71   // string.
72   //
73   // Intended usage:
74   //   PP_TimeTicks start_time(core.GetTimeTicks());
75   //   LogTest("Foo", FooTest(), start_time);
76   //
77   // Where FooTest is defined as:
78   //   std::string FooTest() {
79   //     if (something_horrible_happened)
80   //       return "Something horrible happened";
81   //     return "";
82   //   }
83   //
84   // NOTE: It's important to get the start time in the previous line, rather
85   //       than calling GetTimeTicks in the LogTestLine. There's no guarantee
86   //       that GetTimeTicks will be evaluated before FooTest().
87   void LogTest(const std::string& test_name,
88                const std::string& error_message,
89                PP_TimeTicks start_time);
current_test_name()90   const std::string& current_test_name() { return current_test_name_; }
91 
92   // Appends an error message to the log.
93   void AppendError(const std::string& message);
94 
95   // Passes the message_data through to the HandleMessage method on the
96   // TestClass object that's associated with this instance.
97   virtual void HandleMessage(const pp::Var& message_data);
98 
protocol()99   const std::string& protocol() {
100     return protocol_;
101   }
102 
ssl_server_port()103   int ssl_server_port() { return ssl_server_port_; }
104 
websocket_host()105   const std::string& websocket_host() { return websocket_host_; }
websocket_port()106   int websocket_port() { return websocket_port_; }
107 
108   // Posts a message to the test page to eval() the script.
109   void EvalScript(const std::string& script);
110 
111   // Sets the given cookie in the current document.
112   void SetCookie(const std::string& name, const std::string& value);
113 
114   void ReportProgress(const std::string& progress_value);
115 
116   // Logs the amount of time that a given test took to run. This is to help
117   // debug test timeouts that occur in automated testing.
118   void LogTestTime(const std::string& test_time);
119 
120   // Add a post-condition to the JavaScript on the test_case.html page. This
121   // JavaScript code will be run after the instance is shut down and must
122   // evaluate to |true| or the test will fail.
123   void AddPostCondition(const std::string& script);
124 
125   // See doc for |remove_plugin_|.
set_remove_plugin(bool remove)126   void set_remove_plugin(bool remove) { remove_plugin_ = remove; }
127 
128  private:
129   void ExecuteTests(int32_t unused);
130 
131   // Creates a new TestCase for the give test name, or NULL if there is no such
132   // test. Ownership is passed to the caller. The given string is split by '_'.
133   // The test case name is the first part.
134   TestCase* CaseForTestName(const std::string& name);
135 
136   // Sends a test command to the page using PostMessage.
137   void SendTestCommand(const std::string& command);
138   void SendTestCommand(const std::string& command, const std::string& params);
139 
140   // Appends a list of available tests to the console in the document.
141   void LogAvailableTests();
142 
143   // Appends the given error test to the console in the document.
144   void LogError(const std::string& text);
145 
146   // Appends the given HTML string to the console in the document.
147   void LogHTML(const std::string& html);
148 
149   pp::CompletionCallbackFactory<TestingInstance> callback_factory_;
150 
151   // Owning pointer to the current test case. Valid after Init has been called.
152   TestCase* current_case_;
153 
154   std::string current_test_name_;
155 
156   // A filter to use when running tests. This is passed to 'RunTests', which
157   // runs only tests whose name contains test_filter_ as a substring.
158   std::string test_filter_;
159 
160   // Set once the tests are run so we know not to re-run when the view is sized.
161   bool executed_tests_;
162 
163   // The number of tests executed so far.
164   int32_t number_tests_executed_;
165 
166   // Collects all errors to send the the browser. Empty indicates no error yet.
167   std::string errors_;
168 
169   // True if running in Native Client.
170   bool nacl_mode_;
171 
172   // String representing the protocol.  Used for detecting whether we're running
173   // with http.
174   std::string protocol_;
175 
176   // SSL server port.
177   int ssl_server_port_;
178 
179   // WebSocket host.
180   std::string websocket_host_;
181 
182   // WebSocket port.
183   int websocket_port_;
184 
185   // At the end of each set of tests, the plugin is removed from the web-page.
186   // However, for some tests, it is desirable to not remove the plguin from the
187   // page.
188   bool remove_plugin_;
189 };
190 
191 #endif  // PPAPI_TESTS_TESTING_INSTANCE_H_
192