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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// Copyright (c) 2011 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 "gtest/gtest.h"
#include "gmock/gmock.h"
#include "chrome_frame/chrome_frame_automation.h"
#include "chrome_frame/chrome_frame_npapi.h"
TEST(ChromeFrameNPAPI, DoesNotCrashOnConstruction) {
ChromeFrameNPAPI* api = new ChromeFrameNPAPI();
delete api;
}
// All mocks in the anonymous namespace.
namespace {
using ::testing::_;
using ::testing::Eq;
using ::testing::Field;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::StrEq;
const char* kMimeType = "application/chromeframe";
// The default profile name is by default derived from the currently
// running executable's name.
const wchar_t* kDefaultProfileName = L"chrome_frame_unittests";
class MockNPAPI: public ChromeFrameNPAPI {
public:
MockNPAPI() : mock_automation_client_(NULL) {}
MOCK_METHOD0(GetLocation, std::string());
MOCK_METHOD0(GetBrowserIncognitoMode, bool());
MOCK_METHOD1(JavascriptToNPObject, virtual NPObject*(const std::string&));
// Make public for test purposes
void OnAutomationServerReady() {
ChromeFrameNPAPI::OnAutomationServerReady();
}
ChromeFrameAutomationClient* CreateAutomationClient() {
return mock_automation_client_;
}
ChromeFrameAutomationClient* mock_automation_client_;
};
class MockAutomationClient: public ChromeFrameAutomationClient {
public:
MOCK_METHOD2(Initialize, bool(ChromeFrameDelegate*,
ChromeFrameLaunchParams*));
};
namespace {
MATCHER_P4(LaunchParamEq, version_check, extra, incognito, widget,
"Basic check for ChromeFrameLaunchParams") {
return arg->version_check() == version_check &&
arg->extra_arguments().compare(extra) == 0 &&
arg->incognito() == incognito,
arg->widget_mode() == widget;
}
static const NPIdentifier kOnPrivateMessageId =
reinterpret_cast<NPIdentifier>(0x100);
static const NPIdentifier kPostPrivateMessageId =
reinterpret_cast<NPIdentifier>(0x100);
}
class MockNetscapeFuncs {
public:
MockNetscapeFuncs() {
CHECK(NULL == current_);
current_ = this;
}
~MockNetscapeFuncs() {
CHECK(this == current_);
current_ = NULL;
}
MOCK_METHOD3(GetValue, NPError(NPP, NPNVariable, void *));
MOCK_METHOD3(GetStringIdentifiers, void(const NPUTF8 **,
int32_t,
NPIdentifier *)); // NOLINT
MOCK_METHOD1(RetainObject, NPObject*(NPObject*)); // NOLINT
MOCK_METHOD1(ReleaseObject, void(NPObject*)); // NOLINT
static const NPNetscapeFuncs* netscape_funcs() {
return &netscape_funcs_;
}
private:
static NPError MockGetValue(NPP instance,
NPNVariable variable,
void *ret_value) {
DCHECK(current_);
return current_->GetValue(instance, variable, ret_value);
}
static void MockGetStringIdentifiers(const NPUTF8 **names,
int32_t name_count,
NPIdentifier *identifiers) {
DCHECK(current_);
return current_->GetStringIdentifiers(names, name_count, identifiers);
}
static NPObject* MockRetainObject(NPObject* obj) {
DCHECK(current_);
return current_->RetainObject(obj);
}
static void MockReleaseObject(NPObject* obj) {
DCHECK(current_);
current_->ReleaseObject(obj);
}
static MockNetscapeFuncs* current_;
static NPNetscapeFuncs netscape_funcs_;
};
// Test fixture to allow testing the privileged NPAPI APIs
class TestNPAPIPrivilegedApi: public ::testing::Test {
public:
virtual void SetUp() {
memset(&instance, 0, sizeof(instance));
npapi::InitializeBrowserFunctions(
const_cast<NPNetscapeFuncs*>(mock_funcs.netscape_funcs()));
// Gets owned & destroyed by mock_api (in the
// ChromeFramePlugin<T>::Uninitialize() function).
mock_automation = new MockAutomationClient;
mock_api.mock_automation_client_ = mock_automation;
}
virtual void TearDown() {
// Make sure to uninitialize the mock NPAPI before we uninitialize the
// browser function, otherwise we will get a DCHECK in the NPAPI dtor
// when it tries to perform the uninitialize there.
mock_api.Uninitialize();
npapi::UninitializeBrowserFunctions();
}
void SetupPrivilegeTest(bool is_incognito,
bool expect_privilege_check,
bool is_privileged,
const std::wstring& profile_name,
const std::wstring& language,
const std::wstring& extra_args) {
EXPECT_CALL(mock_api, GetLocation())
.WillOnce(Return(std::string("http://www.google.com")));
EXPECT_CALL(mock_api, GetBrowserIncognitoMode())
.WillOnce(Return(is_incognito));
scoped_refptr<ChromeFrameLaunchParams> launch_params(
new ChromeFrameLaunchParams(GURL(), GURL(), FilePath(), profile_name,
language, extra_args, is_incognito, true, false));
EXPECT_CALL(*mock_automation,
Initialize(_, LaunchParamEq(true, extra_args, is_incognito, true)))
.WillOnce(Return(true));
}
public:
MockNetscapeFuncs mock_funcs;
MockNPAPI mock_api;
MockAutomationClient* mock_automation;
NPP_t instance;
};
} // namespace
|