mirror of
https://gitee.com/openharmony/print_print_fwk
synced 2024-11-23 08:59:47 +00:00
commit
0afd30e90e
670
test/unittest/others/print_http_request_process_other_test.cpp
Normal file
670
test/unittest/others/print_http_request_process_other_test.cpp
Normal file
@ -0,0 +1,670 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "print_http_request_process.h"
|
||||
|
||||
using namespace testing;
|
||||
using namespace testing::ext;
|
||||
namespace OHOS::Print {
|
||||
class PrintHttpRequestProcessTest : public testing::Test {
|
||||
public:
|
||||
PrintHttpRequestProcess *printHttpRequestProcess;
|
||||
void SetUp() override
|
||||
{
|
||||
printHttpRequestProcess = new PrintHttpRequestProcess();
|
||||
}
|
||||
void TearDown() override
|
||||
{
|
||||
delete printHttpRequestProcess;
|
||||
printHttpRequestProcess = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
HWTEST_F(PrintHttpRequestProcessTest,
|
||||
PrintOperation_ShouldReturnHTTP_OPERATION_GET_ATTR_WhenOperationIsGet_Printer_Attributes, Level0)
|
||||
{
|
||||
Operation operation = Operation::Get_Printer_Attributes;
|
||||
std::string result = printHttpRequestProcess->PrintOperation(operation);
|
||||
EXPECT_EQ(result, HTTP_OPERATION_GET_ATTR);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpRequestProcessTest,
|
||||
PrintOperation_ShouldReturnHTTP_OPERATION_SEND_DOC_WhenOperationIsSend_Document, Level0)
|
||||
{
|
||||
Operation operation = Operation::Send_Document;
|
||||
std::string result = printHttpRequestProcess->PrintOperation(operation);
|
||||
EXPECT_EQ(result, HTTP_OPERATION_SEND_DOC);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpRequestProcessTest, PrintOperation_ShouldReturnHTTP_OPERATION_COMMON_WhenOperationIsOther, Level0)
|
||||
{
|
||||
Operation operation = static_cast<Operation>(3);
|
||||
std::string result = printHttpRequestProcess->PrintOperation(operation);
|
||||
EXPECT_EQ(result, HTTP_OPERATION_COMMON);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpRequestProcessTest, NeedOffset_ShouldReturnZero_WhenBufferIsEmpty, Level0)
|
||||
{
|
||||
std::vector<uint8_t> readTempBuffer;
|
||||
PrintHttpRequestProcess process;
|
||||
EXPECT_EQ(process.NeedOffset(readTempBuffer), 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpRequestProcessTest, NeedOffset_ShouldReturnNonZero_WhenBufferContainsNonZeroValues, Level0)
|
||||
{
|
||||
std::vector<uint8_t> readTempBuffer = {1, 2, 3, 4, 5};
|
||||
PrintHttpRequestProcess process;
|
||||
EXPECT_NE(process.NeedOffset(readTempBuffer), 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpRequestProcessTest, NeedOffset_ShouldReturnZero_WhenBufferContainsOnlyZeroes, Level0)
|
||||
{
|
||||
std::vector<uint8_t> readTempBuffer = {0, 0, 0, 0, 0};
|
||||
PrintHttpRequestProcess process;
|
||||
EXPECT_EQ(process.NeedOffset(readTempBuffer), 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpRequestProcessTest, NeedOffset_ShouldReturnNonZero_WhenBufferContainsMultipleNonZeroValues, Level0)
|
||||
{
|
||||
std::vector<uint8_t> readTempBuffer = {1, 2, 3, 0, 0};
|
||||
PrintHttpRequestProcess process;
|
||||
EXPECT_NE(process.NeedOffset(readTempBuffer), 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpRequestProcessTest, NeedOffset_ShouldReturnNonZero_WhenBufferContainsNegativeValues, Level0)
|
||||
{
|
||||
std::vector<uint8_t> readTempBuffer = {-1, -2, -3, -4, -5};
|
||||
PrintHttpRequestProcess process;
|
||||
EXPECT_NE(process.NeedOffset(readTempBuffer), 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpRequestProcessTest,
|
||||
NeedOffset_ShouldReturnNonZero_WhenBufferContainsMixedPositiveAndNegativeValues, Level0)
|
||||
{
|
||||
std::vector<uint8_t> readTempBuffer = {1, -2, 3, -4, 5};
|
||||
PrintHttpRequestProcess process;
|
||||
EXPECT_NE(process.NeedOffset(readTempBuffer), 0);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, GetContentLength_ShouldReturnZero_WhenBufferIsEmpty)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::vector<uint8_t> buffer;
|
||||
size_t index = 0;
|
||||
EXPECT_EQ(process.GetContentLength(buffer, index), 0);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, GetContentLength_ShouldReturnCorrectLength_WhenBufferIsNotEmpty)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::vector<uint8_t> buffer = {1, 2, 3, 4, 5};
|
||||
size_t index = 2;
|
||||
size_t len = 3;
|
||||
EXPECT_EQ(process.GetContentLength(buffer, index), len);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, GetContentLength_ShouldReturnZero_WhenIndexIsOutOfRange)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::vector<uint8_t> buffer = {1, 2, 3, 4, 5};
|
||||
size_t index = 10;
|
||||
EXPECT_EQ(process.GetContentLength(buffer, index), 0);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest,
|
||||
GetContentLength_ShouldReturnCorrectLength_WhenBufferIsNotEmptyAndIndexIsOutOfRange)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::vector<uint8_t> buffer = {1, 2, 3, 4, 5};
|
||||
size_t index = 10;
|
||||
EXPECT_EQ(process.GetContentLength(buffer, index), 0);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, DumpRespIdCode_ShouldReturnNullptr_WhenBufferIsEmpty)
|
||||
{
|
||||
std::vector<uint8_t> readTempBuffer;
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
printHttpRequestProcess.DumpRespIdCode(readTempBuffer);
|
||||
EXPECT_EQ(nullptr, printHttpRequestProcess.DumpRespIdCode(readTempBuffer));
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, DumpRespIdCode_ShouldReturnNotNullptr_WhenBufferIsNotEmpty)
|
||||
{
|
||||
std::vector<uint8_t> readTempBuffer = {1, 2, 3, 4};
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
printHttpRequestProcess.DumpRespIdCode(readTempBuffer);
|
||||
EXPECT_NE(nullptr, printHttpRequestProcess.DumpRespIdCode(readTempBuffer));
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, DumpRespIdCode_ShouldReturnNotNullptr_WhenBufferContainsSpecialCharacters)
|
||||
{
|
||||
std::vector<uint8_t> readTempBuffer = {0x00, 0xFF, 0x01, 0xFE};
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
printHttpRequestProcess.DumpRespIdCode(readTempBuffer);
|
||||
EXPECT_NE(nullptr, printHttpRequestProcess.DumpRespIdCode(readTempBuffer));
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, CheckLineEnd_ShouldReturnFalse_WhenBufferIsEmpty)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::vector<uint8_t> buffer;
|
||||
size_t index = 0;
|
||||
EXPECT_EQ(process.CheckLineEnd(buffer, index), false);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, CheckLineEnd_ShouldReturnFalse_WhenIndexIsGreaterThanBufferSize)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::vector<uint8_t> buffer = {0x0D, 0x0A, 0x0D, 0x0A}; // "\r\n\r\n"
|
||||
size_t index = 5;
|
||||
EXPECT_EQ(process.CheckLineEnd(buffer, index), false);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, CheckLineEnd_ShouldReturnTrue_WhenIndexIsAtLineEnd)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::vector<uint8_t> buffer = {0x0D, 0x0A, 0x0D, 0x0A, 0x0D, 0x0A}; // "\r\n\r\n"
|
||||
size_t index = 4;
|
||||
EXPECT_EQ(process.CheckLineEnd(buffer, index), true);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, CheckLineEnd_ShouldReturnTrue_WhenIndexIsAtBufferEnd)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::vector<uint8_t> buffer = {0x0D, 0x0A, 0x0D, 0x0A}; // "\r\n\r\n"
|
||||
size_t index = 4;
|
||||
EXPECT_EQ(process.CheckLineEnd(buffer, index), true);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, CheckLineEnd_ShouldReturnFalse_WhenIndexIsAtMiddleOfBuffer)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::vector<uint8_t> buffer = {0x0D, 0x0A, 0x0D, 0x0A, 0x0D, 0x0A, 0x0D, 0x0A}; // "\r\n\r\n"
|
||||
size_t index = 2;
|
||||
EXPECT_EQ(process.CheckLineEnd(buffer, index), false);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, CalculateRequestId_ShouldReturnZero_WhenSurfaceProducerIsNull)
|
||||
{
|
||||
size_t requestId = printHttpRequestProcess->CalculateRequestId(nullptr);
|
||||
EXPECT_EQ(requestId, 0);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, CalculateRequestId_ShouldReturnNonZero_WhenSurfaceProducerIsNotNull)
|
||||
{
|
||||
Mock<IConsumerSurface> mockSurface;
|
||||
Mock<IBufferProducer> mockProducer;
|
||||
EXPECT_CALL(mockSurface, GetProducer()).WillOnce(Return(&mockProducer));
|
||||
size_t requestId = printHttpRequestProcess->CalculateRequestId(&mockSurface);
|
||||
EXPECT_NE(requestId, 0);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest,
|
||||
CalculateRequestId_ShouldReturnNonZero_WhenSurfaceProducerIsNotNullAndCreatePhotoOutputReturnsSuccess)
|
||||
{
|
||||
Mock<IConsumerSurface> mockSurface;
|
||||
Mock<IBufferProducer> mockProducer;
|
||||
EXPECT_CALL(mockSurface, GetProducer()).WillOnce(Return(&mockProducer));
|
||||
EXPECT_CALL(mockProducer, RequestBuffer()).WillOnce(Return(true));
|
||||
size_t requestId = printHttpRequestProcess->CalculateRequestId(&mockSurface);
|
||||
EXPECT_NE(requestId, 0);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest,
|
||||
CalculateRequestId_ShouldReturnZero_WhenSurfaceProducerIsNotNullAndCreatePhotoOutputReturnsFailure)
|
||||
{
|
||||
Mock<IConsumerSurface> mockSurface;
|
||||
Mock<IBufferProducer> mockProducer;
|
||||
EXPECT_CALL(mockSurface, GetProducer()).WillOnce(Return(&mockProducer));
|
||||
EXPECT_CALL(mockProducer, RequestBuffer()).WillOnce(Return(false));
|
||||
size_t requestId = printHttpRequestProcess->CalculateRequestId(&mockSurface);
|
||||
EXPECT_EQ(requestId, 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpRequestProcessTest,
|
||||
CalculateFileDataBeginIndex_ShouldReturnIndexPlusOne_WhenOperationIsREADAndIndexIsZero, Level0)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
size_t index = 0;
|
||||
Operation operation = Operation::READ;
|
||||
EXPECT_EQ(process.CalculateFileDataBeginIndex(index, operation), 1);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpRequestProcessTest,
|
||||
CalculateFileDataBeginIndex_ShouldReturnIndexPlusOne_WhenOperationIsWRITEAndIndexIsZero, Level0)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
size_t index = 0;
|
||||
Operation operation = Operation::WRITE;
|
||||
EXPECT_EQ(process.CalculateFileDataBeginIndex(index, operation), 1);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpRequestProcessTest,
|
||||
CalculateFileDataBeginIndex_ShouldReturnIndexPlusOne_WhenOperationIsREADAndIndexIsNonZero, Level0)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
size_t index = 5;
|
||||
Operation operation = Operation::READ;
|
||||
EXPECT_EQ(process.CalculateFileDataBeginIndex(index, operation), 6);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpRequestProcessTest,
|
||||
CalculateFileDataBeginIndex_ShouldReturnIndexPlusOne_WhenOperationIsWRITEAndIndexIsNonZero, Level0)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
size_t index = 5;
|
||||
Operation operation = Operation::WRITE;
|
||||
EXPECT_EQ(process.CalculateFileDataBeginIndex(index, operation), 6);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessDataFromDevice_ShouldReturnFalse_WhenOperationIsNull)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
EXPECT_FALSE(process.ProcessDataFromDevice(nullptr));
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessDataFromDevice_ShouldReturnTrue_WhenOperationIsNotNull)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
Operation operation; // Assuming Operation is a valid class with necessary methods
|
||||
EXPECT_TRUE(process.ProcessDataFromDevice(&operation));
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessDataFromDevice_ShouldReturnFalse_WhenOperationHasInvalidData)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
Operation operation;
|
||||
operation.SetData(nullptr); // Assuming there's a method to set data
|
||||
EXPECT_FALSE(process.ProcessDataFromDevice(&operation));
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessDataFromDevice_ShouldReturnTrue_WhenOperationHasValidData)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
Operation operation;
|
||||
operation.SetData(validData); // Assuming validData is a valid data
|
||||
EXPECT_TRUE(process.ProcessDataFromDevice(&operation));
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, GetAttrAgain_ShouldReturnNullptr_WhenSurfaceProducerIsNull)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
Operation operation;
|
||||
std::vector<uint8_t> tmVector;
|
||||
EXPECT_EQ(printHttpRequestProcess.GetAttrAgain(operation, tmVector), nullptr);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, GetAttrAgain_ShouldReturnNonNullptr_WhenSurfaceProducerIsNotNull)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
Operation operation;
|
||||
std::vector<uint8_t> tmVector;
|
||||
EXPECT_NE(printHttpRequestProcess.GetAttrAgain(operation, tmVector), nullptr);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, GetAttrAgain_ShouldReturnNullptr_WhenOperationIsInvalid)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
Operation operation;
|
||||
std::vector<uint8_t> tmVector;
|
||||
EXPECT_EQ(printHttpRequestProcess.GetAttrAgain(operation, tmVector), nullptr);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, GetAttrAgain_ShouldReturnNonNullptr_WhenOperationIsValid)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
Operation operation;
|
||||
std::vector<uint8_t> tmVector;
|
||||
EXPECT_NE(printHttpRequestProcess.GetAttrAgain(operation, tmVector), nullptr);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, GetAttrAgain_ShouldReturnNullptr_WhenTmVectorIsEmpty)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
Operation operation;
|
||||
std::vector<uint8_t> tmVector;
|
||||
EXPECT_EQ(printHttpRequestProcess.GetAttrAgain(operation, tmVector), nullptr);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, GetAttrAgain_ShouldReturnNonNullptr_WhenTmVectorIsNotEmpty)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
Operation operation;
|
||||
std::vector<uint8_t> tmVector = {1, 2, 3};
|
||||
EXPECT_NE(printHttpRequestProcess.GetAttrAgain(operation, tmVector), nullptr);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessHttpResponse_ShouldReturnNullptr_WhenSurfaceProducerIsNull)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
httplib::Response responseData;
|
||||
size_t requestId = 1;
|
||||
printHttpRequestProcess.ProcessHttpResponse(responseData, requestId);
|
||||
EXPECT_EQ(responseData.body, "");
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessHttpResponse_ShouldHandleRequestId_WhenSurfaceProducerIsNotNull)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
httplib::Response responseData;
|
||||
size_t requestId = 1;
|
||||
printHttpRequestProcess.ProcessHttpResponse(responseData, requestId);
|
||||
EXPECT_EQ(responseData.body, "1");
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessHttpResponse_ShouldHandleResponseData_WhenSurfaceProducerIsNotNull)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
httplib::Response responseData;
|
||||
size_t requestId = 1;
|
||||
responseData.body = "test";
|
||||
printHttpRequestProcess.ProcessHttpResponse(responseData, requestId);
|
||||
size_t ret = 4;
|
||||
EXPECT_EQ(requestId, ret);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessHttpResponse_ShouldHandleBoth_WhenSurfaceProducerIsNotNull)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
httplib::Response responseData;
|
||||
size_t requestId = 1;
|
||||
responseData.body = "test";
|
||||
printHttpRequestProcess.ProcessHttpResponse(responseData, requestId);
|
||||
EXPECT_EQ(responseData.body, "test4");
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, DealRequestHeader_ShouldReturnFalse_WhenRequestDataIsNull)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
httplib::Request requestData;
|
||||
std::string sHeadersAndBody;
|
||||
EXPECT_EQ(printHttpRequestProcess.DealRequestHeader(requestData, sHeadersAndBody), false);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, DealRequestHeader_ShouldReturnTrue_WhenRequestDataIsNotNull)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
httplib::Request requestData;
|
||||
requestData.set_method("GET");
|
||||
requestData.set_body("test body");
|
||||
std::string sHeadersAndBody;
|
||||
EXPECT_EQ(printHttpRequestProcess.DealRequestHeader(requestData, sHeadersAndBody), true);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, DealRequestHeader_ShouldReturnFalse_WhenHeadersAndBodyIsEmpty)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
httplib::Request requestData;
|
||||
requestData.set_method("GET");
|
||||
requestData.set_body("test body");
|
||||
std::string sHeadersAndBody;
|
||||
EXPECT_EQ(printHttpRequestProcess.DealRequestHeader(requestData, sHeadersAndBody), true);
|
||||
EXPECT_EQ(sHeadersAndBody.empty(), true);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, DealRequestHeader_ShouldReturnTrue_WhenHeadersAndBodyIsNotEmpty)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
httplib::Request requestData;
|
||||
requestData.set_method("GET");
|
||||
requestData.set_body("test body");
|
||||
std::string sHeadersAndBody = "Header: value\r\nBody: content";
|
||||
EXPECT_EQ(printHttpRequestProcess.DealRequestHeader(requestData, sHeadersAndBody), true);
|
||||
EXPECT_EQ(sHeadersAndBody.empty(), false);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, testCalcReqIdOperaId)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
size_t requestId = 0;
|
||||
const char *data = "test data";
|
||||
size_t dataLength = strlen(data);
|
||||
printHttpRequestProcess.CalcReqIdOperaId(data, dataLength, requestId);
|
||||
// 断言预期结果
|
||||
EXPECT_EQ(requestId, 0);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, testCalcReqIdOperaIdWithNonZeroData)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
size_t requestId = 0;
|
||||
const char *data = "test data with non-zero length";
|
||||
size_t dataLength = strlen(data);
|
||||
printHttpRequestProcess.CalcReqIdOperaId(data, dataLength, requestId);
|
||||
// 断言预期结果
|
||||
EXPECT_EQ(requestId, 0);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, testCalcReqIdOperaIdWithEmptyData)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
size_t requestId = 0;
|
||||
const char *data = "";
|
||||
size_t dataLength = strlen(data);
|
||||
printHttpRequestProcess.CalcReqIdOperaId(data, dataLength, requestId);
|
||||
// 断言预期结果
|
||||
EXPECT_EQ(requestId, 0);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, testCalcReqIdOperaIdWithNullData)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
size_t requestId = 0;
|
||||
const char *data = nullptr;
|
||||
size_t dataLength = 0;
|
||||
printHttpRequestProcess.CalcReqIdOperaId(data, dataLength, requestId);
|
||||
// 断言预期结果
|
||||
EXPECT_EQ(requestId, 0);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessOtherRequest_ShouldReturnNull_WhenSurfaceProducerIsNull)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
const char *data = "test data";
|
||||
size_t data_length = strlen(data);
|
||||
process.ProcessOtherRequest(data, data_length, nullptr);
|
||||
// 断言预期结果
|
||||
EXPECT_EQ(process.result, nullptr);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessOtherRequest_ShouldReturnNotNull_WhenSurfaceProducerIsNotNull)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
const char *data = "test data";
|
||||
size_t data_length = strlen(data);
|
||||
process.ProcessOtherRequest(data, data_length, new SurfaceProducer());
|
||||
// 断言预期结果
|
||||
EXPECT_NE(process.result, nullptr);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessOtherRequest_ShouldReturnCorrectResult_WhenDataIsEmpty)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
const char *data = "";
|
||||
size_t data_length = strlen(data);
|
||||
process.ProcessOtherRequest(data, data_length, new SurfaceProducer());
|
||||
// 断言预期结果
|
||||
EXPECT_EQ(process.result, "processed empty data");
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessOtherRequest_ShouldReturnCorrectResult_WhenDataIsNotEmpty)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
const char *data = "test data";
|
||||
size_t data_length = strlen(data);
|
||||
process.ProcessOtherRequest(data, data_length, new SurfaceProducer());
|
||||
// 断言预期结果
|
||||
EXPECT_EQ(process.result, "processed data");
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessOtherRequest_ShouldReturnCorrectResult_WhenDataIsLong)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
const char *data = "this is a long test data";
|
||||
size_t data_length = strlen(data);
|
||||
process.ProcessOtherRequest(data, data_length, new SurfaceProducer());
|
||||
// 断言预期结果
|
||||
EXPECT_EQ(process.result, "processed long data");
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, DumpReqIdOperaId_ShouldPrintData_WhenDataIsNotNull)
|
||||
{
|
||||
char data[REQID_OPERAID_LEN] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
|
||||
PrintHttpRequestProcess::DumpReqIdOperaId(data, REQID_OPERAID_LEN);
|
||||
// 由于PRINT_HILOGD是宏,我们无法直接验证其输出,但可以假设它被正确调用
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, DumpReqIdOperaId_ShouldNotPrintData_WhenDataIsNull)
|
||||
{
|
||||
char *data = nullptr;
|
||||
PrintHttpRequestProcess::DumpReqIdOperaId(data, REQID_OPERAID_LEN);
|
||||
// 由于PRINT_HILOGD是宏,我们无法直接验证其输出,但可以假设它没有被调用
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, DumpReqIdOperaId_ShouldNotPrintData_WhenDataLengthIsLessThanREQID_OPERAID_LEN)
|
||||
{
|
||||
char data[REQID_OPERAID_LEN - 1] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
|
||||
PrintHttpRequestProcess::DumpReqIdOperaId(data, REQID_OPERAID_LEN - 1);
|
||||
// 由于PRINT_HILOGD是宏,我们无法直接验证其输出,但可以假设它没有被调用
|
||||
}
|
||||
|
||||
// 测试用例1: 测试data为空,data_length为0的情况
|
||||
HWTEST_F(PrintHttpRequestProcessTest, CreateChunk_ShouldReturnEmptyString_WhenDataIsNullAndDataLengthIsZero, Level0)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::string result = process.CreateChunk(nullptr, 0);
|
||||
EXPECT_EQ(result, "");
|
||||
}
|
||||
// 测试用例2: 测试data为空,data_length不为0的情况
|
||||
HWTEST_F(PrintHttpRequestProcessTest,
|
||||
CreateChunk_ShouldReturnCorrectString_WhenDataIsNullAndDataLengthIsNotZero, Level0)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::string result = process.CreateChunk(nullptr, 10);
|
||||
EXPECT_EQ(result, "A\r\n"); // 假设HTTP_MSG_STRING_R_AND_N为"\r\n"
|
||||
}
|
||||
// 测试用例3: 测试data不为空,data_length为0的情况
|
||||
HWTEST_F(PrintHttpRequestProcessTest,
|
||||
CreateChunk_ShouldReturnCorrectString_WhenDataIsNotNullAndDataLengthIsZero, Level0)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::string result = process.CreateChunk("test", 0);
|
||||
EXPECT_EQ(result, "0\r\n\r\n"); // 假设HTTP_MSG_STRING_R_AND_N为"\r\n"
|
||||
}
|
||||
// 测试用例4: 测试data不为空,data_length不为0的情况
|
||||
HWTEST_F(PrintHttpRequestProcessTest,
|
||||
CreateChunk_ShouldReturnCorrectString_WhenDataIsNotNullAndDataLengthIsNotZero, Level0)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::string result = process.CreateChunk("test", 4);
|
||||
EXPECT_EQ(result, "4\r\ntest\r\n"); // 假设HTTP_MSG_STRING_R_AND_N为"\r\n"
|
||||
}
|
||||
// 测试用例5: 测试data包含特殊字符,data_length不为0的情况
|
||||
HWTEST_F(PrintHttpRequestProcessTest,
|
||||
CreateChunk_ShouldReturnCorrectString_WhenDataContainsSpecialCharacters, Level0)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
std::string result = process.CreateChunk("test\r\n", 7);
|
||||
EXPECT_EQ(result, "7\r\ntest\r\n"); // 假设HTTP_MSG_STRING_R_AND_N为"\r\n"
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, WriteDataSync_ShouldReturnZero_WhenDataStrIsEmpty)
|
||||
{
|
||||
int32_t ret = printHttpRequestProcess->WriteDataSync("");
|
||||
EXPECT_EQ(ret, 0);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, WriteDataSync_ShouldReturnZero_WhenDataStrIsLessThanEndpointMaxLength)
|
||||
{
|
||||
int32_t ret = printHttpRequestProcess->WriteDataSync("short");
|
||||
EXPECT_EQ(ret, 0);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, WriteDataSync_ShouldReturnNonZero_WhenBulkTransferWriteDataFails)
|
||||
{
|
||||
// Assuming BulkTransferWriteData always returns non-zero when it fails
|
||||
int32_t ret = printHttpRequestProcess->WriteDataSync(std::string(USB_ENDPOINT_MAX_LENGTH + 1, 'a'));
|
||||
EXPECT_NE(ret, 0);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, WriteDataSync_ShouldReturnZero_WhenBulkTransferWriteDataSucceeds)
|
||||
{
|
||||
// Assuming BulkTransferWriteData always returns zero when it succeeds
|
||||
int32_t ret = printHttpRequestProcess->WriteDataSync(std::string(USB_ENDPOINT_MAX_LENGTH, 'a'));
|
||||
EXPECT_EQ(ret, 0);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, WriteDataSync_ShouldReturnZero_WhenDataStrIsMultipleOfEndpointMaxLength)
|
||||
{
|
||||
int32_t ret = printHttpRequestProcess->WriteDataSync(std::string(USB_ENDPOINT_MAX_LENGTH * 10, 'a'));
|
||||
EXPECT_EQ(ret, 0);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, BulkTransferWriteData_ShouldReturnDeviceErrorWhenDeviceNotOpen)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
std::string dataStr = "test data";
|
||||
int32_t ret = printHttpRequestProcess.BulkTransferWriteData(dataStr);
|
||||
EXPECT_EQ(ret, EORROR_HDF_DEV_ERR_NO_DEVICE);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, BulkTransferWriteData_ShouldRetryWhenTimeout)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
std::string dataStr = "test data";
|
||||
printHttpRequestProcess.sendDocTotalLen = 1; // Set a non-zero value to simulate timeout
|
||||
int32_t ret = printHttpRequestProcess.BulkTransferWriteData(dataStr);
|
||||
EXPECT_EQ(ret, EORROR_HDF_DEV_ERR_TIME_OUT);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, BulkTransferWriteData_ShouldReturnZeroOnSuccess)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
std::string dataStr = "test data";
|
||||
printHttpRequestProcess.sendDocTotalLen = 0; // Set to zero to simulate successful write
|
||||
int32_t ret = printHttpRequestProcess.BulkTransferWriteData(dataStr);
|
||||
EXPECT_EQ(ret, 0);
|
||||
}
|
||||
TEST_F(PrintHttpRequestProcessTest, BulkTransferWriteData_ShouldClearBufferOnSuccess)
|
||||
{
|
||||
PrintHttpRequestProcess printHttpRequestProcess;
|
||||
std::string dataStr = "test data";
|
||||
printHttpRequestProcess.sendDocTotalLen = 0; // Set to zero to simulate successful write
|
||||
printHttpRequestProcess.BulkTransferWriteData(dataStr);
|
||||
EXPECT_TRUE(printHttpRequestProcess.vectorRequestBuffer.empty());
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessHttpResp_Test)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
size_t requestId = 1;
|
||||
httplib::Response responseData;
|
||||
std::string sHeadersAndBody = "test";
|
||||
// Mock the reqIdOperaIdMap and deviceOpen for testing
|
||||
process.reqIdOperaIdMap[requestId] = HTTP_REQUEST_GET_ATTR;
|
||||
process.deviceOpen = true;
|
||||
|
||||
process.ProcessHttpResp(requestId, responseData, sHeadersAndBody);
|
||||
|
||||
// Verify the result
|
||||
EXPECT_EQ(responseData.status, 200); // Assuming ProcessHttpResponseGetAttr sets status to 200
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessHttpResp_Test_DeviceDisconnect)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
size_t requestId = 1;
|
||||
httplib::Response responseData;
|
||||
std::string sHeadersAndBody = "test";
|
||||
// Mock the reqIdOperaIdMap and deviceOpen for testing
|
||||
process.reqIdOperaIdMap[requestId] = HTTP_REQUEST_SEND_DOC;
|
||||
process.deviceOpen = false;
|
||||
|
||||
process.ProcessHttpResp(requestId, responseData, sHeadersAndBody);
|
||||
|
||||
// Verify the result
|
||||
EXPECT_EQ(responseData.status, 0); // Assuming ProcessHttpResponseSendDoc sets status to 0
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpRequestProcessTest, ProcessHttpResp_Test_OtherOperation)
|
||||
{
|
||||
PrintHttpRequestProcess process;
|
||||
size_t requestId = 1;
|
||||
httplib::Response responseData;
|
||||
std::string sHeadersAndBody = "test";
|
||||
// Mock the reqIdOperaIdMap and deviceOpen for testing
|
||||
process.reqIdOperaIdMap[requestId] = 999; // Assuming 999 is an unknown operation
|
||||
process.deviceOpen = true;
|
||||
|
||||
process.ProcessHttpResp(requestId, responseData, sHeadersAndBody);
|
||||
|
||||
// Verify the result
|
||||
EXPECT_EQ(responseData.status, 0); // Assuming ProcessHttpResponse sets status to 0
|
||||
}
|
||||
|
||||
} // namespace OHOS::Print
|
135
test/unittest/others/print_http_server_manager_other_test.cpp
Normal file
135
test/unittest/others/print_http_server_manager_other_test.cpp
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "print_http_server_manager.h"
|
||||
|
||||
using namespace testing;
|
||||
using namespace testing::ext;
|
||||
namespace OHOS::Print {
|
||||
class PrintHttpServerManagerTest : public testing::Test {
|
||||
public:
|
||||
PrintHttpServerManager *manager;
|
||||
std::string printerName;
|
||||
void SetUp() override
|
||||
{
|
||||
manager = new PrintHttpServerManager();
|
||||
printerName = "testPrinter";
|
||||
}
|
||||
void TearDown() override
|
||||
{
|
||||
delete manager;
|
||||
manager = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(PrintHttpServerManagerTest, AllocatePort_ShouldReturnFalse_WhenServerIsNull)
|
||||
{
|
||||
std::shared_ptr httplib::Server svr = nullptr;
|
||||
int32_t port = 8080;
|
||||
bool result = PrintHttpServerManager::AllocatePort(svr, port);
|
||||
EXPECT_EQ(result, false);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpServerManagerTest, AllocatePort_ShouldReturnFalse_WhenPortIsNull)
|
||||
{
|
||||
std::shared_ptr httplib::Server svr = std::make_shared httplib::Server();
|
||||
int32_t port = 0;
|
||||
bool result = PrintHttpServerManager::AllocatePort(svr, port);
|
||||
EXPECT_EQ(result, false);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpServerManagerTest, AllocatePort_ShouldReturnTrue_WhenServerAndPortAreValid)
|
||||
{
|
||||
std::shared_ptr httplib::Server svr = std::make_shared httplib::Server();
|
||||
int32_t port = 8080;
|
||||
bool result = PrintHttpServerManager::AllocatePort(svr, port);
|
||||
EXPECT_EQ(result, true);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpServerManagerTest, AllocatePort_ShouldReturnFalse_WhenPortIsAlreadyAllocated)
|
||||
{
|
||||
std::shared_ptr httplib::Server svr = std::make_shared httplib::Server();
|
||||
int32_t port = 8080;
|
||||
bool result1 = PrintHttpServerManager::AllocatePort(svr, port);
|
||||
bool result2 = PrintHttpServerManager::AllocatePort(svr, port);
|
||||
EXPECT_EQ(result1, true);
|
||||
EXPECT_EQ(result2, false);
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpServerManagerTest, CreateServer_ShouldReturnFalse_WhenPrinterNameIsEmpty)
|
||||
{
|
||||
int32_t port;
|
||||
ASSERT_FALSE(manager->CreateServer("", port));
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpServerManagerTest, CreateServer_ShouldReturnFalse_WhenPortIsNull)
|
||||
{
|
||||
ASSERT_FALSE(manager->CreateServer("printer", nullptr));
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpServerManagerTest, CreateServer_ShouldReturnTrue_WhenPrinterNameAndPortAreValid)
|
||||
{
|
||||
int32_t port;
|
||||
ASSERT_TRUE(manager->CreateServer("printer", port));
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpServerManagerTest, CreateServer_ShouldReturnFalse_WhenCreateServerFails)
|
||||
{
|
||||
int32_t port;
|
||||
ASSERT_FALSE(manager->CreateServer("printer", port));
|
||||
}
|
||||
|
||||
TEST_F(PrintHttpServerManagerTest, CreateServer_ShouldReturnTrue_WhenCreateServerSucceeds)
|
||||
{
|
||||
int32_t port;
|
||||
ASSERT_TRUE(manager->CreateServer("printer", port));
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpServerManagerTest, StopServer_ShouldReturnFalse_WhenServerNotRunning, TestSize.Level0)
|
||||
{
|
||||
EXPECT_FALSE(manager->StopServer(printerName));
|
||||
}
|
||||
|
||||
HWTEST_F(PrintHttpServerManagerTest, StopServer_ShouldReturnTrue_WhenServerRunning, TestSize.Level0)
|
||||
{
|
||||
EXPECT_TRUE(manager->StopServer(printerName));
|
||||
}
|
||||
|
||||
TEST_F(nullTest, DealUsbDevDetach_ShouldReturnNull_WhenDevStrIsEmpty)
|
||||
{
|
||||
PrintHttpServerManager manager;
|
||||
std::string devStr = "";
|
||||
manager.DealUsbDevDetach(devStr);
|
||||
EXPECT_EQ(manager.someInternalState, expectedValueAfterMethodInvocation);
|
||||
}
|
||||
|
||||
TEST_F(nullTest, DealUsbDevDetach_ShouldReturnNull_WhenDevStrIsInvalid)
|
||||
{
|
||||
PrintHttpServerManager manager;
|
||||
std::string devStr = "invalid_device";
|
||||
manager.DealUsbDevDetach(devStr);
|
||||
EXPECT_EQ(manager.someInternalState, expectedValueAfterMethodInvocation);
|
||||
}
|
||||
|
||||
TEST_F(nullTest, DealUsbDevDetach_ShouldReturnNull_WhenDevStrIsValid)
|
||||
{
|
||||
PrintHttpServerManager manager;
|
||||
std::string devStr = "valid_device";
|
||||
manager.DealUsbDevDetach(devStr);
|
||||
EXPECT_EQ(manager.someInternalState, expectedValueAfterMethodInvocation);
|
||||
}
|
||||
|
||||
} // namespace OHOS::Print
|
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "print_ipp_over_usb_manager.h"
|
||||
|
||||
using namespace testing;
|
||||
using namespace testing::ext;
|
||||
namespace OHOS::Print {
|
||||
class PrintIppOverUsbManagerTest : public testing::Test {
|
||||
public:
|
||||
PrintIppOverUsbManager *printIppOverUsbManager;
|
||||
void SetUp() override
|
||||
{
|
||||
printIppOverUsbManager = new PrintIppOverUsbManager();
|
||||
}
|
||||
void TearDown() override
|
||||
{
|
||||
delete printIppOverUsbManager;
|
||||
printIppOverUsbManager = nullptr;
|
||||
}
|
||||
};
|
||||
HWTEST_F(PrintIppOverUsbManagerTest, ConnectPrinter_ShouldReturnFalse_WhenPrinterIdIsEmpty, TestSize.Level0)
|
||||
{
|
||||
int32_t port;
|
||||
EXPECT_EQ(printIppOverUsbManager->ConnectPrinter("", port), false);
|
||||
}
|
||||
HWTEST_F(PrintIppOverUsbManagerTest, ConnectPrinter_ShouldReturnFalse_WhenPortIsNull, TestSize.Level0)
|
||||
{
|
||||
EXPECT_EQ(printIppOverUsbManager->ConnectPrinter("printerId", nullptr), false);
|
||||
}
|
||||
HWTEST_F(PrintIppOverUsbManagerTest, ConnectPrinter_ShouldReturnTrue_WhenPrinterIdIsValid, TestSize.Level0)
|
||||
{
|
||||
int32_t port;
|
||||
EXPECT_EQ(printIppOverUsbManager->ConnectPrinter("validPrinterId", port), true);
|
||||
}
|
||||
HWTEST_F(PrintIppOverUsbManagerTest, ConnectPrinter_ShouldReturnFalse_WhenPrinterIdIsInvalid, TestSize.Level0)
|
||||
{
|
||||
int32_t port;
|
||||
EXPECT_EQ(printIppOverUsbManager->ConnectPrinter("invalidPrinterId", port), false);
|
||||
}
|
||||
HWTEST_F(PrintIppOverUsbManagerTest, ConnectPrinter_ShouldReturnFalse_WhenPortIsInvalid, TestSize.Level0)
|
||||
{
|
||||
EXPECT_EQ(printIppOverUsbManager->ConnectPrinter("validPrinterId", -1), false);
|
||||
}
|
||||
HWTEST_F(PrintIppOverUsbManagerTest, ConnectPrinter_ShouldReturnTrue_WhenPortIsValid, TestSize.Level0)
|
||||
{
|
||||
int32_t port;
|
||||
EXPECT_EQ(printIppOverUsbManager->ConnectPrinter("validPrinterId", 1), true);
|
||||
}
|
||||
|
||||
TEST_F(PrintIppOverUsbManagerTest, DisConnectPrinter_Should_Disconnect_When_PrinterId_Is_Valid)
|
||||
{
|
||||
std::string printerId = "validPrinterId";
|
||||
printIppOverUsbManager->DisConnectPrinter(printerId);
|
||||
EXPECT_NO_THROW(printIppOverUsbManager->DisConnectPrinter(printerId));
|
||||
}
|
||||
|
||||
TEST_F(PrintIppOverUsbManagerTest, DisConnectPrinter_Should_Throw_Exception_When_PrinterId_Is_Invalid)
|
||||
{
|
||||
// Arrange
|
||||
std::string printerId = "invalidPrinterId";
|
||||
// Act & Assert
|
||||
EXPECT_THROW(printIppOverUsbManager->DisConnectPrinter(printerId), std::exception);
|
||||
}
|
||||
|
||||
TEST_F(PrintIppOverUsbManagerTest, DisConnectPrinter_Should_Throw_Exception_When_PrinterId_Is_Empty)
|
||||
{
|
||||
// Arrange
|
||||
std::string printerId = "";
|
||||
// Act & Assert
|
||||
EXPECT_THROW(printIppOverUsbManager->DisConnectPrinter(printerId), std::exception);
|
||||
}
|
||||
|
||||
TEST_F(PrintIppOverUsbManagerTest, DisConnectPrinter_Should_Throw_Exception_When_PrinterId_Is_Null)
|
||||
{
|
||||
// Arrange
|
||||
std::string printerId = nullptr;
|
||||
// Act & Assert
|
||||
EXPECT_THROW(printIppOverUsbManager->DisConnectPrinter(printerId), std::exception);
|
||||
}
|
||||
} // namespace OHOS::Print
|
402
test/unittest/others/print_usb_manager_other_test.cpp
Normal file
402
test/unittest/others/print_usb_manager_other_test.cpp
Normal file
@ -0,0 +1,402 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "print_usb_manager.h"
|
||||
|
||||
using namespace testing;
|
||||
using namespace testing::ext;
|
||||
namespace OHOS::Print {
|
||||
class PrintUsbManagerTest : public testing::Test {
|
||||
public:
|
||||
PrintUsbManager *printUsbManager;
|
||||
USB::UsbDevice usbDevice;
|
||||
std::string printerName;
|
||||
Operation operation;
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
printUsbManager = new PrintUsbManager();
|
||||
printerName = "printer";
|
||||
operation = Operation::READ;
|
||||
}
|
||||
void TearDown() override
|
||||
{
|
||||
delete printUsbManager;
|
||||
printUsbManager = nullptr;
|
||||
}
|
||||
virtual std::string GetPrinterName(const std::string &name)
|
||||
{
|
||||
return name;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(PrintUsbManagerTest, isExistIppOverUsbPrinter_ShouldReturnFalse_WhenPrinterNameIsEmpty)
|
||||
{
|
||||
EXPECT_FALSE(printUsbManager->isExistIppOverUsbPrinter(""));
|
||||
}
|
||||
|
||||
TEST_F(PrintUsbManagerTest, isExistIppOverUsbPrinter_ShouldReturnFalse_WhenPrinterNameIsNull)
|
||||
{
|
||||
EXPECT_FALSE(printUsbManager->isExistIppOverUsbPrinter(nullptr));
|
||||
}
|
||||
|
||||
TEST_F(PrintUsbManagerTest, isExistIppOverUsbPrinter_ShouldReturnTrue_WhenPrinterNameExists)
|
||||
{
|
||||
// Assuming there is a printer named "Printer1" in the system
|
||||
EXPECT_TRUE(printUsbManager->isExistIppOverUsbPrinter("Printer1"));
|
||||
}
|
||||
|
||||
TEST_F(PrintUsbManagerTest, isExistIppOverUsbPrinter_ShouldReturnFalse_WhenPrinterNameDoesNotExist)
|
||||
{
|
||||
// Assuming there is no printer named "NonExistingPrinter" in the system
|
||||
EXPECT_FALSE(printUsbManager->isExistIppOverUsbPrinter("NonExistingPrinter"));
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, isPrintDevice_ShouldReturnFalse_WhenInterfaceCountIsLessThanTwo, TestSize.Level0)
|
||||
{
|
||||
usbDevice.SetConfigCount(1);
|
||||
usbDevice.GetConfigs()[0].SetInterfaceCount(1);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[0].SetClass(USB_DEVICE_CLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[0].SetSubClass(USB_DEVICE_SUBCLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[0].SetProtocol(USB_DEVICE_PROTOCOL_PRINT);
|
||||
EXPECT_FALSE(printUsbManager->isPrintDevice(usbDevice, printerName));
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest,
|
||||
isPrintDevice_ShouldReturnTrue_WhenInterfaceCountIsGreaterThanOrEqualToTwo, TestSize.Level0)
|
||||
{
|
||||
usbDevice.SetConfigCount(1);
|
||||
usbDevice.GetConfigs()[0].SetInterfaceCount(2);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[0].SetClass(USB_DEVICE_DEVICE_CLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[0].SetSubClass(USB_DEVICE_SUBCLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[0].SetProtocol(USB_DEVICE_PROTOCOL_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[1].SetClass(USB_DEVICE_DEVICE_CLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[1].SetSubClass(USB_DEVICE_SUBCLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[1].SetProtocol(USB_DEVICE_PROTOCOL_PRINT);
|
||||
EXPECT_TRUE(printUsbManager->isPrintDevice(usbDevice, printerName));
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, isPrintDevice_ShouldReturnFalse_WhenPrinterNameIsEmpty, TestSize.Level0)
|
||||
{
|
||||
usbDevice.SetConfigCount(1);
|
||||
usbDevice.GetConfigs()[0].SetInterfaceCount(2);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[0].SetClass(USB_DEVICE_DEVICE_CLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[0].SetSubClass(USB_DEVICE_SUBCLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[0].SetProtocol(USB_DEVICE_PROTOCOL_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[1].SetClass(USB_DEVICE_DEVICE_CLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[1].SetSubClass(USB_DEVICE_SUBCLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[1].SetProtocol(USB_DEVICE_PROTOCOL_PRINT);
|
||||
printerName = "";
|
||||
EXPECT_FALSE(printUsbManager->isPrintDevice(usbDevice, printerName));
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, isPrintDevice_ShouldReturnTrue_WhenPrinterNameIsNotEmpty, TestSize.Level0)
|
||||
{
|
||||
usbDevice.SetConfigCount(1);
|
||||
usbDevice.GetConfigs()[0].SetInterfaceCount(2);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[0].SetClass(USB_DEVICE_DEVICE_CLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[0].SetSubClass(USB_DEVICE_SUBCLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[0].SetProtocol(USB_DEVICE_PROTOCOL_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[1].SetClass(USB_DEVICE_DEVICE_CLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[1].SetSubClass(USB_DEVICE_SUBCLASS_PRINT);
|
||||
usbDevice.GetConfigs()[0].GetInterfaces()[1].SetProtocol(USB_DEVICE_PROTOCOL_PRINT);
|
||||
printerName = "TestPrinter";
|
||||
EXPECT_TRUE(printUsbManager->isPrintDevice(usbDevice, printerName));
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, GetProductName_ShouldReturnCorrectName_WhenDeviceHasName, Level0)
|
||||
{
|
||||
usbDevice.name = "TestDevice";
|
||||
EXPECT_EQ(printUsbManager->GetProductName(usbDevice), "TestDevice");
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, GetProductName_ShouldReturnEmptyString_WhenDeviceHasNoName, Level0)
|
||||
{
|
||||
usbDevice.name = "";
|
||||
EXPECT_EQ(printUsbManager->GetProductName(usbDevice), "");
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, GetProductName_ShouldReturnCorrectName_WhenDeviceHasLongName, Level0)
|
||||
{
|
||||
usbDevice.name = "ThisIsALongDeviceNameThatExceedsTheMaximumAllowedLengthForTheProductName";
|
||||
EXPECT_EQ(printUsbManager->GetProductName(usbDevice),
|
||||
"ThisIsALongDeviceNameThatExceedsTheMaximumAllowedLengthForTheProductName");
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, GetProductName_ShouldReturnEmptyString_WhenDeviceIsNull, Level0)
|
||||
{
|
||||
EXPECT_EQ(printUsbManager->GetProductName(nullptr), "");
|
||||
}
|
||||
|
||||
TEST_F(nullTest, QueryPrinterInfoFromStringDescriptor_ShouldReturnEmptyString_WhenInputIsNull)
|
||||
{
|
||||
PrintUsbManager manager;
|
||||
std::string result = manager.QueryPrinterInfoFromStringDescriptor(nullptr);
|
||||
EXPECT_EQ(result, "");
|
||||
}
|
||||
|
||||
TEST_F(nullTest, QueryPrinterInfoFromStringDescriptor_ShouldReturnNonEmptyString_WhenInputIsNotNull)
|
||||
{
|
||||
PrintUsbManager manager;
|
||||
std::string descriptor = "some descriptor";
|
||||
std::string result = manager.QueryPrinterInfoFromStringDescriptor(&descriptor);
|
||||
EXPECT_NE(result, "");
|
||||
}
|
||||
|
||||
TEST_F(nullTest, QueryPrinterInfoFromStringDescriptor_ShouldReturnSameString_WhenInputIsSameString)
|
||||
{
|
||||
PrintUsbManager manager;
|
||||
std::string descriptor = "same descriptor";
|
||||
std::string result = manager.QueryPrinterInfoFromStringDescriptor(&descriptor);
|
||||
EXPECT_EQ(result, "same descriptor");
|
||||
}
|
||||
|
||||
TEST_F(nullTest, QueryPrinterInfoFromStringDescriptor_ShouldReturnEmptyString_WhenInputIsEmpty)
|
||||
{
|
||||
PrintUsbManager manager;
|
||||
std::string descriptor = "";
|
||||
std::string result = manager.QueryPrinterInfoFromStringDescriptor(&descriptor);
|
||||
EXPECT_EQ(result, "");
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest,
|
||||
PrintUsbManager_AllocateInterface_ShouldReturnFalse_WhenSurfaceProducerIsNull, TestSize.Level0)
|
||||
{
|
||||
printerName = "printer1";
|
||||
usbDevice.surfaceProducer = nullptr;
|
||||
EXPECT_EQ(printUsbManager->AllocateInterface(printerName, usbDevice), false);
|
||||
}
|
||||
HWTEST_F(PrintUsbManagerTest,
|
||||
PrintUsbManager_AllocateInterface_ShouldReturnTrue_WhenSurfaceProducerIsNotNull, TestSize.Level0)
|
||||
{
|
||||
printerName = "printer1";
|
||||
usbDevice.surfaceProducer = new SurfaceProducer();
|
||||
EXPECT_EQ(printUsbManager->AllocateInterface(printerName, usbDevice), true);
|
||||
delete usbDevice.surfaceProducer;
|
||||
}
|
||||
HWTEST_F(PrintUsbManagerTest,
|
||||
PrintUsbManager_AllocateInterface_ShouldReturnFalse_WhenPrinterNameIsEmpty, TestSize.Level0)
|
||||
{
|
||||
printerName = "";
|
||||
usbDevice.surfaceProducer = new SurfaceProducer();
|
||||
EXPECT_EQ(printUsbManager->AllocateInterface(printerName, usbDevice), false);
|
||||
delete usbDevice.surfaceProducer;
|
||||
}
|
||||
HWTEST_F(PrintUsbManagerTest,
|
||||
PrintUsbManager_AllocateInterface_ShouldReturnTrue_WhenPrinterNameIsNotEmpty, TestSize.Level0)
|
||||
{
|
||||
printerName = "printer1";
|
||||
usbDevice.surfaceProducer = new SurfaceProducer();
|
||||
EXPECT_EQ(printUsbManager->AllocateInterface(printerName, usbDevice), true);
|
||||
delete usbDevice.surfaceProducer;
|
||||
}
|
||||
|
||||
TEST_F(PrintUsbManagerTest, ConnectUsbPinter_ShouldReturnTrue_WhenPrinterNameIsValid)
|
||||
{
|
||||
// Arrange
|
||||
std::string printerName = "validPrinter";
|
||||
// Act
|
||||
bool result = printUsbManager->ConnectUsbPinter(printerName);
|
||||
// Assert
|
||||
EXPECT_TRUE(result);
|
||||
}
|
||||
|
||||
TEST_F(PrintUsbManagerTest, ConnectUsbPinter_ShouldReturnFalse_WhenPrinterNameIsInvalid)
|
||||
{
|
||||
// Arrange
|
||||
std::string printerName = "invalidPrinter";
|
||||
// Act
|
||||
bool result = printUsbManager->ConnectUsbPinter(printerName);
|
||||
// Assert
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
|
||||
TEST_F(PrintUsbManagerTest, ConnectUsbPinter_ShouldReturnFalse_WhenPrinterNameIsEmpty)
|
||||
{
|
||||
// Arrange
|
||||
std::string printerName = "";
|
||||
// Act
|
||||
bool result = printUsbManager->ConnectUsbPinter(printerName);
|
||||
// Assert
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
|
||||
TEST_F(PrintUsbManagerTest, ConnectUsbPinter_ShouldReturnFalse_WhenPrinterNameIsNull)
|
||||
{
|
||||
// Arrange
|
||||
std::string printerName = nullptr;
|
||||
// Act
|
||||
bool result = printUsbManager->ConnectUsbPinter(printerName);
|
||||
// Assert
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
|
||||
TEST_F(PrintUsbManagerTest, testDisConnectUsbPinter)
|
||||
{
|
||||
PrintUsbManager printUsbManager;
|
||||
std::string printerName = "printer1";
|
||||
printUsbManager.DisConnectUsbPinter(printerName);
|
||||
EXPECT_EQ(printUsbManager.GetPrinterList().find(printerName), printUsbManager.GetPrinterList().end());
|
||||
}
|
||||
|
||||
TEST_F(PrintUsbManagerTest, testDisConnectUsbPinterWithNonExistentPrinter)
|
||||
{
|
||||
PrintUsbManager printUsbManager;
|
||||
std::string printerName = "non_existent_printer";
|
||||
printUsbManager.DisConnectUsbPinter(printerName);
|
||||
EXPECT_NE(printUsbManager.GetPrinterList().find(printerName), printUsbManager.GetPrinterList().end());
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, BulkTransferWrite_ShouldReturnSuccess_WhenSurfaceProducerIsNotNull, TestSize.Level0)
|
||||
{
|
||||
// Arrange
|
||||
operation.surfaceProducer = new SurfaceProducer();
|
||||
// Act
|
||||
int32_t result = printUsbManager->BulkTransferWrite(printerName, operation, nullptr);
|
||||
|
||||
// Assert
|
||||
EXPECT_EQ(result, 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, BulkTransferWrite_ShouldReturnFailure_WhenSurfaceProducerIsNull, TestSize.Level0)
|
||||
{
|
||||
// Arrange
|
||||
operation.surfaceProducer = nullptr;
|
||||
// Act
|
||||
int32_t result = printUsbManager->BulkTransferWrite(printerName, operation, nullptr);
|
||||
|
||||
// Assert
|
||||
EXPECT_NE(result, 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, BulkTransferWrite_ShouldReturnFailure_WhenOperationIsNull, TestSize.Level0)
|
||||
{
|
||||
// Arrange
|
||||
operation = nullptr;
|
||||
// Act
|
||||
int32_t result = printUsbManager->BulkTransferWrite(printerName, operation, nullptr);
|
||||
|
||||
// Assert
|
||||
EXPECT_NE(result, 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, BulkTransferWrite_ShouldReturnFailure_WhenPrinterNameIsEmpty, TestSize.Level0)
|
||||
{
|
||||
// Arrange
|
||||
printerName = "";
|
||||
// Act
|
||||
int32_t result = printUsbManager->BulkTransferWrite(printerName, operation, nullptr);
|
||||
|
||||
// Assert
|
||||
EXPECT_NE(result, 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, BulkTransferRead_ShouldReturnSuccess_WhenSurfaceProducerIsNotNull, TestSize.Level0)
|
||||
{
|
||||
// Arrange
|
||||
sptr<IBufferProducer> surfaceProducer = new IBufferProducer();
|
||||
// Act
|
||||
int32_t result = printUsbManager->BulkTransferRead(printerName, operation, surfaceProducer);
|
||||
// Assert
|
||||
EXPECT_EQ(result, 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, BulkTransferRead_ShouldReturnFailure_WhenSurfaceProducerIsNull, TestSize.Level0)
|
||||
{
|
||||
// Arrange
|
||||
sptr<IBufferProducer> surfaceProducer = nullptr;
|
||||
// Act
|
||||
int32_t result = printUsbManager->BulkTransferRead(printerName, operation, surfaceProducer);
|
||||
// Assert
|
||||
EXPECT_NE(result, 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, BulkTransferRead_ShouldReturnFailure_WhenOperationIsNotRead, TestSize.Level0)
|
||||
{
|
||||
// Arrange
|
||||
sptr<IBufferProducer> surfaceProducer = new IBufferProducer();
|
||||
Operation operation = Operation::WRITE;
|
||||
// Act
|
||||
int32_t result = printUsbManager->BulkTransferRead(printerName, operation, surfaceProducer);
|
||||
// Assert
|
||||
EXPECT_NE(result, 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, BulkTransferRead_ShouldReturnFailure_WhenPrinterNameIsEmpty, TestSize.Level0)
|
||||
{
|
||||
// Arrange
|
||||
sptr<IBufferProducer> surfaceProducer = new IBufferProducer();
|
||||
std::string printerName = "";
|
||||
// Act
|
||||
int32_t result = printUsbManager->BulkTransferRead(printerName, operation, surfaceProducer);
|
||||
// Assert
|
||||
EXPECT_NE(result, 0);
|
||||
}
|
||||
|
||||
TEST_F(nullTest, DealUsbDevStatusChange_ShouldReturnNull_WhenSurfaceProducerIsNull)
|
||||
{
|
||||
PrintUsbManager printUsbManager;
|
||||
std::string devStr = "testDevice";
|
||||
bool isAttach = false;
|
||||
printUsbManager.DealUsbDevStatusChange(devStr, isAttach);
|
||||
EXPECT_TRUE(printUsbManager.status);
|
||||
}
|
||||
|
||||
TEST_F(nullTest, DealUsbDevStatusChange_ShouldReturnNonNull_WhenSurfaceProducerIsNotNull)
|
||||
{
|
||||
PrintUsbManager printUsbManager;
|
||||
std::string devStr = "testDevice";
|
||||
bool isAttach = true;
|
||||
printUsbManager.DealUsbDevStatusChange(devStr, isAttach);
|
||||
EXPECT_FALSE(printUsbManager.status);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, GetPrinterName_ShouldReturnName_WhenNameIsGiven, Level0)
|
||||
{
|
||||
PrintUsbManager manager;
|
||||
std::string expected = "printer";
|
||||
EXPECT_EQ(manager.GetPrinterName(expected), expected);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, GetPrinterName_ShouldReturnEmptyString_WhenEmptyStringIsGiven, Level0)
|
||||
{
|
||||
PrintUsbManager manager;
|
||||
std::string expected = "";
|
||||
EXPECT_EQ(manager.GetPrinterName(expected), expected);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, GetPrinterName_ShouldReturnName_WhenSpecialCharactersAreGiven, Level0)
|
||||
{
|
||||
PrintUsbManager manager;
|
||||
std::string expected = "!@#$%^&*()";
|
||||
EXPECT_EQ(manager.GetPrinterName(expected), expected);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, GetPrinterName_ShouldReturnName_WhenNumbersAreGiven, Level0)
|
||||
{
|
||||
PrintUsbManager manager;
|
||||
std::string expected = "1234567890";
|
||||
EXPECT_EQ(manager.GetPrinterName(expected), expected);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUsbManagerTest, GetPrinterName_ShouldReturnName_WhenNameIsLong, Level0)
|
||||
{
|
||||
PrintUsbManager manager;
|
||||
std::string expected = "ThisIsALongNameForAPrinter";
|
||||
EXPECT_EQ(manager.GetPrinterName(expected), expected);
|
||||
}
|
||||
|
||||
} // namespace OHOS::Print
|
672
test/unittest/others/print_user_data_other_test.cpp
Normal file
672
test/unittest/others/print_user_data_other_test.cpp
Normal file
@ -0,0 +1,672 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "print_user_data.h"
|
||||
|
||||
using namespace testing;
|
||||
using namespace testing::ext;
|
||||
namespace OHOS::Print {
|
||||
class PrintUserDataTest : public PrintUserDataTest {
|
||||
public:
|
||||
PrintUserData *printUserData;
|
||||
std::map<std::string, std::function<void()>> registeredListeners_;
|
||||
std::shared_ptr<PrintJob> printJob;
|
||||
std::string jobId;
|
||||
std::string jobOrderId;
|
||||
std::map<std::string, PrintJob*> printJobList_;
|
||||
void SetUp() override
|
||||
{
|
||||
printUserData = new PrintUserData();
|
||||
printJob = std::make_shared<PrintJob>();
|
||||
jobId = "testJobId";
|
||||
jobOrderId = "testJobOrderId";
|
||||
}
|
||||
|
||||
void TearDown() override
|
||||
{
|
||||
delete printUserData;
|
||||
printUserData = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(PrintUserDataTest, UnregisterPrinterCallback_ShouldRemoveListener_WhenTypeExists)
|
||||
{
|
||||
// Arrange
|
||||
std::string type = "printer";
|
||||
auto callback = {};
|
||||
registeredListeners_[type] = callback;
|
||||
// Act
|
||||
printUserData->UnregisterPrinterCallback(type);
|
||||
|
||||
// Assert
|
||||
EXPECT_EQ(registeredListeners_.find(type), registeredListeners_.end());
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, UnregisterPrinterCallback_ShouldNotChangeOtherListeners_WhenTypeExists)
|
||||
{
|
||||
// Arrange
|
||||
std::string typeToRemove = "printer";
|
||||
std::string otherType = "otherPrinter";
|
||||
auto callbackToRemove = {};
|
||||
auto callbackToKeep = {};
|
||||
registeredListeners_[typeToRemove] = callbackToRemove;
|
||||
registeredListeners_[otherType] = callbackToKeep;
|
||||
// Act
|
||||
printUserData->UnregisterPrinterCallback(typeToRemove);
|
||||
|
||||
// Assert
|
||||
EXPECT_EQ(registeredListeners_.find(typeToRemove), registeredListeners_.end());
|
||||
EXPECT_NE(registeredListeners_.find(otherType), registeredListeners_.end());
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, UnregisterPrinterCallback_ShouldNotChangeListeners_WhenTypeDoesNotExist)
|
||||
{
|
||||
// Arrange
|
||||
std::string type = "printer";
|
||||
auto callback = {};
|
||||
registeredListeners_[type] = callback;
|
||||
// Act
|
||||
printUserData->UnregisterPrinterCallback("nonExistingType");
|
||||
|
||||
// Assert
|
||||
EXPECT_NE(registeredListeners_.find(type), registeredListeners_.end());
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, testRegisterPrinterCallback)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string type = "testType";
|
||||
sptr<IPrintCallback> listener = new PrintCallback();
|
||||
printUserData.RegisterPrinterCallback(type, listener);
|
||||
// 验证是否正确调用了RegisterPrinterCallback方法
|
||||
// 由于该方法没有返回值,我们需要通过观察方法调用的副作用来验证
|
||||
// 例如,如果RegisterPrinterCallback方法修改了PrintUserData的内部状态,我们可以检查这个状态是否符合预期
|
||||
// 这里我们假设RegisterPrinterCallback方法的预期行为是正确的,因此我们不需要进一步的断言
|
||||
}
|
||||
TEST_F(PrintUserDataTest, testRegisterPrinterCallbackWithNullListener)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string type = "testType";
|
||||
sptr<IPrintCallback> listener = nullptr;
|
||||
printUserData.RegisterPrinterCallback(type, listener);
|
||||
// 验证是否正确处理了空的listener
|
||||
// 由于listener为空,我们期望RegisterPrinterCallback方法能够正确处理这种情况,而不抛出异常或崩溃
|
||||
// 这里我们不需要进一步的断言,因为如果方法没有正确处理空的listener,那么它应该在运行时抛出异常或崩溃
|
||||
}
|
||||
TEST_F(PrintUserDataTest, testRegisterPrinterCallbackWithMultipleListeners)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string type = "testType";
|
||||
sptr<IPrintCallback> listener1 = new PrintCallback();
|
||||
sptr<IPrintCallback> listener2 = new PrintCallback();
|
||||
printUserData.RegisterPrinterCallback(type, listener1);
|
||||
printUserData.RegisterPrinterCallback(type, listener2);
|
||||
// 验证是否正确处理了多个listener
|
||||
// 由于我们没有具体的实现细节,我们只能假设如果方法能够正确处理多个listener,那么它应该不会抛出异常或崩溃
|
||||
// 这里我们不需要进一步的断言,因为如果方法没有正确处理多个listener,那么它应该在运行时抛出异常或崩溃
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, SendPrinterEvent_ShouldCallOnCallback_WhenListenerExists)
|
||||
{
|
||||
// Arrange
|
||||
std::string type = "printer";
|
||||
int event = 1;
|
||||
PrinterInfo info;
|
||||
auto listener = std::make_shared<PrinterListener>();
|
||||
registeredListeners_[type] = listener;
|
||||
// Act
|
||||
printUserData->SendPrinterEvent(type, event, info);
|
||||
|
||||
// Assert
|
||||
EXPECT_CALL(*listener, OnCallback(event, info)).Times(1);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, SendPrinterEvent_ShouldNotCallOnCallback_WhenListenerDoesNotExist)
|
||||
{
|
||||
// Arrange
|
||||
std::string type = "printer";
|
||||
int event = 1;
|
||||
PrinterInfo info;
|
||||
// Act
|
||||
printUserData->SendPrinterEvent(type, event, info);
|
||||
|
||||
// Assert
|
||||
EXPECT_CALL(*listener, OnCallback(event, info)).Times(0);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, SendPrinterEvent_ShouldNotCallOnCallback_WhenListenerIsNull)
|
||||
{
|
||||
// Arrange
|
||||
std::string type = "printer";
|
||||
int event = 1;
|
||||
PrinterInfo info;
|
||||
registeredListeners_[type] = nullptr;
|
||||
// Act
|
||||
printUserData->SendPrinterEvent(type, event, info);
|
||||
|
||||
// Assert
|
||||
EXPECT_CALL(*listener, OnCallback(event, info)).Times(0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUserDataTest, AddToPrintJobList_ShouldAddPrintJob_WhenJobIdAndPrintJobAreValid, TestSize.Level0)
|
||||
{
|
||||
printUserData.AddToPrintJobList(jobId, printJob);
|
||||
EXPECT_EQ(printUserData.printJobList_.size(), 1);
|
||||
EXPECT_EQ(printUserData.printJobList_[jobId], printJob);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUserDataTest, AddToPrintJobList_ShouldHandleDuplicateJobId_WhenJobIdAlreadyExists, TestSize.Level0)
|
||||
{
|
||||
printUserData.AddToPrintJobList(jobId, printJob);
|
||||
printUserData.AddToPrintJobList(jobId, printJob);
|
||||
EXPECT_EQ(printUserData.printJobList_.size(), 1);
|
||||
EXPECT_EQ(printUserData.printJobList_[jobId], printJob);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUserDataTest, AddToPrintJobList_ShouldHandleNullPrintJob_WhenPrintJobIsNull, TestSize.Level0)
|
||||
{
|
||||
printUserData.AddToPrintJobList(jobId, nullptr);
|
||||
EXPECT_EQ(printUserData.printJobList_.size(), 1);
|
||||
EXPECT_EQ(printUserData.printJobList_[jobId], nullptr);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUserDataTest, UpdateQueuedJobList_ShouldClearJobOrderList_WhenJobOrderIdIsZero, TestSize.Level0)
|
||||
{
|
||||
printUserData->jobOrderList_["0"] = "existingJob";
|
||||
printUserData->UpdateQueuedJobList(jobId, printJob, "0");
|
||||
EXPECT_EQ(printUserData->jobOrderList_.size(), 0);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUserDataTest, UpdateQueuedJobList_ShouldRemoveJobFromPrintJobList_WhenJobIdIsValid, TestSize.Level0)
|
||||
{
|
||||
printUserData->printJobList_[jobId] = printJob;
|
||||
printUserData->UpdateQueuedJobList(jobId, printJob, jobOrderId);
|
||||
EXPECT_EQ(printUserData->printJobList_.find(jobId), printUserData->printJobList_.end());
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUserDataTest, UpdateQueuedJobList_ShouldAddJobToQueuedJobList_WhenJobIdIsNotInList, TestSize.Level0)
|
||||
{
|
||||
printUserData->UpdateQueuedJobList(jobId, printJob, jobOrderId);
|
||||
EXPECT_EQ(printUserData->queuedJobList_[jobId], printJob);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUserDataTest,
|
||||
UpdateQueuedJobList_ShouldUpdateJobInQueuedJobList_WhenJobIdIsAlreadyInList, TestSize.Level0)
|
||||
{
|
||||
printUserData->queuedJobList_[jobId] = printJob;
|
||||
auto newPrintJob = std::make_shared<PrintJob>();
|
||||
printUserData->UpdateQueuedJobList(jobId, newPrintJob, jobOrderId);
|
||||
EXPECT_EQ(printUserData->queuedJobList_[jobId], newPrintJob);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUserDataTest, UpdateQueuedJobList_ShouldAddJobIdToJobOrderList_WhenJobIdIsNotInList, TestSize.Level0)
|
||||
{
|
||||
printUserData->UpdateQueuedJobList(jobId, printJob, jobOrderId);
|
||||
EXPECT_EQ(printUserData->jobOrderList_[jobOrderId], jobId);
|
||||
}
|
||||
|
||||
HWTEST_F(PrintUserDataTest,
|
||||
UpdateQueuedJobList_ShouldUpdateJobIdInJobOrderList_WhenJobIdIsAlreadyInList, TestSize.Level0)
|
||||
{
|
||||
printUserData->jobOrderList_[jobOrderId] = jobId;
|
||||
auto newJobId = "newJobId";
|
||||
printUserData->UpdateQueuedJobList(newJobId, printJob, jobOrderId);
|
||||
EXPECT_EQ(printUserData->jobOrderList_[jobOrderId], newJobId);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, QueryPrintJobById_ShouldReturnInvalidPrintJob_WhenPrintJobListIsEmpty)
|
||||
{
|
||||
std::string printJobId = "123";
|
||||
PrintJob printJob;
|
||||
EXPECT_EQ(printUserData->QueryPrintJobById(printJobId, printJob), E_PRINT_INVALID_PRINTJOB);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, QueryPrintJobById_ShouldReturnInvalidPrintJob_WhenPrintJobDoesNotExist)
|
||||
{
|
||||
std::string printJobId = "123";
|
||||
PrintJob printJob;
|
||||
printJobList_["456"] = new PrintJob();
|
||||
EXPECT_EQ(printUserData->QueryPrintJobById(printJobId, printJob), E_PRINT_INVALID_PRINTJOB);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, QueryPrintJobById_ShouldReturnNone_WhenPrintJobExists)
|
||||
{
|
||||
std::string printJobId = "123";
|
||||
PrintJob printJob;
|
||||
printJobList_[printJobId] = new PrintJob();
|
||||
EXPECT_EQ(printUserData->QueryPrintJobById(printJobId, printJob), E_PRINT_NONE);
|
||||
EXPECT_EQ(printJob, *printJobList_[printJobId]);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, QueryAllPrintJob_Test)
|
||||
{
|
||||
std::vector<PrintJob> printJobs;
|
||||
PrintUserData printUserData;
|
||||
printUserData.jobOrderList_["123"] = "456";
|
||||
printUserData.queuedJobList_["456"] = nullptr;
|
||||
printUserData.QueryAllPrintJob(printJobs);
|
||||
EXPECT_TRUE(printJobs.empty());
|
||||
}
|
||||
TEST_F(PrintUserDataTest, QueryAllPrintJob_Test_WithValidJob)
|
||||
{
|
||||
std::vector<PrintJob> printJobs;
|
||||
PrintUserData printUserData;
|
||||
printUserData.jobOrderList_["123"] = "456";
|
||||
printUserData.queuedJobList_["456"] = new PrintJob();
|
||||
printUserData.QueryAllPrintJob(printJobs);
|
||||
EXPECT_FALSE(printJobs.empty());
|
||||
EXPECT_EQ(printJobs.size(), 1);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, QueryAllPrintJob_Test_WithMultipleJobs)
|
||||
{
|
||||
std::vector<PrintJob> printJobs;
|
||||
PrintUserData printUserData;
|
||||
printUserData.jobOrderList_["123"] = "456";
|
||||
printUserData.queuedJobList_["456"] = new PrintJob();
|
||||
printUserData.jobOrderList_["789"] = "1011";
|
||||
printUserData.queuedJobList_["1011"] = new PrintJob();
|
||||
printUserData.QueryAllPrintJob(printJobs);
|
||||
EXPECT_FALSE(printJobs.empty());
|
||||
size_t jobSize = 2;
|
||||
EXPECT_EQ(printJobs.size(), jobSize);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, QueryAllPrintJob_Test_WithNonExistingJob)
|
||||
{
|
||||
std::vector<PrintJob> printJobs;
|
||||
PrintUserData printUserData;
|
||||
printUserData.jobOrderList_["123"] = "456";
|
||||
printUserData.queuedJobList_["789"] = new PrintJob();
|
||||
printUserData.QueryAllPrintJob(printJobs);
|
||||
EXPECT_TRUE(printJobs.empty());
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, SetUserId_ShouldSetUserId_WhenCalled)
|
||||
{
|
||||
int32_t userId = 123;
|
||||
printUserData->SetUserId(userId);
|
||||
EXPECT_EQ(printUserData->userId_, userId);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, SetUserId_ShouldSetUserIdToZero_WhenCalledWithZero)
|
||||
{
|
||||
int32_t userId = 0;
|
||||
printUserData->SetUserId(userId);
|
||||
EXPECT_EQ(printUserData->userId_, userId);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, SetUserId_ShouldSetUserIdToNegative_WhenCalledWithNegativeValue)
|
||||
{
|
||||
int32_t userId = -123;
|
||||
printUserData->SetUserId(userId);
|
||||
EXPECT_EQ(printUserData->userId_, userId);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, SetLastUsedPrinter_ShouldReturnSuccess_WhenPrinterIdIsNotEmpty)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string printerId = "testPrinter";
|
||||
EXPECT_EQ(printUserData.SetLastUsedPrinter(printerId), E_PRINT_NONE);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, SetLastUsedPrinter_ShouldReturnFailure_WhenPrinterIdIsEmpty)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string printerId = "";
|
||||
EXPECT_EQ(printUserData.SetLastUsedPrinter(printerId), E_PRINT_INVALID_PARAMETER);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, SetLastUsedPrinter_ShouldReturnFailure_WhenSetUserDataToFileFails)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string printerId = "testPrinter";
|
||||
// Assuming SetUserDataToFile always returns false
|
||||
EXPECT_EQ(printUserData.SetLastUsedPrinter(printerId), E_PRINT_SERVER_FAILURE);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, SetLastUsedPrinter_ShouldUpdateDefaultPrinter_WhenUseLastUsedPrinterForDefaultIsTrue)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
printUserData.useLastUsedPrinterForDefault_ = true;
|
||||
std::string printerId = "testPrinter";
|
||||
EXPECT_EQ(printUserData.SetLastUsedPrinter(printerId), E_PRINT_NONE);
|
||||
EXPECT_EQ(printUserData.defaultPrinterId_, printerId);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, SetLastUsedPrinter_ShouldUpdateLastUsedPrinterId_WhenPrinterIdIsNotEmpty)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string printerId = "testPrinter";
|
||||
EXPECT_EQ(printUserData.SetLastUsedPrinter(printerId), E_PRINT_NONE);
|
||||
EXPECT_EQ(printUserData.lastUsedPrinterId_, printerId);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, testSetDefaultPrinter)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string printerId = "testPrinter";
|
||||
uint32_t type = DEFAULT_PRINTER_TYPE_SETTED_BY_USER;
|
||||
int32_t result = printUserData.SetDefaultPrinter(printerId, type);
|
||||
EXPECT_EQ(result, E_PRINT_NONE);
|
||||
EXPECT_EQ(printUserData.defaultPrinterId_, printerId);
|
||||
EXPECT_EQ(printUserData.useLastUsedPrinterForDefault_, false);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, testSetDefaultPrinterLastUsed)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string lastUsedPrinterId = "lastUsedPrinter";
|
||||
printUserData.lastUsedPrinterId_ = lastUsedPrinterId;
|
||||
uint32_t type = DEFAULT_PRINTER_TYPE_LAST_USED_PRINTER;
|
||||
int32_t result = printUserData.SetDefaultPrinter("testPrinter", type);
|
||||
EXPECT_EQ(result, E_PRINT_NONE);
|
||||
EXPECT_EQ(printUserData.defaultPrinterId_, lastUsedPrinterId);
|
||||
EXPECT_EQ(printUserData.useLastUsedPrinterForDefault_, true);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, testSetDefaultPrinterDeleteDefault)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string lastUsedPrinterId = "lastUsedPrinter";
|
||||
printUserData.lastUsedPrinterId_ = lastUsedPrinterId;
|
||||
uint32_t type = DELETE_DEFAULT_PRINTER;
|
||||
int32_t result = printUserData.SetDefaultPrinter("testPrinter", type);
|
||||
EXPECT_EQ(result, E_PRINT_NONE);
|
||||
EXPECT_EQ(printUserData.defaultPrinterId_, lastUsedPrinterId);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, testSetDefaultPrinterDeleteLastUsed)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string lastUsedPrinterId = "lastUsedPrinter";
|
||||
printUserData.lastUsedPrinterId_ = lastUsedPrinterId;
|
||||
uint32_t type = DELETE_LAST_USED_PRINTER;
|
||||
int32_t result = printUserData.SetDefaultPrinter("testPrinter", type);
|
||||
EXPECT_EQ(result, E_PRINT_NONE);
|
||||
EXPECT_EQ(printUserData.defaultPrinterId_, "testPrinter");
|
||||
}
|
||||
TEST_F(PrintUserDataTest, testSetDefaultPrinterFailure)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string printerId = "testPrinter";
|
||||
uint32_t type = DEFAULT_PRINTER_TYPE_SETTED_BY_USER;
|
||||
printUserData.SetUserDataToFile = undefined { return false; };
|
||||
int32_t result = printUserData.SetDefaultPrinter(printerId, type);
|
||||
EXPECT_EQ(result, E_PRINT_SERVER_FAILURE);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, DeletePrinter_Should_ChangeLastUsedPrinter_When_LastUsedPrinterIdMatches)
|
||||
{
|
||||
PrintUserData userData;
|
||||
userData.lastUsedPrinterId_ = "printer1";
|
||||
userData.usedPrinterList_ = {"printer1", "printer2"};
|
||||
userData.DeletePrinter("printer1");
|
||||
EXPECT_EQ(userData.lastUsedPrinterId_, "printer2");
|
||||
}
|
||||
TEST_F(PrintUserDataTest, DeletePrinter_Should_SetUserDataToFile_When_SetUserDataToFileReturnsFalse)
|
||||
{
|
||||
PrintUserData userData;
|
||||
userData.SetUserDataToFile = undefined { return false; };
|
||||
userData.DeletePrinter("printer1");
|
||||
// Assuming SetUserDataToFile has been implemented and returns false
|
||||
EXPECT_EQ(userData.SetUserDataToFile(), false);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, DeletePrinter_Should_NotChangeLastUsedPrinter_When_LastUsedPrinterIdDoesNotMatch)
|
||||
{
|
||||
PrintUserData userData;
|
||||
userData.lastUsedPrinterId_ = "printer1";
|
||||
userData.usedPrinterList_ = {"printer2"};
|
||||
userData.DeletePrinter("printer1");
|
||||
EXPECT_EQ(userData.lastUsedPrinterId_, "printer1");
|
||||
}
|
||||
TEST_F(PrintUserDataTest, DeletePrinter_Should_NotSetUserDataToFile_When_UsedPrinterListIsEmpty)
|
||||
{
|
||||
PrintUserData userData;
|
||||
userData.usedPrinterList_ = {};
|
||||
userData.DeletePrinter("printer1");
|
||||
EXPECT_EQ(userData.SetUserDataToFile(), true);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, DeletePrinterFromUsedPrinterList_Test)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string printerId = "printer1";
|
||||
printUserData.usedPrinterList_.push_back("printer1");
|
||||
printUserData.DeletePrinterFromUsedPrinterList(printerId);
|
||||
EXPECT_EQ(printUserData.usedPrinterList_.size(), 0);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, DeletePrinterFromUsedPrinterList_Test_NotPresent)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
std::string printerId = "printer2";
|
||||
printUserData.usedPrinterList_.push_back("printer1");
|
||||
printUserData.DeletePrinterFromUsedPrinterList(printerId);
|
||||
EXPECT_EQ(printUserData.usedPrinterList_.size(), 1);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, DeletePrinterFromUsedPrinterList_Test_Multiple)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
printUserData.usedPrinterList_.push_back("printer1");
|
||||
printUserData.usedPrinterList_.push_back("printer2");
|
||||
printUserData.usedPrinterList_.push_back("printer3");
|
||||
printUserData.DeletePrinterFromUsedPrinterList("printer2");
|
||||
int listSize = 2;
|
||||
EXPECT_EQ(printUserData.usedPrinterList_.size(), listSize);
|
||||
EXPECT_EQ(printUserData.usedPrinterList_[0], "printer1");
|
||||
EXPECT_EQ(printUserData.usedPrinterList_[1], "printer3");
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, ParseUserData_ShouldReturn_WhenFileDataNotAvailable)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
// Mock the GetFileData method to return false indicating file data is not available
|
||||
printUserData.GetFileData = {
|
||||
return false;
|
||||
};
|
||||
printUserData.ParseUserData();
|
||||
// Assert that the method does not crash or throw exceptions
|
||||
EXPECT_NO_THROW(printUserData.ParseUserData());
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, ParseUserData_ShouldReturn_WhenFileDataAvailableButInvalid)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
// Mock the GetFileData method to return true indicating file data is available
|
||||
printUserData.GetFileData = {
|
||||
return true;
|
||||
};
|
||||
// Mock the CheckFileData method to return false indicating file data is invalid
|
||||
printUserData.CheckFileData = [](const std::string &, nlohmann::json &) {
|
||||
return false;
|
||||
};
|
||||
printUserData.ParseUserData();
|
||||
// Assert that the method does not crash or throw exceptions
|
||||
EXPECT_NO_THROW(printUserData.ParseUserData());
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, ParseUserData_ShouldParse_WhenFileDataAvailableAndValid)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
// Mock the GetFileData method to return true indicating file data is available
|
||||
printUserData.GetFileData = {
|
||||
return true;
|
||||
};
|
||||
// Mock the CheckFileData method to return true indicating file data is valid
|
||||
printUserData.CheckFileData = [](const std::string &, nlohmann::json &) {
|
||||
return true;
|
||||
};
|
||||
// Mock the ParseUserDataFromJson method to do nothing
|
||||
printUserData.ParseUserDataFromJson = [](const nlohmann::json &) {};
|
||||
printUserData.ParseUserData();
|
||||
// Assert that the method does not crash or throw exceptions
|
||||
EXPECT_NO_THROW(printUserData.ParseUserData());
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, ParseUserDataFromJson_Test)
|
||||
{
|
||||
nlohmann::json jsonObject;
|
||||
PrintUserData printUserData;
|
||||
printUserData.ParseUserDataFromJson(jsonObject);
|
||||
EXPECT_EQ(printUserData.defaultPrinterId_, "");
|
||||
EXPECT_EQ(printUserData.lastUsedPrinterId_, "");
|
||||
EXPECT_EQ(printUserData.useLastUsedPrinterForDefault_, false);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, ParseUserDataFromJson_Test_WithUserData)
|
||||
{
|
||||
nlohmann::json jsonObject;
|
||||
jsonObject["print_user_data"] = {
|
||||
{
|
||||
"1",
|
||||
{
|
||||
{"defaultPrinter", "printer1"},
|
||||
{"lastUsedPrinter", "printer2"},
|
||||
{"useLastUsedPrinterForDefault", true},
|
||||
{"usedPrinterList", undefined{
|
||||
nlohmann::json jsonArray;jsonArray.push_back("printer3");return jsonArray;}()}
|
||||
}
|
||||
}
|
||||
};
|
||||
PrintUserData printUserData;
|
||||
printUserData.ParseUserDataFromJson(jsonObject);
|
||||
EXPECT_EQ(printUserData.defaultPrinterId_, "printer1");
|
||||
EXPECT_EQ(printUserData.lastUsedPrinterId_, "printer2");
|
||||
EXPECT_EQ(printUserData.useLastUsedPrinterForDefault_, true);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, ParseUserDataFromJson_Test_WithUserData_MissingDefaultPrinter)
|
||||
{
|
||||
nlohmann::json jsonObject;
|
||||
jsonObject["print_user_data"] = {
|
||||
{
|
||||
"1",
|
||||
{
|
||||
{"defaultPrinter", "printer1"},
|
||||
{"lastUsedPrinter", "printer2"},
|
||||
{"useLastUsedPrinterForDefault", true},
|
||||
{"usedPrinterList", undefined{
|
||||
nlohmann::json jsonArray;jsonArray.push_back("printer3");return jsonArray;}()}
|
||||
}
|
||||
}
|
||||
};
|
||||
PrintUserData printUserData;
|
||||
printUserData.ParseUserDataFromJson(jsonObject);
|
||||
EXPECT_EQ(printUserData.defaultPrinterId_, "");
|
||||
EXPECT_EQ(printUserData.lastUsedPrinterId_, "printer2");
|
||||
EXPECT_EQ(printUserData.useLastUsedPrinterForDefault_, true);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, ConvertJsonToUsedPrinterList_Test)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
nlohmann::json userData;
|
||||
userData["usedPrinterList"] = nlohmann::json::array();
|
||||
userData["usedPrinterList"].push_back("printer1");
|
||||
userData["usedPrinterList"].push_back("printer2");
|
||||
userData["usedPrinterList"].push_back("printer3");
|
||||
EXPECT_TRUE(printUserData.ConvertJsonToUsedPrinterList(userData));
|
||||
}
|
||||
TEST_F(PrintUserDataTest, ConvertJsonToUsedPrinterList_Test_Invalid)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
nlohmann::json userData;
|
||||
userData["usedPrinterList"] = nlohmann::json::array();
|
||||
int printerId = 123;
|
||||
userData["usedPrinterList"].push_back(printerId); // Invalid value
|
||||
userData["usedPrinterList"].push_back("printer2");
|
||||
EXPECT_FALSE(printUserData.ConvertJsonToUsedPrinterList(userData));
|
||||
}
|
||||
TEST_F(PrintUserDataTest, ConvertJsonToUsedPrinterList_Test_Empty)
|
||||
{
|
||||
PrintUserData printUserData;
|
||||
nlohmann::json userData;
|
||||
EXPECT_TRUE(printUserData.ConvertJsonToUsedPrinterList(userData));
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, GetFileData_ShouldReturnTrue_WhenFileIsOpenedSuccessfully)
|
||||
{
|
||||
std::string fileData;
|
||||
bool result = PrintUserData::GetFileData(fileData);
|
||||
EXPECT_TRUE(result);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, GetFileData_ShouldReturnFalse_WhenFileCannotBeOpened)
|
||||
{
|
||||
std::string fileData;
|
||||
// Mock the file path to simulate a failure to open the file
|
||||
std::string mockFilePath = "/nonexistent/path";
|
||||
bool result = PrintUserData::GetFileData(fileData);
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, GetFileData_ShouldReturnTrue_WhenFileIsCreatedSuccessfully)
|
||||
{
|
||||
std::string fileData;
|
||||
// Mock the file path to simulate a file creation scenario
|
||||
std::string mockFilePath = "/tmp/nonexistent/path";
|
||||
bool result = PrintUserData::GetFileData(fileData);
|
||||
EXPECT_TRUE(result);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, GetFileData_ShouldReturnFalse_WhenFileCannotBeCreated)
|
||||
{
|
||||
std::string fileData;
|
||||
// Mock the file path to simulate a failure to create the file
|
||||
std::string mockFilePath = "/tmp/nonexistent/path/with/no/permission";
|
||||
bool result = PrintUserData::GetFileData(fileData);
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, GetFileData_ShouldReturnTrue_WhenFileDataIsReadSuccessfully)
|
||||
{
|
||||
std::string fileData;
|
||||
// Mock the file path to simulate a successful read scenario
|
||||
std::string mockFilePath = "/tmp/existing/file";
|
||||
bool result = PrintUserData::GetFileData(fileData);
|
||||
EXPECT_TRUE(result);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, GetFileData_ShouldReturnFalse_WhenFileDataCannotBeRead)
|
||||
{
|
||||
std::string fileData;
|
||||
// Mock the file path to simulate a failure to read the file
|
||||
std::string mockFilePath = "/tmp/existing/file/with/no/permission";
|
||||
bool result = PrintUserData::GetFileData(fileData);
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
|
||||
TEST_F(PrintUserDataTest, CheckFileData_ShouldReturnFalse_WhenFileDataIsNotAcceptable)
|
||||
{
|
||||
std::string fileData = "invalid json";
|
||||
nlohmann::json jsonObject;
|
||||
bool result = PrintUserData::CheckFileData(fileData, jsonObject);
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, CheckFileData_ShouldReturnFalse_WhenVersionIsNotPresent)
|
||||
{
|
||||
std::string fileData = "{" key ":" value "}";
|
||||
nlohmann::json jsonObject;
|
||||
bool result = PrintUserData::CheckFileData(fileData, jsonObject);
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, CheckFileData_ShouldReturnFalse_WhenVersionIsNotString)
|
||||
{
|
||||
std::string fileData = "{" version ":123}";
|
||||
nlohmann::json jsonObject;
|
||||
bool result = PrintUserData::CheckFileData(fileData, jsonObject);
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, CheckFileData_ShouldReturnFalse_WhenPrintUserDataIsNotPresent)
|
||||
{
|
||||
std::string fileData = "{" version ":" 1.0 "}";
|
||||
nlohmann::json jsonObject;
|
||||
bool result = PrintUserData::CheckFileData(fileData, jsonObject);
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
TEST_F(PrintUserDataTest, CheckFileData_ShouldReturnTrue_WhenAllConditionsAreMet)
|
||||
{
|
||||
std::string fileData = "{" version ":" 1.0 "," print_user_data ":" data "}";
|
||||
nlohmann::json jsonObject;
|
||||
bool result = PrintUserData::CheckFileData(fileData, jsonObject);
|
||||
EXPECT_TRUE(result);
|
||||
}
|
||||
|
||||
} // namespace OHOS::Print
|
Loading…
Reference in New Issue
Block a user