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