OLD | NEW |
(Empty) | |
| 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 "base/file_path.h" |
| 6 #include "base/utf_string_conversions.h" |
| 7 #include "content/public/test/render_view_test.h" |
| 8 #include "content/common/view_messages.h" |
| 9 #include "content/public/common/file_chooser_params.h" |
| 10 #include "content/renderer/pepper/pepper_file_chooser_host.h" |
| 11 #include "content/renderer/pepper/pepper_instance_state_accessor.h" |
| 12 #include "content/renderer/render_view_impl.h" |
| 13 #include "content/test/test_content_client.h" |
| 14 #include "ppapi/c/pp_errors.h" |
| 15 #include "ppapi/host/host_message_context.h" |
| 16 #include "ppapi/host/ppapi_host.h" |
| 17 #include "ppapi/proxy/ppapi_messages.h" |
| 18 #include "ppapi/proxy/resource_message_params.h" |
| 19 #include "ppapi/proxy/resource_message_test_sink.h" |
| 20 #include "ppapi/shared_impl/ppapi_permissions.h" |
| 21 #include "ppapi/shared_impl/ppb_file_ref_shared.h" |
| 22 #include "ppapi/shared_impl/resource_tracker.h" |
| 23 #include "ppapi/shared_impl/test_globals.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "ui/base/dialogs/selected_file_info.h" |
| 26 |
| 27 namespace content { |
| 28 |
| 29 namespace { |
| 30 |
| 31 class PepperFileChooserHostTest : public RenderViewTest { |
| 32 public: |
| 33 PepperFileChooserHostTest() : pp_instance_(123456) {} |
| 34 |
| 35 virtual void SetUp() { |
| 36 SetContentClient(&client_); |
| 37 RenderViewTest::SetUp(); |
| 38 |
| 39 globals_.GetResourceTracker()->DidCreateInstance(pp_instance_); |
| 40 } |
| 41 virtual void TearDown() { |
| 42 globals_.GetResourceTracker()->DidDeleteInstance(pp_instance_); |
| 43 |
| 44 RenderViewTest::TearDown(); |
| 45 SetContentClient(NULL); |
| 46 } |
| 47 |
| 48 PP_Instance pp_instance() const { return pp_instance_; } |
| 49 |
| 50 private: |
| 51 PP_Instance pp_instance_; |
| 52 |
| 53 ppapi::TestGlobals globals_; |
| 54 TestContentClient client_; |
| 55 }; |
| 56 |
| 57 // For testing to convert our hardcoded file paths to 8-bit. |
| 58 std::string FilePathToUTF8(const FilePath::StringType& path) { |
| 59 #if defined(OS_WIN) |
| 60 return UTF16ToUTF8(path); |
| 61 #else |
| 62 return path.value(); |
| 63 #endif |
| 64 } |
| 65 |
| 66 class MockInstanceState : public PepperInstanceStateAccessor { |
| 67 public: |
| 68 MockInstangeState() : has_user_gesture_(true) {} |
| 69 virtual ~MockInstangeState() {} |
| 70 |
| 71 void set_has_user_gesture(bool has) { has_user_gesture_ = has; } |
| 72 |
| 73 // PepperInstanceStateAccessor. |
| 74 virtual bool IsValidInstance(PP_Instance instance) OVERRIDE { |
| 75 return true; |
| 76 } |
| 77 virtual bool HasUserGesture(PP_Instance instance) OVERRIDE { |
| 78 return has_user_gesture_; |
| 79 } |
| 80 |
| 81 private: |
| 82 bool has_user_gesture_; |
| 83 }; |
| 84 |
| 85 } // namespace |
| 86 |
| 87 TEST_F(PepperFileChooserHostTest, Show) { |
| 88 PP_Resource pp_resource = 123; |
| 89 |
| 90 MockInstangeState state; |
| 91 ppapi::proxy::ResourceMessageTestSink sink; |
| 92 ppapi::host::PpapiHost host(&sink, NULL, ppapi::PpapiPermissions()); |
| 93 RenderViewImpl* view_impl = static_cast<RenderViewImpl*>(view_); |
| 94 PepperFileChooserHost chooser(&host, pp_instance(), pp_resource, view_impl, |
| 95 &state); |
| 96 |
| 97 std::vector<std::string> accept; |
| 98 accept.push_back("text/plain"); |
| 99 PpapiHostMsg_FileChooser_Show show_msg(false, false, std::string(), accept); |
| 100 |
| 101 ppapi::proxy::ResourceMessageCallParams call_params(pp_resource, 0); |
| 102 ppapi::host::HostMessageContext context(call_params); |
| 103 int32 result = chooser.OnResourceMessageReceived(show_msg, &context); |
| 104 EXPECT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 105 |
| 106 // The render view should have sent a chooser request to the browser |
| 107 // (caught by the render thread's test message sink). |
| 108 const IPC::Message* msg = render_thread_->sink().GetUniqueMessageMatching( |
| 109 ViewHostMsg_RunFileChooser::ID); |
| 110 ASSERT_TRUE(msg); |
| 111 ViewHostMsg_RunFileChooser::Schema::Param call_msg_param; |
| 112 ASSERT_TRUE(ViewHostMsg_RunFileChooser::Read(msg, &call_msg_param)); |
| 113 const FileChooserParams& chooser_params = call_msg_param.a; |
| 114 |
| 115 // Basic validation of request. |
| 116 EXPECT_EQ(FileChooserParams::Open, chooser_params.mode); |
| 117 ASSERT_EQ(1u, chooser_params.accept_types.size()); |
| 118 EXPECT_EQ(accept[0], UTF16ToUTF8(chooser_params.accept_types[0])); |
| 119 |
| 120 // Send a chooser reply to the render view. Note our reply path has to have a |
| 121 // path separator so we include both a Unix and a Windows one. |
| 122 ui::SelectedFileInfo selected_info; |
| 123 selected_info.display_name = FILE_PATH_LITERAL("Hello, world"); |
| 124 selected_info.path = FilePath(FILE_PATH_LITERAL("myp\\ath/foo")); |
| 125 std::vector<ui::SelectedFileInfo> selected_info_vector; |
| 126 selected_info_vector.push_back(selected_info); |
| 127 ViewMsg_RunFileChooserResponse response(view_impl->routing_id(), |
| 128 selected_info_vector); |
| 129 EXPECT_TRUE(view_impl->OnMessageReceived(response)); |
| 130 |
| 131 // This should have sent the Pepper reply to our test sink. |
| 132 ppapi::proxy::ResourceMessageReplyParams reply_params; |
| 133 IPC::Message reply_msg; |
| 134 ASSERT_TRUE(sink.GetFirstResourceReplyMatching( |
| 135 PpapiPluginMsg_FileChooser_ShowReply::ID, &reply_params, &reply_msg)); |
| 136 |
| 137 // Basic validation of reply. |
| 138 EXPECT_EQ(call_params.sequence(), reply_params.sequence()); |
| 139 EXPECT_EQ(PP_OK, reply_params.result()); |
| 140 PpapiPluginMsg_FileChooser_ShowReply::Schema::Param reply_msg_param; |
| 141 ASSERT_TRUE(PpapiPluginMsg_FileChooser_ShowReply::Read(&reply_msg, |
| 142 &reply_msg_param)); |
| 143 const std::vector<ppapi::PPB_FileRef_CreateInfo>& chooser_results = |
| 144 reply_msg_param.a; |
| 145 ASSERT_EQ(1u, chooser_results.size()); |
| 146 // Note path is empty because this is an external filesystem. |
| 147 EXPECT_EQ(std::string(), chooser_results[0].path); |
| 148 EXPECT_EQ(FilePathToUTF8(selected_info.display_name), |
| 149 chooser_results[0].name); |
| 150 } |
| 151 |
| 152 TEST_F(PepperFileChooserHostTest, NoUserGesture) { |
| 153 PP_Resource pp_resource = 123; |
| 154 |
| 155 MockInstangeState state; |
| 156 ppapi::proxy::ResourceMessageTestSink sink; |
| 157 ppapi::host::PpapiHost host(&sink, NULL, ppapi::PpapiPermissions()); |
| 158 RenderViewImpl* view_impl = static_cast<RenderViewImpl*>(view_); |
| 159 PepperFileChooserHost chooser(&host, pp_instance(), pp_resource, view_impl, |
| 160 &state); |
| 161 |
| 162 // Say there's no user gesture. |
| 163 state.set_has_user_gesture(false); |
| 164 |
| 165 std::vector<std::string> accept; |
| 166 accept.push_back("text/plain"); |
| 167 PpapiHostMsg_FileChooser_Show show_msg(false, false, std::string(), accept); |
| 168 |
| 169 ppapi::proxy::ResourceMessageCallParams call_params(pp_resource, 0); |
| 170 ppapi::host::HostMessageContext context(call_params); |
| 171 int32 result = chooser.OnResourceMessageReceived(show_msg, &context); |
| 172 EXPECT_EQ(PP_ERROR_NO_USER_GESTURE, result); |
| 173 } |
| 174 |
| 175 } // namespace content |
OLD | NEW |