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