1 /* 2 * Copyright (c) 2024 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 <chrono> 17 #include "gtest/gtest.h" 18 #include "gmock/gmock.h" 19 #include "system_ability_definition.h" 20 #include "system_ability.h" 21 #include "iservice_registry.h" 22 #include "test_server_service.h" 23 #include "mock_permission.h" 24 #include "start_test_server.h" 25 #include "test_server_error_code.h" 26 #include "pasteboard_client.h" 27 #include <common_event_manager.h> 28 #include <common_event_subscribe_info.h> 29 30 using namespace std; 31 using namespace testing::ext; 32 using namespace OHOS; 33 using namespace OHOS::testserver; 34 35 class TestServerServiceMock : public TestServerService { 36 public: TestServerServiceMock(int32_t saId,bool runOnCreate)37 TestServerServiceMock(int32_t saId, bool runOnCreate) : TestServerService(saId, runOnCreate) {} 38 OnStart()39 void OnStart() 40 { 41 TestServerService::OnStart(); 42 } 43 OnStop()44 void OnStop() 45 { 46 TestServerService::OnStop(); 47 } 48 49 MOCK_METHOD0(IsRootVersion, bool()); 50 MOCK_METHOD0(IsDeveloperMode, bool()); 51 DestorySession()52 void DestorySession() 53 { 54 TestServerService::DestorySession(); 55 } 56 RemoveTestServer()57 bool RemoveTestServer() 58 { 59 return TestServerService::RemoveTestServer(); 60 } 61 CreateSessionMock()62 bool CreateSessionMock() 63 { 64 TestServerService::AddCaller(); 65 return true; 66 } 67 GetCallerCount()68 int GetCallerCount() 69 { 70 return TestServerService::GetCallerCount(); 71 } 72 }; 73 74 class ServiceTest : public testing::Test { 75 public: 76 ~ServiceTest() override = default; 77 78 protected: 79 const int32_t SYSTEM_ABILITY_ID = TEST_SERVER_SA_ID; 80 sptr<ISystemAbilityManager> samgr_ = nullptr; 81 unique_ptr<TestServerServiceMock> testServerServiceMock_ = nullptr; 82 const int32_t UNLOAD_SYSTEMABILITY_WAITTIME = 2000; 83 SetUp()84 void SetUp() override 85 { 86 TestServerMockPermission::MockProcess("testserver"); 87 samgr_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 88 testServerServiceMock_ = make_unique<TestServerServiceMock>(SYSTEM_ABILITY_ID, false); 89 } 90 TearDown()91 void TearDown() override 92 { 93 samgr_->UnloadSystemAbility(SYSTEM_ABILITY_ID); 94 samgr_->RemoveSystemAbility(SYSTEM_ABILITY_ID); 95 this_thread::sleep_for(chrono::milliseconds(UNLOAD_SYSTEMABILITY_WAITTIME)); 96 EXPECT_EQ(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 97 testServerServiceMock_.reset(); 98 EXPECT_EQ(testServerServiceMock_, nullptr); 99 } 100 }; 101 102 HWTEST_F(ServiceTest, testOnStartWhenRoot, TestSize.Level1) 103 { 104 EXPECT_EQ(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 105 EXPECT_CALL(*testServerServiceMock_, IsRootVersion()).WillOnce(testing::Return(true)); 106 testServerServiceMock_->OnStart(); 107 EXPECT_NE(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 108 } 109 110 HWTEST_F(ServiceTest, testOnStartWhenUserDeveloper, TestSize.Level1) 111 { 112 EXPECT_EQ(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 113 EXPECT_CALL(*testServerServiceMock_, IsRootVersion()).WillOnce(testing::Return(false)); 114 EXPECT_CALL(*testServerServiceMock_, IsDeveloperMode()).WillOnce(testing::Return(true)); 115 testServerServiceMock_->OnStart(); 116 EXPECT_NE(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 117 } 118 119 HWTEST_F(ServiceTest, testOnStartWhenUserNonDeveloper, TestSize.Level1) 120 { 121 EXPECT_EQ(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 122 EXPECT_CALL(*testServerServiceMock_, IsRootVersion()).WillOnce(testing::Return(false)); 123 EXPECT_CALL(*testServerServiceMock_, IsDeveloperMode()).WillOnce(testing::Return(false)); 124 testServerServiceMock_->OnStart(); 125 EXPECT_EQ(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 126 } 127 128 HWTEST_F(ServiceTest, testRemoveTestServer, TestSize.Level1) 129 { 130 EXPECT_EQ(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 131 sptr<ITestServerInterface> iTestServerInterface = StartTestServer::GetInstance().LoadTestServer(); 132 EXPECT_NE(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 133 EXPECT_TRUE(testServerServiceMock_->RemoveTestServer()); 134 this_thread::sleep_for(chrono::milliseconds(UNLOAD_SYSTEMABILITY_WAITTIME)); 135 EXPECT_EQ(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 136 } 137 138 HWTEST_F(ServiceTest, testDestorySessionWithCaller, TestSize.Level1) 139 { 140 EXPECT_EQ(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 141 sptr<ITestServerInterface> iTestServerInterface = StartTestServer::GetInstance().LoadTestServer(); 142 EXPECT_NE(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 143 testServerServiceMock_->CreateSessionMock(); 144 testServerServiceMock_->DestorySession(); 145 this_thread::sleep_for(chrono::milliseconds(UNLOAD_SYSTEMABILITY_WAITTIME)); 146 EXPECT_NE(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 147 EXPECT_EQ(testServerServiceMock_->GetCallerCount(), 1); 148 } 149 150 HWTEST_F(ServiceTest, testDestorySessionWithoutCaller, TestSize.Level1) 151 { 152 EXPECT_EQ(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 153 sptr<ITestServerInterface> iTestServerInterface = StartTestServer::GetInstance().LoadTestServer(); 154 EXPECT_NE(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 155 testServerServiceMock_->DestorySession(); 156 this_thread::sleep_for(chrono::milliseconds(UNLOAD_SYSTEMABILITY_WAITTIME)); 157 EXPECT_EQ(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 158 } 159 160 HWTEST_F(ServiceTest, testSetPasteData, TestSize.Level1) 161 { 162 auto pasteBoardMgr = MiscServices::PasteboardClient::GetInstance(); 163 pasteBoardMgr->Clear(); 164 EXPECT_FALSE(pasteBoardMgr->HasPasteData()); 165 string text = "中文文本"; 166 int32_t resCode1 = testServerServiceMock_->SetPasteData(text); 167 EXPECT_EQ(resCode1, 0); 168 EXPECT_TRUE(pasteBoardMgr->HasPasteData()); 169 OHOS::MiscServices::PasteData pasteData; 170 int32_t resCode2 = pasteBoardMgr->GetPasteData(pasteData); 171 uint32_t successErrCode = 27787264; 172 EXPECT_EQ(resCode2, successErrCode); 173 auto primaryText = pasteData.GetPrimaryText(); 174 ASSERT_TRUE(primaryText != nullptr); 175 ASSERT_TRUE(*primaryText == text); 176 } 177 178 HWTEST_F(ServiceTest, testPublishEvent, TestSize.Level1) 179 { 180 OHOS::EventFwk::CommonEventData event; 181 auto want = OHOS::AAFwk::Want(); 182 want.SetAction("uitest.broadcast.command"); 183 event.SetWant(want); 184 bool re = false; 185 int32_t resCode1 = testServerServiceMock_->PublishCommonEvent(event, re); 186 EXPECT_EQ(resCode1, 0); 187 } 188 189 class CallerDetectTimerTest : public testing::Test { 190 public: 191 ~CallerDetectTimerTest() override = default; 192 193 protected: 194 const int32_t SYSTEM_ABILITY_ID = TEST_SERVER_SA_ID; 195 sptr<ISystemAbilityManager> samgr_ = nullptr; 196 const int32_t UNLOAD_SYSTEMABILITY_WAITTIME = 2000; 197 const int32_t CALLER_DECTECT_TIMER_WAITTIME = 12000; 198 SetUp()199 void SetUp() override 200 { 201 samgr_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 202 } 203 TearDown()204 void TearDown() override 205 { 206 samgr_->UnloadSystemAbility(SYSTEM_ABILITY_ID); 207 this_thread::sleep_for(chrono::milliseconds(UNLOAD_SYSTEMABILITY_WAITTIME)); 208 } 209 }; 210 211 HWTEST_F(CallerDetectTimerTest, testCallerDetectTimerWithoutCaller, TestSize.Level1) 212 { 213 EXPECT_EQ(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 214 sptr<ITestServerInterface> iTestServerInterface = StartTestServer::GetInstance().LoadTestServer(); 215 EXPECT_NE(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 216 this_thread::sleep_for(chrono::milliseconds(CALLER_DECTECT_TIMER_WAITTIME)); 217 EXPECT_EQ(samgr_->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr); 218 }