public override void doIt(MArgList args) { MArgDatabase argData = new MArgDatabase(syntax, args); if (argData.isFlagSet(deregisterFlag)) { string eventName = argData.flagArgumentString(deregisterFlag, 0); MUserEventMessage.deregisterUserEvent(eventName); } else if (argData.isFlagSet(registerFlag)) { // Register the new event and add two fixed callbacks to it. string eventName = argData.flagArgumentString(registerFlag, 0); if (!MUserEventMessage.isUserEvent(eventName)) { MUserEventMessage.registerUserEvent(eventName); userCB cb1 = new userCB(); cb1.clientData = "Sample Client Data (an MString object)"; MUserEventMessage.UserEvent[eventName] += cb1.userCallback1; MUserEventMessage.UserEvent[eventName] += cb1.userCallback2; } } else if (argData.isFlagSet(postFlag)) { string eventName = argData.flagArgumentString(postFlag, 0); MUserEventMessage.postUserEvent(eventName); } else if (argData.isFlagSet(testFlag)) { runTests(); } return; }
public void runTests() { // Test 1: Try to register callback for nonexistent event MGlobal.displayInfo("Starting Test 1"); userCB cb = new userCB(); try { MUserEventMessage.UserEvent["TestEvent"] += cb.userCallback1; MGlobal.displayInfo("Test 1 failed"); } catch (System.Exception) { MGlobal.displayInfo("Test 1 passed"); } // Test 2: Register and deregister an event // - Expected output: Entered userMessage.userCallback1 MGlobal.displayInfo("Starting Test 2"); MUserEventMessage.registerUserEvent("TestEvent"); MUserEventMessage.UserEvent["TestEvent"] += cb.userCallback1; MUserEventMessage.postUserEvent("TestEvent"); MUserEventMessage.deregisterUserEvent("TestEvent"); // Test 3: The event should be gone MGlobal.displayInfo("Starting Test 3"); try { MUserEventMessage.UserEvent["TestEvent"] += cb.userCallback1; MGlobal.displayInfo("Test 3 failed"); } catch (System.Exception) { MGlobal.displayInfo("Test 3 passed"); } // Test 4: Try adding multiple callbacks to an event // Expected output: Entered userMessage.userCallback1 // Entered userMessage.userCallback2 MGlobal.displayInfo("Starting Test 4"); MUserEventMessage.registerUserEvent("TestEvent"); MUserEventMessage.UserEvent["TestEvent"] += cb.userCallback1; MUserEventMessage.UserEvent["TestEvent"] += cb.userCallback2; MUserEventMessage.postUserEvent("TestEvent"); MUserEventMessage.deregisterUserEvent("TestEvent"); // Test 5: Try adding and posting to multiple events // Expected output: Posting first event // Entered userMessage.userCallback1 // Entered userMessage.userCallback2 // Posting second event // Entered userMessage.userCallback1 // Entered userMessage.userCallback2 MGlobal.displayInfo("Starting Test 5"); MUserEventMessage.registerUserEvent("TestEvent"); MUserEventMessage.registerUserEvent("TestEvent2"); MUserEventMessage.UserEvent["TestEvent"] += cb.userCallback1; MUserEventMessage.UserEvent["TestEvent"] += cb.userCallback2; MUserEventMessage.UserEvent["TestEvent2"] += cb.userCallback1; MUserEventMessage.UserEvent["TestEvent2"] += cb.userCallback2; MGlobal.displayInfo("Posting first event"); MUserEventMessage.postUserEvent("TestEvent"); MGlobal.displayInfo("Posting second event"); MUserEventMessage.postUserEvent("TestEvent2"); MUserEventMessage.deregisterUserEvent("TestEvent"); MUserEventMessage.deregisterUserEvent("TestEvent2"); MGlobal.displayInfo("Completed all tests"); }