1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "common/observer_registry.h"
18 
19 #include "common/bind.h"
20 #include "gtest/gtest.h"
21 
22 namespace bluetooth {
23 namespace common {
24 
25 class SingleObserverRegistryTest : public ::testing::Test {
26  protected:
SetUp()27   void SetUp() override {
28     registry_ = new SingleObserverRegistry;
29   }
30 
TearDown()31   void TearDown() override {
32     delete registry_;
33   }
34 
35   SingleObserverRegistry* registry_;
36 };
37 
Increment(int * count)38 void Increment(int* count) {
39   (*count)++;
40 }
41 
IncrementBy(int * count,int n)42 void IncrementBy(int* count, int n) {
43   (*count) += n;
44 }
45 
TEST_F(SingleObserverRegistryTest,wrapped_callback)46 TEST_F(SingleObserverRegistryTest, wrapped_callback) {
47   int count = 0;
48   auto wrapped_callback = registry_->Register(Bind(&Increment, Unretained(&count)));
49   wrapped_callback.Run();
50   EXPECT_EQ(count, 1);
51   wrapped_callback.Run();
52   EXPECT_EQ(count, 2);
53   wrapped_callback.Run();
54   EXPECT_EQ(count, 3);
55   registry_->Unregister();
56 }
57 
TEST_F(SingleObserverRegistryTest,unregister)58 TEST_F(SingleObserverRegistryTest, unregister) {
59   int count = 0;
60   auto wrapped_callback = registry_->Register(Bind(&Increment, Unretained(&count)));
61   registry_->Unregister();
62   wrapped_callback.Run();
63   EXPECT_EQ(count, 0);
64 }
65 
TEST_F(SingleObserverRegistryTest,second_register)66 TEST_F(SingleObserverRegistryTest, second_register) {
67   int count = 0;
68   auto wrapped_callback = registry_->Register(Bind(&Increment, Unretained(&count)));
69   registry_->Unregister();
70   auto wrapped_callback2 = registry_->Register(Bind(&Increment, Unretained(&count)));
71   wrapped_callback.Run();
72   EXPECT_EQ(count, 0);
73   wrapped_callback2.Run();
74   EXPECT_EQ(count, 1);
75 }
76 
77 class MultipleObserverRegistryTest : public ::testing::Test {
78  protected:
SetUp()79   void SetUp() override {
80     registry_ = new MultipleObserverRegistry<2>;
81   }
82 
TearDown()83   void TearDown() override {
84     delete registry_;
85   }
86 
87   MultipleObserverRegistry<2>* registry_;
88 };
89 
TEST_F(MultipleObserverRegistryTest,single_wrapped_callback)90 TEST_F(MultipleObserverRegistryTest, single_wrapped_callback) {
91   int count = 0;
92   auto wrapped_callback = registry_->Register(0, Bind(&Increment, Unretained(&count)));
93   wrapped_callback.Run();
94   EXPECT_EQ(count, 1);
95   wrapped_callback.Run();
96   EXPECT_EQ(count, 2);
97   wrapped_callback.Run();
98   EXPECT_EQ(count, 3);
99   registry_->Unregister(0);
100 }
101 
TEST_F(MultipleObserverRegistryTest,multiple_wrapped_callback)102 TEST_F(MultipleObserverRegistryTest, multiple_wrapped_callback) {
103   int count = 0;
104   auto wrapped_callback0 = registry_->Register(0, Bind(&Increment, Unretained(&count)));
105   auto wrapped_callback1 = registry_->Register(1, Bind(&IncrementBy, Unretained(&count), 10));
106   wrapped_callback0.Run();
107   EXPECT_EQ(count, 1);
108   wrapped_callback1.Run();
109   EXPECT_EQ(count, 11);
110   registry_->Unregister(0);
111   wrapped_callback0.Run();
112   EXPECT_EQ(count, 11);
113   wrapped_callback1.Run();
114   EXPECT_EQ(count, 21);
115   registry_->Unregister(1);
116   EXPECT_EQ(count, 21);
117 }
118 
119 }  // namespace common
120 }  // namespace bluetooth
121