mardi 23 août 2016

How can I test an abstract class using Espresso?

Suppose I make an abstract BaseActivity with a Toolbar, like this:

/**
 * A base activity that handles common functionality in the app. 
 * This includes the Toolbar
 */
public abstract class BaseActivity extends AppCompatActivity {
    // Primary toolbar
    private Toolbar mToolbar;

    @Override
    public void setContentView(int layoutResId) {
        super.setContentView(layoutResId);
        setToolbar();
    }

    private void setToolbar() {
        if (mToolbar == null) {
            mToolbar = (Toolbar) findViewById(R.id.toolbar);
            if (mToolbar != null) {              
                mToolbar.setNavigationContentDescription(getResources().getString(
                    R.string.navigation_drawer_description_ally));
            setSupportActionBar(mToolbar);
            }
        }
    }
}

And a MainActivity that extends this BaseActivity, like this:

public class MainActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

The layouts are:

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://ift.tt/nIICcg"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:elevation="@dimen/spacing_tiny"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    >

</android.support.v7.widget.Toolbar>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    >

    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

My question is: How can I test BaseActivity using espresso? To, for example, check if the Toolbar exists?

Aucun commentaire:

Enregistrer un commentaire