Skip to content

Commit 96cb830

Browse files
add-uosdeepin-bot[bot]
authored andcommitted
test: enhance unit tests for computer plugin
- Added comprehensive tests for ComputerStatusBar, ensuring proper construction with and without parent, correct message display, inheritance from BasicStatusBar, and signal-slot functionality. - Improved tests for ComputerViewContainer, validating constructor behavior, root URL handling, view state, and layout setup. - Expanded ProtocolEntryFileEntity tests to cover multiple method calls, error handling, and consistency checks with special characters. - Enhanced UserEntryFileEntity tests to ensure correct handling of multiple method calls, memory management, and user directory properties. - disable death tests in AppExitController unit tests
1 parent 894a0a4 commit 96cb830

13 files changed

+2645
-97
lines changed

autotests/plugins/dfmplugin-computer/test_appentryfileentity.cpp

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,222 @@ TEST_F(UT_AppEntryFileEntity, DesktopInfo_NullPointer_HandlesGracefully)
435435
entity->extraProperties();
436436
});
437437
}
438+
439+
TEST_F(UT_AppEntryFileEntity, MultipleMethodCalls_DifferentParameters_HandlesCorrectly)
440+
{
441+
createEntity();
442+
443+
QString mockDisplayName = "Test App";
444+
QString mockIconName = "test-app-icon";
445+
QString mockExecCommand = "test-app --param";
446+
447+
// Mock all methods
448+
int displayNameCallCount = 0;
449+
int iconCallCount = 0;
450+
int existsCallCount = 0;
451+
int extraPropertiesCallCount = 0;
452+
453+
stub.set_lamda(&DesktopFile::desktopDisplayName, [&]() -> QString {
454+
__DBG_STUB_INVOKE__
455+
displayNameCallCount++;
456+
return mockDisplayName;
457+
});
458+
459+
stub.set_lamda(&DesktopFile::desktopIcon, [&]() -> QString {
460+
__DBG_STUB_INVOKE__
461+
iconCallCount++;
462+
return mockIconName;
463+
});
464+
465+
stub.set_lamda(static_cast<bool (QFile::*)() const>(&QFile::exists), [&]() -> bool {
466+
__DBG_STUB_INVOKE__
467+
existsCallCount++;
468+
return true;
469+
});
470+
471+
stub.set_lamda(&DesktopFile::desktopExec, [&]() -> QString {
472+
__DBG_STUB_INVOKE__
473+
extraPropertiesCallCount++;
474+
return mockExecCommand;
475+
});
476+
477+
// Call multiple methods
478+
QString displayName = entity->displayName();
479+
QIcon icon = entity->icon();
480+
bool exists = entity->exists();
481+
QVariantHash extraProps = entity->extraProperties();
482+
483+
// Verify all methods were called
484+
EXPECT_EQ(displayNameCallCount, 1);
485+
EXPECT_EQ(iconCallCount, 1);
486+
EXPECT_EQ(existsCallCount, 1);
487+
EXPECT_EQ(extraPropertiesCallCount, 1);
488+
489+
// Verify results
490+
EXPECT_EQ(displayName, mockDisplayName);
491+
EXPECT_FALSE(icon.isNull());
492+
EXPECT_TRUE(exists);
493+
EXPECT_TRUE(extraProps.contains(ExtraPropertyName::kExecuteCommand));
494+
}
495+
496+
TEST_F(UT_AppEntryFileEntity, ErrorHandling_InvalidDesktopFile_HandlesGracefully)
497+
{
498+
// Mock ComputerUtils::getAppEntryFileUrl to return invalid URL
499+
stub.set_lamda(&ComputerUtils::getAppEntryFileUrl, [this](const QUrl &) -> QUrl {
500+
__DBG_STUB_INVOKE__
501+
return QUrl(); // Invalid URL
502+
});
503+
504+
// Create entity with invalid desktop file
505+
entity = new AppEntryFileEntity(testUrl);
506+
507+
// Mock DesktopFile methods to throw exceptions or return invalid data
508+
stub.set_lamda(&DesktopFile::desktopDisplayName, [&]() -> QString {
509+
__DBG_STUB_INVOKE__
510+
return ""; // Empty display name
511+
});
512+
513+
stub.set_lamda(&DesktopFile::desktopIcon, [&]() -> QString {
514+
__DBG_STUB_INVOKE__
515+
return ""; // Empty icon name
516+
});
517+
518+
stub.set_lamda(&DesktopFile::desktopExec, [&]() -> QString {
519+
__DBG_STUB_INVOKE__
520+
return ""; // Empty exec command
521+
});
522+
523+
// Test that methods handle invalid data gracefully
524+
EXPECT_NO_THROW({
525+
QString displayName = entity->displayName();
526+
QIcon icon = entity->icon();
527+
QVariantHash extraProps = entity->extraProperties();
528+
529+
EXPECT_TRUE(displayName.isEmpty());
530+
EXPECT_TRUE(icon.isNull());
531+
EXPECT_TRUE(extraProps.contains(ExtraPropertyName::kExecuteCommand));
532+
EXPECT_TRUE(extraProps.value(ExtraPropertyName::kExecuteCommand).toString().isEmpty());
533+
});
534+
}
535+
536+
TEST_F(UT_AppEntryFileEntity, QtMetaObject_CorrectlyInitialized_Success)
537+
{
538+
createEntity();
539+
540+
// Test that Qt meta-object system works correctly
541+
const QMetaObject *metaObject = entity->metaObject();
542+
EXPECT_NE(metaObject, nullptr);
543+
544+
// Test class name
545+
EXPECT_STREQ(metaObject->className(), "dfmplugin_computer::AppEntryFileEntity");
546+
547+
// Test that inherited methods exist in meta-object
548+
EXPECT_GE(metaObject->indexOfMethod("displayName()"), 0);
549+
EXPECT_GE(metaObject->indexOfMethod("icon()"), 0);
550+
EXPECT_GE(metaObject->indexOfMethod("exists()"), 0);
551+
EXPECT_GE(metaObject->indexOfMethod("showProgress()"), 0);
552+
EXPECT_GE(metaObject->indexOfMethod("showTotalSize()"), 0);
553+
EXPECT_GE(metaObject->indexOfMethod("showUsageSize()"), 0);
554+
EXPECT_GE(metaObject->indexOfMethod("description()"), 0);
555+
EXPECT_GE(metaObject->indexOfMethod("order()"), 0);
556+
EXPECT_GE(metaObject->indexOfMethod("extraProperties()"), 0);
557+
EXPECT_GE(metaObject->indexOfMethod("isAccessable()"), 0);
558+
}
559+
560+
TEST_F(UT_AppEntryFileEntity, Inheritance_FromAbstractEntryFileEntity_WorksCorrectly)
561+
{
562+
createEntity();
563+
564+
// Test that AppEntryFileEntity is properly inherited from AbstractEntryFileEntity
565+
AbstractEntryFileEntity *baseEntity = entity;
566+
EXPECT_NE(baseEntity, nullptr);
567+
568+
// Test that we can call base class methods
569+
// AbstractEntryFileEntity doesn't have url() method, so we test other methods
570+
EXPECT_NO_THROW(baseEntity->displayName());
571+
EXPECT_NO_THROW(baseEntity->displayName());
572+
EXPECT_NO_THROW(baseEntity->icon());
573+
EXPECT_NO_THROW(baseEntity->exists());
574+
EXPECT_NO_THROW(baseEntity->showProgress());
575+
EXPECT_NO_THROW(baseEntity->showTotalSize());
576+
EXPECT_NO_THROW(baseEntity->showUsageSize());
577+
EXPECT_NO_THROW(baseEntity->description());
578+
EXPECT_NO_THROW(baseEntity->order());
579+
EXPECT_NO_THROW(baseEntity->extraProperties());
580+
EXPECT_NO_THROW(baseEntity->isAccessable());
581+
}
582+
583+
TEST_F(UT_AppEntryFileEntity, MemoryManagement_DeleteEntity_CleansUpCorrectly)
584+
{
585+
createEntity();
586+
587+
// Store pointer to entity for testing
588+
AppEntryFileEntity *entityPtr = entity;
589+
590+
// Delete entity
591+
delete entity;
592+
entity = nullptr;
593+
594+
// The entity should be deleted, but we can't directly test this
595+
// We just verify that the delete operation doesn't crash
596+
EXPECT_EQ(entity, nullptr);
597+
}
598+
599+
TEST_F(UT_AppEntryFileEntity, SpecialCharacters_InExecCommand_HandlesCorrectly)
600+
{
601+
createEntity();
602+
603+
// Test exec command with various special characters
604+
QString execWithSpecialChars = "test-app \"--option\" '--param=value' %f %U";
605+
QString expectedCleanCommand = "test-app --option --param=value ";
606+
607+
// Mock DesktopFile::desktopExec
608+
stub.set_lamda(&DesktopFile::desktopExec, [&execWithSpecialChars]() -> QString {
609+
__DBG_STUB_INVOKE__
610+
return execWithSpecialChars;
611+
});
612+
613+
// Test extraProperties to trigger getFormattedExecCommand
614+
QVariantHash result = entity->extraProperties();
615+
QString executeCommand = result.value(ExtraPropertyName::kExecuteCommand).toString();
616+
617+
// Verify special characters are handled correctly
618+
EXPECT_EQ(executeCommand, expectedCleanCommand);
619+
}
620+
621+
TEST_F(UT_AppEntryFileEntity, Consistency_MultipleCalls_ReturnConsistentResults)
622+
{
623+
createEntity();
624+
625+
QString mockDisplayName = "Test App";
626+
QString mockIconName = "test-app-icon";
627+
628+
// Mock methods to return consistent values
629+
stub.set_lamda(&DesktopFile::desktopDisplayName, [&mockDisplayName]() -> QString {
630+
__DBG_STUB_INVOKE__
631+
return mockDisplayName;
632+
});
633+
634+
stub.set_lamda(&DesktopFile::desktopIcon, [&mockIconName]() -> QString {
635+
__DBG_STUB_INVOKE__
636+
return mockIconName;
637+
});
638+
639+
// Call methods multiple times
640+
QString displayName1 = entity->displayName();
641+
QString displayName2 = entity->displayName();
642+
QString displayName3 = entity->displayName();
643+
644+
QIcon icon1 = entity->icon();
645+
QIcon icon2 = entity->icon();
646+
QIcon icon3 = entity->icon();
647+
648+
// Verify consistency
649+
EXPECT_EQ(displayName1, mockDisplayName);
650+
EXPECT_EQ(displayName2, mockDisplayName);
651+
EXPECT_EQ(displayName3, mockDisplayName);
652+
653+
EXPECT_FALSE(icon1.isNull());
654+
EXPECT_FALSE(icon2.isNull());
655+
EXPECT_FALSE(icon3.isNull());
656+
}

0 commit comments

Comments
 (0)