testing_instance.h 6.5 KB

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