1 // Copyright (c) 2010 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.
5 #include "content/test/plugin/plugin_test.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/string_util.h"
9 #include "content/test/plugin/npapi_constants.h"
11 namespace NPAPIClient
{
13 PluginTest::PluginTest(NPP id
, NPNetscapeFuncs
*host_functions
) {
16 host_functions_
= host_functions
;
17 test_completed_
= false;
20 PluginTest::~PluginTest() {}
22 bool PluginTest::IsWindowless() const { return false; }
24 NPError
PluginTest::New(uint16 mode
, int16 argc
, const char* argn
[],
25 const char* argv
[], NPSavedData
* saved
) {
26 test_name_
= this->GetArgValue("name", argc
, argn
, argv
);
27 const char* id
= this->GetArgValue("id", argc
, argn
, argv
);
28 if (id
) // NULL for NP_FULL
30 return NPERR_NO_ERROR
;
33 NPError
PluginTest::Destroy() {
34 return NPERR_NO_ERROR
;
37 NPError
PluginTest::SetWindow(NPWindow
* pNPWindow
) {
38 return NPERR_NO_ERROR
;
41 // It's a shame I have to implement URLEncode. But, using webkit's
42 // or using chrome's means a ball of string of dlls and dependencies that
43 // is very very long. After spending far too much time on it,
44 // I'll just encode it myself. Too bad Microsoft doesn't implement
45 // this in a reusable way either. Both webkit and chrome will
46 // end up using libicu, which is a string of dependencies we don't
49 inline unsigned char toHex(const unsigned char x
) {
50 return x
> 9 ? (x
+ 'A' - 10) : (x
+ '0');
53 std::string
URLEncode(const std::string
&sIn
) {
56 const size_t length
= sIn
.length();
57 for (size_t idx
= 0; idx
< length
;) {
58 const char ch
= sIn
.at(idx
);
61 } else if (isspace(ch
) && ((ch
!= '\n') && (ch
!= '\r'))) {
65 sOut
.append(1, toHex(ch
>>4));
66 sOut
.append(1, toHex(ch
%16));
73 void PluginTest::SignalTestCompleted() {
74 NPObject
*window_obj
= NULL
;
75 host_functions_
->getvalue(id_
, NPNVWindowNPObject
, &window_obj
);
79 test_completed_
= true;
80 // To signal test completion, we expect a couple of
81 // javascript functions to be defined in the webpage
82 // which hosts this plugin:
83 // onSuccess(test_name, test_id)
84 // onFailure(test_name, test_id, error_message)
85 std::string
script("javascript:");
87 script
.append("onSuccess(\"");
88 script
.append(test_name_
);
89 script
.append("\",\"");
90 script
.append(test_id_
);
91 script
.append("\");");
93 script
.append("onFailure(\"");
94 script
.append(test_name_
);
95 script
.append("\",\"");
96 script
.append(test_id_
);
97 script
.append("\",\"");
98 script
.append(test_status_
);
99 script
.append("\");");
102 NPString script_string
;
103 script_string
.UTF8Characters
= script
.c_str();
104 script_string
.UTF8Length
= static_cast<unsigned int>(script
.length());
106 NPVariant result_var
;
107 host_functions_
->evaluate(id_
, window_obj
, &script_string
, &result_var
);
110 const char *PluginTest::GetArgValue(const char *name
, const int16 argc
,
111 const char *argn
[], const char *argv
[]) {
112 for (int idx
= 0; idx
< argc
; idx
++)
113 if (base::strcasecmp(argn
[idx
], name
) == 0)
118 void PluginTest::SetError(const std::string
&msg
) {
119 test_status_
.append(msg
);
122 void PluginTest::ExpectStringLowerCaseEqual(const std::string
&val1
,
123 const std::string
&val2
) {
124 if (!base::LowerCaseEqualsASCII(val1
, val2
.c_str())) {
126 err
= "Expected Equal for '";
128 err
.append("' and '");
135 void PluginTest::ExpectAsciiStringNotEqual(const char *val1
, const char *val2
) {
138 err
= "Expected Not Equal for '";
140 err
.append("' and '");
147 void PluginTest::ExpectIntegerEqual(int val1
, int val2
) {
150 err
= "Expected Equal for '";
151 err
.append(base::IntToString(val1
));
152 err
.append("' and '");
153 err
.append(base::IntToString(val2
));
159 NPError
PluginTest::NewStream(NPMIMEType type
, NPStream
* stream
,
160 NPBool seekable
, uint16
* stype
) {
161 // There is no default action here.
162 return NPERR_NO_ERROR
;
165 int32
PluginTest::WriteReady(NPStream
*stream
) {
166 // Take data in small chunks
170 int32
PluginTest::Write(NPStream
*stream
, int32 offset
, int32 len
,
172 // Pretend that we took all the data.
176 NPError
PluginTest::DestroyStream(NPStream
*stream
, NPError reason
) {
177 // There is no default action.
178 return NPERR_NO_ERROR
;
181 void PluginTest::StreamAsFile(NPStream
* stream
, const char* fname
) {
182 // There is no default action.
185 void PluginTest::URLNotify(const char* url
, NPReason reason
, void* data
) {
186 // There is no default action
189 int16
PluginTest::HandleEvent(void* event
) {
190 // There is no default action
194 void PluginTest::URLRedirectNotify(const char* url
, int32_t status
,
196 // There is no default action
199 } // namespace NPAPIClient