blob: 5d471f6751e28adf5285599ad3d63c03d694f6b1 [file] [log] [blame]
[email protected]eccf80312012-07-14 15:43:421// 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
avie029c4132015-12-23 06:45:225#include <stddef.h>
6#include <stdint.h>
7
[email protected]eccf80312012-07-14 15:43:428#include "ppapi/c/dev/ppb_file_chooser_dev.h"
9#include "ppapi/c/pp_errors.h"
[email protected]c6420f082013-09-18 22:42:4110#include "ppapi/c/ppb_file_ref.h"
[email protected]eccf80312012-07-14 15:43:4211#include "ppapi/proxy/file_chooser_resource.h"
[email protected]b73c6592013-03-30 17:08:1312#include "ppapi/proxy/locking_resource_releaser.h"
[email protected]511c58e2013-12-12 12:25:3313#include "ppapi/proxy/plugin_message_filter.h"
[email protected]eccf80312012-07-14 15:43:4214#include "ppapi/proxy/ppapi_messages.h"
15#include "ppapi/proxy/ppapi_proxy_test.h"
[email protected]712835ff2013-02-26 04:54:2216#include "ppapi/shared_impl/proxy_lock.h"
[email protected]eccf80312012-07-14 15:43:4217#include "ppapi/shared_impl/scoped_pp_var.h"
18#include "ppapi/shared_impl/var.h"
[email protected]712835ff2013-02-26 04:54:2219#include "ppapi/thunk/thunk.h"
[email protected]eccf80312012-07-14 15:43:4220
21namespace ppapi {
22namespace proxy {
23
24namespace {
25
26typedef PluginProxyTest FileChooserResourceTest;
27
28void* GetFileRefDataBuffer(void* user_data,
29 uint32_t element_count,
30 uint32_t element_size) {
31 EXPECT_TRUE(element_size == sizeof(PP_Resource));
32 std::vector<PP_Resource>* output =
33 static_cast<std::vector<PP_Resource>*>(user_data);
34 output->resize(element_count);
35 if (element_count > 0)
36 return &(*output)[0];
37 return NULL;
38}
39
40void DoNothingCallback(void* user_data, int32_t result) {
41}
42
43// Calls PopulateAcceptTypes and verifies that the resulting array contains
44// the given values. The values may be NULL if there aren't expected to be
45// that many results.
46bool CheckParseAcceptType(const std::string& input,
47 const char* expected1,
48 const char* expected2) {
49 std::vector<std::string> output;
50 FileChooserResource::PopulateAcceptTypes(input, &output);
51
52 const size_t kCount = 2;
53 const char* expected[kCount] = { expected1, expected2 };
54
55 for (size_t i = 0; i < kCount; i++) {
56 if (!expected[i])
57 return i == output.size();
58 if (output.size() <= i)
59 return false;
60 if (output[i] != expected[i])
61 return false;
62 }
63
64 return output.size() == kCount;
65}
66
67} // namespace
68
69// Does a full test of Show() and reply functionality in the plugin side using
70// the public C interfaces.
71TEST_F(FileChooserResourceTest, Show) {
72 const PPB_FileChooser_Dev_0_6* chooser_iface =
73 thunk::GetPPB_FileChooser_Dev_0_6_Thunk();
[email protected]b73c6592013-03-30 17:08:1374 LockingResourceReleaser res(
[email protected]eccf80312012-07-14 15:43:4275 chooser_iface->Create(pp_instance(), PP_FILECHOOSERMODE_OPEN,
76 PP_MakeUndefined()));
77
78 std::vector<PP_Resource> dest;
79 PP_ArrayOutput output;
80 output.GetDataBuffer = &GetFileRefDataBuffer;
81 output.user_data = &dest;
82
83 int32_t result = chooser_iface->Show(
[email protected]b73c6592013-03-30 17:08:1384 res.get(), output, PP_MakeCompletionCallback(&DoNothingCallback, NULL));
[email protected]eccf80312012-07-14 15:43:4285 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
86
87 // Should have sent a "show" message.
88 ResourceMessageCallParams params;
89 IPC::Message msg;
90 ASSERT_TRUE(sink().GetFirstResourceCallMatching(
91 PpapiHostMsg_FileChooser_Show::ID, &params, &msg));
92
93 ResourceMessageReplyParams reply_params(params.pp_resource(),
94 params.sequence());
95 reply_params.set_result(PP_OK);
96
97 // Synthesize a response with one file ref in it. Note that it must have a
[email protected]c6420f082013-09-18 22:42:4198 // pending_host_resource_id set. Since there isn't actually a host, this can
99 // be whatever we want.
100 std::vector<FileRefCreateInfo> create_info_array;
101 FileRefCreateInfo create_info;
102 create_info.file_system_type = PP_FILESYSTEMTYPE_EXTERNAL;
103 create_info.display_name = "bar";
104 create_info.browser_pending_host_resource_id = 12;
105 create_info.renderer_pending_host_resource_id = 15;
[email protected]eccf80312012-07-14 15:43:42106 create_info_array.push_back(create_info);
[email protected]511c58e2013-12-12 12:25:33107 PluginMessageFilter::DispatchResourceReplyForTest(
108 reply_params, PpapiPluginMsg_FileChooser_ShowReply(create_info_array));
[email protected]eccf80312012-07-14 15:43:42109
110 // Should have populated our vector.
111 ASSERT_EQ(1u, dest.size());
[email protected]b73c6592013-03-30 17:08:13112 LockingResourceReleaser dest_deletor(dest[0]); // Ensure it's cleaned up.
[email protected]eccf80312012-07-14 15:43:42113
114 const PPB_FileRef_1_0* file_ref_iface = thunk::GetPPB_FileRef_1_0_Thunk();
115 EXPECT_EQ(PP_FILESYSTEMTYPE_EXTERNAL,
116 file_ref_iface->GetFileSystemType(dest[0]));
117
[email protected]712835ff2013-02-26 04:54:22118 PP_Var name_var(file_ref_iface->GetName(dest[0]));
119 {
120 ProxyAutoLock lock;
121 ScopedPPVar release_name_var(ScopedPPVar::PassRef(), name_var);
[email protected]c6420f082013-09-18 22:42:41122 EXPECT_VAR_IS_STRING("bar", name_var);
[email protected]712835ff2013-02-26 04:54:22123 }
[email protected]712835ff2013-02-26 04:54:22124 PP_Var path_var(file_ref_iface->GetPath(dest[0]));
125 {
126 ProxyAutoLock lock;
127 ScopedPPVar release_path_var(ScopedPPVar::PassRef(), path_var);
128 EXPECT_EQ(PP_VARTYPE_UNDEFINED, path_var.type);
129 }
[email protected]eccf80312012-07-14 15:43:42130}
131
132TEST_F(FileChooserResourceTest, PopulateAcceptTypes) {
133 EXPECT_TRUE(CheckParseAcceptType(std::string(), NULL, NULL));
134 EXPECT_TRUE(CheckParseAcceptType("/", NULL, NULL));
135 EXPECT_TRUE(CheckParseAcceptType(".", NULL, NULL));
136 EXPECT_TRUE(CheckParseAcceptType(",, , ", NULL, NULL));
137
138 EXPECT_TRUE(CheckParseAcceptType("app/txt", "app/txt", NULL));
139 EXPECT_TRUE(CheckParseAcceptType("app/txt,app/pdf", "app/txt", "app/pdf"));
140 EXPECT_TRUE(CheckParseAcceptType(" app/txt , app/pdf ",
141 "app/txt", "app/pdf"));
142
143 // No dot or slash ones should be skipped.
144 EXPECT_TRUE(CheckParseAcceptType("foo", NULL, NULL));
145 EXPECT_TRUE(CheckParseAcceptType("foo,.txt", ".txt", NULL));
146}
147
148} // namespace proxy
149} // namespace ppapi