summaryrefslogtreecommitdiffstats
path: root/ppapi/tests/test_utils.cc
blob: a062922f4303075cc25b62a32f1bf9febd4d74f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/tests/test_utils.h"

#include "ppapi/c/pp_errors.h"
#include "ppapi/cpp/module.h"

const PPB_Testing_Dev* GetTestingInterface() {
  static const PPB_Testing_Dev* g_testing_interface =
      reinterpret_cast<PPB_Testing_Dev const*>(
          pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE));
  return g_testing_interface;
}

std::string ReportError(const char* method, int32_t error) {
  char error_as_string[12];
  sprintf(error_as_string, "%d", error);
  std::string result = method + std::string(" failed with error: ") +
      error_as_string;
  if (error == PP_ERROR_NOSPACE)
    result += ". Did you run the test with --unlimited-quota-for-files?";
  return result;
}

TestCompletionCallback::TestCompletionCallback()
    : result_(PP_ERROR_WOULDBLOCK),
      post_quit_task_(false) {
}

int32_t TestCompletionCallback::WaitForResult() {
  result_ = PP_ERROR_WOULDBLOCK;  // Reset
  post_quit_task_ = true;
  GetTestingInterface()->RunMessageLoop();
  return result_;
}

TestCompletionCallback::operator pp::CompletionCallback() const {
  return pp::CompletionCallback(&TestCompletionCallback::Handler,
                                const_cast<TestCompletionCallback*>(this));
}

// static
void TestCompletionCallback::Handler(void* user_data, int32_t result) {
  TestCompletionCallback* callback =
      static_cast<TestCompletionCallback*>(user_data);
  callback->result_ = result;
  if (callback->post_quit_task_) {
    callback->post_quit_task_ = false;
    GetTestingInterface()->QuitMessageLoop();
  }
}