• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #define private public
18 #include "advanced_datashare_observer.h"
19 #undef private
20 #include "advanced_aggregation_data_roaming_observer.h"
21 #include "system_event_observer.h"
22 
23 extern void MockGetSystemAbilityManager(bool mockRet);
24 
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace Notification {
29 
30 // Test suite
31 class AdvancedDatashareObserverTest : public ::testing::Test {
32 public:
SetUpTestCase()33     static void SetUpTestCase() {}
TearDownTestCase()34     static void TearDownTestCase() {}
SetUp()35     void SetUp() override {}
TearDown()36     void TearDown() override {}
37 };
38 
39 // Test cases
40 HWTEST_F(AdvancedDatashareObserverTest, SystemAbilityManagerIsNullptr, Function | SmallTest | Level1)
41 {
42     // Arrange
43     MockGetSystemAbilityManager(true);
44 
45     // Act
46     auto result = AdvancedDatashareObserver::GetInstance().CreateDataShareHelper();
47     sptr<AdvancedAggregationDataRoamingObserver> aggregationRoamingObserver =
48         new (std::nothrow) AdvancedAggregationDataRoamingObserver();
49     Uri dataEnableUri("xxxx");
50     AdvancedDatashareObserver::GetInstance().RegisterSettingsObserver(dataEnableUri, aggregationRoamingObserver);
51     AdvancedDatashareObserver::GetInstance().UnRegisterSettingsObserver(dataEnableUri, aggregationRoamingObserver);
52 
53     // Assert
54     EXPECT_EQ(result, nullptr);
55 }
56 
57 HWTEST_F(AdvancedDatashareObserverTest, DataShareHelperIsCreatedSuccessfully, Function | SmallTest | Level1)
58 {
59     // Arrange
60     MockGetSystemAbilityManager(false);
61 
62     // Act
63     auto result = AdvancedDatashareObserver::GetInstance().CreateDataShareHelper();
64     sptr<AdvancedAggregationDataRoamingObserver> aggregationRoamingObserver =
65         new (std::nothrow) AdvancedAggregationDataRoamingObserver();
66     Uri dataEnableUri("xxxx");
67     AdvancedDatashareObserver::GetInstance().RegisterSettingsObserver(dataEnableUri, aggregationRoamingObserver);
68     AdvancedDatashareObserver::GetInstance().UnRegisterSettingsObserver(dataEnableUri, aggregationRoamingObserver);
69 
70     // Assert
71     EXPECT_NE(result, nullptr);
72 }
73 
74 
75 // Test cases
76 HWTEST_F(AdvancedDatashareObserverTest, DataShareReady, Function | SmallTest | Level1) {
77     // Arrange
78     AdvancedDatashareObserver observer;
79     observer.isDataShareReady_ = true;
80 
81     // Act
82     bool result = observer.CheckIfSettingsDataReady();
83 
84     // Assert
85     EXPECT_TRUE(result);
86 }
87 
88 HWTEST_F(AdvancedDatashareObserverTest, SystemAbilityManagerNull, Function | SmallTest | Level1)
89 {
90     // Arrange
91     AdvancedDatashareObserver observer;
92     observer.isDataShareReady_ = false;
93     // Mock SystemAbilityManagerClient to return nullptr
94     MockGetSystemAbilityManager(false);
95 
96     // Act
97     bool result = observer.CheckIfSettingsDataReady();
98 
99     // Assert
100     EXPECT_TRUE(result);
101 }
102 
103 HWTEST_F(AdvancedDatashareObserverTest, DataShareHelperNotReady, Function | SmallTest | Level1)
104 {
105     // Arrange
106     AdvancedDatashareObserver observer;
107     observer.isDataShareReady_ = false;
108     MockGetSystemAbilityManager(true);
109 
110     // Act
111     bool result = observer.CheckIfSettingsDataReady();
112 
113     // Assert
114     EXPECT_FALSE(result);
115 }
116 }
117 }