bmlct / android-test-kit

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

Basic intent test fails. #149

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.Create test
@RunWith(AndroidJUnit4.class)
public class SetupAccountWizardTest {
  @Rule
  public IntentsTestRule<AccountWizard_> testCase = new IntentsTestRule<>(AccountWizard_.class);
@Test
  public void test1AAAAAAA(){
    Intent i = new Intent("BOB");
    testCase.getActivity().sendBroadcast(i);
    intended(
        allOf(isInternal(),
            hasAction("BOB")//,
        ));

  }
}
2. Run android test.
3. Test fails.

What is the expected output? What do you see instead?
Expect to see test complete good!

See fail..

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCaus
eError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: (target package: nz.org.winters.android.nzmobileaccountwidget 
and has action: is "BOB")

Matched intents:[]

Recorded intents:[]
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
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 android.support.test.espresso.intent.Intents.intended(Intents.java:186)
at android.support.test.espresso.intent.Intents.intended(Intents.java:169)
at 
nz.org.winters.android.nzmobileaccountwidget.tests.setup.SetupAccountWizardTest.
test1AAAAAAA(SetupAccountWizardTest.java:107)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java
:45)
at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:
15)
at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:4
2)
at 
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20
)
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:18)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68
)
at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47
)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
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:1853)
Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. 
Actually matched 0 intents.

IntentMatcher: (target package: nz.org.winters.android.nzmobileaccountwidget 
and has action: is "BOB")

Matched intents:[]

Recorded intents:[]
at junit.framework.Assert.fail(Assert.java:50)
at 
android.support.test.espresso.intent.VerificationModes$Times.verify(Verification
Modes.java:87)
at 
android.support.test.espresso.intent.Intents.internalIntended(Intents.java:281)
at android.support.test.espresso.intent.Intents$2.check(Intents.java:189)
at android.support.test.espresso.ViewInteraction$2.run(ViewInteraction.java:170)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Seems no intents are recorded..

All other espresso tests work fine!

Using espresso 2.1

Original issue reported on code.google.com by mat...@winters.org.nz on 4 May 2015 at 2:32

GoogleCodeExporter commented 8 years ago

Original comment by nkors...@google.com on 5 May 2015 at 5:26

GoogleCodeExporter commented 8 years ago

Original comment by slinz...@google.com on 15 Sep 2015 at 10:58

GoogleCodeExporter commented 8 years ago
I can successfully capture and test Intents sent with startActivity() but the 
same intent and same intended() matchers fail for one sent with sendBroadcast() 
or through LocalBroadcastManager.

Original comment by neil.mac...@mobileforming.com on 19 Sep 2015 at 1:20

GoogleCodeExporter commented 8 years ago
I have recently used mockito to test localbroadcastmanager..

1. Create a argument matcher for Intent

public static class ReSync extends ArgumentMatcher<Intent> {

    @Override
    public boolean matches(Object intent) {
      return ((Intent) intent).getAction().equals(Constants.INTENT_RESYNC);
    }

    @Override
    public String toString() {
      return "Intent Action Constants.INTENT_RESYNC";
    }
  }

2. Mock the localbroadcastmanager
    LocalBroadcastManager localBroadcastManager = Mockito.mock(LocalBroadcastManager.class);

3. verify the method was called with the intent.

    Mockito.verify(localBroadcastManager).sendBroadcast(argThat(new IntentMatchers.StartWorking()));
    Mockito.verify(localBroadcastManager).sendBroadcast(argThat(new IntentMatchers.ShowsSynced()));
    Mockito.verify(localBroadcastManager).sendBroadcast(argThat(new IntentMatchers.StopWorking()));

You have to remember to pass in your mocked localbroadcastmanager instance to 
the method being tested!

Original comment by mat...@winters.org.nz on 19 Sep 2015 at 2:25

GoogleCodeExporter commented 8 years ago
This WAI. Espresso Intents does only support Intent stubbing and verification 
for  Activities. Any other components would require a new framework APIs.

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

GoogleCodeExporter commented 8 years ago
Why won't fix?  Those components are just as necessary to test as Activity 
Intents.  I understand it would take new framework APIs for the hooks, but, 
only putting those hooks in for Activity Intents seems incomplete.

Either way, the naming and documentation of EspressoIntents and IntentsTestRule 
don't do a very good job at describing to developers that these can ONLY be 
used for Activity Intents.  At the very least it should note in the docs that 
this does NOT work for sendBroadcast() or startService(), etc.

Original comment by neil.mac...@mobileforming.com on 23 Sep 2015 at 10:10