asifurrouf / android-test-kit

Automatically exported from code.google.com/p/android-test-kit
0 stars 0 forks source link

NoMatchingViewException can not be caught in the first place #75

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Have an AutoCompleteTextView for entering email;
2. The AutoCompleteTextView queries for used email, if there is any, shows a 
ListView, otherwise nothing happens;
3. Have a wrapper class ViewTargetInNonDefaultWindow:
    public ViewTargetInNonDefaultWindow(Matcher<View> matcher) {
        this.viewMatcher = matcher;
    }

    private ViewInteraction interactWith(Activity activity) {
        View decorView = activity.getWindow().getDecorView();
        return onView(viewMatcher).inRoot(withDecorView(not(is(decorView))));
    }

    public void tap(Activity activity) {
        interactWith(activity).perform(click());
    }
4. Have a wrapper class ViewTarget:
    public ViewTarget(int id) {
        this.viewMatcher = withId(id);
        this.viewInteraction = onView(viewMatcher);
    }

    public void enterAndCloseSuggestion(Activity activity, String text) {
        viewInteraction.perform(clearText(), typeText(text));
        try {
            ViewTargetInNonDefaultWindow autoSuggestion = new ViewTargetInNonDefaultWindow(text);
            autoSuggestion.tap(activity);
        } catch (NoMatchingViewException e) {
            Log.i("XYZ", "NMVE is caught in the right place!");
        }
    }
5. Real test code:
    public void testSomething() {
        try {
            autoCompleteTextView.enterAndCloseSuggestion(getActivity(), email);
        } catch (NoMatchingViewException e) {
            Log.i("XYZ", "NMVE is caught!");
        } catch (Exception e) {
            Log.i("XYZ", "Exception is caught!");
        }
        ...
    }

What is the expected output? What do you see instead?
Test should catch the NoMatchingViewException in the first place with the right 
type (caught in ViewTarget wrapper class as NoMatchingViewException).
But actually you can only catch general Exception type in the test body (caught 
in testSomething() as Exception).

What version of the product are you using? On what operating system?
espresso-1.1-bundled
Android 4.2.2 (Genymotion)
Mac OSX 10.9.2

Please provide any additional information below.

Original issue reported on code.google.com by thyrl...@gmail.com on 26 May 2014 at 2:50

GoogleCodeExporter commented 9 years ago
Are you sure that NMVE is being thrown? print out the stack trace in the catch 
(Exception e) block to make sure.

Original comment by vale...@google.com on 9 Jun 2014 at 9:40

GoogleCodeExporter commented 9 years ago

Original comment by vale...@google.com on 9 Jun 2014 at 10:11

GoogleCodeExporter commented 9 years ago
Sorry for that, and you're right.

I don't know why, but before I kept on getting NoMatchingViewException (from 
the stacktrace), and could not even catch generic Exception in the wrapper.

Now I can see that it's actually throwing NoMatchingRootException, and could be 
caught in the wrapper instead of the test code.  (there was no upgrading of my 
espresso library in between, I really don't know what's wrong, sorry)

Original comment by thyrl...@gmail.com on 10 Jun 2014 at 12:47