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