Thanks Billy! I'll give it a go

- Jason

On Apr 19, 1:28 pm, Billy <[email protected]> wrote:
> Jason, here is the solution I am using, and it seems to work well. You
> are basically still doing the testing on thetestthread, but when it
> comes time to call theasynctaskwe switch to theuithread, and block
> the testingthread. When theUIthreadis done, it releases the
> testingthreadand you can continue yourtestcase.
>
> - Billy
>
>      /**
>      * This demonstrates how totestAsyncTasks in androidJUnit.
> Below I used
>      * an in line implementation of aasyncTask, but in real life you
> would want
>      * to replace that with some task in your application.
>      * @throws Throwable
>      */
>     public void testSomeAsynTask () throws Throwable {
>         // create  a signal to let us know when our task is done.
>         final CountDownLatch signal = new CountDownLatch(1);
>
>         /* Just create an in line implementation of anasynctask. Note
> this
>          * would normally not be done, and is just here for
> completeness.
>          * You would just use the task you want to unittestin your
> project.
>          */
>         finalAsyncTask<String, Void, String> myTask = newAsyncTask<String, 
> Void, String>() {
>
>             @Override
>             protected String doInBackground(String... arg0) {
>                 //Do something meaningful.
>                 return "something happened!";
>             }
>
>             @Override
>             protected void onPostExecute(String result) {
>                 super.onPostExecute(result);
>
>                 /* This is the key, normally you would use some type
> of listener
>                  * to notify your activity that the async call was
> finished.
>                  *
>                  * In yourtestmethod you would subscribe to that and
> signal
>                  * from there instead.
>                  */
>                 signal.countDown();
>             }
>         };
>
>         // Execute the async task on theUIthread! THIS IS KEY!
>         runTestOnUiThread(new Runnable() {
>
>             @Override
>             public void run() {
>                 myTask.execute("Do something");
>             }
>         });
>
>         /* The testingthreadwill wait here until theUIthread
> releases it
>          * above with the countDown() or 30 seconds passes and it
> times out.
>          */
>         signal.await(30, TimeUnit.SECONDS);
>
>         // The task is done, and now you can assert some things!
>         assertTrue("Happiness", true);
>     }
>
> On Apr 5, 4:28 pm, Jason <[email protected]> wrote:
>
>
>
>
>
>
>
> > OK.. So after re-thinking this I realized why the problem is
> > occurring, although I still do not have a solution.
>
> > When thetestis running it seems to be treating itself as themainUI
> >thread, which would make sense since it's an
> > ActivityInstrumentationTestCase2.  TheAsyncTaskperforms the
> > necessary callbacks in the onPostExecute method, which also runs on
> > themainUIthread.
>
> > Because mytestis entering a wait loop so it ensures theAsyncTask
> > has completed, it is essentiallyblockingthemainUIthreadand the
> > onPostExecute method cannot execute.
>
> > So.. the question becomes.. how do we assert the results of an
> >AsyncTaskinJUnit?  We can't wait for theAsynctask to complete for
> > the reasons described above, and I don't want to move my callback code
> > to the doInBackground method because in the "real world" I need/want
> > the callback to be on themainUIthread.
>
> > Hopefully someone out there has a pattern for assertingasynctask
> > outcomes in atestcase...
>
> > Thanks.
>
> > On Apr 5, 1:46 pm, Jason <[email protected]> wrote:
>
> > > Hi,
>
> > > Missed your response.  This didn't work for me unfortunately.
>
> > > Interestingly if I mark thetestwith @UiThreadTest I get the same
> > > deadlock behavior, which makes me wonder whether there is some sort 
> > > ofJUnittest state being re-used when thetestis run in a suite?
>
> > > As it stands I have a bunch of importanttestcases that I'm having to
> > > just comment out because they hang when run in the suite (although
> > > they all run and pass when executed individually).
>
> > > I feel like I've tried everything to get these to work, but clearly
> > > I'm missing something here.
>
> > > Any other ideas?
>
> > > Thanks,
>
> > > Jason.
>
> > > On Mar 17, 7:59 pm, Streets Of Boston <[email protected]> wrote:
>
> > > >JUnittest-methods in the instrumentationtest-caseclasses always run in
> > > > anotherthreadthan themainUIthread. I run both indiviualtest-cases and
> > > >test-suites (that execute lists oftest-cases) andtest-methods are always
> > > > called in a differentthreadthan the backgroundthread.
>
> > > > I read this in your original question: "*... This method call 
> > > > (internally)
> > > > launches anAsyncTask...*". I ran into a problem and this may be related 
> > > > to
> > > > your issue:
> > > > You have to *create the firstAsyncTaskof your (test-)process in themain
> > > >UIthread*. If you don't do this, and the (first)AsyncTaskis created in a
> > > > non-UIthread(e.g. during a testXXXX() method), weird stuff can happen. 
> > > > In
> > > > other words: Be sure that the VM loads theAsyncTask*class* in themainUI
> > > >thread. From then on, you can create and execute AsynTasks in anythread.
>
> > > > E.g in your firsttest'ssetup or the first testXXXXX() method:
> > > >   ...
> > > >   runTestOnUIThread(new Runnable() {
> > > >     public void run() {
> > > >       newAsyncTask(); // just create one and let it go; doing this makes
> > > > sure that the class loads in themainUIthread.
> > > >     }
> > > >   }
> > > >   ...
> > > >   ... // now you're free to create and execute AsyncTasks (directly or
> > > > through other method calls).
>
> > > > Maybe this problem is related to the one you see.

-- 
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