summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/ppapi_unittest.cc
blob: c029e89eed7e3631488c3d3e62316287aa17f910 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2012 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 "webkit/plugins/ppapi/ppapi_unittest.h"

#include "base/message_loop/message_loop.h"
#include "ppapi/c/pp_var.h"
#include "ppapi/c/ppp_instance.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/ppapi_permissions.h"
#include "webkit/plugins/ppapi/gfx_conversion.h"
#include "webkit/plugins/ppapi/host_globals.h"
#include "webkit/plugins/ppapi/mock_plugin_delegate.h"
#include "webkit/plugins/ppapi/plugin_module.h"
#include "webkit/plugins/ppapi/ppapi_interface_factory.h"
#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"

namespace webkit {
namespace ppapi {

namespace {

PpapiUnittest* current_unittest = NULL;

const void* MockGetInterface(const char* interface_name) {
  return current_unittest->GetMockInterface(interface_name);
}

int MockInitializeModule(PP_Module, PPB_GetInterface) {
  return PP_OK;
}

// PPP_Instance implementation ------------------------------------------------

PP_Bool Instance_DidCreate(PP_Instance pp_instance,
                           uint32_t argc,
                           const char* argn[],
                           const char* argv[]) {
  return PP_TRUE;
}

void Instance_DidDestroy(PP_Instance instance) {
}

void Instance_DidChangeView(PP_Instance pp_instance, PP_Resource view) {
}

void Instance_DidChangeFocus(PP_Instance pp_instance, PP_Bool has_focus) {
}

PP_Bool Instance_HandleDocumentLoad(PP_Instance pp_instance,
                                    PP_Resource pp_url_loader) {
  return PP_FALSE;
}

static PPP_Instance mock_instance_interface = {
  &Instance_DidCreate,
  &Instance_DidDestroy,
  &Instance_DidChangeView,
  &Instance_DidChangeFocus,
  &Instance_HandleDocumentLoad
};

}  // namespace

// PpapiUnittest --------------------------------------------------------------

PpapiUnittest::PpapiUnittest() {
  DCHECK(!current_unittest);
  current_unittest = this;
}

PpapiUnittest::~PpapiUnittest() {
  DCHECK(current_unittest == this);
  current_unittest = NULL;
}

void PpapiUnittest::SetUp() {
  message_loop_.reset(new base::MessageLoop());
  delegate_.reset(NewPluginDelegate());

  // Initialize the mock module.
  module_ = new PluginModule("Mock plugin", base::FilePath(), this,
                             ::ppapi::PpapiPermissions());
  ::ppapi::PpapiGlobals::Get()->ResetMainThreadMessageLoopForTesting();
  PluginModule::EntryPoints entry_points;
  entry_points.get_interface = &MockGetInterface;
  entry_points.initialize_module = &MockInitializeModule;
  ASSERT_TRUE(module_->InitAsInternalPlugin(entry_points));

  // Initialize the mock instance.
  instance_ = PluginInstance::Create(delegate_.get(), NULL, module(), NULL,
                                     GURL());
}

void PpapiUnittest::TearDown() {
  instance_ = NULL;
  module_ = NULL;
  message_loop_.reset();
  PluginModule::ResetHostGlobalsForTest();
}

MockPluginDelegate* PpapiUnittest::NewPluginDelegate() {
  return new MockPluginDelegate;
}

const void* PpapiUnittest::GetMockInterface(const char* interface_name) const {
  if (strcmp(interface_name, PPP_INSTANCE_INTERFACE_1_0) == 0)
    return &mock_instance_interface;
  return NULL;
}

void PpapiUnittest::ShutdownModule() {
  DCHECK(instance_->HasOneRef());
  instance_ = NULL;
  DCHECK(module_->HasOneRef());
  module_ = NULL;
}

void PpapiUnittest::SetViewSize(int width, int height) const {
  instance_->view_data_.rect = PP_FromGfxRect(gfx::Rect(0, 0, width, height));
  instance_->view_data_.clip_rect = instance_->view_data_.rect;
}

void PpapiUnittest::PluginModuleDead(PluginModule* /* dead_module */) {
  // Nothing needed (this is necessary to make the module compile).
}

// Tests whether custom PPAPI interface factories are called when PPAPI
// interfaces are requested.
class PpapiCustomInterfaceFactoryTest : public PpapiUnittest {
 public:
  PpapiCustomInterfaceFactoryTest() {}
  virtual ~PpapiCustomInterfaceFactoryTest() {}

  bool result() {
    return result_;
  }

  void reset_result() {
    result_ = false;
  }

  static const void* InterfaceFactory(const std::string& interface_name) {
    result_ = true;
    return NULL;
  }

 private:
  static bool result_;
};

bool PpapiCustomInterfaceFactoryTest::result_ = false;

// This test validates whether custom PPAPI interface factories are invoked in
// response to PluginModule::GetPluginInterface calls.
TEST_F(PpapiCustomInterfaceFactoryTest, BasicFactoryTest) {
  PpapiInterfaceFactoryManager::GetInstance()->RegisterFactory(
      PpapiCustomInterfaceFactoryTest::InterfaceFactory);
  (*PluginModule::GetLocalGetInterfaceFunc())("DummyInterface");
  EXPECT_TRUE(result());

  reset_result();
  PpapiInterfaceFactoryManager::GetInstance()->UnregisterFactory(
      PpapiCustomInterfaceFactoryTest::InterfaceFactory);

  (*PluginModule::GetLocalGetInterfaceFunc())("DummyInterface");
  EXPECT_FALSE(result());
}

}  // namespace ppapi
}  // namespace webkit