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.
5 #include "ppapi/tests/test_var_resource.h"
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"
13 REGISTER_TEST_CASE(VarResource
);
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();
28 void TestVarResource::RunTests(const std::string
& filter
) {
29 RUN_TEST(BasicResource
, filter
);
30 RUN_TEST(InvalidAndEmpty
, filter
);
31 RUN_TEST(WrongType
, filter
);
34 std::string
TestVarResource::TestBasicResource() {
35 uint32_t before_object
= testing_interface_
->GetLiveObjectsForInstance(
36 instance_
->pp_instance());
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
);
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
);
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
);
53 // Destroy the var, readback should now fail.
54 var_interface_
->Release(var
);
55 result
= var_resource_interface_
->VarToResource(var
);
58 // Release the resource. There should be no more references to it.
59 core_interface_
->ReleaseResource(file_system
);
62 // Make sure nothing leaked. This checks for both var and resource leaks.
65 testing_interface_
->GetLiveObjectsForInstance(instance_
->pp_instance()));
70 std::string
TestVarResource::TestInvalidAndEmpty() {
71 uint32_t before_object
= testing_interface_
->GetLiveObjectsForInstance(
72 instance_
->pp_instance());
74 PP_Var invalid_resource
;
75 invalid_resource
.type
= PP_VARTYPE_RESOURCE
;
76 invalid_resource
.value
.as_id
= 31415926;
78 // Invalid resource vars should give 0 as the return value.
80 var_resource_interface_
->VarToResource(invalid_resource
);
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
);
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
);
99 // Note: Not necessary to release the resource, since it does not exist.
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
);
109 // Make sure nothing leaked. This checks for both var and resource leaks.
112 testing_interface_
->GetLiveObjectsForInstance(instance_
->pp_instance()));
117 std::string
TestVarResource::TestWrongType() {
119 var_resource_interface_
->VarToResource(PP_MakeUndefined());
120 ASSERT_EQ(0, result
);
122 result
= var_resource_interface_
->VarToResource(PP_MakeNull());
123 ASSERT_EQ(0, result
);
125 result
= var_resource_interface_
->VarToResource(PP_MakeBool(PP_TRUE
));
126 ASSERT_EQ(0, result
);
128 result
= var_resource_interface_
->VarToResource(PP_MakeInt32(42));
129 ASSERT_EQ(0, result
);
131 result
= var_resource_interface_
->VarToResource(PP_MakeDouble(1.0));
132 ASSERT_EQ(0, result
);