Idling Resource - Android Testing Patterns #4

Idling Resource - Android Testing Patterns #4 - Hello friends Android tutorial and tips trik, The article you are reading this time with the title Idling Resource - Android Testing Patterns #4, we have prepared this article for you to read and download the information therein. hopefully fill posts article -, article #4, article android, article Idling, article Patterns, article Resource, article Testing, article Tutorial, we can write this you understand. Well, happy reading.

Title : Idling Resource - Android Testing Patterns #4
link : Idling Resource - Android Testing Patterns #4

Baca juga


Idling Resource - Android Testing Patterns #4

[MUSIC PLAYING] SPEAKER: If your app usesbackground threads for loading or processing data-- and prettymuch every app will need those at some point-- you might haveto take special precautions to make your UItest run reliably.

Espresso does most of theheavy lifting for you, waiting for lifecycle events to finish and checking if the UI is idle.

But it can't always know whatthe other parts of your app are doing.

And you might need to writesome additional code to make synchronization possible.

Let me show you an example.

I'll create a newproject in Android Studio with the emptyactivity template.

Let's do some initialsetup for testing.

I'll add the Androidrunner, rules, and Espresso dependenciesin my Android test scope.

If you're ever unsure what thenewest versions for Espresso packages are, consult thedocumentation or our testing samples on GitHub, whichwe always keep up to date.

I then set up the testinstrumentation runner, and we're done here.

Onto the main activity class.

The only thing itdoes right now is set a layout for the text view.

Let me open the layoutand change that text and give the text view an ID.

And on resume, I'm goingto change the text.

And I'll just quicklyextract the string into a resourcefor later re-use.

Let's create a UI test forthe activity using an Android Studio shortcut.

Add the annotation to use theAndroid test runner first.

Then I need to setup the activity test rule that will start my activityand check if the correct text is displayed.

I run the test, and it passes.

That's because Espressowaits until the app processes all life cycle events.

And I'm sending it a textstring on resume, remember? Let's simulate a morerealistic scenario where the data takessome time to load.

AsyncTask is one of the simplestways to do background work and then pass a resultback to the main thread.

So what happens whenI run the test now? Surprisingly, it passes.

That's becauseEspresso also knows how to synchronize onthe AsyncTask thread pull out of the box.

And because AsyncTask are alsoat the heart of any AsyncTask loader, including cursorloaders, they just work.

Of course, there'slots of other ways you can schedulebackground work.

It can be connecting to a remoteAPI, using a custom thread pull, or throughhandler messaging.

You can see I'mchanging the code to use a separatethread, which sleeps for 5 seconds tosimulate background work, then posts the changeback to the main thread.

When I run the test,it fails immediately.

As soon as on resume returnsand the activity is idle, Espresso is trying to validatethe message on the text view and it failsbecause the runnable hasn't had a chance to run yet.

That's when you haveto use idling resources to tell Espresso when yourapp is busy processing.

I'll use an implementationof an idling resource that we providewith Espresso called counting idling resource.

It's very simple touse, and it basically lets you increment a counterfor every resource that is busy when you're waitingfor something to finish loading and decrementit when it's done.

Espresso willautomatically run whenever the counter is at zero.

I'll give it a name, dataload, used mostly for logging.

But it has to be unique amongregistered idling resources.

Now, increment beforescheduling my runnable and decrement when it's done.

The only thing left isto register the resource with Espresso.

In the test class,I'll add a setup method to grab the idling resourceinstance from the activity.

And it's done.

Run test.

After 5 seconds, thecounter gets decremented and the test passes.

To sum up, if your tests areflaky or outright failing because of background threadsand asynchronous events, use idling resourceto tell Espresso when to pause and resume its work.

That's all for this episode.

Happy testing.

[MUSIC PLAYING].



Thus Article Idling Resource - Android Testing Patterns #4

That article Idling Resource - Android Testing Patterns #4 This time, hopefully we can give you all of the benefits. Well, I'll see you in another article post.

You now read the article Idling Resource - Android Testing Patterns #4 the link address http://svedda79.blogspot.com/2016/01/idling-resource-android-testing.html

0 Response to "Idling Resource - Android Testing Patterns #4"

Post a Comment