1 // Copyright (c) 2012 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.
7 #include "base/synchronization/waitable_event.h"
8 #include "ppapi/c/pp_var.h"
9 #include "ppapi/c/ppb_var.h"
10 #include "ppapi/c/ppp_messaging.h"
11 #include "ppapi/proxy/ppapi_proxy_test.h"
12 #include "ppapi/shared_impl/proxy_lock.h"
13 #include "ppapi/shared_impl/var.h"
20 // This is a poor man's mock of PPP_Messaging using global variables. Eventually
21 // we should generalize making PPAPI interface mocks by using IDL or macro/
23 PP_Instance received_instance
;
25 base::WaitableEvent
handle_message_called(false, false);
27 void HandleMessage(PP_Instance instance
, PP_Var message_data
) {
28 received_instance
= instance
;
29 received_var
= message_data
;
30 handle_message_called
.Signal();
33 // Clear all the 'received' values for our mock. Call this before you expect
34 // one of the functions to be invoked.
35 void ResetReceived() {
36 received_instance
= 0;
37 received_var
.type
= PP_VARTYPE_UNDEFINED
;
38 received_var
.value
.as_id
= 0;
41 PPP_Messaging ppp_messaging_mock
= {
45 class PPP_Messaging_ProxyTest
: public TwoWayTest
{
47 PPP_Messaging_ProxyTest()
48 : TwoWayTest(TwoWayTest::TEST_PPP_INTERFACE
) {
49 plugin().RegisterTestInterface(PPP_MESSAGING_INTERFACE
,
54 void CompareAndReleaseStringVar(PluginProxyTestHarness
* plugin_harness
,
56 const std::string
& test_string
) {
58 Var
* received_string
= plugin_harness
->var_tracker().GetVar(received_var
);
59 ASSERT_TRUE(received_string
);
60 ASSERT_TRUE(received_string
->AsStringVar());
61 EXPECT_EQ(test_string
, received_string
->AsStringVar()->value());
62 // Now release the var, and the string should go away (because the ref
63 // count should be one).
64 plugin_harness
->var_tracker().ReleaseVar(received_var
);
65 EXPECT_FALSE(StringVar::FromPPVar(received_var
));
70 TEST_F(PPP_Messaging_ProxyTest
, SendMessages
) {
71 // Grab the host-side proxy of ppp_messaging.
72 const PPP_Messaging
* ppp_messaging
= static_cast<const PPP_Messaging
*>(
73 host().host_dispatcher()->GetProxiedInterface(
74 PPP_MESSAGING_INTERFACE
));
76 PP_Instance expected_instance
= pp_instance();
77 PP_Var expected_var
= PP_MakeUndefined();
79 ppp_messaging
->HandleMessage(expected_instance
, expected_var
);
80 handle_message_called
.Wait();
81 EXPECT_EQ(expected_instance
, received_instance
);
82 EXPECT_EQ(expected_var
.type
, received_var
.type
);
84 expected_var
= PP_MakeNull();
86 ppp_messaging
->HandleMessage(expected_instance
, expected_var
);
87 handle_message_called
.Wait();
88 EXPECT_EQ(expected_instance
, received_instance
);
89 EXPECT_EQ(expected_var
.type
, received_var
.type
);
91 expected_var
= PP_MakeBool(PP_TRUE
);
93 ppp_messaging
->HandleMessage(expected_instance
, expected_var
);
94 handle_message_called
.Wait();
95 EXPECT_EQ(expected_instance
, received_instance
);
96 EXPECT_EQ(expected_var
.type
, received_var
.type
);
97 EXPECT_EQ(expected_var
.value
.as_bool
, received_var
.value
.as_bool
);
99 expected_var
= PP_MakeInt32(12345);
101 ppp_messaging
->HandleMessage(expected_instance
, expected_var
);
102 handle_message_called
.Wait();
103 EXPECT_EQ(expected_instance
, received_instance
);
104 EXPECT_EQ(expected_var
.type
, received_var
.type
);
105 EXPECT_EQ(expected_var
.value
.as_int
, received_var
.value
.as_int
);
107 expected_var
= PP_MakeDouble(3.1415);
109 ppp_messaging
->HandleMessage(expected_instance
, expected_var
);
110 handle_message_called
.Wait();
111 EXPECT_EQ(expected_instance
, received_instance
);
112 EXPECT_EQ(expected_var
.type
, received_var
.type
);
113 EXPECT_EQ(expected_var
.value
.as_double
, received_var
.value
.as_double
);
115 const std::string
kTestString("Hello world!");
116 expected_var
= StringVar::StringToPPVar(kTestString
);
118 ppp_messaging
->HandleMessage(expected_instance
, expected_var
);
119 // Now release the var, and the string should go away (because the ref
120 // count should be one).
121 host().var_tracker().ReleaseVar(expected_var
);
122 EXPECT_FALSE(StringVar::FromPPVar(expected_var
));
124 handle_message_called
.Wait();
125 EXPECT_EQ(expected_instance
, received_instance
);
126 EXPECT_EQ(expected_var
.type
, received_var
.type
);
127 PostTaskOnRemoteHarness(
128 base::Bind(CompareAndReleaseStringVar
,