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
3// found in the LICENSE file.
4
5#include "content/test/ppapi_unittest.h"
6
7#include "base/message_loop/message_loop.h"
8#include "content/renderer/pepper/gfx_conversion.h"
9#include "content/renderer/pepper/host_globals.h"
10#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
11#include "content/renderer/pepper/plugin_module.h"
12#include "ppapi/c/pp_errors.h"
13#include "ppapi/c/pp_var.h"
14#include "ppapi/c/ppp_instance.h"
15#include "ppapi/shared_impl/ppapi_globals.h"
16#include "ppapi/shared_impl/ppapi_permissions.h"
17
18namespace content {
19
20namespace {
21
22PpapiUnittest* current_unittest = NULL;
23
24const void* MockGetInterface(const char* interface_name) {
25  return current_unittest->GetMockInterface(interface_name);
26}
27
28int MockInitializeModule(PP_Module, PPB_GetInterface) {
29  return PP_OK;
30}
31
32// PPP_Instance implementation ------------------------------------------------
33
34PP_Bool Instance_DidCreate(PP_Instance pp_instance,
35                           uint32_t argc,
36                           const char* argn[],
37                           const char* argv[]) {
38  return PP_TRUE;
39}
40
41void Instance_DidDestroy(PP_Instance instance) {
42}
43
44void Instance_DidChangeView(PP_Instance pp_instance, PP_Resource view) {
45}
46
47void Instance_DidChangeFocus(PP_Instance pp_instance, PP_Bool has_focus) {
48}
49
50PP_Bool Instance_HandleDocumentLoad(PP_Instance pp_instance,
51                                    PP_Resource pp_url_loader) {
52  return PP_FALSE;
53}
54
55static PPP_Instance mock_instance_interface = {
56  &Instance_DidCreate,
57  &Instance_DidDestroy,
58  &Instance_DidChangeView,
59  &Instance_DidChangeFocus,
60  &Instance_HandleDocumentLoad
61};
62
63}  // namespace
64
65// PpapiUnittest --------------------------------------------------------------
66
67PpapiUnittest::PpapiUnittest() {
68  DCHECK(!current_unittest);
69  current_unittest = this;
70}
71
72PpapiUnittest::~PpapiUnittest() {
73  DCHECK(current_unittest == this);
74  current_unittest = NULL;
75}
76
77void PpapiUnittest::SetUp() {
78  message_loop_.reset(new base::MessageLoop());
79
80  // Initialize the mock module.
81  module_ = new PluginModule("Mock plugin", "1.0", base::FilePath(),
82                             ppapi::PpapiPermissions());
83  ppapi::PpapiGlobals::Get()->ResetMainThreadMessageLoopForTesting();
84  PepperPluginInfo::EntryPoints entry_points;
85  entry_points.get_interface = &MockGetInterface;
86  entry_points.initialize_module = &MockInitializeModule;
87  ASSERT_TRUE(module_->InitAsInternalPlugin(entry_points));
88
89  // Initialize the mock instance.
90  instance_ = PepperPluginInstanceImpl::Create(NULL, module(), NULL, GURL());
91}
92
93void PpapiUnittest::TearDown() {
94  instance_ = NULL;
95  module_ = NULL;
96  message_loop_.reset();
97  PluginModule::ResetHostGlobalsForTest();
98}
99
100const void* PpapiUnittest::GetMockInterface(const char* interface_name) const {
101  if (strcmp(interface_name, PPP_INSTANCE_INTERFACE_1_0) == 0)
102    return &mock_instance_interface;
103  return NULL;
104}
105
106void PpapiUnittest::ShutdownModule() {
107  DCHECK(instance_->HasOneRef());
108  instance_ = NULL;
109  DCHECK(module_->HasOneRef());
110  module_ = NULL;
111}
112
113void PpapiUnittest::SetViewSize(int width, int height) const {
114  instance_->view_data_.rect = PP_FromGfxRect(gfx::Rect(0, 0, width, height));
115  instance_->view_data_.clip_rect = instance_->view_data_.rect;
116}
117
118}  // namespace content
119