Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef PPAPI_TESTS_TEST_UTILS_H_ | 5 #ifndef PPAPI_TESTS_TEST_UTILS_H_ |
| 6 #define PPAPI_TESTS_TEST_UTILS_H_ | 6 #define PPAPI_TESTS_TEST_UTILS_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "ppapi/c/dev/ppb_testing_dev.h" | 10 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 11 #include "ppapi/c/pp_instance.h" | 11 #include "ppapi/c/pp_instance.h" |
| 12 #include "ppapi/c/pp_stdint.h" | 12 #include "ppapi/c/pp_stdint.h" |
| 13 #include "ppapi/cpp/completion_callback.h" | 13 #include "ppapi/cpp/completion_callback.h" |
| 14 #include "ppapi/utility/completion_callback_factory.h" | |
| 14 | 15 |
| 15 // Timeout to wait for some action to complete. | 16 // Timeout to wait for some action to complete. |
| 16 extern const int kActionTimeoutMs; | 17 extern const int kActionTimeoutMs; |
| 17 | 18 |
| 18 const PPB_Testing_Dev* GetTestingInterface(); | 19 const PPB_Testing_Dev* GetTestingInterface(); |
| 19 std::string ReportError(const char* method, int32_t error); | 20 std::string ReportError(const char* method, int32_t error); |
| 20 void PlatformSleep(int duration_ms); | 21 void PlatformSleep(int duration_ms); |
| 21 bool GetLocalHostPort(PP_Instance instance, std::string* host, uint16_t* port); | 22 bool GetLocalHostPort(PP_Instance instance, std::string* host, uint16_t* port); |
| 22 | 23 |
| 24 // Used for waiting for a nested event, such as a callback on a PPP interface. | |
|
bbudge
2012/04/04 19:03:00
This comment makes me wonder about the name of the
dmichael (off chromium)
2012/04/04 19:50:47
Agreed... I don't love the name but can't think o
| |
| 25 // "Wait()" will return immediately if it has already been signalled. Otherwise, | |
| 26 // it will run a nested message loop (using PPB_Testing.RunMessageLoop) until | |
| 27 // it has been signalled. | |
| 28 // Example: | |
| 29 // std::string TestFullscreen::TestNormalToFullscreen() { | |
| 30 // pp::Fullscreen screen_mode(instance); | |
| 31 // screen_mode.SetFullscreen(true); | |
| 32 // SimulateUserGesture(); | |
| 33 // // Let DidChangeView run in a nested loop. | |
| 34 // nested_event_.Wait(); | |
| 35 // Pass(); | |
| 36 // } | |
| 37 // | |
| 38 // void TestFullscreen::DidChangeView(const pp::View& view) { | |
| 39 // nested_event_.Signal(); | |
| 40 // } | |
| 41 class NestedEvent { | |
| 42 public: | |
| 43 explicit NestedEvent(PP_Instance instance) | |
| 44 : instance_(instance), waiting_(false), signalled_(false) { | |
| 45 } | |
| 46 // Run a nested message loop and wait until Signal() is called. If Signal() | |
| 47 // has already been called, return immediately without running a nested loop. | |
| 48 void Wait(); | |
| 49 // Signal the NestedEvent. If Wait() has been called, quit the message loop. | |
| 50 void Signal(); | |
| 51 private: | |
| 52 PP_Instance instance_; | |
| 53 bool waiting_; | |
| 54 bool signalled_; | |
| 55 // Disable copy and assign. | |
| 56 NestedEvent(const NestedEvent&); | |
| 57 NestedEvent& operator=(const NestedEvent&); | |
| 58 }; | |
| 59 | |
| 60 enum CallbackType { PP_REQUIRED, PP_OPTIONAL, PP_BLOCKING }; | |
| 23 class TestCompletionCallback { | 61 class TestCompletionCallback { |
| 24 public: | 62 public: |
| 25 TestCompletionCallback(PP_Instance instance); | 63 explicit TestCompletionCallback(PP_Instance instance); |
| 64 // TODO(dmichael): Remove this constructor. | |
| 26 TestCompletionCallback(PP_Instance instance, bool force_async); | 65 TestCompletionCallback(PP_Instance instance, bool force_async); |
| 27 | 66 |
| 67 TestCompletionCallback(PP_Instance instance, CallbackType callback_type); | |
| 68 | |
| 28 // Waits for the callback to be called and returns the | 69 // Waits for the callback to be called and returns the |
| 29 // result. Returns immediately if the callback was previously called | 70 // result. Returns immediately if the callback was previously called |
| 30 // and the result wasn't returned (i.e. each result value received | 71 // and the result wasn't returned (i.e. each result value received |
| 31 // by the callback is returned by WaitForResult() once and only | 72 // by the callback is returned by WaitForResult() once and only |
| 32 // once). | 73 // once). DEPRECATED: Please use the one below. |
| 74 // TODO(dmichael): Remove this one when all the tests are updated. | |
| 33 int32_t WaitForResult(); | 75 int32_t WaitForResult(); |
| 34 | 76 |
| 35 operator pp::CompletionCallback() const; | 77 // Wait for a result, given the return from the call which took this callback |
| 78 // as a parameter. If |result| is PP_OK_COMPLETIONPENDING, WaitForResult will | |
| 79 // block until its callback has been invoked (in some cases, this will already | |
| 80 // have happened, and WaitForCallback can return immediately). | |
| 81 // For any other values, WaitForResult will simply set its internal "result_" | |
| 82 // field. To retrieve the final result of the operation (i.e., the result | |
| 83 // the callback has run, if necessary), call result(). You can call result() | |
| 84 // as many times as necessary until a new pp::CompletionCallback is retrieved. | |
| 85 // | |
| 86 // In some cases, you may want to check that the callback was invoked in the | |
| 87 // expected way (i.e., if the callback was "Required", then it should be | |
| 88 // invoked asynchronously). Within the body of a test (where returning a non- | |
| 89 // empty string indicates test failure), you can use the | |
| 90 // CHECK_CALLBACK_BEHAVIOR(callback) macro. From within a helper function, | |
| 91 // you can use failed() and errors(). | |
| 92 // | |
| 93 // Example usage within a test: | |
| 94 // callback.WaitForResult(foo.DoSomething(callback)); | |
| 95 // CHECK_CALLBACK_BEHAVIOR(callback); | |
| 96 // ASSERT_EQ(PP_OK, callback.result()); | |
| 97 // | |
| 98 // Example usage within a helper function: | |
| 99 // void HelperFunction(std::string* error_message) { | |
| 100 // callback.WaitForResult(foo.DoSomething(callback)); | |
| 101 // if (callback.failed()) | |
| 102 // error_message->assign(callback.errors()); | |
| 103 // } | |
| 104 void WaitForResult(int32_t result); | |
| 36 | 105 |
| 106 // Used when you expect to receive either synchronous completion with PP_OK | |
| 107 // or a PP_ERROR_ABORTED asynchronously. | |
| 108 // Example usage: | |
| 109 // int32_t result = 0; | |
| 110 // { | |
| 111 // pp::URLLoader temp(instance_); | |
| 112 // result = temp.Open(request, callback); | |
| 113 // } | |
| 114 // callback.WaitForAbortResult(result); | |
| 115 // CHECK_CALLBACK_BEHAVIOR(callback); | |
| 116 void WaitForAbortResult(int32_t result); | |
| 117 | |
| 118 // Retrieve a pp::CompletionCallback for use in testing. This Reset()s the | |
| 119 // TestCompletionCallback. | |
| 120 pp::CompletionCallback GetCallback(); | |
| 121 operator pp::CompletionCallback() { | |
| 122 return GetCallback(); | |
| 123 } | |
| 124 | |
| 125 // TODO(dmichael): Remove run_count when all tests are updated. Most cases | |
| 126 // that use this can simply use CHECK_CALLBACK_BEHAVIOR. | |
| 37 unsigned run_count() const { return run_count_; } | 127 unsigned run_count() const { return run_count_; } |
| 128 // TODO(dmichael): Remove this; tests should use Reset() instead. | |
| 38 void reset_run_count() { run_count_ = 0; } | 129 void reset_run_count() { run_count_ = 0; } |
| 39 | 130 |
| 131 bool failed() { return !errors_.empty(); } | |
| 132 const std::string& errors() { return errors_; } | |
| 133 | |
| 40 int32_t result() const { return result_; } | 134 int32_t result() const { return result_; } |
| 41 | 135 |
| 136 // Reset so that this callback can be used again. | |
| 137 void Reset(); | |
| 138 | |
| 42 private: | 139 private: |
| 43 static void Handler(void* user_data, int32_t result); | 140 static void Handler(void* user_data, int32_t result); |
| 44 | 141 |
| 142 // Used to check that WaitForResult is only called once for each usage of the | |
| 143 // callback. | |
| 144 bool wait_for_result_called_; | |
| 145 // Indicates whether we have already been invoked. | |
| 45 bool have_result_; | 146 bool have_result_; |
| 147 // The last result received (or PP_OK_COMPLETIONCALLBACK if none). | |
| 46 int32_t result_; | 148 int32_t result_; |
| 47 bool force_async_; | 149 CallbackType callback_type_; |
| 48 bool post_quit_task_; | 150 bool post_quit_task_; |
| 151 std::string errors_; | |
| 49 unsigned run_count_; | 152 unsigned run_count_; |
| 50 PP_Instance instance_; | 153 PP_Instance instance_; |
| 51 }; | 154 }; |
| 52 | 155 |
| 156 // Verifies that the callback didn't record any errors. If the callback is run | |
| 157 // in an unexpected way (e.g., if it's invoked asynchronously when the call | |
| 158 // should have blocked), this returns an appropriate error string. | |
| 159 #define CHECK_CALLBACK_BEHAVIOR(callback) \ | |
| 160 do { \ | |
| 161 if ((callback).failed()) \ | |
| 162 return (callback).errors(); \ | |
| 163 } while (false) | |
| 164 | |
| 53 /* | 165 /* |
| 54 * A set of macros to use for platform detection. These were largely copied | 166 * A set of macros to use for platform detection. These were largely copied |
| 55 * from chromium's build_config.h. | 167 * from chromium's build_config.h. |
| 56 */ | 168 */ |
| 57 #if defined(__APPLE__) | 169 #if defined(__APPLE__) |
| 58 #define PPAPI_OS_MACOSX 1 | 170 #define PPAPI_OS_MACOSX 1 |
| 59 #elif defined(ANDROID) | 171 #elif defined(ANDROID) |
| 60 #define PPAPI_OS_ANDROID 1 | 172 #define PPAPI_OS_ANDROID 1 |
| 61 #elif defined(__native_client__) | 173 #elif defined(__native_client__) |
| 62 #define PPAPI_OS_NACL 1 | 174 #define PPAPI_OS_NACL 1 |
| 63 #elif defined(__linux__) | 175 #elif defined(__linux__) |
| 64 #define PPAPI_OS_LINUX 1 | 176 #define PPAPI_OS_LINUX 1 |
| 65 #elif defined(_WIN32) | 177 #elif defined(_WIN32) |
| 66 #define PPAPI_OS_WIN 1 | 178 #define PPAPI_OS_WIN 1 |
| 67 #elif defined(__FreeBSD__) | 179 #elif defined(__FreeBSD__) |
| 68 #define PPAPI_OS_FREEBSD 1 | 180 #define PPAPI_OS_FREEBSD 1 |
| 69 #elif defined(__OpenBSD__) | 181 #elif defined(__OpenBSD__) |
| 70 #define PPAPI_OS_OPENBSD 1 | 182 #define PPAPI_OS_OPENBSD 1 |
| 71 #elif defined(__sun) | 183 #elif defined(__sun) |
| 72 #define PPAPI_OS_SOLARIS 1 | 184 #define PPAPI_OS_SOLARIS 1 |
| 73 #else | 185 #else |
| 74 #error Please add support for your platform in ppapi/c/pp_macros.h. | 186 #error Please add support for your platform in ppapi/tests/test_utils.h |
| 75 #endif | 187 #endif |
| 76 | 188 |
| 77 /* These are used to determine POSIX-like implementations vs Windows. */ | 189 /* These are used to determine POSIX-like implementations vs Windows. */ |
| 78 #if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \ | 190 #if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \ |
| 79 defined(__OpenBSD__) || defined(__sun) || defined(__native_client__) | 191 defined(__OpenBSD__) || defined(__sun) || defined(__native_client__) |
| 80 #define PPAPI_POSIX 1 | 192 #define PPAPI_POSIX 1 |
| 81 #endif | 193 #endif |
| 82 | 194 |
| 83 #endif // PPAPI_TESTS_TEST_UTILS_H_ | 195 #endif // PPAPI_TESTS_TEST_UTILS_H_ |
| OLD | NEW |