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 <cstring>
6
7#include "base/strings/utf_string_conversions.h"
8#include "ppapi/c/dev/ppb_memory_dev.h"
9#include "ppapi/c/pp_errors.h"
10#include "ppapi/c/ppb_image_data.h"
11#include "ppapi/proxy/pdf_resource.h"
12#include "ppapi/proxy/ppapi_messages.h"
13#include "ppapi/proxy/ppapi_proxy_test.h"
14#include "ppapi/proxy/ppb_image_data_proxy.h"
15#include "ppapi/proxy/serialized_handle.h"
16#include "ppapi/shared_impl/proxy_lock.h"
17#include "ppapi/shared_impl/scoped_pp_var.h"
18#include "ppapi/shared_impl/var.h"
19#include "ppapi/thunk/thunk.h"
20
21namespace ppapi {
22namespace proxy {
23
24namespace {
25
26typedef PluginProxyTest PDFResourceTest;
27
28}  // namespace
29
30TEST_F(PDFResourceTest, GetLocalizedString) {
31  const PPB_PDF* pdf_iface = thunk::GetPPB_PDF_Thunk();
32
33  std::string expected_string = "hello";
34  PpapiPluginMsg_PDF_GetLocalizedStringReply reply_msg(expected_string);
35  ResourceSyncCallHandler handler(
36      &sink(),
37      PpapiHostMsg_PDF_GetLocalizedString::ID,
38      PP_OK,
39      reply_msg);
40  sink().AddFilter(&handler);
41
42  PP_Var var = pdf_iface->GetLocalizedString(
43      pp_instance(), PP_RESOURCESTRING_PDFGETPASSWORD);
44
45  {
46    ProxyAutoLock lock;
47    ScopedPPVar release_var(ScopedPPVar::PassRef(), var);
48    StringVar* string_var = StringVar::FromPPVar(var);
49    ASSERT_TRUE(string_var != NULL);
50    std::string actual_string = string_var->value();
51
52    ASSERT_EQ(PpapiHostMsg_PDF_GetLocalizedString::ID,
53              handler.last_handled_msg().type());
54    ASSERT_EQ(expected_string, actual_string);
55  }
56
57  // Remove the filter or it will be destroyed before the sink() is destroyed.
58  sink().RemoveFilter(&handler);
59}
60
61TEST_F(PDFResourceTest, SearchString) {
62  ProxyAutoLock lock;
63  // Instantiate a resource explicitly so we can specify the locale.
64  scoped_refptr<PDFResource> pdf_resource(
65      new PDFResource(Connection(&sink(), &sink()), pp_instance()));
66  pdf_resource->SetLocaleForTest("en-US");
67
68  base::string16 input;
69  base::string16 term;
70  base::UTF8ToUTF16("abcdefabcdef", 12, &input);
71  base::UTF8ToUTF16("bc", 2, &term);
72
73  PP_PrivateFindResult* results;
74  int count = 0;
75  pdf_resource->SearchString(
76      reinterpret_cast<const unsigned short*>(input.c_str()),
77      reinterpret_cast<const unsigned short*>(term.c_str()),
78      true,
79      &results,
80      &count);
81
82  ASSERT_EQ(2, count);
83  ASSERT_EQ(1, results[0].start_index);
84  ASSERT_EQ(2, results[0].length);
85  ASSERT_EQ(7, results[1].start_index);
86  ASSERT_EQ(2, results[1].length);
87
88  const PPB_Memory_Dev* memory_iface = thunk::GetPPB_Memory_Dev_0_1_Thunk();
89  memory_iface->MemFree(results);
90}
91
92TEST_F(PDFResourceTest, DidStartLoading) {
93  const PPB_PDF* pdf_iface = thunk::GetPPB_PDF_Thunk();
94
95  pdf_iface->DidStartLoading(pp_instance());
96
97  ResourceMessageCallParams params;
98  IPC::Message msg;
99  ASSERT_TRUE(sink().GetFirstResourceCallMatching(
100      PpapiHostMsg_PDF_DidStartLoading::ID, &params, &msg));
101}
102
103TEST_F(PDFResourceTest, DidStopLoading) {
104  const PPB_PDF* pdf_iface = thunk::GetPPB_PDF_Thunk();
105
106  pdf_iface->DidStopLoading(pp_instance());
107
108  ResourceMessageCallParams params;
109  IPC::Message msg;
110  ASSERT_TRUE(sink().GetFirstResourceCallMatching(
111      PpapiHostMsg_PDF_DidStopLoading::ID, &params, &msg));
112}
113
114TEST_F(PDFResourceTest, SetContentRestriction) {
115  const PPB_PDF* pdf_iface = thunk::GetPPB_PDF_Thunk();
116
117  int restrictions = 5;
118  pdf_iface->SetContentRestriction(pp_instance(), restrictions);
119
120  ResourceMessageCallParams params;
121  IPC::Message msg;
122  ASSERT_TRUE(sink().GetFirstResourceCallMatching(
123      PpapiHostMsg_PDF_SetContentRestriction::ID, &params, &msg));
124}
125
126TEST_F(PDFResourceTest, HasUnsupportedFeature) {
127  const PPB_PDF* pdf_iface = thunk::GetPPB_PDF_Thunk();
128
129  pdf_iface->HasUnsupportedFeature(pp_instance());
130
131  ResourceMessageCallParams params;
132  IPC::Message msg;
133  ASSERT_TRUE(sink().GetFirstResourceCallMatching(
134      PpapiHostMsg_PDF_HasUnsupportedFeature::ID, &params, &msg));
135}
136
137TEST_F(PDFResourceTest, Print) {
138  const PPB_PDF* pdf_iface = thunk::GetPPB_PDF_Thunk();
139
140  pdf_iface->Print(pp_instance());
141
142  ResourceMessageCallParams params;
143  IPC::Message msg;
144  ASSERT_TRUE(sink().GetFirstResourceCallMatching(
145      PpapiHostMsg_PDF_Print::ID, &params, &msg));
146}
147
148TEST_F(PDFResourceTest, SaveAs) {
149  const PPB_PDF* pdf_iface = thunk::GetPPB_PDF_Thunk();
150
151  pdf_iface->SaveAs(pp_instance());
152
153  ResourceMessageCallParams params;
154  IPC::Message msg;
155  ASSERT_TRUE(sink().GetFirstResourceCallMatching(
156      PpapiHostMsg_PDF_SaveAs::ID, &params, &msg));
157}
158
159TEST_F(PDFResourceTest, GetResourceImageForScale) {
160  const PPB_PDF* pdf_iface = thunk::GetPPB_PDF_Thunk();
161
162  HostResource expected_resource;
163  expected_resource.SetHostResource(pp_instance(), 5);
164  PP_ImageDataDesc expected_desc = {
165      PP_IMAGEDATAFORMAT_BGRA_PREMUL,
166      { 5, 10 },
167      20,
168  };
169  SerializedHandle serialized_handle(SerializedHandle::SHARED_MEMORY);
170  PpapiPluginMsg_PDF_GetResourceImageReply reply_msg(expected_resource,
171                                                     expected_desc);
172  ResourceSyncCallHandler handler(
173      &sink(),
174      PpapiHostMsg_PDF_GetResourceImage::ID,
175      PP_OK,
176      reply_msg);
177  handler.set_serialized_handle(&serialized_handle);
178  sink().AddFilter(&handler);
179
180  PP_Resource resource = pdf_iface->GetResourceImageForScale(pp_instance(),
181      PP_RESOURCEIMAGE_PDF_BUTTON_FTP, 1.0f);
182  {
183    ProxyAutoLock lock;
184    PluginResourceTracker* resource_tracker =
185        static_cast<PluginResourceTracker*>(
186            PluginGlobals::Get()->GetResourceTracker());
187    Resource* resource_object = resource_tracker->GetResource(resource);
188    ImageData* image_data_object = static_cast<ImageData*>(resource_object);
189    PP_ImageDataDesc actual_desc = image_data_object->desc();
190    ASSERT_EQ(expected_desc.format, actual_desc.format);
191    ASSERT_EQ(expected_desc.size.width, actual_desc.size.width);
192    ASSERT_EQ(expected_desc.size.height, actual_desc.size.height);
193    ASSERT_EQ(expected_desc.stride, actual_desc.stride);
194
195    ASSERT_EQ(resource_tracker->PluginResourceForHostResource(
196        expected_resource), resource);
197
198    resource_tracker->ReleaseResource(resource);
199  }
200
201  // Remove the filter or it will be destroyed before the sink() is destroyed.
202  sink().RemoveFilter(&handler);
203}
204
205}  // namespace proxy
206}  // namespace ppapi
207