I have a problem. I want to write a test for code that relies on execution of the PopupWindow.OnDismissListener.onDismiss() method. However, it never seems to be called. Am I doing something wrong?
Sample code:
View content = new View(Robolectric.application);
PopupWindow popup = new PopupWindow(content, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
Assert.fail();
}
});
View anchor = new View(Robolectric.application);
popup.showAsDropDown(anchor);
popup.dismiss();
The above test never fails! I have tried adding a small sleep after, in case there were some timing issues. I have looked at the generated code for the PopupWindow.class, but couldn't find anything there either.
Thanks!
The reason why the listener isn't called is because the ShadowPopupWindow from Robolectric overrides the default implementation and simply doesn't call it.
Code from source:
public void dismiss() {
if (context != null) {
getWindowManager().removeView(containerView);
}
showing = false;
}
Depending on what it is you need to test, there may be a valid workaround (your sample code above pretty much just tests the internals of PopupWindow, which I am guessing is not what you are actually looking to achieve). You could also make your own custom shadow, extending this one, where you choose to do a different behaviour.