简体   繁体   中英

Android Robotium Sort Tester

I'm trying to write some code in java robitum which will test and check that my sort button is working correctly.

I do this is by activating the sort button, then iterate through the sorted array to make checks on the current value against the next value. The problem is that I'm comparing strings such as "Tenancy 8 & Tenancy 12", where it is then telling me that Tenancy 8 > Tenancy 12.

I know this is happening because software sorts based on ASCII, but I was wondering how I could get around this issue. My code is as follows:

        solo.clickLongOnView(tenancy_sort);

    for(int i = 0; i<9; i++)
    {
        TextView tenancy_current = (TextView)
                solo.getView(R.id.device_selector_row_tenancy_text,i);
        TextView tenancy_next = (TextView)
                solo.getView(R.id.device_selector_row_tenancy_text,i+1);

        String tenancy_current_text = tenancy_current.getText().toString();
        String tenancy_next_text = tenancy_next.getText().toString();

        int result = tenancy_current_text.compareTo(tenancy_next_text);
        assertTrue(result <= 0);
    }

They way I sort the array in the actual application is by using a custom sort class which contains an algorithm for natural sorting. Thanks in advance for any help, Will.

Do you always know what the text is going to be in the application? if so the simplest answer is to just hard code the checks to the text in each position, this is probably the simplest way and gives a high level of assurance.

If however you do not know the text and need to figure it out on the fly you are going to have to build an oracle which you have attempted above. Heres comes the next question, are you trying to validate that the "sort" button sorts the list as per your comparator or are you trying to test that the comparator is right? If you are sure your comparator is right via other testing then validating that the sort works is fairly trivial just loop through the text views as you did and store the values (in a List probably) and check they match with the same list after it being sorted with your comparator therefore proving they are sorted as per your comparator.

One thing to be aware of though is that if you have no control over the values in the list, they may have been by chance in order already and therefore your test could give a false positive, you could check for this if you wanted to by getting the values before you press the sort button and doing the same comparasion as above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM