13f4cbf05Sopenharmony_ci/*
23f4cbf05Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License.
53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at
63f4cbf05Sopenharmony_ci *
73f4cbf05Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83f4cbf05Sopenharmony_ci *
93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and
133f4cbf05Sopenharmony_ci * limitations under the License.
143f4cbf05Sopenharmony_ci */
153f4cbf05Sopenharmony_ci#include <gtest/gtest.h>
163f4cbf05Sopenharmony_ci#include "observer.h"
173f4cbf05Sopenharmony_ci#include <algorithm>
183f4cbf05Sopenharmony_ci#include <iostream>
193f4cbf05Sopenharmony_ci#include <fstream>
203f4cbf05Sopenharmony_ciusing namespace testing::ext;
213f4cbf05Sopenharmony_ciusing namespace std;
223f4cbf05Sopenharmony_ci
233f4cbf05Sopenharmony_cinamespace OHOS {
243f4cbf05Sopenharmony_cinamespace {
253f4cbf05Sopenharmony_ciclass BookList: public Observable {
263f4cbf05Sopenharmony_cipublic:
273f4cbf05Sopenharmony_ci    BookList() { books_.clear(); }
283f4cbf05Sopenharmony_ci    void AddBook(const string& book)
293f4cbf05Sopenharmony_ci    {
303f4cbf05Sopenharmony_ci        books_.insert(book);
313f4cbf05Sopenharmony_ci        SetChanged();
323f4cbf05Sopenharmony_ci        NotifyObservers();
333f4cbf05Sopenharmony_ci    }
343f4cbf05Sopenharmony_ci
353f4cbf05Sopenharmony_ci    void RemoveBook(const string& book)
363f4cbf05Sopenharmony_ci    {
373f4cbf05Sopenharmony_ci        books_.erase(book);
383f4cbf05Sopenharmony_ci        SetChanged();
393f4cbf05Sopenharmony_ci        NotifyObservers();
403f4cbf05Sopenharmony_ci    }
413f4cbf05Sopenharmony_ci
423f4cbf05Sopenharmony_ci    void NoChangeNotify()
433f4cbf05Sopenharmony_ci    {
443f4cbf05Sopenharmony_ci        if (HasChanged() == false) {
453f4cbf05Sopenharmony_ci            NotifyObservers();
463f4cbf05Sopenharmony_ci        }
473f4cbf05Sopenharmony_ci    }
483f4cbf05Sopenharmony_ci
493f4cbf05Sopenharmony_ci    const set<string>& GetBooks() { return books_; }
503f4cbf05Sopenharmony_ciprivate:
513f4cbf05Sopenharmony_ci    set<string> books_;
523f4cbf05Sopenharmony_ci
533f4cbf05Sopenharmony_ci};
543f4cbf05Sopenharmony_ci
553f4cbf05Sopenharmony_ciclass BookObserver: public Observer {
563f4cbf05Sopenharmony_cipublic:
573f4cbf05Sopenharmony_ci    virtual void Update(const Observable* o, const ObserverArg* /* arg */)
583f4cbf05Sopenharmony_ci    {
593f4cbf05Sopenharmony_ci        BookList* bookList = reinterpret_cast<BookList*>(const_cast<Observable*>(o));
603f4cbf05Sopenharmony_ci        books_ = bookList->GetBooks();
613f4cbf05Sopenharmony_ci    }
623f4cbf05Sopenharmony_ci
633f4cbf05Sopenharmony_ci    int GetBooksCount() { return static_cast<int>(books_.size()); }
643f4cbf05Sopenharmony_ci    bool BookExists(const string& book) { return books_.count(book) > 0;}
653f4cbf05Sopenharmony_ciprivate:
663f4cbf05Sopenharmony_ci    set<string> books_;
673f4cbf05Sopenharmony_ci};
683f4cbf05Sopenharmony_ci
693f4cbf05Sopenharmony_ci
703f4cbf05Sopenharmony_ciclass UtilsObserverTest : public testing::Test {
713f4cbf05Sopenharmony_cipublic :
723f4cbf05Sopenharmony_ci    static void SetUpTestCase(void);
733f4cbf05Sopenharmony_ci    static void TearDownTestCase(void);
743f4cbf05Sopenharmony_ci    void SetUp();
753f4cbf05Sopenharmony_ci    void TearDown();
763f4cbf05Sopenharmony_ci};
773f4cbf05Sopenharmony_ci
783f4cbf05Sopenharmony_civoid UtilsObserverTest::SetUpTestCase(void)
793f4cbf05Sopenharmony_ci{
803f4cbf05Sopenharmony_ci}
813f4cbf05Sopenharmony_ci
823f4cbf05Sopenharmony_civoid UtilsObserverTest::TearDownTestCase(void)
833f4cbf05Sopenharmony_ci{
843f4cbf05Sopenharmony_ci}
853f4cbf05Sopenharmony_ci
863f4cbf05Sopenharmony_civoid UtilsObserverTest::SetUp(void)
873f4cbf05Sopenharmony_ci{
883f4cbf05Sopenharmony_ci}
893f4cbf05Sopenharmony_ci
903f4cbf05Sopenharmony_civoid UtilsObserverTest::TearDown(void)
913f4cbf05Sopenharmony_ci{
923f4cbf05Sopenharmony_ci}
933f4cbf05Sopenharmony_ci
943f4cbf05Sopenharmony_ci/*
953f4cbf05Sopenharmony_ci * @tc.name: test_Observer
963f4cbf05Sopenharmony_ci * @tc.desc: Test add null or repeat observer to the observable object.
973f4cbf05Sopenharmony_ci */
983f4cbf05Sopenharmony_ciHWTEST_F(UtilsObserverTest, test_Observer, TestSize.Level0)
993f4cbf05Sopenharmony_ci{
1003f4cbf05Sopenharmony_ci    BookList bookList;
1013f4cbf05Sopenharmony_ci    bookList.AddObserver(nullptr);
1023f4cbf05Sopenharmony_ci    shared_ptr<BookObserver> bookObserver1 = make_shared<BookObserver>();
1033f4cbf05Sopenharmony_ci    bookList.AddObserver(bookObserver1);
1043f4cbf05Sopenharmony_ci    bookList.AddObserver(bookObserver1);
1053f4cbf05Sopenharmony_ci    bookList.NoChangeNotify();
1063f4cbf05Sopenharmony_ci    int ret = bookList.GetObserversCount();
1073f4cbf05Sopenharmony_ci    EXPECT_EQ(ret, 1);
1083f4cbf05Sopenharmony_ci}
1093f4cbf05Sopenharmony_ci
1103f4cbf05Sopenharmony_ciHWTEST_F(UtilsObserverTest, test_ObserverNotify, TestSize.Level0)
1113f4cbf05Sopenharmony_ci{
1123f4cbf05Sopenharmony_ci    BookList bookList;
1133f4cbf05Sopenharmony_ci    shared_ptr<BookObserver> bookObserver1 = make_shared<BookObserver>();
1143f4cbf05Sopenharmony_ci    shared_ptr<BookObserver> bookObserver2 = make_shared<BookObserver>();
1153f4cbf05Sopenharmony_ci    shared_ptr<BookObserver> bookObserver3 = make_shared<BookObserver>();
1163f4cbf05Sopenharmony_ci
1173f4cbf05Sopenharmony_ci    bookList.AddObserver(bookObserver1);
1183f4cbf05Sopenharmony_ci    bookList.AddObserver(bookObserver2);
1193f4cbf05Sopenharmony_ci    bookList.AddObserver(bookObserver3);
1203f4cbf05Sopenharmony_ci    bookList.AddBook("book1");
1213f4cbf05Sopenharmony_ci
1223f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
1233f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver2->GetBooksCount(), 1);
1243f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver3->GetBooksCount(), 1);
1253f4cbf05Sopenharmony_ci
1263f4cbf05Sopenharmony_ci    bookList.RemoveObserver(bookObserver1);
1273f4cbf05Sopenharmony_ci    bookList.RemoveBook("book1");
1283f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
1293f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver2->GetBooksCount(), 0);
1303f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver3->GetBooksCount(), 0);
1313f4cbf05Sopenharmony_ci
1323f4cbf05Sopenharmony_ci    bookList.RemoveObserver(bookObserver2);
1333f4cbf05Sopenharmony_ci    bookList.AddBook("book2");
1343f4cbf05Sopenharmony_ci    bookList.AddBook("book3");
1353f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
1363f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver2->GetBooksCount(), 0);
1373f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver3->GetBooksCount(), 2);
1383f4cbf05Sopenharmony_ci
1393f4cbf05Sopenharmony_ci}
1403f4cbf05Sopenharmony_ci
1413f4cbf05Sopenharmony_ci
1423f4cbf05Sopenharmony_ciHWTEST_F(UtilsObserverTest, test_RemoveAllObserver, TestSize.Level0)
1433f4cbf05Sopenharmony_ci{
1443f4cbf05Sopenharmony_ci    BookList bookList;
1453f4cbf05Sopenharmony_ci    shared_ptr<BookObserver> bookObserver1 = make_shared<BookObserver>();
1463f4cbf05Sopenharmony_ci    shared_ptr<BookObserver> bookObserver2 = make_shared<BookObserver>();
1473f4cbf05Sopenharmony_ci    shared_ptr<BookObserver> bookObserver3 = make_shared<BookObserver>();
1483f4cbf05Sopenharmony_ci
1493f4cbf05Sopenharmony_ci    bookList.AddObserver(bookObserver1);
1503f4cbf05Sopenharmony_ci    bookList.AddObserver(bookObserver2);
1513f4cbf05Sopenharmony_ci    bookList.AddObserver(bookObserver3);
1523f4cbf05Sopenharmony_ci    bookList.AddBook("book1");
1533f4cbf05Sopenharmony_ci
1543f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
1553f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver2->GetBooksCount(), 1);
1563f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver3->GetBooksCount(), 1);
1573f4cbf05Sopenharmony_ci
1583f4cbf05Sopenharmony_ci    bookList.RemoveAllObservers();
1593f4cbf05Sopenharmony_ci    bookList.RemoveBook("book1");
1603f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver1->GetBooksCount(), 1);
1613f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver2->GetBooksCount(), 1);
1623f4cbf05Sopenharmony_ci    EXPECT_EQ(bookObserver3->GetBooksCount(), 1);
1633f4cbf05Sopenharmony_ci    EXPECT_EQ(bookList.GetObserversCount(), 0);
1643f4cbf05Sopenharmony_ci}
1653f4cbf05Sopenharmony_ci}  // namespace
1663f4cbf05Sopenharmony_ci}  // namespace OHOS