From 888f6e1835c2c24b3c356b1a2f33637343f30ae6 Mon Sep 17 00:00:00 2001 From: liuwenfeng Date: Sat, 2 Nov 2024 04:05:27 +0000 Subject: [PATCH] convert shutdown printer to unavailable Signed-off-by: liuwenfeng --- services/print_service/src/vendor_helper.cpp | 45 +++++++++++-------- .../vendor_helper_test.cpp | 12 +++-- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/services/print_service/src/vendor_helper.cpp b/services/print_service/src/vendor_helper.cpp index d64e480..5a93e6b 100755 --- a/services/print_service/src/vendor_helper.cpp +++ b/services/print_service/src/vendor_helper.cpp @@ -104,6 +104,15 @@ bool ConvertJsonToStringList(const std::string &jsonString, std::vector(); +} + bool ConvertStringToLong(const char *src, long &dst) { if (src == nullptr) { @@ -164,24 +173,24 @@ bool ConvertQualityToJson(const Print_Quality &code, nlohmann::json &jsonObject) bool ConvertStringToPrinterState(const std::string &stateData, Print_PrinterState &state) { long result = 0; - if (!ConvertStringToLong(stateData.c_str(), result)) { - if (!nlohmann::json::accept(stateData)) { - PRINT_HILOGW("invalid stateData"); - return false; - } - nlohmann::json jsonObject = nlohmann::json::parse(stateData, nullptr, false); - if (jsonObject.is_discarded()) { - PRINT_HILOGW("stateData discarded"); - return false; - } - if (!jsonObject.contains("state") || !jsonObject["state"].is_string()) { - PRINT_HILOGW("can not find state"); - return false; - } - std::string stateValue = jsonObject["state"].get(); - if (!ConvertStringToLong(stateValue.c_str(), result)) { - return false; - } + if (!nlohmann::json::accept(stateData)) { + PRINT_HILOGW("invalid stateData"); + return false; + } + nlohmann::json jsonObject = nlohmann::json::parse(stateData, nullptr, false); + if (jsonObject.is_discarded()) { + PRINT_HILOGW("stateData discarded"); + return false; + } + std::string stateValue = GetStringValueFromJson(jsonObject, "state"); + if (!ConvertStringToLong(stateValue.c_str(), result)) { + return false; + } + std::string reasonValue = GetStringValueFromJson(jsonObject, "reason"); + if (reasonValue == "shutdown") { + PRINT_HILOGD("printer shutdown"); + state = PRINTER_UNAVAILABLE; + return true; } if (result < 0 || result > PRINTER_UNAVAILABLE + 1) { PRINT_HILOGW("invalid state"); diff --git a/test/unittest/fwk_vendor_manager_test/vendor_helper_test.cpp b/test/unittest/fwk_vendor_manager_test/vendor_helper_test.cpp index f1f6b86..d040bdf 100755 --- a/test/unittest/fwk_vendor_manager_test/vendor_helper_test.cpp +++ b/test/unittest/fwk_vendor_manager_test/vendor_helper_test.cpp @@ -165,16 +165,14 @@ HWTEST_F(VendorHelperTest, VendorHelperTest_0004, TestSize.Level1) { Print_PrinterState state = PRINTER_UNAVAILABLE; EXPECT_FALSE(ConvertStringToPrinterState("-1", state)); - EXPECT_FALSE(ConvertStringToPrinterState("12", state)); - EXPECT_FALSE(ConvertStringToPrinterState("", state)); - EXPECT_TRUE(ConvertStringToPrinterState("0", state)); - EXPECT_EQ(state, PRINTER_IDLE); - EXPECT_TRUE(ConvertStringToPrinterState("2", state)); - EXPECT_EQ(state, PRINTER_IDLE); EXPECT_FALSE(ConvertStringToPrinterState("\"state\"", state)); EXPECT_FALSE(ConvertStringToPrinterState("{\"state\":2}", state)); - EXPECT_TRUE(ConvertStringToPrinterState("{\"state\":\"2\"}", state)); + EXPECT_TRUE(ConvertStringToPrinterState("{\"state\":\"0\"}", state)); EXPECT_EQ(state, PRINTER_IDLE); + EXPECT_TRUE(ConvertStringToPrinterState("{\"state\":\"1\",\"reason\":\"none\"}", state)); + EXPECT_EQ(state, PRINTER_BUSY); + EXPECT_TRUE(ConvertStringToPrinterState("{\"state\":\"2\",\"reason\":\"shutdown\"}", state)); + EXPECT_EQ(state, PRINTER_UNAVAILABLE); } HWTEST_F(VendorHelperTest, VendorHelperTest_0005, TestSize.Level1)