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 "base/files/file_path.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "content/common/view_messages.h"
8 #include "content/public/common/file_chooser_file_info.h"
9 #include "content/public/common/file_chooser_params.h"
10 #include "content/public/test/render_view_test.h"
11 #include "content/renderer/pepper/mock_renderer_ppapi_host.h"
12 #include "content/renderer/pepper/pepper_file_chooser_host.h"
13 #include "content/renderer/render_view_impl.h"
14 #include "content/test/test_content_client.h"
15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/host/host_message_context.h"
17 #include "ppapi/host/ppapi_host.h"
18 #include "ppapi/proxy/ppapi_messages.h"
19 #include "ppapi/proxy/resource_message_params.h"
20 #include "ppapi/proxy/resource_message_test_sink.h"
21 #include "ppapi/shared_impl/file_ref_create_info.h"
22 #include "ppapi/shared_impl/ppapi_permissions.h"
23 #include "ppapi/shared_impl/proxy_lock.h"
24 #include "ppapi/shared_impl/resource_tracker.h"
25 #include "ppapi/shared_impl/test_globals.h"
26 #include "testing/gtest/include/gtest/gtest.h"
32 class PepperFileChooserHostTest
: public RenderViewTest
{
34 PepperFileChooserHostTest() : pp_instance_(123456) {}
36 void SetUp() override
{
37 SetContentClient(&client_
);
38 RenderViewTest::SetUp();
40 globals_
.GetResourceTracker()->DidCreateInstance(pp_instance_
);
42 void TearDown() override
{
43 globals_
.GetResourceTracker()->DidDeleteInstance(pp_instance_
);
45 RenderViewTest::TearDown();
48 PP_Instance
pp_instance() const { return pp_instance_
; }
51 PP_Instance pp_instance_
;
53 // Disables locking for the duration of the test.
54 ppapi::ProxyLock::LockingDisablerForTest disable_locking_
;
55 ppapi::TestGlobals globals_
;
56 TestContentClient client_
;
59 // For testing to convert our hardcoded file paths to 8-bit.
60 std::string
FilePathToUTF8(const base::FilePath::StringType
& path
) {
62 return base::UTF16ToUTF8(path
);
70 TEST_F(PepperFileChooserHostTest
, Show
) {
71 PP_Resource pp_resource
= 123;
73 MockRendererPpapiHost
host(view_
, pp_instance());
74 PepperFileChooserHost
chooser(&host
, pp_instance(), pp_resource
);
76 // Say there's a user gesture.
77 host
.set_has_user_gesture(true);
79 std::vector
<std::string
> accept
;
80 accept
.push_back("text/plain");
81 PpapiHostMsg_FileChooser_Show
show_msg(false, false, std::string(), accept
);
83 ppapi::proxy::ResourceMessageCallParams
call_params(pp_resource
, 0);
84 ppapi::host::HostMessageContext
context(call_params
);
85 int32 result
= chooser
.OnResourceMessageReceived(show_msg
, &context
);
86 EXPECT_EQ(PP_OK_COMPLETIONPENDING
, result
);
88 // The render view should have sent a chooser request to the browser
89 // (caught by the render thread's test message sink).
90 const IPC::Message
* msg
= render_thread_
->sink().GetUniqueMessageMatching(
91 ViewHostMsg_RunFileChooser::ID
);
93 ViewHostMsg_RunFileChooser::Schema::Param call_msg_param
;
94 ASSERT_TRUE(ViewHostMsg_RunFileChooser::Read(msg
, &call_msg_param
));
95 const FileChooserParams
& chooser_params
= base::get
<0>(call_msg_param
);
97 // Basic validation of request.
98 EXPECT_EQ(FileChooserParams::Open
, chooser_params
.mode
);
99 ASSERT_EQ(1u, chooser_params
.accept_types
.size());
100 EXPECT_EQ(accept
[0], base::UTF16ToUTF8(chooser_params
.accept_types
[0]));
102 // Send a chooser reply to the render view. Note our reply path has to have a
103 // path separator so we include both a Unix and a Windows one.
104 content::FileChooserFileInfo selected_info
;
105 selected_info
.display_name
= FILE_PATH_LITERAL("Hello, world");
106 selected_info
.file_path
= base::FilePath(FILE_PATH_LITERAL("myp\\ath/foo"));
107 std::vector
<content::FileChooserFileInfo
> selected_info_vector
;
108 selected_info_vector
.push_back(selected_info
);
109 RenderViewImpl
* view_impl
= static_cast<RenderViewImpl
*>(view_
);
110 ViewMsg_RunFileChooserResponse
response(view_impl
->routing_id(),
111 selected_info_vector
);
112 EXPECT_TRUE(view_impl
->OnMessageReceived(response
));
114 // This should have sent the Pepper reply to our test sink.
115 ppapi::proxy::ResourceMessageReplyParams reply_params
;
116 IPC::Message reply_msg
;
117 ASSERT_TRUE(host
.sink().GetFirstResourceReplyMatching(
118 PpapiPluginMsg_FileChooser_ShowReply::ID
, &reply_params
, &reply_msg
));
120 // Basic validation of reply.
121 EXPECT_EQ(call_params
.sequence(), reply_params
.sequence());
122 EXPECT_EQ(PP_OK
, reply_params
.result());
123 PpapiPluginMsg_FileChooser_ShowReply::Schema::Param reply_msg_param
;
125 PpapiPluginMsg_FileChooser_ShowReply::Read(&reply_msg
, &reply_msg_param
));
126 const std::vector
<ppapi::FileRefCreateInfo
>& chooser_results
=
127 base::get
<0>(reply_msg_param
);
128 ASSERT_EQ(1u, chooser_results
.size());
129 EXPECT_EQ(FilePathToUTF8(selected_info
.display_name
),
130 chooser_results
[0].display_name
);
133 TEST_F(PepperFileChooserHostTest
, NoUserGesture
) {
134 PP_Resource pp_resource
= 123;
136 MockRendererPpapiHost
host(view_
, pp_instance());
137 PepperFileChooserHost
chooser(&host
, pp_instance(), pp_resource
);
139 // Say there's no user gesture.
140 host
.set_has_user_gesture(false);
142 std::vector
<std::string
> accept
;
143 accept
.push_back("text/plain");
144 PpapiHostMsg_FileChooser_Show
show_msg(false, false, std::string(), accept
);
146 ppapi::proxy::ResourceMessageCallParams
call_params(pp_resource
, 0);
147 ppapi::host::HostMessageContext
context(call_params
);
148 int32 result
= chooser
.OnResourceMessageReceived(show_msg
, &context
);
149 EXPECT_EQ(PP_ERROR_NO_USER_GESTURE
, result
);
152 } // namespace content