Search code examples
xamarinxamarin.formsxamarin.uitestmessagingcenter

Is it possible to use MessagingCenter or Intents with Xamarin.UI tests?


I am writing Xamarin.UI tests for my Xamarin.Forms application. I am wondering if it is possible to simulate messages from MessagingCenter with tests? Or maybe send an Intent that will be captured by the Android application?

I would like to simulate the inputs/outputs from some hardware, which is communicating it's status with MessagingCenter inside my application.

If anyone has encountered a similar situation or has determined that it is impossible to do this, I would be glad if they share their experience.

Many thanks :)


Solution

  • So, I have managed to achieve was I was looking for with a neat thing that I didn't know of until now -> Backdoor methods

    I have added a backdoor method to my MainActivity.cs, which receives the hardware status from the UITests, and then sends it to my Forms application via MessagingCenter.

    Backdoor method in MainActivity.cs:

    [Export("BackdoorSendStatus")]
    public void BackdoorSendStatus(string status)
    {
        // In through the backdoor - do some work.
        MessagingCenter.Send(App.Instance, "StatusTopic", status);
    }
    

    Test case that uses this metod:

    [Test]
    public void Test_Backdoor()
    {
        App.Invoke("BackdoorSendStatus", "OFFLINE");
        Thread.Sleep(5000);
        App.Invoke("BackdoorSendStatus", "ONLINE");
    }