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 #define protected public 19 #include "frontend_api_handler.h" 20 #undef private 21 22 using namespace std; 23 using namespace testing::ext; 24 using namespace OHOS; 25 using namespace OHOS::perftest; 26 27 class FrontedApiHandlerTest : public testing::Test { 28 public: 29 ~FrontedApiHandlerTest() override = default; 30 protected: 31 FrontendApiServer &server = FrontendApiServer::Get(); 32 string apiId = "testApi"; 33 }; 34 35 HWTEST_F(FrontedApiHandlerTest, testAddHandler, TestSize.Level1) 36 { 37 ASSERT_FALSE(server.HasHandlerFor(apiId)); __anonc947b9b60102(const ApiCallInfo &in, ApiReplyInfo &out) 38 auto handler = [](const ApiCallInfo &in, ApiReplyInfo &out) {}; 39 server.AddHandler(apiId, handler); 40 ASSERT_TRUE(server.HasHandlerFor(apiId)); 41 server.RemoveHandler(apiId); 42 } 43 44 HWTEST_F(FrontedApiHandlerTest, testRemoveHandler, TestSize.Level1) 45 { 46 ASSERT_FALSE(server.HasHandlerFor(apiId)); __anonc947b9b60202(const ApiCallInfo &in, ApiReplyInfo &out) 47 auto handler = [](const ApiCallInfo &in, ApiReplyInfo &out) {}; 48 server.AddHandler(apiId, handler); 49 ASSERT_TRUE(server.HasHandlerFor(apiId)); 50 server.RemoveHandler(apiId); 51 ASSERT_FALSE(server.HasHandlerFor(apiId)); 52 } 53 54 HWTEST_F(FrontedApiHandlerTest, testCall, TestSize.Level1) 55 { 56 auto call1 = ApiCallInfo {.apiId_ = apiId}; 57 auto reply1 = ApiReplyInfo(); 58 server.Call(call1, reply1); 59 ASSERT_EQ(reply1.exception_.code_, ERR_INTERNAL); 60 ASSERT_TRUE(reply1.exception_.message_.find("No handler found") != string::npos); __anonc947b9b60302(const ApiCallInfo &in, ApiReplyInfo &out) 61 auto handler = [](const ApiCallInfo &in, ApiReplyInfo &out) {}; 62 server.AddHandler(apiId, handler); 63 auto call2 = ApiCallInfo {.apiId_ = apiId}; 64 auto reply2 = ApiReplyInfo(); 65 server.Call(call2, reply2); 66 ASSERT_EQ(reply2.exception_.code_, NO_ERROR); 67 server.RemoveHandler(apiId); 68 } 69 70 HWTEST_F(FrontedApiHandlerTest, testPerfTestCreate, TestSize.Level1) 71 { 72 auto call1 = ApiCallInfo {.apiId_ = "PerfTest.create"}; 73 auto reply1 = ApiReplyInfo(); 74 server.Call(call1, reply1); 75 ASSERT_EQ(reply1.exception_.code_, ERR_INVALID_INPUT); 76 auto call2 = ApiCallInfo {.apiId_ = "PerfTest.create"}; 77 nlohmann::json perfTestStrategyJson; 78 call2.paramList_.emplace_back(perfTestStrategyJson); 79 auto reply2 = ApiReplyInfo(); 80 server.Call(call2, reply2); 81 ASSERT_EQ(reply2.exception_.code_, ERR_INVALID_INPUT); 82 auto call3 = ApiCallInfo {.apiId_ = "PerfTest.create"}; 83 perfTestStrategyJson["metrics"] = {0}; 84 call3.paramList_.emplace_back(perfTestStrategyJson); 85 auto reply3 = ApiReplyInfo(); 86 server.Call(call3, reply3); 87 ASSERT_EQ(reply3.exception_.code_, ERR_INVALID_INPUT); 88 auto call4 = ApiCallInfo {.apiId_ = "PerfTest.create"}; 89 perfTestStrategyJson["actionCode"] = "callback#1"; 90 call4.paramList_.emplace_back(perfTestStrategyJson); 91 auto reply4 = ApiReplyInfo(); 92 server.Call(call4, reply4); 93 ASSERT_EQ(reply4.exception_.code_, NO_ERROR); 94 } 95 96 HWTEST_F(FrontedApiHandlerTest, testPerfTestRun, TestSize.Level1) 97 { 98 auto call0 = ApiCallInfo {.apiId_ = "PerfTest.create"}; 99 nlohmann::json perfTestStrategyJson; 100 perfTestStrategyJson["metrics"] = {0}; 101 perfTestStrategyJson["actionCode"] = "callback#1"; 102 call0.paramList_.emplace_back(perfTestStrategyJson); 103 auto reply0 = ApiReplyInfo(); 104 server.Call(call0, reply0); 105 ASSERT_EQ(reply0.exception_.code_, NO_ERROR); 106 auto call1 = ApiCallInfo {.apiId_ = "PerfTest.run", .callerObjRef_ = reply0.resultValue_.get<string>()}; 107 auto reply1 = ApiReplyInfo(); 108 server.Call(call1, reply1); 109 ASSERT_EQ(reply1.exception_.code_, ERR_INTERNAL); 110 } 111 112 HWTEST_F(FrontedApiHandlerTest, testGetMeasureResult, TestSize.Level1) 113 { 114 auto call0 = ApiCallInfo {.apiId_ = "PerfTest.create"}; 115 nlohmann::json perfTestStrategyJson; 116 perfTestStrategyJson["metrics"] = {0}; 117 perfTestStrategyJson["actionCode"] = "callback#1"; 118 call0.paramList_.emplace_back(perfTestStrategyJson); 119 auto reply0 = ApiReplyInfo(); 120 server.Call(call0, reply0); 121 ASSERT_EQ(reply0.exception_.code_, NO_ERROR); 122 auto call1 = ApiCallInfo {.apiId_ = "PerfTest.getMeasureResult", 123 .callerObjRef_ = reply0.resultValue_.get<string>()}; 124 auto reply1 = ApiReplyInfo(); 125 server.Call(call1, reply1); 126 ASSERT_EQ(reply1.exception_.code_, ERR_INVALID_INPUT); 127 auto call2 = ApiCallInfo {.apiId_ = "PerfTest.getMeasureResult", 128 .callerObjRef_ = reply0.resultValue_.get<string>()}; 129 call2.paramList_.emplace_back(0); 130 auto reply2 = ApiReplyInfo(); 131 server.Call(call2, reply2); 132 ASSERT_EQ(reply2.exception_.code_, ERR_GET_RESULT_FAILED); 133 auto call3 = ApiCallInfo {.apiId_ = "PerfTest.getMeasureResult", 134 .callerObjRef_ = reply0.resultValue_.get<string>()}; 135 call3.paramList_.emplace_back(1000); 136 auto reply3 = ApiReplyInfo(); 137 server.Call(call3, reply3); 138 ASSERT_EQ(reply3.exception_.code_, ERR_INVALID_INPUT); 139 } 140 141 HWTEST_F(FrontedApiHandlerTest, testGetBundleNameByPid, TestSize.Level1) 142 { 143 auto call1 = ApiCallInfo {.apiId_ = "PerfTest.destroy"}; 144 auto reply1 = ApiReplyInfo(); 145 server.Call(call1, reply1); 146 ASSERT_EQ(reply1.exception_.code_, ERR_INTERNAL); 147 auto call0 = ApiCallInfo {.apiId_ = "PerfTest.create"}; 148 nlohmann::json perfTestStrategyJson; 149 perfTestStrategyJson["metrics"] = {0}; 150 perfTestStrategyJson["actionCode"] = "callback#1"; 151 call0.paramList_.emplace_back(perfTestStrategyJson); 152 auto reply0 = ApiReplyInfo(); 153 server.Call(call0, reply0); 154 ASSERT_EQ(reply0.exception_.code_, NO_ERROR); 155 auto call2 = ApiCallInfo {.apiId_ = "PerfTest.destroy", .callerObjRef_ = reply0.resultValue_.get<string>()}; 156 auto reply2 = ApiReplyInfo(); 157 server.Call(call2, reply2); 158 ASSERT_EQ(reply2.exception_.code_, NO_ERROR); 159 }