1 /*
2 * Copyright (c) 2023 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
16 #include <gtest/gtest.h>
17
18 #include "setting_observer.h"
19
20 using namespace testing;
21 using namespace testing::ext;
22
23 using UpdateFunc = std::function<void(const std::string&)>;
24
25 namespace OHOS {
26 namespace Rosen {
27 class SettingObserverTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 void SetUp() override;
32 void TearDown() override;
33 sptr<SettingObserver> settingObserver_ = nullptr;
34 static constexpr uint32_t WAIT_SYNC_IN_NS = 500000;
35 };
36
SetUpTestCase()37 void SettingObserverTest::SetUpTestCase()
38 {
39 }
40
TearDownTestCase()41 void SettingObserverTest::TearDownTestCase()
42 {
43 }
44
SetUp()45 void SettingObserverTest::SetUp()
46 {
47 settingObserver_ = new SettingObserver();
48 }
49
TearDown()50 void SettingObserverTest::TearDown()
51 {
52 settingObserver_ = nullptr;
53 usleep(WAIT_SYNC_IN_NS);
54 }
55
56 namespace {
57 /**
58 * @tc.name: SetUpdateFunc
59 * @tc.desc: test function : SetUpdateFunc
60 * @tc.type: FUNC
61 */
62 HWTEST_F(SettingObserverTest, SetUpdateFunc, Function | SmallTest | Level1)
63 {
__anon408bcdd90202(const std::string& key) 64 UpdateFunc func = [this](const std::string& key) {
65 settingObserver_->SetKey(key);
66 };
67 EXPECT_EQ(nullptr, settingObserver_->update_);
68 settingObserver_->SetUpdateFunc(func);
69 EXPECT_NE(nullptr, settingObserver_->update_);
70 }
71
72 /**
73 * @tc.name: SetKey
74 * @tc.desc: test function : SetKey
75 * @tc.type: FUNC
76 */
77 HWTEST_F(SettingObserverTest, SetKey, Function | SmallTest | Level1)
78 {
79 const char* s = "";
80 EXPECT_EQ(settingObserver_->key_, s);
81 settingObserver_->SetKey("SetKey");
82 std::string res = settingObserver_->GetKey();
83 EXPECT_EQ(res, settingObserver_->key_);
84 }
85
86 /**
87 * @tc.name: OnChange
88 * @tc.desc: test function : OnChange
89 * @tc.type: FUNC
90 */
91 HWTEST_F(SettingObserverTest, OnChange, Function | SmallTest | Level1)
92 {
93 EXPECT_EQ(settingObserver_->update_, nullptr);
94 settingObserver_->SetKey("OnChange");
95 settingObserver_->OnChange();
96
__anon408bcdd90302(const std::string& key) 97 UpdateFunc func = [this](const std::string& key) {
98 settingObserver_->SetKey(key);
99 };
100 settingObserver_->SetUpdateFunc(func);
101 EXPECT_NE(nullptr, settingObserver_->update_);
102 settingObserver_->SetKey("OnChange");
103 settingObserver_->OnChange();
104 EXPECT_EQ("OnChange", settingObserver_->key_);
105 }
106 }
107 }
108 }