I am testing an application with multiple activities and one service
using jUnit (Robotium et al is not an option). I need to figure out a
way to test all of the components in such a way as to mimic a user. I
can easily test one activity and a service, but have not found a way
to test an entire application comprising several activities and a
service.  My test code for the login activity is a follows. What i
needs is some help as to how i would add other activities and a
service to this file (if its possible) and to be able to control the
state transitions from activity to activity.

package com.drayagerecorder.loginactivity.test;
import com.drayagerecorder.LoginActivity;
import com.drayagerecorder.R;
import android.app.Instrumentation;
import android.app.NotificationManager;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.test.UiThreadTest;
import android.test.ViewAsserts;
import android.test.TouchUtils;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;

public class LoginActivityTest extends
                ActivityInstrumentationTestCase2<LoginActivity> {

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//Class members
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        private static String tag = "LoginActivityTest";
        private static final String TRIP_RESULT = "test";
        private LoginActivity mActivity;
        private Button mStartButton;
        private Button mStopButton;
        private EditText mEditTextTripName;
        public NotificationManager mNM;

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//LoginActivityTest constructor definition
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        public LoginActivityTest(){
                super("com.drayagerecorder", LoginActivity.class);
        }

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//Setup and other methods
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        @Override
        protected void setUp() throws Exception{
                super.setUp();
                setActivityInitialTouchMode(false);
                mActivity = getActivity();
                mStartButton = (Button)
mActivity.findViewById(com.drayagerecorder.R.id.ButtonStart);
                mStopButton = (Button)
mActivity.findViewById(com.drayagerecorder.R.id.ButtonStop);
                mEditTextTripName = (EditText)
mActivity.findViewById(R.id.EditTextTripName);
        }

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//Clean up
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        @Override
        protected void tearDown() throws Exception {
                 super.tearDown();
        }

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//Test Preconditions
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        @SmallTest
        public void testPreConditions(){
                assertNotNull(getActivity());
                assertNotNull(mEditTextTripName);
                assertNotNull(mStartButton);
                assertNotNull(mStopButton);
        }

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//Test that the view is visible on the screen
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        @SmallTest
        public void testViewVisible(){
         ViewAsserts.assertOnScreen(mEditTextTripName.getRootView(),
mEditTextTripName);
 }

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//Test that the view is empty when it appears
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        @SmallTest
        public void testViewEmpty(){
                assertTrue("Trip name field is empty",
"".equals(mEditTextTripName.getText().toString()));
        }

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//Test that text can be entered into the view
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        @SmallTest
        public void testViewFull(){
                mEditTextTripName.requestFocus();
                mEditTextTripName.clearComposingText();
                TouchUtils.tapView(this, mEditTextTripName);
                sendKeys("T E S T");
                //sendKeys("SHIFT_LEFT T E S T");
                Log.i(tag, mEditTextTripName.getText().toString());
                String tripName = mEditTextTripName.getText().toString();
                assertEquals(TRIP_RESULT, tripName);

}

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//Test the start button is visible on the screen and is
//enabled
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        @SmallTest
        public void testStartButtonVisible(){
         ViewAsserts.assertOnScreen(mStartButton.getRootView(),
mStartButton);
         //assertTrue(mStartButton.isEnabled());
 }

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//Test the start button listener
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        @UiThreadTest
        public void testStartListener(){
                mStartButton.requestFocus();
                mStartButton.performClick();//the onClickListener was called
        }

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//Test the stop button is visible on the screen but
//that it is not enabled
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        @SmallTest
        public void testStopButtonVisible(){
        ViewAsserts.assertOnScreen(mStartButton.getRootView(), mStartButton);
        //assertFalse(mStopButton.isEnabled());
        }

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//Test the stop button listener
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        @UiThreadTest
        public void testStopListener(){
                mStopButton.requestFocus();
                //mStopButton.performClick();//the onClickListener was called
        }

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//Test see if the activity's state is saved when
//it is shut down and restarted
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        public void testStateDestroy() {
                mEditTextTripName.requestFocus();
                mEditTextTripName.clearComposingText();
                TouchUtils.tapView(this, mEditTextTripName);
                sendKeys("T E S T");
                Log.i(tag, mEditTextTripName.getText().toString());
                String tripName = mEditTextTripName.getText().toString();
                assertEquals(TRIP_RESULT, tripName);
          mActivity.finish();
          mActivity = getActivity();
          mEditTextTripName.requestFocus();
          assertEquals(TRIP_RESULT, tripName);
         }

//%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
//Test see if the activity's state is saved when
//it is paused
//%&%&%%&%&%&%&%&%&%&%&%&%&%&%&%&%%&%&%&%&%&%&%&%&%&%&%&%&

        //@UiThreadTest
         public void testStatePause() {
                 Instrumentation mInstr = this.getInstrumentation();
                 mInstr.callActivityOnPause(mActivity);
                 mEditTextTripName.requestFocus();
                 mEditTextTripName.clearComposingText();
                 TouchUtils.tapView(this, mEditTextTripName);
                 sendKeys("T E S T");
                 Log.i(tag, mEditTextTripName.getText().toString());
                 String tripName = mEditTextTripName.getText().toString();
                 assertEquals(TRIP_RESULT, tripName);
                 mInstr.callActivityOnResume(mActivity);
                 assertEquals(TRIP_RESULT, tripName);
         }
}


My question, is how do i add to this? do i have to create a super
class above the one I have to wrap the application? I essentially wish
to make the entire application the UUT and not just the activity.

Andy help is appreciated

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to