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
GetBooks()42 const set<string>& GetBooks() { return books_; }
43 private:
44 set<string> books_;
45
46 };
47
48 class BookObserver: public Observer {
49 public:
Update(const Observable * o,const ObserverArg *)50 virtual void Update(const Observable* o, const ObserverArg* /* arg */)
51 {
52 BookList* bookList = reinterpret_cast<BookList*>(const_cast<Observable*>(o));
53 books_ = bookList->GetBooks();
54 }
55
GetBooksCount()56 int GetBooksCount() { return static_cast<int>(books_.size()); }
BookExists(const string & book)57 bool BookExists(const string& book) { return books_.count(book) > 0;}
58 private:
59 set<string> books_;
60 };
61
62
63 class UtilsObserverTest : public testing::Test {
64 public :
65 static void SetUpTestCase(void);
66 static void TearDownTestCase(void);
67 void SetUp();
68 void TearDown();
69 };
70
SetUpTestCase(void)71 void UtilsObserverTest::SetUpTestCase(void)
72 {
73 }
74
TearDownTestCase(void)75 void UtilsObserverTest::TearDownTestCase(void)
76 {
77 }
78
SetUp(void)79 void UtilsObserverTest::SetUp(void)
80 {
81 }
82
TearDown(void)83 void UtilsObserverTest::TearDown(void)
84 {
85 }
86
87 HWTEST_F(UtilsObserverTest, test_ObserverNotify, TestSize.Level0)
88 {
89 BookList bookList;
90 shared_ptr<BookObserver> bookObserver1 = make_shared<BookObserver>();
91 shared_ptr<BookObserver> bookObserver2 = make_shared<BookObserver>();
92 shared_ptr<BookObserver> bookObserver3 = make_shared<BookObserver>();
93
94 bookList.AddObserver(bookObserver1);
95 bookList.AddObserver(bookObserver2);
96 bookList.AddObserver(bookObserver3);
97 bookList.AddBook("book1");
98
99 EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
100 EXPECT_EQ(bookObserver2->GetBooksCount(), 1);
101 EXPECT_EQ(bookObserver3->GetBooksCount(), 1);
102
103 bookList.RemoveObserver(bookObserver1);
104 bookList.RemoveBook("book1");
105 EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
106 EXPECT_EQ(bookObserver2->GetBooksCount(), 0);
107 EXPECT_EQ(bookObserver3->GetBooksCount(), 0);
108
109 bookList.RemoveObserver(bookObserver2);
110 bookList.AddBook("book2");
111 bookList.AddBook("book3");
112 EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
113 EXPECT_EQ(bookObserver2->GetBooksCount(), 0);
114 EXPECT_EQ(bookObserver3->GetBooksCount(), 2);
115
116 }
117
118
119 HWTEST_F(UtilsObserverTest, test_RemoveAllObserver, TestSize.Level0)
120 {
121 BookList bookList;
122 shared_ptr<BookObserver> bookObserver1 = make_shared<BookObserver>();
123 shared_ptr<BookObserver> bookObserver2 = make_shared<BookObserver>();
124 shared_ptr<BookObserver> bookObserver3 = make_shared<BookObserver>();
125
126 bookList.AddObserver(bookObserver1);
127 bookList.AddObserver(bookObserver2);
128 bookList.AddObserver(bookObserver3);
129 bookList.AddBook("book1");
130
131 EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
132 EXPECT_EQ(bookObserver2->GetBooksCount(), 1);
133 EXPECT_EQ(bookObserver3->GetBooksCount(), 1);
134
135 bookList.RemoveAllObservers();
136 bookList.RemoveBook("book1");
137 EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
138 EXPECT_EQ(bookObserver2->GetBooksCount(), 1);
139 EXPECT_EQ(bookObserver3->GetBooksCount(), 1);
140 EXPECT_EQ(bookList.GetObserversCount(), 0);
141 }
142 } // namespace
143 } // namespace OHOS