[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 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 | |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 8 | #include "ppapi/c/dev/ppb_file_chooser_dev.h" |
| 9 | #include "ppapi/c/pp_errors.h" |
[email protected] | c6420f08 | 2013-09-18 22:42:41 | [diff] [blame] | 10 | #include "ppapi/c/ppb_file_ref.h" |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 11 | #include "ppapi/proxy/file_chooser_resource.h" |
[email protected] | b73c659 | 2013-03-30 17:08:13 | [diff] [blame] | 12 | #include "ppapi/proxy/locking_resource_releaser.h" |
[email protected] | 511c58e | 2013-12-12 12:25:33 | [diff] [blame] | 13 | #include "ppapi/proxy/plugin_message_filter.h" |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 14 | #include "ppapi/proxy/ppapi_messages.h" |
| 15 | #include "ppapi/proxy/ppapi_proxy_test.h" |
[email protected] | 712835ff | 2013-02-26 04:54:22 | [diff] [blame] | 16 | #include "ppapi/shared_impl/proxy_lock.h" |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 17 | #include "ppapi/shared_impl/scoped_pp_var.h" |
| 18 | #include "ppapi/shared_impl/var.h" |
[email protected] | 712835ff | 2013-02-26 04:54:22 | [diff] [blame] | 19 | #include "ppapi/thunk/thunk.h" |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 20 | |
| 21 | namespace ppapi { |
| 22 | namespace proxy { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | typedef PluginProxyTest FileChooserResourceTest; |
| 27 | |
| 28 | void* 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 | |
| 40 | void 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. |
| 46 | bool 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. |
| 71 | TEST_F(FileChooserResourceTest, Show) { |
| 72 | const PPB_FileChooser_Dev_0_6* chooser_iface = |
| 73 | thunk::GetPPB_FileChooser_Dev_0_6_Thunk(); |
[email protected] | b73c659 | 2013-03-30 17:08:13 | [diff] [blame] | 74 | LockingResourceReleaser res( |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 75 | 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] | b73c659 | 2013-03-30 17:08:13 | [diff] [blame] | 84 | res.get(), output, PP_MakeCompletionCallback(&DoNothingCallback, NULL)); |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 85 | 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, ¶ms, &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] | c6420f08 | 2013-09-18 22:42:41 | [diff] [blame] | 98 | // 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] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 106 | create_info_array.push_back(create_info); |
[email protected] | 511c58e | 2013-12-12 12:25:33 | [diff] [blame] | 107 | PluginMessageFilter::DispatchResourceReplyForTest( |
| 108 | reply_params, PpapiPluginMsg_FileChooser_ShowReply(create_info_array)); |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 109 | |
| 110 | // Should have populated our vector. |
| 111 | ASSERT_EQ(1u, dest.size()); |
[email protected] | b73c659 | 2013-03-30 17:08:13 | [diff] [blame] | 112 | LockingResourceReleaser dest_deletor(dest[0]); // Ensure it's cleaned up. |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 113 | |
| 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] | 712835ff | 2013-02-26 04:54:22 | [diff] [blame] | 118 | 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] | c6420f08 | 2013-09-18 22:42:41 | [diff] [blame] | 122 | EXPECT_VAR_IS_STRING("bar", name_var); |
[email protected] | 712835ff | 2013-02-26 04:54:22 | [diff] [blame] | 123 | } |
[email protected] | 712835ff | 2013-02-26 04:54:22 | [diff] [blame] | 124 | 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] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | TEST_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 |