1 // Copyright (c) 2011 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 "base/memory/scoped_ptr.h"
6 #include "base/process/process.h"
7 #include "ppapi/proxy/mock_resource.h"
8 #include "ppapi/proxy/plugin_dispatcher.h"
9 #include "ppapi/proxy/plugin_resource_tracker.h"
10 #include "ppapi/proxy/ppapi_messages.h"
11 #include "ppapi/proxy/ppapi_proxy_test.h"
12 #include "ppapi/shared_impl/proxy_lock.h"
13
14 namespace ppapi {
15 namespace proxy {
16
17 namespace {
18
19 // Object so we know when a resource has been released.
20 class TrackedMockResource : public MockResource {
21 public:
TrackedMockResource(const HostResource & serialized)22 TrackedMockResource(const HostResource& serialized)
23 : MockResource(serialized) {
24 tracked_alive_count++;
25 }
~TrackedMockResource()26 ~TrackedMockResource() {
27 tracked_alive_count--;
28 }
29
30 static int tracked_alive_count;
31 };
32
33 int TrackedMockResource::tracked_alive_count = 0;
34
35 } // namespace
36
37 class PluginResourceTrackerTest : public PluginProxyTest {
38 public:
PluginResourceTrackerTest()39 PluginResourceTrackerTest() {}
~PluginResourceTrackerTest()40 ~PluginResourceTrackerTest() {}
41 };
42
TEST_F(PluginResourceTrackerTest,PluginResourceForHostResource)43 TEST_F(PluginResourceTrackerTest, PluginResourceForHostResource) {
44 ProxyAutoLock lock;
45
46 PP_Resource host_resource = 0x5678;
47
48 HostResource serialized;
49 serialized.SetHostResource(pp_instance(), host_resource);
50
51 // When we haven't added an object, the return value should be 0.
52 EXPECT_EQ(0, resource_tracker().PluginResourceForHostResource(serialized));
53
54 EXPECT_EQ(0, TrackedMockResource::tracked_alive_count);
55 TrackedMockResource* object = new TrackedMockResource(serialized);
56 EXPECT_EQ(1, TrackedMockResource::tracked_alive_count);
57 PP_Resource plugin_resource = object->GetReference();
58
59 // Now that the object has been added, the return value should be the plugin
60 // resource ID we already got.
61 EXPECT_EQ(plugin_resource,
62 resource_tracker().PluginResourceForHostResource(serialized));
63
64 // Releasing the resource should have freed it.
65 resource_tracker().ReleaseResource(plugin_resource);
66 EXPECT_EQ(0, TrackedMockResource::tracked_alive_count);
67 EXPECT_EQ(0, resource_tracker().PluginResourceForHostResource(serialized));
68 }
69
70 } // namespace proxy
71 } // namespace ppapi
72