bmlct / android-test-kit

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

how to check a toast shown in fragment(a dialog fragment, not full screen)? #179

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.MainActivity is FragmentActivity and shows a dialogFragment which is not full 
screen.
2.there is a button in DialogFragment. if press the button, a toast will show.
3.i want to test whether the toast is shown.

the code in test:
onView(withText("test")).inRoot(withDecorView(not(is(getActivity().getWindow().g
etDecorView())))).check(matches(isDisplayed()));

What is the expected output? What do you see instead?
i know this code works in activity, but fails in fragment.
the exception:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy 
found matching: with text: is "test"

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=960, height=798, has-focus=false, 
has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, 
is-focused=false, is-focusable=false, is-layout-requested=false, 
is-selected=false, root-is-layout-requested=false, has-input-connection=false, 
x=0.0, y=0.0, child-count=1}
|
+->FrameLayout{id=-1, visibility=VISIBLE, width=960, height=798, 
has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, 
is-enabled=true, is-focused=false, is-focusable=false, 
is-layout-requested=false, is-selected=false, root-is-layout-requested=false, 
has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+-->FrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=960, 
height=798, has-focus=false, has-focusable=true, has-window-focus=true, 
is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, 
is-layout-requested=false, is-selected=false, root-is-layout-requested=false, 
has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+--->NoSaveStateFrameLayout{id=-1, visibility=VISIBLE, width=960, height=798, 
has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, 
is-enabled=true, is-focused=false, is-focusable=false, 
is-layout-requested=false, is-selected=false, root-is-layout-requested=false, 
has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+---->RelativeLayout{id=-1, visibility=VISIBLE, width=960, height=798, 
has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, 
is-enabled=true, is-focused=false, is-focusable=false, 
is-layout-requested=false, is-selected=false, root-is-layout-requested=false, 
has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+----->RelativeLayout{id=2131230834, res-name=rlay_paypwd_whole_x, 
visibility=VISIBLE, width=960, height=798, has-focus=false, has-focusable=true, 
has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, 
is-focusable=false, is-layout-requested=false, is-selected=false, 
root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, 
child-count=12}
|
+------>ImageView{id=2131230835, res-name=iv_paypwd_close_c, 
visibility=VISIBLE, width=126, height=126, has-focus=false, 
has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, 
is-focused=false, is-focusable=false, is-layout-requested=false, 
is-selected=false, root-is-layout-requested=false, has-input-connection=false, 
x=834.0, y=0.0}
|
+------>TextView{id=2131230836, res-name=tv_paypwd_title_x, visibility=VISIBLE, 
width=336, height=120, has-focus=false, has-focusable=false, 
has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, 
is-focusable=false, is-layout-requested=false, is-selected=false, 
root-is-layout-requested=false, has-input-connection=false, x=312.0, y=0.0, 
text=支付详情, input-type=0, ime-target=false, has-links=false}
|
+------>View{id=2131230837, res-name=divider_title, visibility=VISIBLE, 
width=960, height=1, has-focus=false, has-focusable=false, 
has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, 
is-focusable=false, is-layout-requested=false, is-selected=false, 
root-is-layout-requested=false, has-input-connection=false, x=0.0, y=120.0}
|
+------>TextView{id=2131230838, res-name=tv_order_count, visibility=VISIBLE, 
width=240, height=150, has-focus=false, has-focusable=false, 
has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, 
is-focusable=false, is-layout-requested=false, is-selected=false, 
root-is-layout-requested=false, has-input-connection=false, x=45.0, y=121.0, 
text=订单金额:, input-type=0, ime-target=false, has-links=false}
|
+------>TextView{id=2131230839, res-name=tv_order_money_x, visibility=VISIBLE, 
width=675, height=150, has-focus=false, has-focusable=false, 
has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, 
is-focusable=false, is-layout-requested=false, is-selected=false, 
root-is-layout-requested=false, has-input-connection=false, x=285.0, y=121.0, 
text=¥12.34, input-type=0, ime-target=false, has-links=false}
|
+------>View{id=2131230840, res-name=divider_one, visibility=VISIBLE, 
width=870, height=1, has-focus=false, has-focusable=false, 
has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, 
is-focusable=false, is-layout-requested=false, is-selected=false, 
root-is-layout-requested=false, has-input-connection=false, x=45.0, y=271.0}
|
+------>TextView{id=2131230841, res-name=tv_red_paper, visibility=VISIBLE, 
width=144, height=150, has-focus=false, has-focusable=false, 
has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, 
is-focusable=false, is-layout-requested=false, is-selected=false, 
root-is-layout-requested=false, has-input-connection=false, x=45.0, y=272.0, 
text=红包:, input-type=0, ime-target=false, has-links=false}
|
+------>TextView{id=2131230842, res-name=tv_red_paper_count_select_c, 
visibility=VISIBLE, width=771, height=150, has-focus=false, 
has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, 
is-focused=false, is-focusable=false, is-layout-requested=false, 
is-selected=false, root-is-layout-requested=false, has-input-connection=false, 
x=189.0, y=272.0, text=-¥3.00, input-type=0, ime-target=false, 
has-links=false}
|
+------>View{id=2131230843, res-name=divider_two, visibility=VISIBLE, 
width=870, height=1, has-focus=false, has-focusable=false, 
has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, 
is-focusable=false, is-layout-requested=false, is-selected=false, 
root-is-layout-requested=false, has-input-connection=false, x=45.0, y=422.0}
|
+------>TextView{id=2131230844, res-name=tv_need_pay, visibility=VISIBLE, 
width=240, height=180, has-focus=false, has-focusable=false, 
has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, 
is-focusable=false, is-layout-requested=false, is-selected=false, 
root-is-layout-requested=false, has-input-connection=false, x=45.0, y=423.0, 
text=需付总额:, input-type=0, ime-target=false, has-links=false}
|
+------>TextView{id=2131230845, res-name=tv_need_pay_count_x, 
visibility=VISIBLE, width=675, height=180, has-focus=false, 
has-focusable=false, has-window-focus=true, is-clickable=false, 
is-enabled=true, is-focused=false, is-focusable=false, 
is-layout-requested=false, is-selected=false, root-is-layout-requested=false, 
has-input-connection=false, x=285.0, y=423.0, text=¥9.34, input-type=0, 
ime-target=false, has-links=false}
|
+------>Button{id=2131230846, res-name=btn_confirm_c, visibility=VISIBLE, 
width=870, height=150, has-focus=false, has-focusable=true, 
has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, 
is-focusable=true, is-layout-requested=false, is-selected=false, 
root-is-layout-requested=false, has-input-connection=false, x=45.0, y=603.0, 
text=确 定, input-type=0, ime-target=false, has-links=false}
|
+-->ViewStub{id=16909095, visibility=GONE, width=0, height=0, has-focus=false, 
has-focusable=false, has-window-focus=true, is-clickable=false, 
is-enabled=true, is-focused=false, is-focusable=false, 
is-layout-requested=true, is-selected=false, root-is-layout-requested=false, 
has-input-connection=false, x=0.0, y=0.0}
|
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:579)
at 
android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(De
faultFailureHandler.java:82)
at 
android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHa
ndler.java:53)
at 
android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInt
eraction.java:184)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
at com.netease.epay.sdk.client.pay.ExceptionTest.test(ExceptionTest.java:60)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java
:50)
at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:
12)
at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:4
7)
at 
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17
)
at 
android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadState
ment.java:55)
at 
android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTe
stRule.java:257)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78
)
at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57
)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at 
android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at 
android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:2
28)
at 
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

the View Hierarchy is the fragment page.i don't know how to fix it

What version of the product are you using? On what operating system?
the version:com.android.support.test.espresso:espresso-core:2.2
system: windows

Please provide any additional information below.

Original issue reported on code.google.com by lxxCarol...@gmail.com on 21 Sep 2015 at 12:30

GoogleCodeExporter commented 8 years ago

Original comment by slinz...@google.com on 23 Sep 2015 at 9:50

GoogleCodeExporter commented 8 years ago
This WAI. The question was asked many times before, please follow this thread 
on our forum: 
https://groups.google.com/forum/#!msg/android-test-kit-discuss/uaHdXuVm-Bw/YUphd
IMW9GUJ

The gist is, that this is not supported and hard to get right in a thread safe 
way. The proper solution to verify this behaviour in a reliable way is to use 
an Interface + Mockito. Since testing a Android Framework API does not really 
make sense verification that the call actually happened is a sufficient test.

Original comment by slinz...@google.com on 23 Sep 2015 at 10:03