Home | History | Annotate | Download | only in tests
      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);
     90 
     91   // Appends an error message to the log.
     92   void AppendError(const std::string& message);
     93 
     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);
     97 
     98   const std::string& protocol() {
     99     return protocol_;
    100   }
    101 
    102   int ssl_server_port() { return ssl_server_port_; }
    103 
    104   const std::string& websocket_host() { return websocket_host_; }
    105   int websocket_port() { return websocket_port_; }
    106 
    107   // Posts a message to the test page to eval() the script.
    108   void EvalScript(const std::string& script);
    109 
    110   // Sets the given cookie in the current document.
    111   void SetCookie(const std::string& name, const std::string& value);
    112 
    113   void ReportProgress(const std::string& progress_value);
    114 
    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);
    118 
    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);
    123 
    124   // See doc for |remove_plugin_|.
    125   void set_remove_plugin(bool remove) { remove_plugin_ = remove; }
    126 
    127  private:
    128   void ExecuteTests(int32_t unused);
    129 
    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);
    134 
    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);
    138 
    139   // Appends a list of available tests to the console in the document.
    140   void LogAvailableTests();
    141 
    142   // Appends the given error test to the console in the document.
    143   void LogError(const std::string& text);
    144 
    145   // Appends the given HTML string to the console in the document.
    146   void LogHTML(const std::string& html);
    147 
    148   pp::CompletionCallbackFactory<TestingInstance> callback_factory_;
    149 
    150   // Owning pointer to the current test case. Valid after Init has been called.
    151   TestCase* current_case_;
    152 
    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_;
    156 
    157   // Set once the tests are run so we know not to re-run when the view is sized.
    158   bool executed_tests_;
    159 
    160   // The number of tests executed so far.
    161   int32_t number_tests_executed_;
    162 
    163   // Collects all errors to send the the browser. Empty indicates no error yet.
    164   std::string errors_;
    165 
    166   // True if running in Native Client.
    167   bool nacl_mode_;
    168 
    169   // String representing the protocol.  Used for detecting whether we're running
    170   // with http.
    171   std::string protocol_;
    172 
    173   // SSL server port.
    174   int ssl_server_port_;
    175 
    176   // WebSocket host.
    177   std::string websocket_host_;
    178 
    179   // WebSocket port.
    180   int websocket_port_;
    181 
    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
    184   // page.
    185   bool remove_plugin_;
    186 };
    187 
    188 #endif  // PPAPI_TESTS_TESTING_INSTANCE_H_
    189