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 "ppapi/tests/test_file_system.h" 6 7 #include <string.h> 8 9 #include "ppapi/c/pp_errors.h" 10 #include "ppapi/cpp/file_io.h" 11 #include "ppapi/cpp/file_system.h" 12 #include "ppapi/cpp/resource.h" 13 #include "ppapi/tests/test_utils.h" 14 #include "ppapi/tests/testing_instance.h" 15 16 REGISTER_TEST_CASE(FileSystem); 17 18 bool TestFileSystem::Init() { 19 return CheckTestingInterface() && EnsureRunningOverHTTP(); 20 } 21 22 void TestFileSystem::RunTests(const std::string& filter) { 23 RUN_CALLBACK_TEST(TestFileSystem, Open, filter); 24 RUN_CALLBACK_TEST(TestFileSystem, MultipleOpens, filter); 25 RUN_CALLBACK_TEST(TestFileSystem, ResourceConversion, filter); 26 } 27 28 std::string TestFileSystem::TestOpen() { 29 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); 30 31 // Open. 32 pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); 33 callback.WaitForResult(file_system.Open(1024, callback.GetCallback())); 34 CHECK_CALLBACK_BEHAVIOR(callback); 35 ASSERT_EQ(PP_OK, callback.result()); 36 37 // Open aborted. 38 int32_t rv = 0; 39 { 40 pp::FileSystem fs(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); 41 rv = fs.Open(1024, callback.GetCallback()); 42 } 43 callback.WaitForAbortResult(rv); 44 CHECK_CALLBACK_BEHAVIOR(callback); 45 46 PASS(); 47 } 48 49 std::string TestFileSystem::TestMultipleOpens() { 50 // Should not allow multiple opens, regardless of whether or not the first 51 // open has completed. 52 TestCompletionCallback callback_1(instance_->pp_instance(), callback_type()); 53 pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); 54 int32_t rv_1 = file_system.Open(1024, callback_1.GetCallback()); 55 56 TestCompletionCallback callback_2(instance_->pp_instance(), callback_type()); 57 callback_2.WaitForResult(file_system.Open(1024, callback_2.GetCallback())); 58 CHECK_CALLBACK_BEHAVIOR(callback_2); 59 // FileSystem should not allow multiple opens. 60 ASSERT_NE(PP_OK, callback_2.result()); 61 62 callback_1.WaitForResult(rv_1); 63 CHECK_CALLBACK_BEHAVIOR(callback_1); 64 ASSERT_EQ(PP_OK, callback_1.result()); 65 66 TestCompletionCallback callback_3(instance_->pp_instance(), callback_type()); 67 callback_3.WaitForResult(file_system.Open(1024, callback_3.GetCallback())); 68 CHECK_CALLBACK_BEHAVIOR(callback_3); 69 ASSERT_NE(PP_OK, callback_3.result()); 70 71 PASS(); 72 } 73 74 std::string TestFileSystem::TestResourceConversion() { 75 // Test conversion from file system Resource to FileSystem. 76 pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); 77 pp::Resource file_system_resource(file_system); 78 ASSERT_TRUE(pp::FileSystem::IsFileSystem(file_system_resource)); 79 pp::FileSystem file_system_resource_file_system(file_system_resource); 80 ASSERT_FALSE(file_system_resource_file_system.is_null()); 81 82 // Test conversion that a non-file system Resource does not register as a 83 // FileSystem. (We cannot test conversion, since this is an assertion on a 84 // debug build.) 85 pp::FileIO non_file_system(instance_); 86 pp::Resource non_file_system_resource(non_file_system); 87 ASSERT_FALSE(pp::FileSystem::IsFileSystem(non_file_system_resource)); 88 89 PASS(); 90 } 91