sangmingming / robotium

Automatically exported from code.google.com/p/robotium
0 stars 0 forks source link

Preference objects not supported by Solo (e.g. ListPreference, CheckBoxPreference) #250

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
First of all many thanks for such a great testing tool!

I couldn't find any standard way to check state of objects descending from 
Preference, such as ListPreference, CheckBoxPreference, etc. It would be great 
if it would be possible to do such as solo.isCheckBoxPreferenceChecked() or 
solo.getListPreferenceValue()...
By researching into the ways of accomplishing mentioned tasks I came up with 
the following code:

    private Preference findPreference(String key) throws NullPointerException
    {
        ArrayList<Activity> av = solo.getAllOpenedActivities(); 
        for (int i=0;i<av.size();i++)
            if (av.get(i) instanceof PreferenceActivity)
            {
                PreferenceActivity pa = (PreferenceActivity) av.get(i);
                Preference preference = pa.findPreference(key);

                if (preference == null)
                    throw new NullPointerException("Preference coult not be found: " + key);
                return preference;
            }
        return null;
    }

    private ListPreference getListPreference(String key) throws NullPointerException
    {
        Preference preference = findPreference(key);
        if (preference instanceof ListPreference)
            return (ListPreference) preference;
        else
            throw new NullPointerException("Discovered Preference is not ListPreference");
    }
    private CheckBoxPreference getCheckBoxPreference(String key) throws NullPointerException
    {
        Preference preference = findPreference(key);
        if (preference instanceof CheckBoxPreference)
            return (CheckBoxPreference) preference;
        else
            throw new NullPointerException("Discovered Preference is not CheckBoxPreference");
    }

    private String getListPreferenceValue(String key)
    {
        return getListPreference(key).getEntry().toString();
    }

Hope it is of any use to someone...

Original issue reported on code.google.com by andrius....@gmail.com on 19 Apr 2012 at 10:50

GoogleCodeExporter commented 9 years ago
Thanks for this. Will open up and include into Robotium if more people show an 
interest for this functionality. 

Original comment by renasr...@gmail.com on 24 Apr 2012 at 11:49

GoogleCodeExporter commented 9 years ago
I'm interested in being able to do this.

Original comment by davecomf...@gmail.com on 13 May 2013 at 10:35

GoogleCodeExporter commented 9 years ago
I am also blocked to automate CheckBoxPreference. I am using Robotium 5.1. It 
will be good if you add support for this.

Original comment by snehalko...@gmail.com on 18 Jun 2014 at 2:40

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
It would be more than helpful to have such methods bundled within Robotium. +1 
from me.

Original comment by kornelze...@gmail.com on 14 Feb 2015 at 2:16