Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ppapi / proxy / ppb_var_unittest.cc
blob98b642832219e015f575f312a235dc10722e21c5
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.
5 #include <string>
6 #include <vector>
8 #include "base/strings/string_number_conversions.h"
9 #include "base/threading/platform_thread.h"
10 #include "ppapi/c/pp_var.h"
11 #include "ppapi/c/ppb_var.h"
12 #include "ppapi/proxy/ppapi_proxy_test.h"
13 #include "ppapi/shared_impl/ppb_var_shared.h"
15 namespace {
16 std::string VarToString(const PP_Var& var, const PPB_Var* ppb_var) {
17 uint32_t len = 0;
18 const char* utf8 = ppb_var->VarToUtf8(var, &len);
19 return std::string(utf8, len);
21 const size_t kNumStrings = 100;
22 const size_t kNumThreads = 20;
23 const int kRefsToAdd = 20;
24 } // namespace
26 namespace ppapi {
27 namespace proxy {
29 class PPB_VarTest : public PluginProxyTest {
30 public:
31 PPB_VarTest()
32 : test_strings_(kNumStrings), vars_(kNumStrings),
33 ppb_var_(ppapi::PPB_Var_Shared::GetVarInterface1_2()) {
34 // Set the value of test_strings_[i] to "i".
35 for (size_t i = 0; i < kNumStrings; ++i)
36 test_strings_[i] = base::IntToString(static_cast<int>(i));
38 protected:
39 std::vector<std::string> test_strings_;
40 std::vector<PP_Var> vars_;
41 const PPB_Var* ppb_var_;
44 // Test basic String operations.
45 TEST_F(PPB_VarTest, Strings) {
46 for (size_t i = 0; i < kNumStrings; ++i) {
47 vars_[i] = ppb_var_->VarFromUtf8(
48 test_strings_[i].c_str(),
49 static_cast<uint32_t>(test_strings_[i].length()));
50 EXPECT_EQ(test_strings_[i], VarToString(vars_[i], ppb_var_));
52 // At this point, they should each have a ref count of 1. Add some more.
53 for (int ref = 0; ref < kRefsToAdd; ++ref) {
54 for (size_t i = 0; i < kNumStrings; ++i) {
55 ppb_var_->AddRef(vars_[i]);
56 // Make sure the string is still there with the right value.
57 EXPECT_EQ(test_strings_[i], VarToString(vars_[i], ppb_var_));
60 for (int ref = 0; ref < kRefsToAdd; ++ref) {
61 for (size_t i = 0; i < kNumStrings; ++i) {
62 ppb_var_->Release(vars_[i]);
63 // Make sure the string is still there with the right value.
64 EXPECT_EQ(test_strings_[i], VarToString(vars_[i], ppb_var_));
67 // Now remove the ref counts for each string and make sure they are gone.
68 for (size_t i = 0; i < kNumStrings; ++i) {
69 ppb_var_->Release(vars_[i]);
70 uint32_t len = 10;
71 const char* utf8 = ppb_var_->VarToUtf8(vars_[i], &len);
72 EXPECT_EQ(NULL, utf8);
73 EXPECT_EQ(0u, len);
77 // PPB_VarTest.Threads tests string operations accessed by multiple threads.
78 namespace {
79 // These three delegate classes which precede the test are for use with
80 // PlatformThread. The test goes roughly like this:
81 // 1) Spawn kNumThreads 'CreateVar' threads, giving each a roughly equal subset
82 // of test_strings_ to 'create'. Each 'CreateVar' thread also converts its
83 // set of vars back in to strings so that the main test thread can verify
84 // their values were correctly converted.
85 // 2) Spawn kNumThreads 'ChangeRefVar' threads. Each of these threads will
86 // incremement & decrement the reference count of ALL vars kRefsToAdd times.
87 // Finally, each thread adds 1 ref count. This leaves each var with a ref-
88 // count of |kNumThreads + 1|. The main test thread removes a ref, leaving
89 // each var with a ref-count of |kNumThreads|.
90 // 3) Spawn kNumThreads 'RemoveVar' threads. Each of these threads releases each
91 // var once. Once all the threads have finished, there should be no vars
92 // left.
93 class CreateVarThreadDelegate : public base::PlatformThread::Delegate {
94 public:
95 // |strings_in|, |vars|, and |strings_out| are arrays, and size is their size.
96 // For each |strings_in[i]|, we will set |vars[i]| using that value. Then we
97 // read the var back out to |strings_out[i]|.
98 CreateVarThreadDelegate(PP_Module pp_module, const std::string* strings_in,
99 PP_Var* vars_out, std::string* strings_out,
100 size_t size)
101 : pp_module_(pp_module), strings_in_(strings_in), vars_out_(vars_out),
102 strings_out_(strings_out), size_(size) {
104 virtual ~CreateVarThreadDelegate() {}
105 virtual void ThreadMain() {
106 const PPB_Var* ppb_var = ppapi::PPB_Var_Shared::GetVarInterface1_2();
107 for (size_t i = 0; i < size_; ++i) {
108 vars_out_[i] = ppb_var->VarFromUtf8(
109 strings_in_[i].c_str(),
110 static_cast<uint32_t>(strings_in_[i].length()));
111 strings_out_[i] = VarToString(vars_out_[i], ppb_var);
114 private:
115 PP_Module pp_module_;
116 const std::string* strings_in_;
117 PP_Var* vars_out_;
118 std::string* strings_out_;
119 size_t size_;
122 // A thread that will increment and decrement the reference count of every var
123 // multiple times.
124 class ChangeRefVarThreadDelegate : public base::PlatformThread::Delegate {
125 public:
126 ChangeRefVarThreadDelegate(const std::vector<PP_Var>& vars) : vars_(vars) {
128 virtual ~ChangeRefVarThreadDelegate() {}
129 virtual void ThreadMain() {
130 const PPB_Var* ppb_var = ppapi::PPB_Var_Shared::GetVarInterface1_2();
131 // Increment and decrement the reference count for each var kRefsToAdd
132 // times. Note that we always AddRef once before doing the matching Release,
133 // to ensure that we never accidentally release the last reference.
134 for (int ref = 0; ref < kRefsToAdd; ++ref) {
135 for (size_t i = 0; i < kNumStrings; ++i) {
136 ppb_var->AddRef(vars_[i]);
137 ppb_var->Release(vars_[i]);
140 // Now add 1 ref to each Var. The net result is that all Vars will have a
141 // ref-count of (kNumThreads + 1) after this. That will allow us to have all
142 // threads release all vars later.
143 for (size_t i = 0; i < kNumStrings; ++i) {
144 ppb_var->AddRef(vars_[i]);
147 private:
148 std::vector<PP_Var> vars_;
151 // A thread that will decrement the reference count of every var once.
152 class RemoveRefVarThreadDelegate : public base::PlatformThread::Delegate {
153 public:
154 RemoveRefVarThreadDelegate(const std::vector<PP_Var>& vars) : vars_(vars) {
156 virtual ~RemoveRefVarThreadDelegate() {}
157 virtual void ThreadMain() {
158 const PPB_Var* ppb_var = ppapi::PPB_Var_Shared::GetVarInterface1_2();
159 for (size_t i = 0; i < kNumStrings; ++i) {
160 ppb_var->Release(vars_[i]);
163 private:
164 std::vector<PP_Var> vars_;
167 } // namespace
169 TEST_F(PPB_VarTest, Threads) {
170 std::vector<base::PlatformThreadHandle> create_var_threads(kNumThreads);
171 std::vector<CreateVarThreadDelegate> create_var_delegates;
172 // The strings that the threads will re-extract from Vars (so we can check
173 // that they match the original strings).
174 std::vector<std::string> strings_out(kNumStrings);
175 size_t strings_per_thread = kNumStrings/kNumThreads;
176 // Give each thread an equal slice of strings to turn in to vars. (Except the
177 // last thread may get fewer if kNumStrings is not evenly divisible by
178 // kNumThreads).
179 for (size_t slice_start= 0; slice_start < kNumStrings;
180 slice_start += strings_per_thread) {
181 create_var_delegates.push_back(
182 CreateVarThreadDelegate(pp_module(),
183 &test_strings_[slice_start],
184 &vars_[slice_start],
185 &strings_out[slice_start],
186 std::min(strings_per_thread,
187 kNumStrings - slice_start)));
189 // Now run then join all the threads.
190 for (size_t i = 0; i < kNumThreads; ++i)
191 base::PlatformThread::Create(0, &create_var_delegates[i],
192 &create_var_threads[i]);
193 for (size_t i = 0; i < kNumThreads; ++i)
194 base::PlatformThread::Join(create_var_threads[i]);
195 // Now check that the strings have the expected values.
196 EXPECT_EQ(test_strings_, strings_out);
198 // Tinker with the reference counts in a multithreaded way.
199 std::vector<base::PlatformThreadHandle> change_ref_var_threads(kNumThreads);
200 std::vector<ChangeRefVarThreadDelegate> change_ref_var_delegates;
201 for (size_t i = 0; i < kNumThreads; ++i)
202 change_ref_var_delegates.push_back(ChangeRefVarThreadDelegate(vars_));
203 for (size_t i = 0; i < kNumThreads; ++i) {
204 base::PlatformThread::Create(0, &change_ref_var_delegates[i],
205 &change_ref_var_threads[i]);
207 for (size_t i = 0; i < kNumThreads; ++i)
208 base::PlatformThread::Join(change_ref_var_threads[i]);
210 // Now each var has a refcount of (kNumThreads + 1). Let's decrement each var
211 // once so that every 'RemoveRef' thread (spawned below) owns 1 reference, and
212 // when the last one removes a ref, the Var will be deleted.
213 for (size_t i = 0; i < kNumStrings; ++i) {
214 ppb_var_->Release(vars_[i]);
217 // Check that all vars are still valid and have the values we expect.
218 for (size_t i = 0; i < kNumStrings; ++i)
219 EXPECT_EQ(test_strings_[i], VarToString(vars_[i], ppb_var_));
221 // Remove the last reference counts for all vars.
222 std::vector<base::PlatformThreadHandle> remove_ref_var_threads(kNumThreads);
223 std::vector<RemoveRefVarThreadDelegate> remove_ref_var_delegates;
224 for (size_t i = 0; i < kNumThreads; ++i)
225 remove_ref_var_delegates.push_back(RemoveRefVarThreadDelegate(vars_));
226 for (size_t i = 0; i < kNumThreads; ++i) {
227 base::PlatformThread::Create(0, &remove_ref_var_delegates[i],
228 &remove_ref_var_threads[i]);
230 for (size_t i = 0; i < kNumThreads; ++i)
231 base::PlatformThread::Join(remove_ref_var_threads[i]);
233 // All the vars should no longer represent valid strings.
234 for (size_t i = 0; i < kNumStrings; ++i) {
235 uint32_t len = 10;
236 const char* utf8 = ppb_var_->VarToUtf8(vars_[i], &len);
237 EXPECT_EQ(NULL, utf8);
238 EXPECT_EQ(0u, len);
242 } // namespace proxy
243 } // namespace ppapi