2026-11-18 09:22:04 +08:00
// Offline widget tests: never creates a RobotClient or dispatches an RPC.
# include "../src/taskorchestrationwidget.cpp"
# include <stdexcept>
# include <cstdio>
# include "../src/taskdeviceoptions.h"
# include <QTabWidget>
# include <QGroupBox>
static void check ( bool ok , const char * text ) { if ( ! ok ) throw std : : runtime_error ( text ) ; }
int main ( int argc , char * * argv ) {
QApplication app ( argc , argv ) ; ApplicationStyle : : install ( app ) ;
try {
const QVariantMap six { { " jointNames " , QStringList { " shoulder_pan " , " shoulder_lift " , " elbow " , " wrist_1 " , " wrist_2 " , " wrist_3 " } } ,
{ " jointLimits " , QVariantMap { { " elbow " , QVariantMap { { " lower " , - 1.0 } , { " upper " , 1.0 } } } } } } ;
TaskJointEditor joints ; joints . setDevice ( six ) ; QString error ; QJsonArray out ;
check ( ! joints . values ( out , error ) , " empty targets must not silently become zero " ) ;
auto * table = joints . findChild < QTableWidget * > ( ) ; check ( table - > rowCount ( ) = = 6 , " six actual joint rows " ) ;
check ( table - > item ( 2 , 0 ) - > text ( ) = = " elbow " , " joint name stays next to target " ) ;
for ( int i = 0 ; i < 6 ; + + i ) qobject_cast < QLineEdit * > ( table - > cellWidget ( i , 1 ) ) - > setText ( " 30 " ) ;
error . clear ( ) ; check ( joints . values ( out , error ) , " valid angle targets " ) ; check ( std : : abs ( out [ 2 ] . toDouble ( ) - M_PI / 6 ) < 1e-10 , " degrees become radians " ) ;
qobject_cast < QLineEdit * > ( table - > cellWidget ( 2 , 1 ) ) - > setText ( " 90 " ) ; error . clear ( ) ; check ( ! joints . values ( out , error ) , " server limits enforced " ) ;
joints . setDevice ( six , QJsonArray { 0 , 0 } ) ; error . clear ( ) ; check ( ! joints . values ( out , error ) , " mismatched target count requires fresh inputs " ) ;
joints . setDevice ( { } ) ; error . clear ( ) ; check ( ! joints . values ( out , error ) , " missing device metadata blocks position input " ) ;
TaskProtoEditor calibration ; calibration . setDeviceContext ( six ) ;
calibration . setOperation ( " cmvr.api.ArmService " , " calibrateZeroQ " ) ;
auto * selector = calibration . findChild < QComboBox * > ( " taskJointSelector " ) ;
check ( selector & & ! selector - > isEditable ( ) & & selector - > count ( ) = = 7 , " calibration selects actual device joints " ) ;
QJsonObject request ; error . clear ( ) ; check ( ! calibration . request ( request , error ) , " calibration requires an explicit selection " ) ;
selector - > setCurrentIndex ( selector - > findData ( " elbow " ) ) ; error . clear ( ) ;
check ( calibration . request ( request , error ) & & request . value ( " joint_name " ) = = " elbow " , " selected joint name is serialized " ) ;
calibration . setDeviceContext ( { { " jointNames " , QStringList { " L1 " , " L2 " } } } ) ;
calibration . setOperation ( " cmvr.api.ArmService " , " calibrateZeroQ " , request ) ; error . clear ( ) ;
check ( ! calibration . request ( request , error ) , " switching device never substitutes a different joint silently " ) ;
calibration . setDeviceContext ( { } ) ; calibration . setOperation ( " cmvr.api.ArmService " , " calibrateZeroQ " ) ; error . clear ( ) ;
selector = calibration . findChild < QComboBox * > ( " taskJointSelector " ) ;
check ( selector & & ! selector - > isEnabled ( ) & & ! calibration . request ( request , error ) , " missing metadata disables calibration selector " ) ;
const auto decoded = TaskDeviceOptions : : decode ( " ListMotors " , { { " motors " , QJsonArray { QJsonObject { { " nodeId " , 7 } , { " jointName " , " drive_left " } } } } } ) ;
TaskProtoEditor motor ; motor . setDeviceContext ( decoded ) ; motor . setOperation ( " cmvr.api.MotorService " , " setMode " ) ;
auto * motors = motor . findChild < QComboBox * > ( " taskMotorSelector " ) ; check ( motors & & motors - > count ( ) = = 2 , " motor list from real metadata " ) ;
motors - > setCurrentIndex ( 1 ) ; error . clear ( ) ; check ( motor . request ( request , error ) , " motor request valid " ) ;
check ( request . value ( " node_id " ) . toInt ( ) = = 7 & & request . value ( " joint_name " ) = = " drive_left " , " motor identity serialized together " ) ;
const auto stationData = TaskDeviceOptions : : decode ( " listStations " , { { " stations " , QJsonArray { QJsonObject { { " id " , " station-a " } , { " description " , QStringLiteral ( " 工作站 A " ) } } } } } ) ;
TaskProtoEditor nav ; nav . setDeviceContext ( stationData ) ; nav . setOperation ( " cmvr.api.AgvService " , " navigateToStation " ) ;
auto * stations = nav . findChild < QComboBox * > ( " taskOption_station_id " ) ; check ( stations & & stations - > findData ( " station-a " ) > 0 , " station picker uses service response " ) ;
stations - > setCurrentIndex ( 1 ) ; error . clear ( ) ; check ( nav . request ( request , error ) & & request . value ( " station_id " ) = = " station-a " , " station id remains machine-readable " ) ;
TaskProtoEditor map ; map . setDeviceContext ( TaskDeviceOptions : : decode ( " listMaps " , { { " maps " , QJsonArray { " factory " } } } ) ) ; map . setOperation ( " cmvr.api.AgvService " , " switchMap " ) ;
check ( map . findChild < QComboBox * > ( " taskOption_map_name " ) - > findData ( " factory " ) > 0 , " existing map picker " ) ;
const auto coordinateData = TaskDeviceOptions : : decode ( " ListUserCoordinateSystems " , { { " coordinates " , QJsonArray { QJsonObject { { " name " , QStringLiteral ( " 工件 A " ) } , { " revision " , " 2 " } } } } } ) ;
TaskProtoEditor coordinateEditor ; coordinateEditor . setDeviceContext ( coordinateData ) ; coordinateEditor . setOperation ( " cmvr.api.ArmService " , " ApplyUserCoordinateSystem " ) ;
auto * coordinate = coordinateEditor . findChild < QComboBox * > ( " taskOption_name " ) ; check ( coordinate & & coordinate - > count ( ) = = 2 , " saved coordinate picker " ) ;
coordinate - > setCurrentIndex ( 1 ) ; error . clear ( ) ; check ( coordinateEditor . request ( request , error ) & & request . value ( " name " ) = = QStringLiteral ( " 工件 A " ) , " saved coordinate request valid " ) ;
TaskProtoEditor longForm ; longForm . setDeviceContext ( coordinateData ) ;
const QJsonObject pose { { " x " , .1 } , { " y " , .2 } , { " z " , .3 } , { " rx " , 0 } , { " ry " , 0 } , { " rz " , 0 } } ;
longForm . setOperation ( " cmvr.api.ArmService " , " moveL " , { { " target " , pose } } ) ;
auto * tabs = longForm . findChild < QTabWidget * > ( " taskParameterTabs " ) ; check ( tabs & & tabs - > count ( ) > = 3 , " long forms split into meaningful tabs " ) ;
check ( longForm . findChildren < QScrollArea * > ( ) . isEmpty ( ) , " no nested editor scroll area " ) ;
for ( auto * group : longForm . findChildren < QGroupBox * > ( ) ) if ( group - > property ( " taskMessageEnabled " ) . isValid ( ) ) check ( ! group - > isCheckable ( ) , " message sections no longer require checkbox activation " ) ;
tabs - > setCurrentIndex ( 2 ) ; auto state = longForm . inputState ( ) ; longForm . setOperation ( " cmvr.api.ArmService " , " moveL " , { { " target " , pose } } ) ; longForm . restoreInputState ( state ) ;
tabs = longForm . findChild < QTabWidget * > ( " taskParameterTabs " ) ; check ( tabs - > currentIndex ( ) = = 2 , " metadata refresh retains active tab " ) ;
error . clear ( ) ; check ( longForm . request ( request , error ) & & request . value ( " target " ) . toObject ( ) = = pose , " all tabs serialize into one unchanged RPC request " ) ;
QLineEdit * poseX = nullptr ; for ( auto * edit : longForm . findChildren < QLineEdit * > ( ) ) if ( edit - > property ( " taskField " ) . toString ( ) = = " x " ) poseX = edit ;
check ( poseX , " pose x control exists " ) ; poseX - > setText ( " incomplete " ) ; state = longForm . inputState ( ) ;
longForm . setOperation ( " cmvr.api.ArmService " , " moveL " , { { " target " , pose } } ) ; longForm . restoreInputState ( state ) ; poseX = nullptr ;
for ( auto * edit : longForm . findChildren < QLineEdit * > ( ) ) if ( edit - > property ( " taskField " ) . toString ( ) = = " x " ) poseX = edit ;
check ( poseX & & poseX - > text ( ) = = " incomplete " , " refresh preserves even incomplete input " ) ;
const auto baseFrames = TaskDeviceOptions : : decode ( " ListBaseFrame " , { { " frameNames " , QJsonArray { " robot_base " , " fixture " } } } ) ;
TaskProtoEditor poseQuery ; poseQuery . setDeviceContext ( baseFrames ) ; poseQuery . setOperation ( " cmvr.api.ArmService " , " getPose " ) ;
check ( poseQuery . findChild < QComboBox * > ( " taskOption_base_link " ) - > findData ( " fixture " ) > 0 , " frame response aliases supported " ) ;
// Build every supported form and check the user-facing dictionary recursively.
const auto catalog = TaskApi : : catalog ( ) ; QSet < QString > seen ; int count = 0 ;
std : : function < void ( const QString & ) > inspect = [ & ] ( const QString & name ) {
if ( seen . contains ( name ) ) return ; seen . insert ( name ) ;
for ( const auto & field : catalog . value ( " messages " ) . toObject ( ) . value ( name ) . toObject ( ) . value ( " fields " ) . toArray ( ) ) {
const auto f = field . toObject ( ) ; const auto key = f . value ( " name " ) . toString ( ) ;
check ( TaskUi : : text ( " fields " , key ) ! = key , qPrintable ( " untranslated field: " + key ) ) ;
if ( f . contains ( " messageType " ) ) inspect ( f . value ( " messageType " ) . toString ( ) ) ;
}
} ;
for ( const auto & service : catalog . value ( " services " ) . toArray ( ) ) for ( const auto & value : service . toObject ( ) . value ( " methods " ) . toArray ( ) ) {
const auto m = value . toObject ( ) ; if ( m . value ( " clientStreaming " ) . toBool ( ) | | m . value ( " serverStreaming " ) . toBool ( ) ) continue ;
check ( TaskUi : : text ( " operations " , m . value ( " method " ) . toString ( ) ) ! = m . value ( " method " ) . toString ( ) , " translated operation " ) ; inspect ( m . value ( " requestType " ) . toString ( ) ) ;
TaskProtoEditor editor ; editor . setDeviceContext ( six ) ; editor . setOperation ( m . value ( " service " ) . toString ( ) , m . value ( " method " ) . toString ( ) ) ; + + count ;
}
check ( count = = 124 , " all unary editors covered " ) ;
TaskOrchestrationWidget owner ; owner . setDeviceInfo ( " right_arm " , six ) ;
owner . setDeviceInfo ( " left_arm " , { { " jointNames " , QStringList { " L1 " , " L2 " , " L3 " , " L4 " , " L5 " , " L6 " , " L7 " } } } ) ;
const QVariantList devices { QVariantMap { { " deviceId " , " right_arm " } , { " type " , 6 } } , QVariantMap { { " deviceId " , " left_arm " } , { " type " , 6 } } } ;
QString name = QStringLiteral ( " 关节定位 " ) , type = QStringLiteral ( " 机械臂 " ) , id = " right_arm " , action = " MoveJ " , unit = " ms " ;
QJsonObject params { { " positions " , QJsonArray { 0 , 0 , 0 , 0 , 0 , 0 } } , { " velocity " , .1 } , { " acceleration " , .1 } } ; int wait = 0 ; QString failure ;
QTimer : : singleShot ( 100 , [ & ] {
try {
auto * dialog = qobject_cast < QDialog * > ( QApplication : : activeModalWidget ( ) ) ; check ( dialog , " task dialog opened " ) ;
auto * device = dialog - > findChild < QComboBox * > ( " taskDeviceCombo " ) ; auto * operation = dialog - > findChild < QComboBox * > ( " taskActionCombo " ) ;
auto * jointTable = dialog - > findChild < QTableWidget * > ( " taskJointTable " ) ; check ( jointTable & & jointTable - > rowCount ( ) = = 6 , " dialog uses device joints " ) ;
check ( operation - > currentText ( ) = = QStringLiteral ( " 关节角度运动 " ) , " friendly operation title " ) ;
qobject_cast < QLineEdit * > ( jointTable - > cellWidget ( 0 , 1 ) ) - > setText ( " 45 " ) ;
device - > setCurrentIndex ( 1 ) ; check ( jointTable - > rowCount ( ) = = 7 , " changing device changes DOF " ) ;
check ( qobject_cast < QLineEdit * > ( jointTable - > cellWidget ( 0 , 1 ) ) - > text ( ) . isEmpty ( ) , " new device does not inherit targets " ) ;
device - > setCurrentIndex ( 0 ) ; check ( jointTable - > rowCount ( ) = = 6 , " switch back restores rows " ) ;
check ( qobject_cast < QLineEdit * > ( jointTable - > cellWidget ( 0 , 1 ) ) - > text ( ) = = " 45 " , " per-device draft preserved " ) ;
QApplication : : processEvents ( ) ;
dialog - > grab ( ) . save ( " build/tests/task-joints-preview.png " ) ;
operation - > setCurrentIndex ( operation - > findData ( QStringLiteral ( " 接口 · moveJ " ) ) ) ;
auto tables = dialog - > findChildren < QTableWidget * > ( " taskJointTable " ) ; check ( tables . size ( ) > = 2 , " direct RPC also uses joint rows " ) ;
for ( auto * t : tables ) if ( t ! = jointTable ) check ( t - > rowCount ( ) = = 6 , " direct RPC uses actual DOF " ) ;
operation - > setCurrentIndex ( operation - > findData ( QStringLiteral ( " 接口 · moveL " ) ) ) ;
auto * parameterTabs = dialog - > findChild < QTabWidget * > ( " taskParameterTabs " ) ; check ( parameterTabs & & parameterTabs - > count ( ) > = 3 , " modal uses parameter tabs " ) ;
check ( dialog - > findChildren < QScrollArea * > ( ) . size ( ) = = 1 , " one scrolling surface in the entire dialog " ) ;
QApplication : : processEvents ( ) ; check ( parameterTabs - > parentWidget ( ) - > height ( ) > = parameterTabs - > height ( ) , " tab content must not be clipped by editor " ) ; dialog - > grab ( ) . save ( " build/tests/task-movel-basic-preview.png " ) ;
parameterTabs - > setCurrentIndex ( 2 ) ; QApplication : : processEvents ( ) ; dialog - > grab ( ) . save ( " build/tests/task-movel-coordinates-preview.png " ) ;
operation - > setCurrentIndex ( operation - > findData ( " MoveJ " ) ) ;
auto * buttons = dialog - > findChild < QDialogButtonBox * > ( ) ; check ( buttons , " fixed dialog footer " ) ;
auto * scroll = dialog - > findChild < QScrollArea * > ( " taskParameterScroll " ) ; QApplication : : processEvents ( ) ;
QCoreApplication : : sendPostedEvents ( nullptr , QEvent : : LayoutRequest ) ; QApplication : : processEvents ( ) ; dialog - > grab ( ) . save ( " build/tests/task-legacy-footer-preview.png " ) ;
check ( scroll - > mapTo ( dialog , QPoint ( 0 , scroll - > height ( ) ) ) . y ( ) < buttons - > mapTo ( dialog , QPoint ( ) ) . y ( ) , " footer must not overlap parameter area " ) ;
qobject_cast < QLineEdit * > ( jointTable - > cellWidget ( 0 , 1 ) ) - > clear ( ) ;
QTimer : : singleShot ( 0 , [ dialog ] { if ( auto * notice = qobject_cast < QDialog * > ( QApplication : : activeModalWidget ( ) ) ) if ( notice ! = dialog ) notice - > accept ( ) ; } ) ;
buttons - > button ( QDialogButtonBox : : Ok ) - > click ( ) ; check ( dialog - > isVisible ( ) , " invalid input keeps the task dialog open " ) ;
qobject_cast < QLineEdit * > ( jointTable - > cellWidget ( 0 , 1 ) ) - > setText ( " 45 " ) ; buttons - > button ( QDialogButtonBox : : Ok ) - > click ( ) ;
} catch ( const std : : exception & e ) { failure = e . what ( ) ; std : : fprintf ( stderr , " MODAL FAILURE: %s \n " , e . what ( ) ) ; if ( auto * d = qobject_cast < QDialog * > ( QApplication : : activeModalWidget ( ) ) ) d - > reject ( ) ; }
} ) ;
const bool accepted = editTaskDialog ( & owner , name , type , id , action , params , wait , unit , devices , { } ) ; check ( accepted , qPrintable ( failure ) ) ;
check ( std : : abs ( params . value ( " positions " ) . toArray ( ) [ 0 ] . toDouble ( ) - M_PI / 4 ) < 1e-10 , " saved legacy request remains radians " ) ;
2026-11-25 17:25:39 +08:00
if ( argc > 1 ) { owner . resize ( 1480 , 920 ) ; owner . show ( ) ; QApplication : : processEvents ( ) ; owner . grab ( ) . save ( " build/tests/task-workspace.png " ) ; }
2026-11-18 09:22:04 +08:00
std : : printf ( " PASS: 124 forms; joint/motor/station/map/coordinate selectors; tabs and one scroll area; no clipping or footer overlap; partial-input retention; JSON requests; no RPCs \n " ) ; return 0 ;
} catch ( const std : : exception & e ) { std : : fprintf ( stderr , " FAIL: %s \n " , e . what ( ) ) ; return 1 ; }
}