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/message_loop.h" |
| 6 #include "ppapi/c/dev/ppb_file_chooser_dev.h" |
| 7 #include "ppapi/c/pp_errors.h" |
| 8 #include "ppapi/proxy/file_chooser_resource.h" |
| 9 #include "ppapi/proxy/ppapi_messages.h" |
| 10 #include "ppapi/proxy/ppapi_proxy_test.h" |
| 11 #include "ppapi/thunk/thunk.h" |
| 12 #include "ppapi/shared_impl/scoped_pp_resource.h" |
| 13 #include "ppapi/shared_impl/var.h" |
| 14 |
| 15 namespace ppapi { |
| 16 namespace proxy { |
| 17 |
| 18 namespace { |
| 19 |
| 20 typedef PluginProxyTest FileChooserResourceTest; |
| 21 |
| 22 void* GetFileRefDataBuffer(void* user_data, |
| 23 uint32_t element_count, |
| 24 uint32_t element_size) { |
| 25 EXPECT_TRUE(element_size == sizeof(PP_Resource)); |
| 26 std::vector<PP_Resource>* output = |
| 27 static_cast<std::vector<PP_Resource>*>(user_data); |
| 28 output->resize(element_count); |
| 29 if (element_count > 0) |
| 30 return &(*output)[0]; |
| 31 return NULL; |
| 32 } |
| 33 |
| 34 void DoNothingCallback(void* user_data, int32_t result) { |
| 35 } |
| 36 |
| 37 // Calls PopulateAcceptTypes and verifies that the resulting array contains |
| 38 // the given values. The values may be NULL if there aren't expected to be |
| 39 // that many results. |
| 40 bool CheckParseAcceptType(const std::string& input, |
| 41 const char* expected1, |
| 42 const char* expected2) { |
| 43 std::vector<std::string> output; |
| 44 FileChooserResource::PopulateAcceptTypes(input, &output); |
| 45 |
| 46 const size_t kCount = 2; |
| 47 const char* expected[kCount] = { expected1, expected2 }; |
| 48 |
| 49 for (size_t i = 0; i < kCount; i++) { |
| 50 if (!expected[i]) |
| 51 return i == output.size(); |
| 52 if (output.size() <= i) |
| 53 return false; |
| 54 if (output[i] != expected[i]) |
| 55 return false; |
| 56 } |
| 57 |
| 58 return output.size() == kCount; |
| 59 } |
| 60 |
| 61 } // namespace |
| 62 |
| 63 // Does a full test of Show() and reply functionality in the plugin side using |
| 64 // the public C interfaces. |
| 65 TEST_F(FileChooserResourceTest, Show) { |
| 66 const PPB_FileChooser_Dev_0_6* chooser_iface = |
| 67 thunk::GetPPB_FileChooser_Dev_0_6_Thunk(); |
| 68 ScopedPPResource res(ScopedPPResource::PassRef(), |
| 69 chooser_iface->Create(pp_instance(), PP_FILECHOOSERMODE_OPEN, |
| 70 PP_MakeUndefined())); |
| 71 |
| 72 std::vector<PP_Resource> dest; |
| 73 PP_ArrayOutput output; |
| 74 output.GetDataBuffer = &GetFileRefDataBuffer; |
| 75 output.user_data = &dest; |
| 76 |
| 77 int32_t result = chooser_iface->Show( |
| 78 res, output, PP_MakeCompletionCallback(&DoNothingCallback, NULL)); |
| 79 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 80 |
| 81 // Should have sent a "show" message. |
| 82 ResourceMessageCallParams params; |
| 83 IPC::Message msg; |
| 84 ASSERT_TRUE(sink().GetFirstResourceCallMatching( |
| 85 PpapiHostMsg_FileChooser_Show::ID, ¶ms, &msg)); |
| 86 |
| 87 ResourceMessageReplyParams reply_params(params.pp_resource(), |
| 88 params.sequence()); |
| 89 reply_params.set_result(PP_OK); |
| 90 |
| 91 // Synthesize a response with one file ref in it. Note that it must have a |
| 92 // host resource value set or deserialization will fail. Since there isn't |
| 93 // actually a host, this can be whatever we want. |
| 94 std::vector<PPB_FileRef_CreateInfo> create_info_array; |
| 95 PPB_FileRef_CreateInfo create_info; |
| 96 create_info.resource.SetHostResource(pp_instance(), 123); |
| 97 create_info.path = "foo/bar"; |
| 98 create_info.name = "baz"; |
| 99 create_info_array.push_back(create_info); |
| 100 ASSERT_TRUE(plugin_dispatcher()->OnMessageReceived( |
| 101 PpapiPluginMsg_ResourceReply(reply_params, |
| 102 PpapiPluginMsg_FileChooser_ShowReply(create_info_array)))); |
| 103 |
| 104 // Should have populated our vector. |
| 105 ASSERT_EQ(1u, dest.size()); |
| 106 ScopedPPResource dest_deletor(dest[0]); // Ensure it's cleaned up. |
| 107 |
| 108 const PPB_FileRef_1_0* file_ref_iface = thunk::GetPPB_FileRef_1_0_Thunk(); |
| 109 EXPECT_EQ(PP_FILESYSTEMTYPE_EXTERNAL, |
| 110 file_ref_iface->GetFileSystemType(dest[0])); |
| 111 |
| 112 ScopedPPVar name_var(file_ref_iface->GetName(dest[0])); |
| 113 EXPECT_VAR_IS_STRING(create_info.name, name_var); |
| 114 |
| 115 // Path should be undefined since it's external filesystem. |
| 116 ScopedPPVar path_var(file_ref_iface->GetPath(dest[0])); |
| 117 EXPECT_EQ(PP_VARTYPE_UNDEFINED, path_var.get().type); |
| 118 } |
| 119 |
| 120 TEST_F(FileChooserResourceTest, PopulateAcceptTypes) { |
| 121 EXPECT_TRUE(CheckParseAcceptType(std::string(), NULL, NULL)); |
| 122 EXPECT_TRUE(CheckParseAcceptType("/", NULL, NULL)); |
| 123 EXPECT_TRUE(CheckParseAcceptType(".", NULL, NULL)); |
| 124 EXPECT_TRUE(CheckParseAcceptType(",, , ", NULL, NULL)); |
| 125 |
| 126 EXPECT_TRUE(CheckParseAcceptType("app/txt", "app/txt", NULL)); |
| 127 EXPECT_TRUE(CheckParseAcceptType("app/txt,app/pdf", "app/txt", "app/pdf")); |
| 128 EXPECT_TRUE(CheckParseAcceptType(" app/txt , app/pdf ", |
| 129 "app/txt", "app/pdf")); |
| 130 |
| 131 // No dot or slash ones should be skipped. |
| 132 EXPECT_TRUE(CheckParseAcceptType("foo", NULL, NULL)); |
| 133 EXPECT_TRUE(CheckParseAcceptType("foo,.txt", ".txt", NULL)); |
| 134 } |
| 135 |
| 136 } // namespace proxy |
| 137 } // namespace ppapi |
OLD | NEW |