OLD | NEW |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdio.h> | 5 #include "native_client/tests/ppapi_test_lib/get_browser_interface.h" |
6 #include <stdlib.h> | 6 #include "native_client/tests/ppapi_test_lib/test_interface.h" |
7 #include <string.h> | 7 #include "native_client/src/shared/platform/nacl_check.h" |
8 | 8 #include "ppapi/c/dev/ppb_file_system_dev.h" |
9 #include "native_client/src/include/portability.h" | 9 #include "ppapi/c/ppb_core.h" |
10 #include "ppapi/c/dev/ppb_var_deprecated.h" | 10 #include "ppapi/c/ppb_url_request_info.h" |
11 #include "ppapi/c/pp_errors.h" | 11 #include "ppapi/c/pp_errors.h" |
12 #include "ppapi/c/pp_module.h" | |
13 #include "ppapi/c/pp_var.h" | |
14 #include "ppapi/c/ppb.h" | |
15 #include "ppapi/c/ppp_instance.h" | |
16 #include "ppapi/c/ppp.h" | |
17 | |
18 | |
19 // Defined in scriptable_object.c | |
20 extern "C" struct PP_Var GetScriptableObject(PP_Instance plugin_instance); | |
21 | |
22 // Global variables | |
23 extern PPB_GetInterface get_browser_interface_func; | |
24 extern PP_Module module; | |
25 | 12 |
26 namespace { | 13 namespace { |
27 | 14 |
28 PP_Bool DidCreate(PP_Instance /*instance*/, | 15 void OpenCallback(void* /*data*/, int32_t /*result*/) { |
29 uint32_t /*argc*/, | 16 } |
30 const char** /*argn*/, | 17 |
31 const char** /*argv*/) { | 18 const PP_FileSystemType_Dev kFileSystemTypes[] = { |
32 return PP_TRUE; | 19 PP_FILESYSTEMTYPE_EXTERNAL, |
33 } | 20 PP_FILESYSTEMTYPE_LOCALPERSISTENT, |
34 | 21 PP_FILESYSTEMTYPE_LOCALTEMPORARY |
35 void DidDestroy(PP_Instance /*instance*/) { | 22 }; |
36 } | 23 |
37 | 24 const size_t kNumFileSystemTypes = |
38 void DidChangeView(PP_Instance /*instance*/, | 25 sizeof(kFileSystemTypes) / sizeof(kFileSystemTypes[0]); |
39 const struct PP_Rect* /*position*/, | 26 |
40 const struct PP_Rect* /*clip*/) { | 27 void TestCreate() { |
41 } | 28 PP_Resource file_system = kInvalidResource; |
42 | 29 const struct PPB_Core* const ppb_core = PPBCore(); |
43 void DidChangeFocus(PP_Instance /*instance*/, | 30 const struct PPB_FileSystem_Dev* const ppb_file_system = PPBFileSystemDev(); |
44 PP_Bool /*has_focus*/) { | 31 /* |
45 } | 32 * Test to see if PPB_FileSystem_Dev::Create returns PP_Resource value of 0 |
46 | 33 * if the instance parameter is invalid. |
47 PP_Bool HandleInputEvent(PP_Instance /*instance*/, | 34 */ |
48 const struct PP_InputEvent* /*event*/) { | 35 file_system = ppb_file_system->Create(kInvalidInstance, |
49 return PP_TRUE; | 36 PP_FILESYSTEMTYPE_EXTERNAL); |
50 } | 37 EXPECT_ASYNC(file_system == kInvalidResource); |
51 | 38 file_system = ppb_file_system->Create(kInvalidInstance, |
52 static PP_Bool HandleDocumentLoad(PP_Instance /*instance*/, | 39 PP_FILESYSTEMTYPE_LOCALPERSISTENT); |
53 PP_Resource /*url_loader*/) { | 40 EXPECT_ASYNC(file_system == kInvalidResource); |
54 return PP_TRUE; | 41 file_system = ppb_file_system->Create(kInvalidInstance, |
55 } | 42 PP_FILESYSTEMTYPE_LOCALTEMPORARY); |
56 | 43 EXPECT_ASYNC(file_system == kInvalidResource); |
57 struct PP_Var GetInstanceObject(PP_Instance instance) { | 44 |
58 return GetScriptableObject(instance); | 45 /* Test for failure when an invalid file system type is requested. */ |
| 46 file_system = ppb_file_system->Create(pp_instance(), |
| 47 PP_FILESYSTEMTYPE_INVALID); |
| 48 EXPECT_ASYNC(file_system == kInvalidResource); |
| 49 |
| 50 /* |
| 51 * Test to see if PPB_FileSystem_Dev::Create returns a valid PP_Resource |
| 52 * value when given a valid PP_Instance value parameter. Test for all |
| 53 * three file system types PPB_FileSystem_Dev supports. |
| 54 */ |
| 55 for (size_t j = 0; j < kNumFileSystemTypes; ++j) { |
| 56 file_system = |
| 57 ppb_file_system->Create(pp_instance(), kFileSystemTypes[j]); |
| 58 EXPECT_ASYNC(file_system != kInvalidResource); |
| 59 ppb_core->ReleaseResource(file_system); |
| 60 } |
| 61 TEST_PASSED_ASYNC; |
| 62 } |
| 63 |
| 64 void TestIsFileSystem() { |
| 65 const struct PPB_Core* const ppb_core = PPBCore(); |
| 66 const struct PPB_FileSystem_Dev* const ppb_file_system = PPBFileSystemDev(); |
| 67 const struct PPB_URLRequestInfo* const ppb_url_request_info = |
| 68 PPBURLRequestInfo(); |
| 69 PP_Resource url_request_info = kInvalidResource; |
| 70 PP_Resource file_system = kInvalidResource; |
| 71 size_t j = 0; |
| 72 PP_Bool is_file_system = PP_FALSE; |
| 73 |
| 74 /* Test fail for invalid resource. */ |
| 75 EXPECT_ASYNC(ppb_file_system->IsFileSystem(kInvalidResource) != PP_TRUE); |
| 76 |
| 77 /* |
| 78 * Test pass for the different valid system types, and test fail against a |
| 79 * resource that has been released. |
| 80 */ |
| 81 for (j = 0; j < kNumFileSystemTypes; ++j) { |
| 82 file_system = ppb_file_system->Create(pp_instance(), |
| 83 kFileSystemTypes[j]); |
| 84 CHECK(file_system != kInvalidResource); |
| 85 |
| 86 is_file_system = ppb_file_system->IsFileSystem(file_system); |
| 87 ppb_core->ReleaseResource(file_system); |
| 88 |
| 89 EXPECT_ASYNC(is_file_system == PP_TRUE); |
| 90 |
| 91 is_file_system = ppb_file_system->IsFileSystem(file_system); |
| 92 EXPECT_ASYNC(is_file_system == PP_FALSE); |
| 93 } |
| 94 |
| 95 /* Test fail against a non-filesystem resource */ |
| 96 url_request_info = ppb_url_request_info->Create(pp_instance()); |
| 97 CHECK(url_request_info != kInvalidResource); |
| 98 is_file_system = ppb_file_system->IsFileSystem(url_request_info); |
| 99 ppb_core->ReleaseResource(url_request_info); |
| 100 EXPECT_ASYNC(is_file_system == PP_FALSE); |
| 101 |
| 102 TEST_PASSED_ASYNC; |
| 103 } |
| 104 |
| 105 void TestOpen() { |
| 106 size_t j = 0; |
| 107 PP_Resource file_system = 0; |
| 108 const struct PP_CompletionCallback open_callback = |
| 109 MakeTestableCompletionCallback( |
| 110 "OpenCallback", |
| 111 OpenCallback, |
| 112 NULL); // user data |
| 113 const struct PPB_Core* const ppb_core = PPBCore(); |
| 114 const struct PPB_FileSystem_Dev* const ppb_file_system = PPBFileSystemDev(); |
| 115 |
| 116 /* Test to make sure opening an invalid file system fails. */ |
| 117 int32_t pp_error = ppb_file_system->Open(kInvalidResource, |
| 118 1024, /* Dummy value */ |
| 119 open_callback); |
| 120 EXPECT_ASYNC(pp_error == PP_ERROR_BADRESOURCE); |
| 121 |
| 122 /* |
| 123 * Test to make sure external file system is not supported. |
| 124 * TODO(sanga): Once Chrome supports external file systems, change this test |
| 125 * to reflect the change. |
| 126 */ |
| 127 file_system = ppb_file_system->Create(pp_instance(), |
| 128 PP_FILESYSTEMTYPE_EXTERNAL); |
| 129 pp_error = ppb_file_system->Open(file_system, |
| 130 1024, /* Dummy value */ |
| 131 open_callback); |
| 132 ppb_core->ReleaseResource(file_system); |
| 133 EXPECT_ASYNC(pp_error != PP_OK); |
| 134 EXPECT_ASYNC(pp_error != PP_OK_COMPLETIONPENDING); |
| 135 |
| 136 /* Test local temporary and local persistant file systems */ |
| 137 for (j = 1; j < kNumFileSystemTypes; ++j) { |
| 138 #ifdef __native_client__ |
| 139 /* Test fail for blocking open */ |
| 140 /* |
| 141 * Only conduct this test with nexe. Trusted ppapi plugin does not work |
| 142 * with synchronous Open call. |
| 143 * See http://code.google.com/p/chromium/issues/detail?id=78449 |
| 144 */ |
| 145 file_system = ppb_file_system->Create(pp_instance(), |
| 146 kFileSystemTypes[j]); |
| 147 pp_error = ppb_file_system->Open(file_system, |
| 148 1024, /* Dummy value */ |
| 149 PP_BlockUntilComplete()); |
| 150 ppb_core->ReleaseResource(file_system); |
| 151 EXPECT_ASYNC(pp_error == PP_ERROR_BADARGUMENT); |
| 152 #endif |
| 153 |
| 154 /* Test success for asynchronous open */ |
| 155 file_system = ppb_file_system->Create(pp_instance(), |
| 156 kFileSystemTypes[j]); |
| 157 pp_error = ppb_file_system->Open(file_system, |
| 158 1024, /* Dummy value */ |
| 159 open_callback); |
| 160 ppb_core->ReleaseResource(file_system); |
| 161 EXPECT_ASYNC(pp_error == PP_OK_COMPLETIONPENDING); |
| 162 |
| 163 /* Test fail for multiple opens */ |
| 164 file_system = ppb_file_system->Create(pp_instance(), |
| 165 kFileSystemTypes[j]); |
| 166 pp_error = ppb_file_system->Open(file_system, |
| 167 1024, /* Dummy value */ |
| 168 open_callback); |
| 169 CHECK(pp_error == PP_OK_COMPLETIONPENDING); /* Previously tested */ |
| 170 pp_error = ppb_file_system->Open(file_system, |
| 171 1024, /* Dummy value */ |
| 172 open_callback); |
| 173 ppb_core->ReleaseResource(file_system); |
| 174 EXPECT_ASYNC(pp_error == PP_ERROR_FAILED); |
| 175 } |
| 176 TEST_PASSED_ASYNC; |
| 177 } |
| 178 |
| 179 void TestGetType() { |
| 180 const struct PPB_Core* const ppb_core = PPBCore(); |
| 181 const struct PPB_FileSystem_Dev* const ppb_file_system = PPBFileSystemDev(); |
| 182 const struct PPB_URLRequestInfo* const ppb_url_request_info = |
| 183 PPBURLRequestInfo(); |
| 184 PP_Resource url_request_info = kInvalidResource; |
| 185 PP_Resource file_system = kInvalidResource; |
| 186 size_t j = 0; |
| 187 PP_FileSystemType_Dev type = PP_FILESYSTEMTYPE_INVALID; |
| 188 |
| 189 /* Test for invalid resource. */ |
| 190 EXPECT_ASYNC(PP_FILESYSTEMTYPE_INVALID == ppb_file_system->GetType(0)); |
| 191 |
| 192 /* Test pass for the different valid system types */ |
| 193 for (j = 0; j < kNumFileSystemTypes; ++j) { |
| 194 file_system = ppb_file_system->Create(pp_instance(), |
| 195 kFileSystemTypes[j]); |
| 196 CHECK(file_system != kInvalidResource); |
| 197 |
| 198 type = ppb_file_system->GetType(file_system); |
| 199 ppb_core->ReleaseResource(file_system); |
| 200 |
| 201 EXPECT_ASYNC(type == kFileSystemTypes[j]); |
| 202 } |
| 203 |
| 204 /* Test fail against a non-filesystem resource */ |
| 205 url_request_info = ppb_url_request_info->Create(pp_instance()); |
| 206 CHECK(url_request_info != kInvalidResource); |
| 207 |
| 208 type = ppb_file_system->GetType(url_request_info); |
| 209 ppb_core->ReleaseResource(url_request_info); |
| 210 EXPECT_ASYNC(type == PP_FILESYSTEMTYPE_INVALID); |
| 211 |
| 212 TEST_PASSED_ASYNC; |
59 } | 213 } |
60 | 214 |
61 } // namespace | 215 } // namespace |
62 | 216 |
63 // Implementations of the PPP entry points expected by the browser | 217 void SetupTests() { |
64 extern "C" { | 218 RegisterTest("TestCreate", TestCreate); |
65 PP_EXPORT int32_t PPP_InitializeModule(PP_Module module_id, | 219 RegisterTest("TestIsFileSystem", TestIsFileSystem); |
66 PPB_GetInterface get_browser_interface) { | 220 RegisterTest("TestOpen", TestOpen); |
67 module = module_id; | 221 RegisterTest("TestGetType", TestGetType); |
68 get_browser_interface_func = get_browser_interface; | 222 } |
69 return PP_OK; | 223 |
70 } | 224 void SetupPluginInterfaces() { |
71 | 225 /* No PPP interface to test. */ |
72 PP_EXPORT void PPP_ShutdownModule() { | 226 } |
73 } | |
74 | |
75 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) { | |
76 if (0 == strncmp(PPP_INSTANCE_INTERFACE, interface_name, | |
77 strlen(PPP_INSTANCE_INTERFACE))) { | |
78 static struct PPP_Instance instance_interface = { | |
79 DidCreate, | |
80 DidDestroy, | |
81 DidChangeView, | |
82 DidChangeFocus, | |
83 HandleInputEvent, | |
84 HandleDocumentLoad, | |
85 GetInstanceObject | |
86 }; | |
87 return &instance_interface; | |
88 } | |
89 return NULL; | |
90 } | |
91 } // extern "C" | |
OLD | NEW |