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