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