Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / pepper_file_chooser_host_unittest.cc
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/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"
27
28 namespace content {
29
30 namespace {
31
32 class PepperFileChooserHostTest : public RenderViewTest {
33  public:
34   PepperFileChooserHostTest() : pp_instance_(123456) {}
35
36   void SetUp() override {
37     SetContentClient(&client_);
38     RenderViewTest::SetUp();
39     ppapi::ProxyLock::DisableLockingOnThreadForTest();
40
41     globals_.GetResourceTracker()->DidCreateInstance(pp_instance_);
42   }
43   void TearDown() override {
44     globals_.GetResourceTracker()->DidDeleteInstance(pp_instance_);
45
46     RenderViewTest::TearDown();
47   }
48
49   PP_Instance pp_instance() const { return pp_instance_; }
50
51  private:
52   PP_Instance pp_instance_;
53
54   ppapi::TestGlobals globals_;
55   TestContentClient client_;
56 };
57
58 // For testing to convert our hardcoded file paths to 8-bit.
59 std::string FilePathToUTF8(const base::FilePath::StringType& path) {
60 #if defined(OS_WIN)
61   return base::UTF16ToUTF8(path);
62 #else
63   return path;
64 #endif
65 }
66
67 }  // namespace
68
69 TEST_F(PepperFileChooserHostTest, Show) {
70   PP_Resource pp_resource = 123;
71
72   MockRendererPpapiHost host(view_, pp_instance());
73   PepperFileChooserHost chooser(&host, pp_instance(), pp_resource);
74
75   // Say there's a user gesture.
76   host.set_has_user_gesture(true);
77
78   std::vector<std::string> accept;
79   accept.push_back("text/plain");
80   PpapiHostMsg_FileChooser_Show show_msg(false, false, std::string(), accept);
81
82   ppapi::proxy::ResourceMessageCallParams call_params(pp_resource, 0);
83   ppapi::host::HostMessageContext context(call_params);
84   int32 result = chooser.OnResourceMessageReceived(show_msg, &context);
85   EXPECT_EQ(PP_OK_COMPLETIONPENDING, result);
86
87   // The render view should have sent a chooser request to the browser
88   // (caught by the render thread's test message sink).
89   const IPC::Message* msg = render_thread_->sink().GetUniqueMessageMatching(
90       ViewHostMsg_RunFileChooser::ID);
91   ASSERT_TRUE(msg);
92   ViewHostMsg_RunFileChooser::Schema::Param call_msg_param;
93   ASSERT_TRUE(ViewHostMsg_RunFileChooser::Read(msg, &call_msg_param));
94   const FileChooserParams& chooser_params = call_msg_param.a;
95
96   // Basic validation of request.
97   EXPECT_EQ(FileChooserParams::Open, chooser_params.mode);
98   ASSERT_EQ(1u, chooser_params.accept_types.size());
99   EXPECT_EQ(accept[0], base::UTF16ToUTF8(chooser_params.accept_types[0]));
100
101   // Send a chooser reply to the render view. Note our reply path has to have a
102   // path separator so we include both a Unix and a Windows one.
103   content::FileChooserFileInfo selected_info;
104   selected_info.display_name = FILE_PATH_LITERAL("Hello, world");
105   selected_info.file_path = base::FilePath(FILE_PATH_LITERAL("myp\\ath/foo"));
106   std::vector<content::FileChooserFileInfo> selected_info_vector;
107   selected_info_vector.push_back(selected_info);
108   RenderViewImpl* view_impl = static_cast<RenderViewImpl*>(view_);
109   ViewMsg_RunFileChooserResponse response(view_impl->routing_id(),
110                                           selected_info_vector);
111   EXPECT_TRUE(view_impl->OnMessageReceived(response));
112
113   // This should have sent the Pepper reply to our test sink.
114   ppapi::proxy::ResourceMessageReplyParams reply_params;
115   IPC::Message reply_msg;
116   ASSERT_TRUE(host.sink().GetFirstResourceReplyMatching(
117       PpapiPluginMsg_FileChooser_ShowReply::ID, &reply_params, &reply_msg));
118
119   // Basic validation of reply.
120   EXPECT_EQ(call_params.sequence(), reply_params.sequence());
121   EXPECT_EQ(PP_OK, reply_params.result());
122   PpapiPluginMsg_FileChooser_ShowReply::Schema::Param reply_msg_param;
123   ASSERT_TRUE(
124       PpapiPluginMsg_FileChooser_ShowReply::Read(&reply_msg, &reply_msg_param));
125   const std::vector<ppapi::FileRefCreateInfo>& chooser_results =
126       reply_msg_param.a;
127   ASSERT_EQ(1u, chooser_results.size());
128   EXPECT_EQ(FilePathToUTF8(selected_info.display_name),
129             chooser_results[0].display_name);
130 }
131
132 TEST_F(PepperFileChooserHostTest, NoUserGesture) {
133   PP_Resource pp_resource = 123;
134
135   MockRendererPpapiHost host(view_, pp_instance());
136   PepperFileChooserHost chooser(&host, pp_instance(), pp_resource);
137
138   // Say there's no user gesture.
139   host.set_has_user_gesture(false);
140
141   std::vector<std::string> accept;
142   accept.push_back("text/plain");
143   PpapiHostMsg_FileChooser_Show show_msg(false, false, std::string(), accept);
144
145   ppapi::proxy::ResourceMessageCallParams call_params(pp_resource, 0);
146   ppapi::host::HostMessageContext context(call_params);
147   int32 result = chooser.OnResourceMessageReceived(show_msg, &context);
148   EXPECT_EQ(PP_ERROR_NO_USER_GESTURE, result);
149 }
150
151 }  // namespace content