1 // Copyright (c) 2011 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 NATIVE_CLIENT_TESTS_PPAPI_TEST_LIB_TESTABLE_CALLBACK_H 6 #define NATIVE_CLIENT_TESTS_PPAPI_TEST_LIB_TESTABLE_CALLBACK_H 7 8 #include "ppapi/c/pp_completion_callback.h" 9 #include "ppapi/c/pp_instance.h" 10 #include "ppapi/c/pp_stdint.h" 11 12 // NOTE: if you use you TestableCallback you will need to enable 13 // testing interfaces in PPAPIBrowserTester(), e.g. 14 // "--enable-pepper-testing", c.f. 15 // tests/ppapi_browser/ppb_graphics2d/nacl.scons 16 // 17 // Example Usage: 18 // 19 // void TestProgressSimple() { 20 // TestableCallback callback(pp_instance(), true); 21 // ... 22 // rv = PPBURLLoader()->Open(loader, request, callback.GetCallback()); 23 // EXPECT(rv == PP_OK_COMPLETIONPENDING); 24 25 // rv = callback.WaitForResult(); 26 // EXPECT(rv == PP_OK); 27 // ... 28 // } 29 30 class TestableCallback { 31 public: 32 TestableCallback(PP_Instance instance, bool force_async); 33 34 // Get the callback to be passed to an asynchronous PPAPI call 35 PP_CompletionCallback GetCallback(); 36 37 // Waits for the callback to be called and returns the 38 // result. Returns immediately if the callback was previously called 39 // and the result wasn't returned (i.e. each result value received 40 // by the callback is returned by WaitForResult() once and only 41 // once). 42 int32_t WaitForResult(); 43 44 bool HasRun() const { return run_count_ != 0; } 45 46 // Make instance runnable again. 47 void Reset() { 48 run_count_ = 0; 49 have_result_ = false; 50 } 51 52 int32_t Result() const { return result_; } 53 54 private: 55 static void Handler(void* user_data, int32_t result); 56 57 bool have_result_; // is a result available? 58 int32_t result_; // value of the result 59 bool force_async_; // force callback to be always called 60 bool post_quit_task_; // has cleanup beem performed 61 unsigned run_count_; // number of times the callback has been called 62 PP_Instance instance_; 63 }; 64 65 #endif // NATIVE_CLIENT_TESTS_PPAPI_TEST_LIB_TESTABLE_CALLBACK_H 66