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 PPAPI_TESTS_TESTING_INSTANCE_H_
6 #define PPAPI_TESTS_TESTING_INSTANCE_H_
10 #include "ppapi/utility/completion_callback_factory.h"
12 #if defined(__native_client__)
13 #include "ppapi/cpp/instance.h"
15 #include "ppapi/cpp/private/instance_private.h"
18 // Windows defines 'PostMessage', so we have to undef it.
25 // How signaling works:
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
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.
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__)
51 explicit TestingInstance(PP_Instance instance
);
52 virtual ~TestingInstance();
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
);
59 #if !(defined __native_client__)
60 virtual pp::Var
GetInstanceObject();
63 // Outputs the information from one test run, using the format
64 // <test_name> [PASS|FAIL <error_message>]
66 // You should generally use one of the RUN_TEST* macros in test_case.h
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
74 // PP_TimeTicks start_time(core.GetTimeTicks());
75 // LogTest("Foo", FooTest(), start_time);
77 // Where FooTest is defined as:
78 // std::string FooTest() {
79 // if (something_horrible_happened)
80 // return "Something horrible happened";
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
);
91 // Appends an error message to the log.
92 void AppendError(const std::string
& message
);
94 // Passes the message_data through to the HandleMessage method on the
95 // TestClass object that's associated with this instance.
96 virtual void HandleMessage(const pp::Var
& message_data
);
98 const std::string
& protocol() {
102 int ssl_server_port() { return ssl_server_port_
; }
104 const std::string
& websocket_host() { return websocket_host_
; }
105 int websocket_port() { return websocket_port_
; }
107 // Posts a message to the test page to eval() the script.
108 void EvalScript(const std::string
& script
);
110 // Sets the given cookie in the current document.
111 void SetCookie(const std::string
& name
, const std::string
& value
);
113 void ReportProgress(const std::string
& progress_value
);
115 // Logs the amount of time that a given test took to run. This is to help
116 // debug test timeouts that occur in automated testing.
117 void LogTestTime(const std::string
& test_time
);
119 // Add a post-condition to the JavaScript on the test_case.html page. This
120 // JavaScript code will be run after the instance is shut down and must
121 // evaluate to |true| or the test will fail.
122 void AddPostCondition(const std::string
& script
);
124 // See doc for |remove_plugin_|.
125 void set_remove_plugin(bool remove
) { remove_plugin_
= remove
; }
128 void ExecuteTests(int32_t unused
);
130 // Creates a new TestCase for the give test name, or NULL if there is no such
131 // test. Ownership is passed to the caller. The given string is split by '_'.
132 // The test case name is the first part.
133 TestCase
* CaseForTestName(const std::string
& name
);
135 // Sends a test command to the page using PostMessage.
136 void SendTestCommand(const std::string
& command
);
137 void SendTestCommand(const std::string
& command
, const std::string
& params
);
139 // Appends a list of available tests to the console in the document.
140 void LogAvailableTests();
142 // Appends the given error test to the console in the document.
143 void LogError(const std::string
& text
);
145 // Appends the given HTML string to the console in the document.
146 void LogHTML(const std::string
& html
);
148 pp::CompletionCallbackFactory
<TestingInstance
> callback_factory_
;
150 // Owning pointer to the current test case. Valid after Init has been called.
151 TestCase
* current_case_
;
153 // A filter to use when running tests. This is passed to 'RunTests', which
154 // runs only tests whose name contains test_filter_ as a substring.
155 std::string test_filter_
;
157 // Set once the tests are run so we know not to re-run when the view is sized.
158 bool executed_tests_
;
160 // The number of tests executed so far.
161 int32_t number_tests_executed_
;
163 // Collects all errors to send the the browser. Empty indicates no error yet.
166 // True if running in Native Client.
169 // String representing the protocol. Used for detecting whether we're running
171 std::string protocol_
;
174 int ssl_server_port_
;
177 std::string websocket_host_
;
182 // At the end of each set of tests, the plugin is removed from the web-page.
183 // However, for some tests, it is desirable to not remove the plguin from the
188 #endif // PPAPI_TESTS_TESTING_INSTANCE_H_