1 /* 2 * Copyright (c) 2022 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 <iostream> 17 #include <memory> 18 #include <string> 19 20 #include <gtest/gtest.h> 21 22 #include <latency_control/inetwork_latency_switcher.h> 23 #include <latency_control/network_latency_controller.h> 24 25 namespace OHOS { 26 namespace ResourceSchedule { 27 struct MockSwitcher : INetworkLatencySwitcher { 28 struct Counter { 29 int onCount = 0; 30 int offCount = 0; 31 }; 32 MockSwitcherOHOS::ResourceSchedule::MockSwitcher33 explicit MockSwitcher(std::shared_ptr<Counter> &counter) 34 : counter(counter) 35 { } 36 LowLatencyOnOHOS::ResourceSchedule::MockSwitcher37 void LowLatencyOn() override 38 { 39 ++counter->onCount; 40 } 41 LowLatencyOffOHOS::ResourceSchedule::MockSwitcher42 void LowLatencyOff() override 43 { 44 ++counter->offCount; 45 } 46 47 private: 48 std::shared_ptr<Counter> counter; 49 }; 50 51 class NetworkLatencyControllerTest : public testing::Test { 52 public: SetUp()53 void SetUp() 54 { 55 counter = std::make_shared<MockSwitcher::Counter>(); 56 switcher = std::make_unique<MockSwitcher>(counter); 57 ctrl.Init(std::move(switcher)); 58 } 59 TearDown()60 void TearDown() 61 { } 62 63 protected: 64 std::shared_ptr<MockSwitcher::Counter> counter; 65 std::unique_ptr<MockSwitcher> switcher; 66 NetworkLatencyController ctrl; 67 }; 68 69 /** 70 * @tc.name: controllerInit001 71 * @tc.desc: test controller init 72 * @tc.type: FUNC 73 * @tc.require: issueI8VZVN 74 */ 75 HWTEST_F(NetworkLatencyControllerTest, controllerInit001, testing::ext::TestSize.Level1) 76 { 77 ctrl.Init(); 78 SUCCEED(); 79 } 80 81 /** 82 * @tc.name: singleUser_001 83 * @tc.desc: test latency switching with a single user 84 * @tc.type: FUNC 85 * @tc.require: SR000H029E SR000GVT7U 86 */ 87 HWTEST_F(NetworkLatencyControllerTest, singleUser_001, testing::ext::TestSize.Level1) 88 { 89 const std::string identity("test.application.1"); 90 91 // enable low latency 92 ctrl.HandleRequest(NetworkLatencyController::NETWORK_LATENCY_REQUEST_LOW, identity); 93 EXPECT_EQ(counter->onCount, 1); 94 EXPECT_EQ(counter->offCount, 0); 95 96 // disable low latency 97 ctrl.HandleRequest(NetworkLatencyController::NETWORK_LATENCY_REQUEST_NORMAL, identity); 98 EXPECT_EQ(counter->onCount, 1); 99 EXPECT_EQ(counter->offCount, 1); 100 101 //default 102 ctrl.HandleRequest(-1, identity); 103 SUCCEED(); 104 } 105 106 /** 107 * @tc.name: multiUser_002 108 * @tc.desc: test latency switching with multiple users 109 * @tc.type: FUNC 110 * @tc.require: SR000H029E SR000GVT7U 111 */ 112 HWTEST_F(NetworkLatencyControllerTest, multiUser_002, testing::ext::TestSize.Level1) 113 { 114 const std::string identity1("test.application.1"); 115 const std::string identity2("test.application.2"); 116 117 // enable low latency from the first user 118 ctrl.HandleRequest(NetworkLatencyController::NETWORK_LATENCY_REQUEST_LOW, identity1); 119 EXPECT_EQ(counter->onCount, 1); 120 EXPECT_EQ(counter->offCount, 0); 121 122 // enable low latency from the second user 123 ctrl.HandleRequest(NetworkLatencyController::NETWORK_LATENCY_REQUEST_LOW, identity2); 124 EXPECT_EQ(counter->onCount, 1); // already activated 125 EXPECT_EQ(counter->offCount, 0); 126 127 // try reset to normal latency from the first user 128 ctrl.HandleRequest(NetworkLatencyController::NETWORK_LATENCY_REQUEST_NORMAL, identity1); 129 EXPECT_EQ(counter->onCount, 1); 130 EXPECT_EQ(counter->offCount, 0); // there is a second user alive 131 132 // finally reset from the second user 133 ctrl.HandleRequest(NetworkLatencyController::NETWORK_LATENCY_REQUEST_NORMAL, identity2); 134 EXPECT_EQ(counter->onCount, 1); 135 EXPECT_EQ(counter->offCount, 1); // finally disable 136 } 137 138 /** 139 * @tc.name: errorDuplicateRequests_004 140 * @tc.desc: test that duplicate requests are ignored 141 * @tc.type: FUNC 142 * @tc.require: SR000H029E SR000GVT7U 143 */ 144 HWTEST_F(NetworkLatencyControllerTest, errorDuplicateRequests_004, testing::ext::TestSize.Level1) 145 { 146 const std::string identity("my.test.application"); 147 148 // send initial latency request 149 ctrl.HandleRequest(NetworkLatencyController::NETWORK_LATENCY_REQUEST_LOW, identity); 150 EXPECT_EQ(counter->onCount, 1); 151 EXPECT_EQ(counter->offCount, 0); 152 153 // create another latency request shouldn't change anything 154 ctrl.HandleRequest(NetworkLatencyController::NETWORK_LATENCY_REQUEST_LOW, identity); 155 EXPECT_EQ(counter->onCount, 1); 156 EXPECT_EQ(counter->offCount, 0); 157 } 158 159 /** 160 * @tc.name: errorCancelNonExistentRequest_005 161 * @tc.desc: test that it is impossible to cancel non-existent request 162 * @tc.type: FUNC 163 * @tc.require: SR000H029E SR000GVT7U 164 */ 165 HWTEST_F(NetworkLatencyControllerTest, errorCancelNonExistentRequest_005, testing::ext::TestSize.Level1) 166 { 167 const std::string identity("my.test.application"); 168 169 // cancelling a non-existing latency request should not do anything 170 ctrl.HandleRequest(NetworkLatencyController::NETWORK_LATENCY_REQUEST_NORMAL, identity); 171 EXPECT_EQ(counter->onCount, 0); 172 EXPECT_EQ(counter->offCount, 0); // nothing changed 173 } 174 175 /** 176 * @tc.name: errorCancelForeignRequest_006 177 * @tc.desc: test that it is impossible to cancel another user's request 178 * @tc.type: FUNC 179 * @tc.require: SR000H029E SR000GVT7U 180 */ 181 HWTEST_F(NetworkLatencyControllerTest, errorCancelForeignRequest_006, testing::ext::TestSize.Level1) 182 { 183 const std::string identity1("my.test.application1"); 184 const std::string identity2("my.test.application2"); 185 186 // register a latency request from the first user 187 ctrl.HandleRequest(NetworkLatencyController::NETWORK_LATENCY_REQUEST_LOW, identity1); 188 EXPECT_EQ(counter->onCount, 1); 189 EXPECT_EQ(counter->offCount, 0); 190 191 // cannot cancel the request using another identity 192 ctrl.HandleRequest(NetworkLatencyController::NETWORK_LATENCY_REQUEST_NORMAL, identity2); 193 EXPECT_EQ(counter->onCount, 1); // nothing changed 194 EXPECT_EQ(counter->offCount, 0); 195 } 196 197 /** 198 * @tc.name: errorInvalidLatencyValue_007 199 * @tc.desc: test that invalid request values are ignored 200 * @tc.type: FUNC 201 * @tc.require: SR000H029E SR000GVT7U 202 */ 203 HWTEST_F(NetworkLatencyControllerTest, errorInvalidLatencyValue_007, testing::ext::TestSize.Level1) 204 { 205 const std::string identity("my.test.application"); 206 long long badValue = 9999; 207 208 // try to register non existing request value 209 ctrl.HandleRequest(badValue, identity); 210 EXPECT_EQ(counter->onCount, 0); // should not activate 211 EXPECT_EQ(counter->offCount, 0); 212 } 213 } // namespace ResourceSchedule 214 } // namespace OHOS 215