• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2020 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 package org.webrtc;
12 
13 import static com.google.common.truth.Truth.assertThat;
14 import static org.mockito.Mockito.never;
15 import static org.mockito.Mockito.verify;
16 
17 import androidx.test.runner.AndroidJUnit4;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.robolectric.annotation.Config;
24 
25 @RunWith(AndroidJUnit4.class)
26 @Config(manifest = Config.NONE)
27 public class RefCountDelegateTest {
28   @Mock Runnable mockReleaseCallback;
29   private RefCountDelegate refCountDelegate;
30 
31   @Before
setUp()32   public void setUp() {
33     MockitoAnnotations.initMocks(this);
34 
35     refCountDelegate = new RefCountDelegate(mockReleaseCallback);
36   }
37 
38   @Test
testReleaseRunsReleaseCallback()39   public void testReleaseRunsReleaseCallback() {
40     refCountDelegate.release();
41     verify(mockReleaseCallback).run();
42   }
43 
44   @Test
testRetainIncreasesRefCount()45   public void testRetainIncreasesRefCount() {
46     refCountDelegate.retain();
47 
48     refCountDelegate.release();
49     verify(mockReleaseCallback, never()).run();
50 
51     refCountDelegate.release();
52     verify(mockReleaseCallback).run();
53   }
54 
55   @Test(expected = IllegalStateException.class)
testReleaseAfterFreeThrowsIllegalStateException()56   public void testReleaseAfterFreeThrowsIllegalStateException() {
57     refCountDelegate.release();
58     refCountDelegate.release();
59   }
60 
61   @Test(expected = IllegalStateException.class)
testRetainAfterFreeThrowsIllegalStateException()62   public void testRetainAfterFreeThrowsIllegalStateException() {
63     refCountDelegate.release();
64     refCountDelegate.retain();
65   }
66 
67   @Test
testSafeRetainBeforeFreeReturnsTrueAndIncreasesRefCount()68   public void testSafeRetainBeforeFreeReturnsTrueAndIncreasesRefCount() {
69     assertThat(refCountDelegate.safeRetain()).isTrue();
70 
71     refCountDelegate.release();
72     verify(mockReleaseCallback, never()).run();
73 
74     refCountDelegate.release();
75     verify(mockReleaseCallback).run();
76   }
77 
78   @Test
testSafeRetainAfterFreeReturnsFalse()79   public void testSafeRetainAfterFreeReturnsFalse() {
80     refCountDelegate.release();
81     assertThat(refCountDelegate.safeRetain()).isFalse();
82   }
83 }
84