test_utils.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef PPAPI_TESTS_TEST_UTILS_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PPAPI_TESTS_TEST_UTILS_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ppapi/c/dev/ppb_testing_dev.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ppapi/c/pp_instance.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ppapi/c/pp_stdint.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ppapi/cpp/completion_callback.h" 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/cpp/message_loop.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ppapi/utility/completion_callback_factory.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Timeout to wait for some action to complete. 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const int kActionTimeoutMs; 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const PPB_Testing_Dev* GetTestingInterface(); 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string ReportError(const char* method, int32_t error); 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PlatformSleep(int duration_ms); 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool GetLocalHostPort(PP_Instance instance, std::string* host, uint16_t* port); 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NestedEvent allows you to run a nested MessageLoop and wait for a particular 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// event to complete. For example, you can use it to wait for a callback on a 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PPP interface, which will "Signal" the event and make the loop quit. 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "Wait()" will return immediately if it has already been signalled. Otherwise, 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// it will run a nested message loop (using PPB_Testing.RunMessageLoop) and will 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// return only after it has been signalled. 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Example: 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// std::string TestFullscreen::TestNormalToFullscreen() { 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// pp::Fullscreen screen_mode(instance); 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// screen_mode.SetFullscreen(true); 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SimulateUserGesture(); 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// // Let DidChangeView run in a nested message loop. 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// nested_event_.Wait(); 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Pass(); 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// } 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// void TestFullscreen::DidChangeView(const pp::View& view) { 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// nested_event_.Signal(); 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// } 44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// 45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// All methods except Signal and PostSignal must be invoked on the main thread. 46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// It's OK to signal from a background thread, so you can (for example) Signal() 47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// from the Audio thread. 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NestedEvent { 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) explicit NestedEvent(PP_Instance instance) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : instance_(instance), waiting_(false), signalled_(false) { 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Run a nested message loop and wait until Signal() is called. If Signal() 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // has already been called, return immediately without running a nested loop. 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Wait(); 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Signal the NestedEvent. If Wait() has been called, quit the message loop. 57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // This can be called from any thread. 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Signal(); 59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Signal the NestedEvent in |wait_ms| milliseconds. This can be called from 60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // any thread. 61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void PostSignal(int32_t wait_ms); 62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Reset the NestedEvent so it can be used again. 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Reset(); 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void SignalOnMainThread(); 67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) static void SignalThunk(void* async_event, int32_t result); 68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PP_Instance instance_; 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool waiting_; 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool signalled_; 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Disable copy and assign. 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NestedEvent(const NestedEvent&); 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NestedEvent& operator=(const NestedEvent&); 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum CallbackType { PP_REQUIRED, PP_OPTIONAL, PP_BLOCKING }; 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TestCompletionCallback { 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class Delegate { 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~Delegate() {} 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void OnCallback(void* user_data, int32_t result) = 0; 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) explicit TestCompletionCallback(PP_Instance instance); 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(dmichael): Remove this constructor. 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TestCompletionCallback(PP_Instance instance, bool force_async); 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TestCompletionCallback(PP_Instance instance, CallbackType callback_type); 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Sets a Delegate instance. OnCallback() of this instance will be invoked 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // when the completion callback is invoked. 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The delegate will be reset when Reset() or GetCallback() is called. 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void SetDelegate(Delegate* delegate) { delegate_ = delegate; } 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Wait for a result, given the return from the call which took this callback 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // as a parameter. If |result| is PP_OK_COMPLETIONPENDING, WaitForResult will 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // block until its callback has been invoked (in some cases, this will already 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // have happened, and WaitForCallback can return immediately). 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // For any other values, WaitForResult will simply set its internal "result_" 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // field. To retrieve the final result of the operation (i.e., the result 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the callback has run, if necessary), call result(). You can call result() 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // as many times as necessary until a new pp::CompletionCallback is retrieved. 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // In some cases, you may want to check that the callback was invoked in the 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // expected way (i.e., if the callback was "Required", then it should be 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // invoked asynchronously). Within the body of a test (where returning a non- 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // empty string indicates test failure), you can use the 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // CHECK_CALLBACK_BEHAVIOR(callback) macro. From within a helper function, 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // you can use failed() and errors(). 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Example usage within a test: 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // callback.WaitForResult(foo.DoSomething(callback)); 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // CHECK_CALLBACK_BEHAVIOR(callback); 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ASSERT_EQ(PP_OK, callback.result()); 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Example usage within a helper function: 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // void HelperFunction(std::string* error_message) { 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // callback.WaitForResult(foo.DoSomething(callback)); 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // if (callback.failed()) 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // error_message->assign(callback.errors()); 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // } 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void WaitForResult(int32_t result); 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Used when you expect to receive either synchronous completion with PP_OK 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // or a PP_ERROR_ABORTED asynchronously. 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Example usage: 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // int32_t result = 0; 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // { 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // pp::URLLoader temp(instance_); 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // result = temp.Open(request, callback); 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // } 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // callback.WaitForAbortResult(result); 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // CHECK_CALLBACK_BEHAVIOR(callback); 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void WaitForAbortResult(int32_t result); 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Retrieve a pp::CompletionCallback for use in testing. This Reset()s the 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TestCompletionCallback. 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pp::CompletionCallback GetCallback(); 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool failed() { return !errors_.empty(); } 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& errors() { return errors_; } 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int32_t result() const { return result_; } 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Reset so that this callback can be used again. 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Reset(); 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) CallbackType callback_type() { return callback_type_; } 150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void set_target_loop(const pp::MessageLoop& loop) { target_loop_ = loop; } 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void Handler(void* user_data, int32_t result); 152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 153c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) protected: 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void RunMessageLoop(); 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void QuitMessageLoop(); 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Used to check that WaitForResult is only called once for each usage of the 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // callback. 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool wait_for_result_called_; 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Indicates whether we have already been invoked. 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool have_result_; 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The last result received (or PP_OK_COMPLETIONCALLBACK if none). 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int32_t result_; 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CallbackType callback_type_; 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool post_quit_task_; 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string errors_; 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PP_Instance instance_; 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Delegate* delegate_; 1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) pp::MessageLoop target_loop_; 1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}; 1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 172c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace internal { 173c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)template <typename OutputT, typename CallbackT> 175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class TestCompletionCallbackWithOutputBase { 1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public: 177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) explicit TestCompletionCallbackWithOutputBase(PP_Instance instance) 178c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) : callback_(instance) { 1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) TestCompletionCallbackWithOutputBase(PP_Instance instance, bool force_async) 182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) : callback_(instance, force_async) { 1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 185c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) TestCompletionCallbackWithOutputBase(PP_Instance instance, 186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) CallbackType callback_type) 187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) : callback_(instance, callback_type) { 1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 190c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) CallbackT GetCallback(); 191c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) OutputT output() { 192c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return CallbackT::TraitsType::StorageToPluginArg( 193c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) output_storage_); 1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Delegate functions to TestCompletionCallback 197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void SetDelegate(TestCompletionCallback::Delegate* delegate) { 198c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) callback_.SetDelegate(delegate); 199c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 200c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void WaitForResult(int32_t result) { callback_.WaitForResult(result); } 201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void WaitForAbortResult(int32_t result) { 202c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) callback_.WaitForAbortResult(result); 203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 204c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) bool failed() { return callback_.failed(); } 205c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const std::string& errors() { return callback_.errors(); } 206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int32_t result() const { return callback_.result(); } 207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void Reset() { return callback_.Reset(); } 2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) private: 210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) TestCompletionCallback callback_; 211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) typename CallbackT::OutputStorageType output_storage_; 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)template <typename OutputT, typename CallbackT> 215c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)CallbackT 216c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TestCompletionCallbackWithOutputBase<OutputT, CallbackT>::GetCallback() { 217c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) callback_.Reset(); 218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (callback_.callback_type() == PP_BLOCKING) { 219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) CallbackT cc(&output_storage_); 2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return cc; 2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) callback_.set_target_loop(pp::MessageLoop::GetCurrent()); 224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) CallbackT cc(&TestCompletionCallback::Handler, this, &output_storage_); 225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (callback_.callback_type() == PP_OPTIONAL) 2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) cc.set_flags(PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); 2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return cc; 2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} // namespace internal 231c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)template <typename OutputT> 233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class TestCompletionCallbackWithOutput 234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) : public internal::TestCompletionCallbackWithOutputBase< 235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) OutputT, pp::CompletionCallbackWithOutput<OutputT> > { 236c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public: 237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) explicit TestCompletionCallbackWithOutput(PP_Instance instance) 238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) : BaseType(instance) { 239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 240c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 241c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) TestCompletionCallbackWithOutput(PP_Instance instance, bool force_async) 242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) : BaseType(instance, force_async) { 243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 244c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 245c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) TestCompletionCallbackWithOutput(PP_Instance instance, 246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) CallbackType callback_type) 247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) : BaseType(instance, callback_type) { 248c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) private: 251c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) typedef internal::TestCompletionCallbackWithOutputBase< 252c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) OutputT, pp::CompletionCallbackWithOutput<OutputT> > BaseType; 253c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}; 254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 255c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)template <typename OutputT> 256c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class TestExtCompletionCallbackWithOutput 257c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) : public internal::TestCompletionCallbackWithOutputBase< 258c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) OutputT, pp::ext::ExtCompletionCallbackWithOutput<OutputT> > { 259c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public: 260c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) explicit TestExtCompletionCallbackWithOutput(PP_Instance instance) 261c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) : BaseType(instance) { 262c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 263c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 264c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) TestExtCompletionCallbackWithOutput(PP_Instance instance, bool force_async) 265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) : BaseType(instance, force_async) { 266c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 267c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 268c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) TestExtCompletionCallbackWithOutput(PP_Instance instance, 269c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) CallbackType callback_type) 270c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) : BaseType(instance, callback_type) { 271c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 272c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 273c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) private: 274c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) typedef internal::TestCompletionCallbackWithOutputBase< 275c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) OutputT, pp::ext::ExtCompletionCallbackWithOutput<OutputT> > BaseType; 276c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}; 2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Verifies that the callback didn't record any errors. If the callback is run 2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in an unexpected way (e.g., if it's invoked asynchronously when the call 2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// should have blocked), this returns an appropriate error string. 2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_CALLBACK_BEHAVIOR(callback) \ 2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)do { \ 2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if ((callback).failed()) \ 284c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return MakeFailureMessage(__FILE__, __LINE__, \ 285c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) (callback).errors().c_str()); \ 2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} while (false) 2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * A set of macros to use for platform detection. These were largely copied 2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * from chromium's build_config.h. 2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(__APPLE__) 2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PPAPI_OS_MACOSX 1 2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(ANDROID) 2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PPAPI_OS_ANDROID 1 2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(__native_client__) 2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PPAPI_OS_NACL 1 2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(__linux__) 2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PPAPI_OS_LINUX 1 3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(_WIN32) 3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PPAPI_OS_WIN 1 3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(__FreeBSD__) 3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PPAPI_OS_FREEBSD 1 3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(__OpenBSD__) 3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PPAPI_OS_OPENBSD 1 3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(__sun) 3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PPAPI_OS_SOLARIS 1 3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else 3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#error Please add support for your platform in ppapi/tests/test_utils.h 3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* These are used to determine POSIX-like implementations vs Windows. */ 3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \ 3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) defined(__OpenBSD__) || defined(__sun) || defined(__native_client__) 3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PPAPI_POSIX 1 3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // PPAPI_TESTS_TEST_UTILS_H_ 319