You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/gunit.h"
  28. #include "talk/base/referencecountedsingletonfactory.h"
  29. namespace talk_base {
  30. class MyExistenceWatcher {
  31. public:
  32. MyExistenceWatcher() { create_called_ = true; }
  33. ~MyExistenceWatcher() { delete_called_ = true; }
  34. static bool create_called_;
  35. static bool delete_called_;
  36. };
  37. bool MyExistenceWatcher::create_called_ = false;
  38. bool MyExistenceWatcher::delete_called_ = false;
  39. class TestReferenceCountedSingletonFactory :
  40. public ReferenceCountedSingletonFactory<MyExistenceWatcher> {
  41. protected:
  42. virtual bool SetupInstance() {
  43. instance_.reset(new MyExistenceWatcher());
  44. return true;
  45. }
  46. virtual void CleanupInstance() {
  47. instance_.reset();
  48. }
  49. };
  50. static void DoCreateAndGoOutOfScope(
  51. ReferenceCountedSingletonFactory<MyExistenceWatcher> *factory) {
  52. rcsf_ptr<MyExistenceWatcher> ptr(factory);
  53. ptr.get();
  54. // and now ptr should go out of scope.
  55. }
  56. TEST(ReferenceCountedSingletonFactory, ZeroReferenceCountCausesDeletion) {
  57. TestReferenceCountedSingletonFactory factory;
  58. MyExistenceWatcher::delete_called_ = false;
  59. DoCreateAndGoOutOfScope(&factory);
  60. EXPECT_TRUE(MyExistenceWatcher::delete_called_);
  61. }
  62. TEST(ReferenceCountedSingletonFactory, NonZeroReferenceCountDoesNotDelete) {
  63. TestReferenceCountedSingletonFactory factory;
  64. rcsf_ptr<MyExistenceWatcher> ptr(&factory);
  65. ptr.get();
  66. MyExistenceWatcher::delete_called_ = false;
  67. DoCreateAndGoOutOfScope(&factory);
  68. EXPECT_FALSE(MyExistenceWatcher::delete_called_);
  69. }
  70. TEST(ReferenceCountedSingletonFactory, ReturnedPointersReferToSameThing) {
  71. TestReferenceCountedSingletonFactory factory;
  72. rcsf_ptr<MyExistenceWatcher> one(&factory), two(&factory);
  73. EXPECT_EQ(one.get(), two.get());
  74. }
  75. TEST(ReferenceCountedSingletonFactory, Release) {
  76. TestReferenceCountedSingletonFactory factory;
  77. rcsf_ptr<MyExistenceWatcher> one(&factory);
  78. one.get();
  79. MyExistenceWatcher::delete_called_ = false;
  80. one.release();
  81. EXPECT_TRUE(MyExistenceWatcher::delete_called_);
  82. }
  83. TEST(ReferenceCountedSingletonFactory, GetWithoutRelease) {
  84. TestReferenceCountedSingletonFactory factory;
  85. rcsf_ptr<MyExistenceWatcher> one(&factory);
  86. one.get();
  87. MyExistenceWatcher::create_called_ = false;
  88. one.get();
  89. EXPECT_FALSE(MyExistenceWatcher::create_called_);
  90. }
  91. TEST(ReferenceCountedSingletonFactory, GetAfterRelease) {
  92. TestReferenceCountedSingletonFactory factory;
  93. rcsf_ptr<MyExistenceWatcher> one(&factory);
  94. MyExistenceWatcher::create_called_ = false;
  95. one.release();
  96. one.get();
  97. EXPECT_TRUE(MyExistenceWatcher::create_called_);
  98. }
  99. TEST(ReferenceCountedSingletonFactory, MultipleReleases) {
  100. TestReferenceCountedSingletonFactory factory;
  101. rcsf_ptr<MyExistenceWatcher> one(&factory), two(&factory);
  102. MyExistenceWatcher::create_called_ = false;
  103. MyExistenceWatcher::delete_called_ = false;
  104. one.release();
  105. EXPECT_FALSE(MyExistenceWatcher::delete_called_);
  106. one.release();
  107. EXPECT_FALSE(MyExistenceWatcher::delete_called_);
  108. one.release();
  109. EXPECT_FALSE(MyExistenceWatcher::delete_called_);
  110. one.get();
  111. EXPECT_TRUE(MyExistenceWatcher::create_called_);
  112. }
  113. TEST(ReferenceCountedSingletonFactory, Existentialism) {
  114. TestReferenceCountedSingletonFactory factory;
  115. rcsf_ptr<MyExistenceWatcher> one(&factory);
  116. MyExistenceWatcher::create_called_ = false;
  117. MyExistenceWatcher::delete_called_ = false;
  118. one.get();
  119. EXPECT_TRUE(MyExistenceWatcher::create_called_);
  120. one.release();
  121. EXPECT_TRUE(MyExistenceWatcher::delete_called_);
  122. }
  123. } // namespace talk_base