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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
// Copyright (c) 2010 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.
//
// Tests for NPAPI Object Wrapper classes.
#include "ceee/common/npobject_impl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gmock/include/gmock/gmock.h"
using testing::_;
using testing::Return;
namespace {
// A bare bones implementation class which really just uses the default
// implementations of NpObjectImpl/NpObjectBase so we can test those
// wrapper classes for correct default NPAPI functionality.
class BareNpObject : public NpObjectImpl<BareNpObject> {
public:
explicit BareNpObject(NPP npp) : NpObjectImpl<BareNpObject>(npp) {
}
~BareNpObject() {
}
private:
DISALLOW_COPY_AND_ASSIGN(BareNpObject);
};
// A mock class which allows us to verify that the corresponding NPObject
// functions are called when a client calls functions through the NPClass
// struct's function pointers.
class MockNpObject : public NpObjectImpl<MockNpObject> {
public:
explicit MockNpObject(NPP npp) : NpObjectImpl<MockNpObject>(npp) {
}
~MockNpObject() {
}
MOCK_METHOD0(Invalidate, void());
MOCK_METHOD1(HasMethod, bool(NPIdentifier name));
MOCK_METHOD4(Invoke, bool(NPIdentifier name, const NPVariant* args,
uint32_t argCount, NPVariant* result));
MOCK_METHOD3(InvokeDefault, bool(const NPVariant* args, uint32_t argCount,
NPVariant* result));
MOCK_METHOD1(HasProperty, bool(NPIdentifier name));
MOCK_METHOD2(GetProperty, bool(NPIdentifier name, NPVariant* result));
MOCK_METHOD2(SetProperty, bool(NPIdentifier name, const NPVariant* value));
MOCK_METHOD1(RemoveProperty, bool(NPIdentifier name));
MOCK_METHOD2(Enumeration, bool(NPIdentifier** value, uint32_t* count));
MOCK_METHOD3(Construct, bool(const NPVariant* args, uint32_t argCount,
NPVariant* result));
private:
DISALLOW_COPY_AND_ASSIGN(MockNpObject);
};
// Test the functions in the NPClass struct to ensure that the correct
// NP*FunctionPtr function defaults come out. We use
// https://developer.mozilla.org/en/NPClass as a rough guide for what default
// values should be.
TEST(NpApiWrappers, NpObjectDefaults) {
// Grab the NPClass structure so we can access the functions that have been
// hooked up by this particular implementation.
NPClass* np_class = BareNpObject::object_class();
ASSERT_TRUE(np_class != NULL);
// Create an instance of our barebones class so we can use it in the function
// calls from the NPClass struct. Also exercises allocation.
NPObject* np_object = np_class->allocate(NULL, np_class);
ASSERT_TRUE(np_object != NULL);
EXPECT_GE(np_class->structVersion, (uint32_t)3);
EXPECT_FALSE(np_class->hasMethod(np_object, NULL));
EXPECT_FALSE(np_class->invoke(np_object, NULL, NULL, 0, NULL));
EXPECT_FALSE(np_class->invokeDefault(np_object, NULL, 0, NULL));
EXPECT_FALSE(np_class->hasProperty(np_object, NULL));
EXPECT_FALSE(np_class->getProperty(np_object, NULL, NULL));
EXPECT_FALSE(np_class->setProperty(np_object, NULL, NULL));
EXPECT_FALSE(np_class->removeProperty(np_object, NULL));
EXPECT_FALSE(np_class->enumerate(np_object, NULL, NULL));
EXPECT_FALSE(np_class->construct(np_object, NULL, 0, NULL));
// Invalidation and deallocation. No return values to check here - just
// exercising the code. Deallocation should clean up the object for us.
np_class->invalidate(np_object);
np_class->deallocate(np_object);
np_object = NULL;
}
// Uses a Mock class to ensure that the correct functions are being called
// on the NPAPI Object instance when they are called through the NPClass
// structure that our wrapper sets up.
TEST(NpApiWrappers, NpObjectCalls) {
NPClass* np_class = MockNpObject::object_class();
ASSERT_TRUE(np_class != NULL);
MockNpObject np_object(NULL);
EXPECT_CALL(np_object, HasMethod(NULL))
.Times(1)
.WillOnce(Return(false));
EXPECT_FALSE(np_class->hasMethod(&np_object, NULL));
EXPECT_CALL(np_object, Invoke(NULL, NULL, 0, NULL))
.Times(1)
.WillOnce(Return(false));
EXPECT_FALSE(np_class->invoke(&np_object, NULL, NULL, 0, NULL));
EXPECT_CALL(np_object, InvokeDefault(NULL, 0, NULL))
.Times(1)
.WillOnce(Return(false));
EXPECT_FALSE(np_class->invokeDefault(&np_object, NULL, 0, NULL));
EXPECT_CALL(np_object, HasProperty(NULL))
.Times(1)
.WillOnce(Return(false));
EXPECT_FALSE(np_class->hasProperty(&np_object, NULL));
EXPECT_CALL(np_object, GetProperty(NULL, NULL))
.Times(1)
.WillOnce(Return(false));
EXPECT_FALSE(np_class->getProperty(&np_object, NULL, NULL));
EXPECT_CALL(np_object, SetProperty(NULL, NULL))
.Times(1)
.WillOnce(Return(false));
EXPECT_FALSE(np_class->setProperty(&np_object, NULL, NULL));
EXPECT_CALL(np_object, RemoveProperty(NULL))
.Times(1)
.WillOnce(Return(false));
EXPECT_FALSE(np_class->removeProperty(&np_object, NULL));
EXPECT_CALL(np_object, Enumeration(NULL, NULL))
.Times(1)
.WillOnce(Return(false));
EXPECT_FALSE(np_class->enumerate(&np_object, NULL, NULL));
EXPECT_CALL(np_object, Construct(NULL, 0, NULL))
.Times(1)
.WillOnce(Return(false));
EXPECT_FALSE(np_class->construct(&np_object, NULL, 0, NULL));
}
} // namespace
|