Thanks - just establishing theres no framework way to get menus.
So I did as you suggested and there still some wierdness. I added a
getter for the menu but it always returns null:

public class MyAppActivityTest extends
ActivityInstrumentationTestCase2< MyAppActivity> {

@UiThreadTest
public void testMyAppAboutBox() {
        MyAppActivity main = (MyAppActivity) getActivity();
        Menu menu = main.getMenu();
}

Could it be the Activities onCreateOptionsMenu() hasn't been called?
Is there more I should be doing in my test to help establish the
Activities runtime context?

On a different thread (:-)), I tried this:

@UiThreadTest
public void testAbout() {
      MainActivity main = (MainActivity) getActivity();
      sendKeys(KeyEvent.KEYCODE_MENU);

      Menu menu = main.getMenu();
      menu.performIdentifierAction(R.id.about, 0);
}

Hoping the key event would cause the menu to be inflated, but that
just causes:

java.lang.RuntimeException: This method can not be called from the
main application thread
at android.app.Instrumentation.validateNotAppThread
(Instrumentation.java:1500)
at android.app.Instrumentation.sendKeySync(Instrumentation.java:933)
...

Thanks

On Aug 18, 8:59 pm, Dianne Hackborn <[email protected]> wrote:
> Well you'll need to have some way for your instrumentation to get at the
> activity state it wants.  Either give your dynamically created menus an ID,
> or have APIs on the activity to get the menus, or do something else.
>
> On Tue, Aug 18, 2009 at 12:31 PM, Carl Whalley
> <[email protected]>wrote:
>
>
>
>
>
>
>
> > I'm writing unit tests in a class derived from
> > ActivityInstrumentationTestCase2. I know I can access anything defined
> > in the applications XML using findViewById(), but this application
> > defines its Menu programmatically. I'm trying to access that menu so I
> > can fire click() events to it in my tests the same way I'm doing with
> > Button Views etc.
>
> > To illustrate, this works fine and shows I can control/test various
> > Views:
>
> > public void testMyAppEdit() {
> >        MyAppActivity main = (MyAppActivity) getActivity();
>
> >        EditText val1Edit = (EditText) main.findViewById(R.id.EditText1);
> >        val1Edit.setText("3");
>
> >        Button addButton = (Button) main.findViewById(R.id.ButtonAdd);
> >        addButton.performClick();
>
> >        TextView resultText = (TextView) main.findViewById(R.id.Result);
>
> >        assertEquals("Result incorrect", resultText.getText(), "3");
> > }
>
> > But I need to do the same to fire Menu events:
>
> > public void testMyAppAboutBox() {
> >        MyAppActivity main = (MyAppActivity) getActivity();
>
> >        // Huh? this menu has no id, and no obvious way of accessing it...
> >        //Menu menu = (Menu) main.findViewById(id);
> > ...
> > }
>
> > Thanks.
>
> > --
> > Android Academyhttp://www.androidacademy.com
>
> --
> Dianne Hackborn
> Android framework engineer
> [email protected]
>
> Note: please don't send private questions to me, as I don't have time to
> provide private support, and so won't reply to such e-mails.  All such
> questions should be posted on public forums, where I and others can see and
> answer them.
--~--~---------~--~----~------------~-------~--~----~
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