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