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 "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" 15 16 namespace content { 17 18 namespace { 19 20 // Mock implementation of |PepperPrintSettingsManager| for test purposes. 21 class MockPepperPrintSettingsManager 22 : public PepperPrintSettingsManager { 23 public: 24 MockPepperPrintSettingsManager(const PP_PrintSettings_Dev& settings); 25 virtual ~MockPepperPrintSettingsManager() {} 26 27 // PepperPrintSettingsManager implementation. 28 virtual void GetDefaultPrintSettings( 29 PepperPrintSettingsManager::Callback callback) OVERRIDE; 30 private: 31 PP_PrintSettings_Dev settings_; 32 33 DISALLOW_COPY_AND_ASSIGN(MockPepperPrintSettingsManager); 34 }; 35 36 MockPepperPrintSettingsManager::MockPepperPrintSettingsManager( 37 const PP_PrintSettings_Dev& settings) 38 : settings_(settings) { 39 } 40 41 void MockPepperPrintSettingsManager::GetDefaultPrintSettings( 42 PepperPrintSettingsManager::Callback callback) { 43 callback.Run(PepperPrintSettingsManager::Result(settings_, PP_OK)); 44 } 45 46 class PepperPrintingHostTest 47 : public testing::Test, 48 public BrowserPpapiHostTest { 49 public: 50 PepperPrintingHostTest() { 51 } 52 53 virtual ~PepperPrintingHostTest() { 54 } 55 56 DISALLOW_COPY_AND_ASSIGN(PepperPrintingHostTest); 57 }; 58 59 bool PP_SizeEqual(const PP_Size& lhs, const PP_Size& rhs) { 60 return lhs.width == rhs.width && lhs.height == rhs.height; 61 } 62 63 bool PP_RectEqual(const PP_Rect& lhs, const PP_Rect& rhs) { 64 return lhs.point.x == rhs.point.x && 65 lhs.point.y == rhs.point.y && 66 PP_SizeEqual(lhs.size, rhs.size); 67 } 68 69 } // namespace 70 71 TEST_F(PepperPrintingHostTest, GetDefaultPrintSettings) { 72 PP_Instance pp_instance = 12345; 73 PP_Resource pp_resource = 67890; 74 PP_PrintSettings_Dev expected_settings = { 75 { { 0, 0 }, { 500, 515 } }, 76 { { 25, 35 }, { 300, 720 } }, 77 { 600, 700 }, 78 200, 79 PP_PRINTORIENTATION_NORMAL, 80 PP_PRINTSCALINGOPTION_NONE, 81 PP_FALSE, 82 PP_PRINTOUTPUTFORMAT_PDF 83 }; 84 85 // Construct the resource host. 86 scoped_ptr<PepperPrintSettingsManager> manager( 87 new MockPepperPrintSettingsManager(expected_settings)); 88 PepperPrintingHost printing(GetBrowserPpapiHost()->GetPpapiHost(), 89 pp_instance, pp_resource, manager.Pass()); 90 91 // Simulate a message being received. 92 ppapi::proxy::ResourceMessageCallParams call_params(pp_resource, 1); 93 ppapi::host::HostMessageContext context(call_params); 94 int32 result = printing.OnResourceMessageReceived( 95 PpapiHostMsg_Printing_GetDefaultPrintSettings(), &context); 96 EXPECT_EQ(PP_OK_COMPLETIONPENDING, result); 97 98 // This should have sent the Pepper reply to our test sink. 99 ppapi::proxy::ResourceMessageReplyParams reply_params; 100 IPC::Message reply_msg; 101 ASSERT_TRUE(sink().GetFirstResourceReplyMatching( 102 PpapiPluginMsg_Printing_GetDefaultPrintSettingsReply::ID, &reply_params, 103 &reply_msg)); 104 105 // Validation of reply. 106 EXPECT_EQ(call_params.sequence(), reply_params.sequence()); 107 EXPECT_EQ(PP_OK, reply_params.result()); 108 PpapiPluginMsg_Printing_GetDefaultPrintSettingsReply::Schema::Param 109 reply_msg_param; 110 ASSERT_TRUE(PpapiPluginMsg_Printing_GetDefaultPrintSettingsReply::Read( 111 &reply_msg, &reply_msg_param)); 112 PP_PrintSettings_Dev actual_settings = reply_msg_param.a; 113 114 EXPECT_TRUE(PP_RectEqual(expected_settings.printable_area, 115 actual_settings.printable_area)); 116 EXPECT_TRUE(PP_RectEqual(expected_settings.content_area, 117 actual_settings.content_area)); 118 EXPECT_TRUE(PP_SizeEqual(expected_settings.paper_size, 119 actual_settings.paper_size)); 120 EXPECT_EQ(expected_settings.dpi, actual_settings.dpi); 121 EXPECT_EQ(expected_settings.orientation, actual_settings.orientation); 122 EXPECT_EQ(expected_settings.print_scaling_option, 123 actual_settings.print_scaling_option); 124 EXPECT_EQ(expected_settings.grayscale, actual_settings.grayscale); 125 EXPECT_EQ(expected_settings.format, actual_settings.format); 126 } 127 128 } // namespace content 129