1 // Copyright 2013 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_var_resource.h" 6 7 #include "ppapi/c/pp_resource.h" 8 #include "ppapi/c/pp_var.h" 9 #include "ppapi/cpp/instance.h" 10 #include "ppapi/cpp/module.h" 11 #include "ppapi/tests/testing_instance.h" 12 13 REGISTER_TEST_CASE(VarResource); 14 15 bool TestVarResource::Init() { 16 core_interface_ = static_cast<const PPB_Core*>( 17 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); 18 file_system_interface_ = static_cast<const PPB_FileSystem*>( 19 pp::Module::Get()->GetBrowserInterface(PPB_FILESYSTEM_INTERFACE)); 20 var_interface_ = static_cast<const PPB_Var*>( 21 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); 22 var_resource_interface_ = static_cast<const PPB_VarResource_Dev*>( 23 pp::Module::Get()->GetBrowserInterface(PPB_VAR_RESOURCE_DEV_INTERFACE)); 24 return core_interface_ && file_system_interface_ && var_interface_ && 25 var_resource_interface_ && CheckTestingInterface(); 26 } 27 28 void TestVarResource::RunTests(const std::string& filter) { 29 RUN_TEST(BasicResource, filter); 30 RUN_TEST(InvalidAndEmpty, filter); 31 RUN_TEST(WrongType, filter); 32 } 33 34 std::string TestVarResource::TestBasicResource() { 35 uint32_t before_object = testing_interface_->GetLiveObjectsForInstance( 36 instance_->pp_instance()); 37 { 38 // Create an unopened FileSystem resource. 39 PP_Resource file_system = file_system_interface_->Create( 40 instance_->pp_instance(), PP_FILESYSTEMTYPE_LOCALTEMPORARY); 41 ASSERT_NE(0, file_system); 42 43 // Build a var to wrap the resource. 44 PP_Var var = var_resource_interface_->VarFromResource(file_system); 45 ASSERT_EQ(PP_VARTYPE_RESOURCE, var.type); 46 47 // Reading back the resource should work. This will increment the reference 48 // on the resource, so we must release it afterwards. 49 PP_Resource result = var_resource_interface_->VarToResource(var); 50 ASSERT_EQ(file_system, result); 51 core_interface_->ReleaseResource(result); 52 53 // Destroy the var, readback should now fail. 54 var_interface_->Release(var); 55 result = var_resource_interface_->VarToResource(var); 56 ASSERT_EQ(0, result); 57 58 // Release the resource. There should be no more references to it. 59 core_interface_->ReleaseResource(file_system); 60 } 61 62 // Make sure nothing leaked. This checks for both var and resource leaks. 63 ASSERT_EQ( 64 before_object, 65 testing_interface_->GetLiveObjectsForInstance(instance_->pp_instance())); 66 67 PASS(); 68 } 69 70 std::string TestVarResource::TestInvalidAndEmpty() { 71 uint32_t before_object = testing_interface_->GetLiveObjectsForInstance( 72 instance_->pp_instance()); 73 { 74 PP_Var invalid_resource; 75 invalid_resource.type = PP_VARTYPE_RESOURCE; 76 invalid_resource.value.as_id = 31415926; 77 78 // Invalid resource vars should give 0 as the return value. 79 PP_Resource result = 80 var_resource_interface_->VarToResource(invalid_resource); 81 ASSERT_EQ(0, result); 82 83 // Test writing and reading a non-existant resource. 84 PP_Resource fake_resource = 27182818; 85 PP_Var var = var_resource_interface_->VarFromResource(fake_resource); 86 if (testing_interface()->IsOutOfProcess()) { 87 // An out-of-process plugin is expected to generate null in this case. 88 ASSERT_EQ(PP_VARTYPE_NULL, var.type); 89 result = var_resource_interface_->VarToResource(var); 90 ASSERT_EQ(0, result); 91 } else { 92 // An in-process plugin is expected to generate a valid resource var 93 // (because it does not validate the resource). 94 ASSERT_EQ(PP_VARTYPE_RESOURCE, var.type); 95 result = var_resource_interface_->VarToResource(var); 96 ASSERT_EQ(fake_resource, result); 97 var_interface_->Release(var); 98 } 99 // Note: Not necessary to release the resource, since it does not exist. 100 101 // Write the resource 0; expect a valid resource var with 0. 102 var = var_resource_interface_->VarFromResource(0); 103 ASSERT_EQ(PP_VARTYPE_RESOURCE, var.type); 104 result = var_resource_interface_->VarToResource(var); 105 ASSERT_EQ(0, result); 106 var_interface_->Release(var); 107 } 108 109 // Make sure nothing leaked. This checks for both var and resource leaks. 110 ASSERT_EQ( 111 before_object, 112 testing_interface_->GetLiveObjectsForInstance(instance_->pp_instance())); 113 114 PASS(); 115 } 116 117 std::string TestVarResource::TestWrongType() { 118 PP_Resource result = 119 var_resource_interface_->VarToResource(PP_MakeUndefined()); 120 ASSERT_EQ(0, result); 121 122 result = var_resource_interface_->VarToResource(PP_MakeNull()); 123 ASSERT_EQ(0, result); 124 125 result = var_resource_interface_->VarToResource(PP_MakeBool(PP_TRUE)); 126 ASSERT_EQ(0, result); 127 128 result = var_resource_interface_->VarToResource(PP_MakeInt32(42)); 129 ASSERT_EQ(0, result); 130 131 result = var_resource_interface_->VarToResource(PP_MakeDouble(1.0)); 132 ASSERT_EQ(0, result); 133 134 PASS(); 135 } 136