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.
5 #include "content/browser/renderer_host/pepper/browser_ppapi_host_test.h"
6 #include "content/browser/renderer_host/pepper/pepper_print_settings_manager.h"
7 #include "content/browser/renderer_host/pepper/pepper_printing_host.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/host/host_message_context.h"
10 #include "ppapi/host/ppapi_host.h"
11 #include "ppapi/proxy/ppapi_messages.h"
12 #include "ppapi/proxy/resource_message_params.h"
13 #include "ppapi/proxy/resource_message_test_sink.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 // Mock implementation of |PepperPrintSettingsManager| for test purposes.
21 class MockPepperPrintSettingsManager
: public PepperPrintSettingsManager
{
23 MockPepperPrintSettingsManager(const PP_PrintSettings_Dev
& settings
);
24 virtual ~MockPepperPrintSettingsManager() {}
26 // PepperPrintSettingsManager implementation.
27 virtual void GetDefaultPrintSettings(
28 PepperPrintSettingsManager::Callback callback
) OVERRIDE
;
31 PP_PrintSettings_Dev settings_
;
33 DISALLOW_COPY_AND_ASSIGN(MockPepperPrintSettingsManager
);
36 MockPepperPrintSettingsManager::MockPepperPrintSettingsManager(
37 const PP_PrintSettings_Dev
& settings
)
38 : settings_(settings
) {}
40 void MockPepperPrintSettingsManager::GetDefaultPrintSettings(
41 PepperPrintSettingsManager::Callback callback
) {
42 callback
.Run(PepperPrintSettingsManager::Result(settings_
, PP_OK
));
45 class PepperPrintingHostTest
: public testing::Test
,
46 public BrowserPpapiHostTest
{
48 PepperPrintingHostTest() {}
50 virtual ~PepperPrintingHostTest() {}
52 DISALLOW_COPY_AND_ASSIGN(PepperPrintingHostTest
);
55 bool PP_SizeEqual(const PP_Size
& lhs
, const PP_Size
& rhs
) {
56 return lhs
.width
== rhs
.width
&& lhs
.height
== rhs
.height
;
59 bool PP_RectEqual(const PP_Rect
& lhs
, const PP_Rect
& rhs
) {
60 return lhs
.point
.x
== rhs
.point
.x
&& lhs
.point
.y
== rhs
.point
.y
&&
61 PP_SizeEqual(lhs
.size
, rhs
.size
);
66 TEST_F(PepperPrintingHostTest
, GetDefaultPrintSettings
) {
67 PP_Instance pp_instance
= 12345;
68 PP_Resource pp_resource
= 67890;
69 PP_PrintSettings_Dev expected_settings
= {{{0, 0}, {500, 515}},
70 {{25, 35}, {300, 720}},
73 PP_PRINTORIENTATION_NORMAL
,
74 PP_PRINTSCALINGOPTION_NONE
,
76 PP_PRINTOUTPUTFORMAT_PDF
};
78 // Construct the resource host.
79 scoped_ptr
<PepperPrintSettingsManager
> manager(
80 new MockPepperPrintSettingsManager(expected_settings
));
81 PepperPrintingHost
printing(GetBrowserPpapiHost()->GetPpapiHost(),
86 // Simulate a message being received.
87 ppapi::proxy::ResourceMessageCallParams
call_params(pp_resource
, 1);
88 ppapi::host::HostMessageContext
context(call_params
);
89 int32 result
= printing
.OnResourceMessageReceived(
90 PpapiHostMsg_Printing_GetDefaultPrintSettings(), &context
);
91 EXPECT_EQ(PP_OK_COMPLETIONPENDING
, result
);
93 // This should have sent the Pepper reply to our test sink.
94 ppapi::proxy::ResourceMessageReplyParams reply_params
;
95 IPC::Message reply_msg
;
96 ASSERT_TRUE(sink().GetFirstResourceReplyMatching(
97 PpapiPluginMsg_Printing_GetDefaultPrintSettingsReply::ID
,
101 // Validation of reply.
102 EXPECT_EQ(call_params
.sequence(), reply_params
.sequence());
103 EXPECT_EQ(PP_OK
, reply_params
.result());
104 PpapiPluginMsg_Printing_GetDefaultPrintSettingsReply::Schema::Param
106 ASSERT_TRUE(PpapiPluginMsg_Printing_GetDefaultPrintSettingsReply::Read(
107 &reply_msg
, &reply_msg_param
));
108 PP_PrintSettings_Dev actual_settings
= reply_msg_param
.a
;
110 EXPECT_TRUE(PP_RectEqual(expected_settings
.printable_area
,
111 actual_settings
.printable_area
));
112 EXPECT_TRUE(PP_RectEqual(expected_settings
.content_area
,
113 actual_settings
.content_area
));
115 PP_SizeEqual(expected_settings
.paper_size
, actual_settings
.paper_size
));
116 EXPECT_EQ(expected_settings
.dpi
, actual_settings
.dpi
);
117 EXPECT_EQ(expected_settings
.orientation
, actual_settings
.orientation
);
118 EXPECT_EQ(expected_settings
.print_scaling_option
,
119 actual_settings
.print_scaling_option
);
120 EXPECT_EQ(expected_settings
.grayscale
, actual_settings
.grayscale
);
121 EXPECT_EQ(expected_settings
.format
, actual_settings
.format
);
124 } // namespace content