OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "ppapi/shared_impl/proxy_lock.h" |
8 #include "ppapi/shared_impl/resource.h" | 9 #include "ppapi/shared_impl/resource.h" |
9 #include "ppapi/shared_impl/resource_tracker.h" | 10 #include "ppapi/shared_impl/resource_tracker.h" |
10 #include "ppapi/shared_impl/test_globals.h" | 11 #include "ppapi/shared_impl/test_globals.h" |
11 | 12 |
12 namespace ppapi { | 13 namespace ppapi { |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 int mock_resource_alive_count = 0; | 17 int mock_resource_alive_count = 0; |
17 int last_plugin_ref_was_deleted_count = 0; | 18 int last_plugin_ref_was_deleted_count = 0; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 ResourceTracker& resource_tracker() { return *globals_.GetResourceTracker(); } | 53 ResourceTracker& resource_tracker() { return *globals_.GetResourceTracker(); } |
53 | 54 |
54 private: | 55 private: |
55 TestGlobals globals_; | 56 TestGlobals globals_; |
56 }; | 57 }; |
57 | 58 |
58 // Test that LastPluginRefWasDeleted is called when the last plugin ref was | 59 // Test that LastPluginRefWasDeleted is called when the last plugin ref was |
59 // deleted but the object lives on. | 60 // deleted but the object lives on. |
60 TEST_F(ResourceTrackerTest, LastPluginRef) { | 61 TEST_F(ResourceTrackerTest, LastPluginRef) { |
61 PP_Instance instance = 0x1234567; | 62 PP_Instance instance = 0x1234567; |
| 63 ProxyAutoLock lock; |
62 resource_tracker().DidCreateInstance(instance); | 64 resource_tracker().DidCreateInstance(instance); |
63 | 65 |
64 scoped_refptr<MyMockResource> resource(new MyMockResource(instance)); | 66 scoped_refptr<MyMockResource> resource(new MyMockResource(instance)); |
65 PP_Resource pp_resource = resource->GetReference(); | 67 PP_Resource pp_resource = resource->GetReference(); |
66 EXPECT_TRUE(resource_tracker().GetResource(pp_resource)); | 68 EXPECT_TRUE(resource_tracker().GetResource(pp_resource)); |
67 | 69 |
68 // Releasing it should keep the object (because we have a ref) but fire the | 70 // Releasing it should keep the object (because we have a ref) but fire the |
69 // "last plugin ref" message. | 71 // "last plugin ref" message. |
70 resource_tracker().ReleaseResource(pp_resource); | 72 resource_tracker().ReleaseResource(pp_resource); |
71 EXPECT_EQ(1, last_plugin_ref_was_deleted_count); | 73 EXPECT_EQ(1, last_plugin_ref_was_deleted_count); |
72 EXPECT_EQ(1, mock_resource_alive_count); | 74 EXPECT_EQ(1, mock_resource_alive_count); |
73 | 75 |
74 resource_tracker().DidDeleteInstance(instance); | 76 resource_tracker().DidDeleteInstance(instance); |
75 resource = NULL; | 77 resource = NULL; |
76 EXPECT_FALSE(resource_tracker().GetResource(pp_resource)); | 78 EXPECT_FALSE(resource_tracker().GetResource(pp_resource)); |
77 } | 79 } |
78 | 80 |
79 // Tests when the plugin is holding a ref to a resource when the instance is | 81 // Tests when the plugin is holding a ref to a resource when the instance is |
80 // deleted. | 82 // deleted. |
81 TEST_F(ResourceTrackerTest, InstanceDeletedWithPluginRef) { | 83 TEST_F(ResourceTrackerTest, InstanceDeletedWithPluginRef) { |
82 // Make a resource with one ref held by the plugin, and delete the instance. | 84 // Make a resource with one ref held by the plugin, and delete the instance. |
83 PP_Instance instance = 0x2345678; | 85 PP_Instance instance = 0x2345678; |
| 86 ProxyAutoLock lock; |
84 resource_tracker().DidCreateInstance(instance); | 87 resource_tracker().DidCreateInstance(instance); |
85 MyMockResource* resource = new MyMockResource(instance); | 88 MyMockResource* resource = new MyMockResource(instance); |
86 resource->GetReference(); | 89 resource->GetReference(); |
87 EXPECT_EQ(1, mock_resource_alive_count); | 90 EXPECT_EQ(1, mock_resource_alive_count); |
88 resource_tracker().DidDeleteInstance(instance); | 91 resource_tracker().DidDeleteInstance(instance); |
89 | 92 |
90 // The resource should have been deleted, and before it was, it should have | 93 // The resource should have been deleted, and before it was, it should have |
91 // received a "last plugin ref was deleted" notification. | 94 // received a "last plugin ref was deleted" notification. |
92 EXPECT_EQ(0, mock_resource_alive_count); | 95 EXPECT_EQ(0, mock_resource_alive_count); |
93 EXPECT_EQ(1, last_plugin_ref_was_deleted_count); | 96 EXPECT_EQ(1, last_plugin_ref_was_deleted_count); |
94 EXPECT_EQ(0, instance_was_deleted_count); | 97 EXPECT_EQ(0, instance_was_deleted_count); |
95 } | 98 } |
96 | 99 |
97 // Test when the plugin and the internal implementation (via scoped_refptr) is | 100 // Test when the plugin and the internal implementation (via scoped_refptr) is |
98 // holding a ref to a resource when the instance is deleted. | 101 // holding a ref to a resource when the instance is deleted. |
99 TEST_F(ResourceTrackerTest, InstanceDeletedWithBothRefed) { | 102 TEST_F(ResourceTrackerTest, InstanceDeletedWithBothRefed) { |
100 // Create a new instance. | 103 // Create a new instance. |
101 PP_Instance instance = 0x3456789; | 104 PP_Instance instance = 0x3456789; |
| 105 ProxyAutoLock lock; |
102 | 106 |
103 // Make a resource with one ref held by the plugin and one ref held by us | 107 // Make a resource with one ref held by the plugin and one ref held by us |
104 // (outlives the plugin), and delete the instance. | 108 // (outlives the plugin), and delete the instance. |
105 resource_tracker().DidCreateInstance(instance); | 109 resource_tracker().DidCreateInstance(instance); |
106 scoped_refptr<MyMockResource> resource = new MyMockResource(instance); | 110 scoped_refptr<MyMockResource> resource = new MyMockResource(instance); |
107 resource->GetReference(); | 111 resource->GetReference(); |
108 EXPECT_EQ(1, mock_resource_alive_count); | 112 EXPECT_EQ(1, mock_resource_alive_count); |
109 resource_tracker().DidDeleteInstance(instance); | 113 resource_tracker().DidDeleteInstance(instance); |
110 | 114 |
111 // The resource should NOT have been deleted, and it should have received both | 115 // The resource should NOT have been deleted, and it should have received both |
112 // a "last plugin ref was deleted" and a "instance was deleted" notification. | 116 // a "last plugin ref was deleted" and a "instance was deleted" notification. |
113 EXPECT_EQ(1, mock_resource_alive_count); | 117 EXPECT_EQ(1, mock_resource_alive_count); |
114 EXPECT_EQ(1, last_plugin_ref_was_deleted_count); | 118 EXPECT_EQ(1, last_plugin_ref_was_deleted_count); |
115 EXPECT_EQ(1, instance_was_deleted_count); | 119 EXPECT_EQ(1, instance_was_deleted_count); |
116 EXPECT_EQ(0, resource->pp_instance()); | 120 EXPECT_EQ(0, resource->pp_instance()); |
117 | 121 |
118 resource = NULL; | 122 resource = NULL; |
119 EXPECT_EQ(0, mock_resource_alive_count); | 123 EXPECT_EQ(0, mock_resource_alive_count); |
120 } | 124 } |
121 | 125 |
122 } // namespace ppapi | 126 } // namespace ppapi |
OLD | NEW |