• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #include <list>
6 
7 #include "libcef_dll/wrapper/cef_browser_info_map.h"
8 #include "tests/gtest/include/gtest/gtest.h"
9 
10 namespace {
11 
12 struct MyObject {
MyObject__anon5d69b8f20111::MyObject13   MyObject(int val = 0) : member(val) {}
14   int member;
15 };
16 
17 int g_destruct_ct = 0;
18 
19 struct MyObjectTraits {
Destruct__anon5d69b8f20111::MyObjectTraits20   static void Destruct(MyObject info) { g_destruct_ct++; }
21 };
22 
23 typedef CefBrowserInfoMap<int, MyObject, MyObjectTraits> MyObjectMap;
24 
25 class MyVisitor : public MyObjectMap::Visitor {
26  public:
MyVisitor(bool remove=false,int remove_browser_id=0,InfoIdType remove_info_id=0)27   MyVisitor(bool remove = false,
28             int remove_browser_id = 0,
29             InfoIdType remove_info_id = 0)
30       : remove_(remove),
31         remove_browser_id_(remove_browser_id),
32         remove_info_id_(remove_info_id) {}
33 
OnNextInfo(int browser_id,InfoIdType info_id,InfoObjectType info,bool * remove)34   bool OnNextInfo(int browser_id,
35                   InfoIdType info_id,
36                   InfoObjectType info,
37                   bool* remove) override {
38     Info info_rec;
39     info_rec.browser_id = browser_id;
40     info_rec.info_id = info_id;
41     info_rec.info = info;
42     info_list_.push_back(info_rec);
43 
44     // Based on test configuration remove no objects, all objects, or only the
45     // specified object.
46     *remove = remove_ ||
47               (browser_id == remove_browser_id_ && info_id == remove_info_id_);
48     return true;
49   }
50 
51   // Returns true if the specified info was passed to OnNextInfo. Removes the
52   // record if found.
Exists(int browser_id,InfoIdType info_id,InfoObjectType info)53   bool Exists(int browser_id, InfoIdType info_id, InfoObjectType info) {
54     InfoList::iterator it = info_list_.begin();
55     for (; it != info_list_.end(); ++it) {
56       const Info& found_info = *it;
57       if (browser_id == found_info.browser_id &&
58           info_id == found_info.info_id &&
59           info.member == found_info.info.member) {
60         info_list_.erase(it);
61         return true;
62       }
63     }
64     return false;
65   }
66 
info_size() const67   size_t info_size() const { return info_list_.size(); }
68 
69  private:
70   bool remove_;
71   int remove_browser_id_;
72   InfoIdType remove_info_id_;
73 
74   struct Info {
75     int browser_id;
76     InfoIdType info_id;
77     InfoObjectType info;
78   };
79 
80   // Track calls to OnNextInfo.
81   typedef std::list<Info> InfoList;
82   InfoList info_list_;
83 };
84 
85 }  // namespace
86 
TEST(BrowserInfoMapTest,AddSingleBrowser)87 TEST(BrowserInfoMapTest, AddSingleBrowser) {
88   MyObjectMap map;
89   const int kBrowserId = 1;
90 
91   g_destruct_ct = 0;
92 
93   EXPECT_EQ(0U, map.size());
94   EXPECT_EQ(0U, map.size(kBrowserId));
95 
96   MyObject obj1(1);
97   map.Add(kBrowserId, 1, obj1);
98   EXPECT_EQ(1U, map.size());
99   EXPECT_EQ(1U, map.size(kBrowserId));
100 
101   MyObject obj2(2);
102   map.Add(kBrowserId, 2, obj2);
103   EXPECT_EQ(2U, map.size());
104   EXPECT_EQ(2U, map.size(kBrowserId));
105 
106   MyObject obj3(3);
107   map.Add(kBrowserId, 3, obj3);
108   EXPECT_EQ(3U, map.size());
109   EXPECT_EQ(3U, map.size(kBrowserId));
110 
111   EXPECT_EQ(0, g_destruct_ct);
112 
113   map.clear(kBrowserId);
114   EXPECT_EQ(0U, map.size());
115   EXPECT_EQ(0U, map.size(kBrowserId));
116 
117   EXPECT_EQ(3, g_destruct_ct);
118 }
119 
TEST(BrowserInfoMapTest,AddMultipleBrowsers)120 TEST(BrowserInfoMapTest, AddMultipleBrowsers) {
121   MyObjectMap map;
122   const int kBrowserId1 = 1;
123   const int kBrowserId2 = 2;
124 
125   g_destruct_ct = 0;
126 
127   EXPECT_EQ(0U, map.size());
128   EXPECT_EQ(0U, map.size(kBrowserId1));
129   EXPECT_EQ(0U, map.size(kBrowserId2));
130 
131   MyObject obj1(1);
132   map.Add(kBrowserId1, 1, obj1);
133   EXPECT_EQ(1U, map.size());
134   EXPECT_EQ(1U, map.size(kBrowserId1));
135   EXPECT_EQ(0U, map.size(kBrowserId2));
136 
137   MyObject obj2(2);
138   map.Add(kBrowserId1, 2, obj2);
139   EXPECT_EQ(2U, map.size());
140   EXPECT_EQ(2U, map.size(kBrowserId1));
141   EXPECT_EQ(0U, map.size(kBrowserId2));
142 
143   MyObject obj3(3);
144   map.Add(kBrowserId2, 3, obj3);
145   EXPECT_EQ(3U, map.size());
146   EXPECT_EQ(2U, map.size(kBrowserId1));
147   EXPECT_EQ(1U, map.size(kBrowserId2));
148 
149   MyObject obj4(4);
150   map.Add(kBrowserId2, 4, obj4);
151   EXPECT_EQ(4U, map.size());
152   EXPECT_EQ(2U, map.size(kBrowserId1));
153   EXPECT_EQ(2U, map.size(kBrowserId2));
154 
155   EXPECT_EQ(0, g_destruct_ct);
156 
157   map.clear(kBrowserId1);
158   EXPECT_EQ(2U, map.size());
159   EXPECT_EQ(0U, map.size(kBrowserId1));
160   EXPECT_EQ(2U, map.size(kBrowserId2));
161 
162   EXPECT_EQ(2, g_destruct_ct);
163 
164   map.clear(kBrowserId2);
165   EXPECT_EQ(0U, map.size());
166   EXPECT_EQ(0U, map.size(kBrowserId1));
167   EXPECT_EQ(0U, map.size(kBrowserId2));
168 
169   EXPECT_EQ(4, g_destruct_ct);
170 }
171 
TEST(BrowserInfoMapTest,FindSingleBrowser)172 TEST(BrowserInfoMapTest, FindSingleBrowser) {
173   MyObjectMap map;
174   const int kBrowserId = 1;
175 
176   g_destruct_ct = 0;
177 
178   // obj1 not added yet.
179   MyObject nf1 = map.Find(kBrowserId, 1, nullptr);
180   EXPECT_EQ(0, nf1.member);
181 
182   MyObject obj1(1);
183   map.Add(kBrowserId, 1, obj1);
184 
185   // obj1 should exist.
186   MyObject f1 = map.Find(kBrowserId, 1, nullptr);
187   EXPECT_EQ(obj1.member, f1.member);
188 
189   // obj2 not added yet.
190   MyObject nf2 = map.Find(kBrowserId, 2, nullptr);
191   EXPECT_EQ(0, nf2.member);
192 
193   MyObject obj2(2);
194   map.Add(kBrowserId, 2, obj2);
195 
196   // obj2 should exist.
197   MyObject f2 = map.Find(kBrowserId, 2, nullptr);
198   EXPECT_EQ(obj2.member, f2.member);
199 
200   // find obj1 again.
201   MyObject f1b = map.Find(kBrowserId, 1, nullptr);
202   EXPECT_EQ(obj1.member, f1b.member);
203 
204   // find obj2 again.
205   MyObject f2b = map.Find(kBrowserId, 2, nullptr);
206   EXPECT_EQ(obj2.member, f2b.member);
207 
208   // doesn't exist.
209   MyObject nf3 = map.Find(kBrowserId, 3, nullptr);
210   EXPECT_EQ(0, nf3.member);
211   MyObject nf4 = map.Find(10, 1, nullptr);
212   EXPECT_EQ(0, nf4.member);
213   MyObject nf5 = map.Find(10, 3, nullptr);
214   EXPECT_EQ(0, nf5.member);
215 
216   EXPECT_EQ(0, g_destruct_ct);
217   map.clear();
218   EXPECT_EQ(2, g_destruct_ct);
219 }
220 
TEST(BrowserInfoMapTest,FindMultipleBrowsers)221 TEST(BrowserInfoMapTest, FindMultipleBrowsers) {
222   MyObjectMap map;
223   const int kBrowserId1 = 1;
224   const int kBrowserId2 = 2;
225 
226   g_destruct_ct = 0;
227 
228   // obj1 not added yet.
229   MyObject nf1 = map.Find(kBrowserId1, 1, nullptr);
230   EXPECT_EQ(0, nf1.member);
231 
232   MyObject obj1(1);
233   map.Add(kBrowserId1, 1, obj1);
234 
235   // obj1 should exist.
236   MyObject f1 = map.Find(kBrowserId1, 1, nullptr);
237   EXPECT_EQ(obj1.member, f1.member);
238 
239   // obj2 not added yet.
240   MyObject nf2 = map.Find(kBrowserId1, 2, nullptr);
241   EXPECT_EQ(0, nf2.member);
242 
243   MyObject obj2(2);
244   map.Add(kBrowserId1, 2, obj2);
245 
246   // obj2 should exist.
247   MyObject f2 = map.Find(kBrowserId1, 2, nullptr);
248   EXPECT_EQ(obj2.member, f2.member);
249 
250   // obj3 not added yet.
251   MyObject nf3 = map.Find(kBrowserId2, 3, nullptr);
252   EXPECT_EQ(0, nf3.member);
253 
254   MyObject obj3(3);
255   map.Add(kBrowserId2, 3, obj3);
256 
257   // obj3 should exist.
258   MyObject f3 = map.Find(kBrowserId2, 3, nullptr);
259   EXPECT_EQ(obj3.member, f3.member);
260 
261   // obj4 not added yet.
262   MyObject nf4 = map.Find(kBrowserId2, 4, nullptr);
263   EXPECT_EQ(0, nf4.member);
264 
265   MyObject obj4(4);
266   map.Add(kBrowserId2, 4, obj4);
267 
268   // obj4 should exist.
269   MyObject f4 = map.Find(kBrowserId2, 4, nullptr);
270   EXPECT_EQ(obj4.member, f4.member);
271 
272   // obj1-3 should exist.
273   MyObject f1b = map.Find(kBrowserId1, 1, nullptr);
274   EXPECT_EQ(obj1.member, f1b.member);
275   MyObject f2b = map.Find(kBrowserId1, 2, nullptr);
276   EXPECT_EQ(obj2.member, f2b.member);
277   MyObject f3b = map.Find(kBrowserId2, 3, nullptr);
278   EXPECT_EQ(obj3.member, f3b.member);
279 
280   // wrong browser
281   MyObject nf5 = map.Find(kBrowserId1, 4, nullptr);
282   EXPECT_EQ(0, nf5.member);
283   MyObject nf6 = map.Find(kBrowserId2, 1, nullptr);
284   EXPECT_EQ(0, nf6.member);
285 
286   // deosn't exist
287   MyObject nf7 = map.Find(kBrowserId2, 5, nullptr);
288   EXPECT_EQ(0, nf7.member);
289   MyObject nf8 = map.Find(8, 1, nullptr);
290   EXPECT_EQ(0, nf8.member);
291   MyObject nf9 = map.Find(8, 10, nullptr);
292   EXPECT_EQ(0, nf9.member);
293 
294   EXPECT_EQ(0, g_destruct_ct);
295   map.clear();
296   EXPECT_EQ(4, g_destruct_ct);
297 }
298 
TEST(BrowserInfoMapTest,Find)299 TEST(BrowserInfoMapTest, Find) {
300   MyObjectMap map;
301   const int kBrowserId1 = 1;
302   const int kBrowserId2 = 2;
303 
304   g_destruct_ct = 0;
305 
306   MyObject obj1(1);
307   map.Add(kBrowserId1, 1, obj1);
308 
309   MyObject obj2(2);
310   map.Add(kBrowserId1, 2, obj2);
311 
312   MyObject obj3(3);
313   map.Add(kBrowserId2, 3, obj3);
314 
315   MyObject obj4(4);
316   map.Add(kBrowserId2, 4, obj4);
317 
318   EXPECT_EQ(4U, map.size());
319   EXPECT_EQ(2U, map.size(kBrowserId1));
320   EXPECT_EQ(2U, map.size(kBrowserId2));
321 
322   // should only visit the single object
323   MyVisitor visitor;
324   map.Find(kBrowserId2, 4, &visitor);
325   EXPECT_EQ(1U, visitor.info_size());
326   EXPECT_TRUE(visitor.Exists(kBrowserId2, 4, obj4));
327   EXPECT_EQ(0U, visitor.info_size());
328 
329   EXPECT_EQ(4U, map.size());
330   EXPECT_EQ(2U, map.size(kBrowserId1));
331   EXPECT_EQ(2U, map.size(kBrowserId2));
332 
333   EXPECT_EQ(0, g_destruct_ct);
334   map.clear();
335   EXPECT_EQ(4, g_destruct_ct);
336 }
337 
TEST(BrowserInfoMapTest,FindAndRemove)338 TEST(BrowserInfoMapTest, FindAndRemove) {
339   MyObjectMap map;
340   const int kBrowserId1 = 1;
341   const int kBrowserId2 = 2;
342 
343   g_destruct_ct = 0;
344 
345   MyObject obj1(1);
346   map.Add(kBrowserId1, 1, obj1);
347 
348   MyObject obj2(2);
349   map.Add(kBrowserId1, 2, obj2);
350 
351   MyObject obj3(3);
352   map.Add(kBrowserId2, 3, obj3);
353 
354   MyObject obj4(4);
355   map.Add(kBrowserId2, 4, obj4);
356 
357   EXPECT_EQ(4U, map.size());
358   EXPECT_EQ(2U, map.size(kBrowserId1));
359   EXPECT_EQ(2U, map.size(kBrowserId2));
360 
361   // should only visit and remove the single object.
362   MyVisitor visitor(true);
363   map.Find(kBrowserId1, 2, &visitor);
364   EXPECT_EQ(1U, visitor.info_size());
365   EXPECT_TRUE(visitor.Exists(kBrowserId1, 2, obj2));
366   EXPECT_EQ(0U, visitor.info_size());
367 
368   EXPECT_EQ(3U, map.size());
369   EXPECT_EQ(1U, map.size(kBrowserId1));
370   EXPECT_EQ(2U, map.size(kBrowserId2));
371 
372   // visited object shouldn't exist
373   MyObject nf2 = map.Find(kBrowserId1, 2, nullptr);
374   EXPECT_EQ(0, nf2.member);
375 
376   // other objects should exist
377   MyObject nf1 = map.Find(kBrowserId1, 1, nullptr);
378   EXPECT_EQ(obj1.member, nf1.member);
379   MyObject nf3 = map.Find(kBrowserId2, 3, nullptr);
380   EXPECT_EQ(obj3.member, nf3.member);
381   MyObject nf4 = map.Find(kBrowserId2, 4, nullptr);
382   EXPECT_EQ(obj4.member, nf4.member);
383 
384   EXPECT_EQ(0, g_destruct_ct);
385   map.clear();
386   // should destruct the remaining 3 objects
387   EXPECT_EQ(3, g_destruct_ct);
388 }
389 
TEST(BrowserInfoMapTest,FindAll)390 TEST(BrowserInfoMapTest, FindAll) {
391   MyObjectMap map;
392   const int kBrowserId1 = 1;
393   const int kBrowserId2 = 2;
394 
395   g_destruct_ct = 0;
396 
397   MyObject obj1(1);
398   map.Add(kBrowserId1, 1, obj1);
399 
400   MyObject obj2(2);
401   map.Add(kBrowserId1, 2, obj2);
402 
403   MyObject obj3(3);
404   map.Add(kBrowserId2, 3, obj3);
405 
406   MyObject obj4(4);
407   map.Add(kBrowserId2, 4, obj4);
408 
409   EXPECT_EQ(4U, map.size());
410   EXPECT_EQ(2U, map.size(kBrowserId1));
411   EXPECT_EQ(2U, map.size(kBrowserId2));
412 
413   // should visit all objects
414   MyVisitor visitor;
415   map.FindAll(&visitor);
416   EXPECT_EQ(4U, visitor.info_size());
417   EXPECT_TRUE(visitor.Exists(kBrowserId1, 1, obj1));
418   EXPECT_TRUE(visitor.Exists(kBrowserId1, 2, obj2));
419   EXPECT_TRUE(visitor.Exists(kBrowserId2, 3, obj3));
420   EXPECT_TRUE(visitor.Exists(kBrowserId2, 4, obj4));
421   // should be no unexpected visits
422   EXPECT_EQ(0U, visitor.info_size());
423 
424   EXPECT_EQ(4U, map.size());
425   EXPECT_EQ(2U, map.size(kBrowserId1));
426   EXPECT_EQ(2U, map.size(kBrowserId2));
427 
428   EXPECT_EQ(0, g_destruct_ct);
429   map.clear();
430   EXPECT_EQ(4, g_destruct_ct);
431 }
432 
TEST(BrowserInfoMapTest,FindAllByBrowser)433 TEST(BrowserInfoMapTest, FindAllByBrowser) {
434   MyObjectMap map;
435   const int kBrowserId1 = 1;
436   const int kBrowserId2 = 2;
437 
438   g_destruct_ct = 0;
439 
440   MyObject obj1(1);
441   map.Add(kBrowserId1, 1, obj1);
442 
443   MyObject obj2(2);
444   map.Add(kBrowserId1, 2, obj2);
445 
446   MyObject obj3(3);
447   map.Add(kBrowserId2, 3, obj3);
448 
449   MyObject obj4(4);
450   map.Add(kBrowserId2, 4, obj4);
451 
452   EXPECT_EQ(4U, map.size());
453   EXPECT_EQ(2U, map.size(kBrowserId1));
454   EXPECT_EQ(2U, map.size(kBrowserId2));
455 
456   // should only visit browser2 objects
457   MyVisitor visitor;
458   map.FindAll(kBrowserId2, &visitor);
459   EXPECT_EQ(2U, visitor.info_size());
460   EXPECT_TRUE(visitor.Exists(kBrowserId2, 3, obj3));
461   EXPECT_TRUE(visitor.Exists(kBrowserId2, 4, obj4));
462   // should be no unexpected visits
463   EXPECT_EQ(0U, visitor.info_size());
464 
465   EXPECT_EQ(4U, map.size());
466   EXPECT_EQ(2U, map.size(kBrowserId1));
467   EXPECT_EQ(2U, map.size(kBrowserId2));
468 
469   EXPECT_EQ(0, g_destruct_ct);
470   map.clear();
471   EXPECT_EQ(4, g_destruct_ct);
472 }
473 
TEST(BrowserInfoMapTest,FindAllAndRemoveAll)474 TEST(BrowserInfoMapTest, FindAllAndRemoveAll) {
475   MyObjectMap map;
476   const int kBrowserId1 = 1;
477   const int kBrowserId2 = 2;
478 
479   g_destruct_ct = 0;
480 
481   MyObject obj1(1);
482   map.Add(kBrowserId1, 1, obj1);
483 
484   MyObject obj2(2);
485   map.Add(kBrowserId1, 2, obj2);
486 
487   MyObject obj3(3);
488   map.Add(kBrowserId2, 3, obj3);
489 
490   MyObject obj4(4);
491   map.Add(kBrowserId2, 4, obj4);
492 
493   EXPECT_EQ(4U, map.size());
494   EXPECT_EQ(2U, map.size(kBrowserId1));
495   EXPECT_EQ(2U, map.size(kBrowserId2));
496 
497   // should visit all objects
498   MyVisitor visitor(true);
499   map.FindAll(&visitor);
500   EXPECT_EQ(4U, visitor.info_size());
501   EXPECT_TRUE(visitor.Exists(kBrowserId1, 1, obj1));
502   EXPECT_TRUE(visitor.Exists(kBrowserId1, 2, obj2));
503   EXPECT_TRUE(visitor.Exists(kBrowserId2, 3, obj3));
504   EXPECT_TRUE(visitor.Exists(kBrowserId2, 4, obj4));
505   // should be no unexpected visits
506   EXPECT_EQ(0U, visitor.info_size());
507 
508   EXPECT_EQ(0U, map.size());
509   EXPECT_EQ(0U, map.size(kBrowserId1));
510   EXPECT_EQ(0U, map.size(kBrowserId2));
511 
512   EXPECT_EQ(0, g_destruct_ct);
513 }
514 
TEST(BrowserInfoMapTest,FindAllAndRemoveOne)515 TEST(BrowserInfoMapTest, FindAllAndRemoveOne) {
516   MyObjectMap map;
517   const int kBrowserId1 = 1;
518   const int kBrowserId2 = 2;
519 
520   g_destruct_ct = 0;
521 
522   MyObject obj1(1);
523   map.Add(kBrowserId1, 1, obj1);
524 
525   MyObject obj2(2);
526   map.Add(kBrowserId1, 2, obj2);
527 
528   MyObject obj3(3);
529   map.Add(kBrowserId2, 3, obj3);
530 
531   MyObject obj4(4);
532   map.Add(kBrowserId2, 4, obj4);
533 
534   EXPECT_EQ(4U, map.size());
535   EXPECT_EQ(2U, map.size(kBrowserId1));
536   EXPECT_EQ(2U, map.size(kBrowserId2));
537 
538   // should visit all objects and remove one
539   MyVisitor visitor(false, kBrowserId2, 3);
540   map.FindAll(&visitor);
541   EXPECT_EQ(4U, visitor.info_size());
542   EXPECT_TRUE(visitor.Exists(kBrowserId1, 1, obj1));
543   EXPECT_TRUE(visitor.Exists(kBrowserId1, 2, obj2));
544   EXPECT_TRUE(visitor.Exists(kBrowserId2, 3, obj3));
545   EXPECT_TRUE(visitor.Exists(kBrowserId2, 4, obj4));
546   // should be no unexpected visits
547   EXPECT_EQ(0U, visitor.info_size());
548 
549   EXPECT_EQ(3U, map.size());
550   EXPECT_EQ(2U, map.size(kBrowserId1));
551   EXPECT_EQ(1U, map.size(kBrowserId2));
552 
553   // removed object shouldn't exist
554   MyObject nf3 = map.Find(kBrowserId2, 3, nullptr);
555   EXPECT_EQ(0, nf3.member);
556 
557   // other objects should exist
558   MyObject f1 = map.Find(kBrowserId1, 1, nullptr);
559   EXPECT_EQ(obj1.member, f1.member);
560   MyObject f2 = map.Find(kBrowserId1, 2, nullptr);
561   EXPECT_EQ(obj2.member, f2.member);
562   MyObject f4 = map.Find(kBrowserId2, 4, nullptr);
563   EXPECT_EQ(obj4.member, f4.member);
564 
565   EXPECT_EQ(0, g_destruct_ct);
566   map.clear();
567   // should destruct the remaining 3 objects
568   EXPECT_EQ(3, g_destruct_ct);
569 }
570 
TEST(BrowserInfoMapTest,FindAllAndRemoveAllByBrowser)571 TEST(BrowserInfoMapTest, FindAllAndRemoveAllByBrowser) {
572   MyObjectMap map;
573   const int kBrowserId1 = 1;
574   const int kBrowserId2 = 2;
575 
576   g_destruct_ct = 0;
577 
578   MyObject obj1(1);
579   map.Add(kBrowserId1, 1, obj1);
580 
581   MyObject obj2(2);
582   map.Add(kBrowserId1, 2, obj2);
583 
584   MyObject obj3(3);
585   map.Add(kBrowserId2, 3, obj3);
586 
587   MyObject obj4(4);
588   map.Add(kBrowserId2, 4, obj4);
589 
590   EXPECT_EQ(4U, map.size());
591   EXPECT_EQ(2U, map.size(kBrowserId1));
592   EXPECT_EQ(2U, map.size(kBrowserId2));
593 
594   // should only visit browser1 objects
595   MyVisitor visitor(true);
596   map.FindAll(kBrowserId1, &visitor);
597   EXPECT_EQ(2U, visitor.info_size());
598   EXPECT_TRUE(visitor.Exists(kBrowserId1, 1, obj1));
599   EXPECT_TRUE(visitor.Exists(kBrowserId1, 2, obj2));
600   // should be no unexpected visits
601   EXPECT_EQ(0U, visitor.info_size());
602 
603   EXPECT_EQ(2U, map.size());
604   EXPECT_EQ(0U, map.size(kBrowserId1));
605   EXPECT_EQ(2U, map.size(kBrowserId2));
606 
607   // browser1 objects shouldn't exist
608   MyObject nf1 = map.Find(kBrowserId1, 1, nullptr);
609   EXPECT_EQ(0, nf1.member);
610   MyObject nf2 = map.Find(kBrowserId1, 2, nullptr);
611   EXPECT_EQ(0, nf2.member);
612 
613   // browser 2 objects should exist
614   MyObject f3 = map.Find(kBrowserId2, 3, nullptr);
615   EXPECT_EQ(obj3.member, f3.member);
616   MyObject f4 = map.Find(kBrowserId2, 4, nullptr);
617   EXPECT_EQ(obj4.member, f4.member);
618 
619   EXPECT_EQ(0, g_destruct_ct);
620   map.clear();
621   // should destruct the remaining 2 objects
622   EXPECT_EQ(2, g_destruct_ct);
623 }
624 
TEST(BrowserInfoMapTest,FindAllAndRemoveOneByBrowser)625 TEST(BrowserInfoMapTest, FindAllAndRemoveOneByBrowser) {
626   MyObjectMap map;
627   const int kBrowserId1 = 1;
628   const int kBrowserId2 = 2;
629 
630   g_destruct_ct = 0;
631 
632   MyObject obj1(1);
633   map.Add(kBrowserId1, 1, obj1);
634 
635   MyObject obj2(2);
636   map.Add(kBrowserId1, 2, obj2);
637 
638   MyObject obj3(3);
639   map.Add(kBrowserId2, 3, obj3);
640 
641   MyObject obj4(4);
642   map.Add(kBrowserId2, 4, obj4);
643 
644   EXPECT_EQ(4U, map.size());
645   EXPECT_EQ(2U, map.size(kBrowserId1));
646   EXPECT_EQ(2U, map.size(kBrowserId2));
647 
648   // should visit browser2 objects and remove one
649   MyVisitor visitor(false, kBrowserId2, 4);
650   map.FindAll(kBrowserId2, &visitor);
651   EXPECT_EQ(2U, visitor.info_size());
652   EXPECT_TRUE(visitor.Exists(kBrowserId2, 3, obj3));
653   EXPECT_TRUE(visitor.Exists(kBrowserId2, 4, obj4));
654   // should be no unexpected visits
655   EXPECT_EQ(0U, visitor.info_size());
656 
657   EXPECT_EQ(3U, map.size());
658   EXPECT_EQ(2U, map.size(kBrowserId1));
659   EXPECT_EQ(1U, map.size(kBrowserId2));
660 
661   // removed object shouldn't exist
662   MyObject nf4 = map.Find(kBrowserId2, 4, nullptr);
663   EXPECT_EQ(0, nf4.member);
664 
665   // other objects should exist
666   MyObject f1 = map.Find(kBrowserId1, 1, nullptr);
667   EXPECT_EQ(obj1.member, f1.member);
668   MyObject f2 = map.Find(kBrowserId1, 2, nullptr);
669   EXPECT_EQ(obj2.member, f2.member);
670   MyObject f3 = map.Find(kBrowserId2, 3, nullptr);
671   EXPECT_EQ(obj3.member, f3.member);
672 
673   EXPECT_EQ(0, g_destruct_ct);
674   map.clear();
675   // should destruct the remaining 3 objects
676   EXPECT_EQ(3, g_destruct_ct);
677 }
678 
679 namespace {
680 
681 class MyHeapObject {
682  public:
MyHeapObject(int * destroy_ct)683   MyHeapObject(int* destroy_ct) : destroy_ct_(destroy_ct) {}
~MyHeapObject()684   ~MyHeapObject() { (*destroy_ct_)++; }
685 
686  private:
687   int* destroy_ct_;
688 };
689 
690 }  // namespace
691 
TEST(BrowserInfoMapTest,DefaultTraits)692 TEST(BrowserInfoMapTest, DefaultTraits) {
693   CefBrowserInfoMap<int, MyHeapObject*> map;
694 
695   int destroy_ct = 0;
696   map.Add(1, 1, new MyHeapObject(&destroy_ct));
697   map.Add(1, 2, new MyHeapObject(&destroy_ct));
698   map.Add(2, 1, new MyHeapObject(&destroy_ct));
699   map.Add(2, 2, new MyHeapObject(&destroy_ct));
700   map.Add(3, 1, new MyHeapObject(&destroy_ct));
701   map.Add(3, 2, new MyHeapObject(&destroy_ct));
702 
703   EXPECT_EQ(6U, map.size());
704 
705   map.clear(1);
706   EXPECT_EQ(4U, map.size());
707   EXPECT_EQ(2, destroy_ct);
708 
709   map.clear();
710   EXPECT_EQ(0U, map.size());
711   EXPECT_EQ(6, destroy_ct);
712 }
713