1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <gtest/gtest.h>
16 #include "observer.h"
17 #include <algorithm>
18 #include <iostream>
19 #include <fstream>
20 using namespace testing::ext;
21 using namespace std;
22
23 namespace OHOS {
24 namespace {
25 class BookList: public Observable {
26 public:
BookList()27 BookList() { books_.clear(); }
AddBook(const string & book)28 void AddBook(const string& book)
29 {
30 books_.insert(book);
31 SetChanged();
32 NotifyObservers();
33 }
34
RemoveBook(const string & book)35 void RemoveBook(const string& book)
36 {
37 books_.erase(book);
38 SetChanged();
39 NotifyObservers();
40 }
41
NoChangeNotify()42 void NoChangeNotify()
43 {
44 if (HasChanged() == false) {
45 NotifyObservers();
46 }
47 }
48
GetBooks()49 const set<string>& GetBooks() { return books_; }
50 private:
51 set<string> books_;
52
53 };
54
55 class BookObserver: public Observer {
56 public:
Update(const Observable * o,const ObserverArg *)57 virtual void Update(const Observable* o, const ObserverArg* /* arg */)
58 {
59 BookList* bookList = reinterpret_cast<BookList*>(const_cast<Observable*>(o));
60 books_ = bookList->GetBooks();
61 }
62
GetBooksCount()63 int GetBooksCount() { return static_cast<int>(books_.size()); }
BookExists(const string & book)64 bool BookExists(const string& book) { return books_.count(book) > 0;}
65 private:
66 set<string> books_;
67 };
68
69
70 class UtilsObserverTest : public testing::Test {
71 public :
72 static void SetUpTestCase(void);
73 static void TearDownTestCase(void);
74 void SetUp();
75 void TearDown();
76 };
77
SetUpTestCase(void)78 void UtilsObserverTest::SetUpTestCase(void)
79 {
80 }
81
TearDownTestCase(void)82 void UtilsObserverTest::TearDownTestCase(void)
83 {
84 }
85
SetUp(void)86 void UtilsObserverTest::SetUp(void)
87 {
88 }
89
TearDown(void)90 void UtilsObserverTest::TearDown(void)
91 {
92 }
93
94 /*
95 * @tc.name: test_Observer
96 * @tc.desc: Test add null or repeat observer to the observable object.
97 */
98 HWTEST_F(UtilsObserverTest, test_Observer, TestSize.Level0)
99 {
100 BookList bookList;
101 bookList.AddObserver(nullptr);
102 shared_ptr<BookObserver> bookObserver1 = make_shared<BookObserver>();
103 bookList.AddObserver(bookObserver1);
104 bookList.AddObserver(bookObserver1);
105 bookList.NoChangeNotify();
106 int ret = bookList.GetObserversCount();
107 EXPECT_EQ(ret, 1);
108 }
109
110 HWTEST_F(UtilsObserverTest, test_ObserverNotify, TestSize.Level0)
111 {
112 BookList bookList;
113 shared_ptr<BookObserver> bookObserver1 = make_shared<BookObserver>();
114 shared_ptr<BookObserver> bookObserver2 = make_shared<BookObserver>();
115 shared_ptr<BookObserver> bookObserver3 = make_shared<BookObserver>();
116
117 bookList.AddObserver(bookObserver1);
118 bookList.AddObserver(bookObserver2);
119 bookList.AddObserver(bookObserver3);
120 bookList.AddBook("book1");
121
122 EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
123 EXPECT_EQ(bookObserver2->GetBooksCount(), 1);
124 EXPECT_EQ(bookObserver3->GetBooksCount(), 1);
125
126 bookList.RemoveObserver(bookObserver1);
127 bookList.RemoveBook("book1");
128 EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
129 EXPECT_EQ(bookObserver2->GetBooksCount(), 0);
130 EXPECT_EQ(bookObserver3->GetBooksCount(), 0);
131
132 bookList.RemoveObserver(bookObserver2);
133 bookList.AddBook("book2");
134 bookList.AddBook("book3");
135 EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
136 EXPECT_EQ(bookObserver2->GetBooksCount(), 0);
137 EXPECT_EQ(bookObserver3->GetBooksCount(), 2);
138
139 }
140
141
142 HWTEST_F(UtilsObserverTest, test_RemoveAllObserver, TestSize.Level0)
143 {
144 BookList bookList;
145 shared_ptr<BookObserver> bookObserver1 = make_shared<BookObserver>();
146 shared_ptr<BookObserver> bookObserver2 = make_shared<BookObserver>();
147 shared_ptr<BookObserver> bookObserver3 = make_shared<BookObserver>();
148
149 bookList.AddObserver(bookObserver1);
150 bookList.AddObserver(bookObserver2);
151 bookList.AddObserver(bookObserver3);
152 bookList.AddBook("book1");
153
154 EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
155 EXPECT_EQ(bookObserver2->GetBooksCount(), 1);
156 EXPECT_EQ(bookObserver3->GetBooksCount(), 1);
157
158 bookList.RemoveAllObservers();
159 bookList.RemoveBook("book1");
160 EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
161 EXPECT_EQ(bookObserver2->GetBooksCount(), 1);
162 EXPECT_EQ(bookObserver3->GetBooksCount(), 1);
163 EXPECT_EQ(bookList.GetObserversCount(), 0);
164 }
165 } // namespace
166 } // namespace OHOS