Espresso Idling Resource (UI Testing for Beginners PART 14)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are gonna work on something known as the espresso idling resource or actually sorry in other words I'm gonna introduce something known as the espresso idling resource and what the espresso idling resource is used for is it's used for facilitating testing when things when the tests involved background threads on Android so right now you know just kind of in general espresso has the ability to kind of look forward in your code in your code that's being tested and figure out generally what's going on but it does run into problems when you have things that are going to be happening on background threads so if you're doing like a network request that requires obviously a background thread if you're doing a cache request whatever it is some kind of big computational thing that requires time it has issues with that so the espresso idling resource is the solution to that problem it's what we have to use to facilitate those those long running tasks those background tasks basically anything that takes time we have to use this so what I'm gonna be showing you is a continuation from the previous video that we worked on I'll throw up the thumbnail of that video here on the screen because I haven't published it on youtube or on my website yet so it'll be a continuation from that video and we're going to be basically just adding like a fake kind of network delay into the previous requests where we get the data and then we're gonna use the espresso well first I'll show you that the test fails whenever you add like a fake delay and then we are going to introduce the espresso idling resource and I'll show you that then after that it's smooth sailing and the tests all run really just exactly how they're supposed to so if you're watching this video and you don't know what this course is the course that I just mentioned this video is part of a full-length free course on my website on UI testing for beginners you can just go to Kony pitch com go to courses up here and select UI testing for beginners and you can watch this completely free all you gonna do is register my website and it tracks your progress you can follow along with all the videos and I do recommend watching in in that order they kind of all fit together nicely that way so this video as I said is a continuation it's a continuation from the previous video again I'll throw the thumbnail up here where we have just kind of a basic application with the recyclerview there's some movies here they got you know image title a little bit of information about the star actors in that in that particular movie if I click one I'm taken to a detail fragment title image directors star actors the description if I click on directors is a list of directors if I click on star actors there's a list of star actors and they're all different obviously and you just saw that that little loading animation there you're probably wondering what that is if you again if you're watching this course this video for the first time you haven't watch the rest of the course you know that you don't know that the animations on this testing device are actually disabled so this is what the progress bar looks like because I've disabled the the prot the animations all right so let's take a look at the code and we'll see and I'm gonna add I'm going to show you what I've altered from the previous video so those of you who are still watching right now I'm assuming that you've watched the previous one I've said it multiple times now this is a continuation so you're gonna need to get the code from the previous video to be able to watch this one so let's let's take a look at the code and see how this has changed from the previous video like I said so if we go into movie list fragment and we scroll down here we have this get data function where it uses a chemist's interface to tell the UI to show a loading animation it creates a job using a KO routine so it lodges it on a background thread and then it adds this kind of fake network delay so this fake network delay is just one second of delay if you go into data go into fake movie data you can see up here that fake network delay is just one second so it's just delaying the getting the retrieving of of that data one second and then it's going to continue on with getting the data so it says once the job is complete which it will be complete after one second so job invoke on completion then we want to tell the the interface to tell the UI to stop showing the loading animation and then we actually just submit the data to the recyclerview so it's just pretending to get you know take one second to get the information from the network and then it's publishing it to the recyclerview so if I was to run the test that we created on the in the previous video the one before this one so let's actually go into the Android test directory here and going to move evilest fragment test so this is the test class that we built in the previous video if I was run these now since that Network delays there these tests will all fail because or they should all fail unless there's one that doesn't require getting the data but let's just run this and I'll let you take a look here and see if see if they pass so fail fail fail and they should this one might note that fails too they should all fail all right so they all fail and that's because the only thing different again from the previous video compared to this video is I've added that fate this kind of fake network delay so you can see that we have a problem there's there's obviously something that this delay does that makes all of our tests fail and so now we can introduce the espresso idling resource and we can we can solve this problem all right so first things first we need to add some dependency so I'm actually going to close everything here and open up the build up Gradle app file if you scroll down you'll see that I've already added a new dependency this is the Android X test espresso espresso idling resource dependency and one thing you should notice here is that I'm calling I'm using implementation and I'm not calling Android test implementation so this might confuse some of you because we're running UI tests why wouldn't we add this dependency in the UI testing I guess namespace for these dependencies and that's because the espresso idling resource it's actually going to be a class that we create and we have to add it to our production code so I'll talk more about that later because you're probably a little confused but just for now add this dependency looks like there's actually a newer version available but it doesn't matter I'm going to use three point one point zero because I know that works and well if you're just adding this obviously you have to click sync in the build up Gradle file and now let's let's move forward so actually before we write any code let me actually pull up the documentation for the espresso idling resource so I actually I do recommend taking a look at this this this single page here so at developer.android.com/design you don't need to go to this this hashtag you can just go to this page I recommend reading this because there's a lot of really good information here it tells you you know what the idling resource is for common use cases some examples so counting idling resource you are idling resource so on so on how to create your own idling resource lots lots of really good stuff here so in general though there's a lot of ways to do this but the the punchline is you need to use an idling resource if there's any sort of a timed delay and there's always time delays in Android anytime you do background work there's a potential time delay there it could be anything from setting an image with glide maybe a high-resolution image takes a long time to set maybe you're submitting a list of data to your recyclerview but it's a big list it might take some time doing network requests obviously doing cache requests long long network background or long like computational heavy tasks again everything takes time so this is gonna it's a very important tool in your testing tool belt you need to know about this so if you look at the documentation here it says if you read this whole page basically it recommends and also you read the Google samples and everything like I have it recommends using this counting idling resource so what this counting counting idling resource thing does is it maintains a counter of the active tasks so basically every time there's an active task that takes some time delay this counting resource will increment and then as soon as that task is done it D increments so so the tests will wait for when there is nothing in the Khitan there's nothing in the count so if there's a task running it count goes up task is done count goes to 0 then the UI test will continue so that's how it works it basically looks and says is there anything that's pending yes then I'll wait if there isn't then the test will continue alright so now let's build this class that we can use in our code so the way this works is a little strange because you actually have to put it in your production code or at least they recommend putting it in your production code so I'm going to create a class here that we are gonna use literally in our production code that's going to increment a counter when a when a any kind of a background task is about to begin and then D increment it when that task is done and again I'm going to talk more about this because I'm sure a lot of you were thinking whoa whoa whoa I don't want to put any kind of a testing thing in my production code that's not that's gonna look ugly but don't worry I'm gonna talk more about that and the best way to go about that because I've thought about this a lot myself and I've read a couple articles on it and there's a bunch of different things you can do but I'll tell you what I recommended methods all right so this is going to be called the espresso idling resource class so pretty pretty self-explanatory so this is going to be a singleton object so I'm going to go object espresso idling resource and open this up and give them give myself some more room here so we can scroll down later it's going to have a constant so private constant value I'll call it resource and this will just be global there's no you can name this any whatever you want it's just naming for the counter that keeps track of your tasks whatever you want to call this I just called it global because that's what they call it in Google sample now I'm gonna write at JVM field so this is a static field counting idling resource is what it will be called and they'll speak hwal to a counting idling resource and then I passed that that resource global content that I created up there so you have to give it a name and the name is global again you can give it whatever name you want so now it's gonna have two functions one is going to be increment so I just want to go counting idling resource dot increment and then the second one will be D increment so function decrement open this up and then I want to do if this not counting idling resource dot is idle now then I want to do counseling idling resource D and current so if it's not already idle if the counter is not already at zero then D increment it that's all this is gonna do it's very simple just to keep track of the tasks so now let's let's use this thing so let's go into movie list fragment and we want to go into here into the get data function and I want to do a special item resource it's a singleton so I can just access its meth its functions and methods I'm gonna go increment because now I'm telling it something is in use here and then down below once I've already submitted the list of data to the recyclerview I want to do espresso idling resource D increment so this is this is the game this is what you have to do I know it's kind of strange to be putting this this kind of stuff in your production code but this is what they recommend doing if you read the Google samples if you read the medium articles written by the developers at Google if you read the documentation they all recommend there are ways to go around this but they all recommend at the end of the day the best way just to put this in your production code so if you're probably wondering yeah like you're saying this you're probably thinking that this is ugly you don't want to do this the other thing you can do is you can create a debug build and a release build and you can put only only the espresso idling resource stuff into your debug build not into your release build that would keep it out of your production code mostly and that's what I recommend doing I'm not gonna show you how to do that in this video I'm gonna show you how to do that though probably not then in the next video in the next video I want to show you kind of a weird bug a weird espresso bug and then in the video after that I'll show you how to create a debug build and a release build so that you can only add this stuff to the really or the debug version so when you actually release the app that code won't exist because you don't want to put code there that doesn't do anything because that's what it would do so now that we have this in here I'm actually going to add it somewhere else too so right here we have a delay and so obviously we have to use our espresso idling resource well somewhere else we could add it is inside of our movies list adapter so here I'm using the diff callback the diff util for the recyclerview I have a video on this by the way how to use this it's the the best practice way to use recyclerview these days so it so what I want to do here is when I'm submitting this list of data to the recycler view I want to use the espresso idling resource because potentially I could be if I was submitting like a huge list of data this could take time it might because the way that the async lists differ works is it takes the list of data and throws it to a background thread and it compute does computations to determine what's the best way to submit that new list or integrate those new items into the current list so there's there's a background there's background work being done here and it potentially could take time so using the espresso I'd only resource is a good idea so I want to do is specify only resource increment and as you can see here I kind of gave away what I was doing I forgot to remove this from the source code but I'm creating a runnable callback so it's called data commit callback equals a new runnable and that what this is good this callback gets called when the submit list function is done what it's doing so you can see here if I hover over here it takes a new list but it also takes a runnable commit to call back so one that new data has been committed to the recyclerview list this callback will run so you can pass it as the second parameter right here so once that all of everything's done that's what I'm going to do a decrypting chram entrust idling resource some of you most of you probably didn't know that you could do this with the diff util so you've probably learned something as a bonus here so just creating a runnable using a lambda and then whenever that list is done being added to the recycle view what is inside this runnable will be executed so we want to D in chram entire counter so now how do we use this inside of our tests we've got a way to increment the counter D increment the counter now let's take a look at the test so let's go into movie list fragment test and I'll show you how to set up the espresso idling resource so first we need and we need a before and an after block so I'm going to write at before and this function will be called I'll just call it register idling resource so register idling resource and what I'm going to do is go idling registry get instance dot register and then I want to specify the class so we're using a singleton so I can use espresso idling resource and then just reference that counting idling resource now I need an after block so at after fun this will be called unregister unregistered idling resource and then same kind of thing idling resource dot get instance dot unregister this time and then just reference that singleton object so that is it that's all you need to do i all you need to do is you know create the custom I mean I guess you could call this a custom class but really we're just using the standard counting idling resource create two functions increment and T increments and then put it into your production code in our case we just have two places we have it for getting the data and then also for when we're submitting a new list of data tutors to the recyclerview then any time any tests so if you have a test class where inside of the test there's functions that have situations that have delays all you're going to do is add the before register in the item resource after unregistered the idling resource so the idling resource will be registered before each test function and then unregistered after each test function so now let's let's run our tests again so I'm gonna press shift f10 or you could right like go to run whatever way you want let's take a look here and see if our tests pass this time I hope I didn't forget anything it's always nice when they pass the first time green checkmark number one that's probably probably means they're all gonna pass those green checkmark number two and green checkmark number three and should be green checkmark number four and last one green checkmark there we go so all the tests pass we are good to go that is how you incorporate a espresso idling resource in your your your tests that require delays alright so in the next video as I said I'm gonna show you kind of a weird espresso bug that I encountered and I actually want to see what you think because I legitimate ly have no idea what's going on very strange so I just kind of want to show you guys make you guys aware of it because I wasted a lot of time on this bug I I'm assuming it's a bug and I'm not doing something wrong but I wasted a good you know four or five hours probably just kind of mapping out what this bug is and how to work around it so I want to share that with you and then in the video after that one I'm gonna show you how to create the debug and release builds so that you can only have so that you don't have any useless code in your production in your production project so like this will only be present in the debug build as opposed to the release build so if you liked the video leave a like if you got any questions if you think that I should be or if you want to see anything specific tested let me know below I'm more than happy to do that if enough people recommend it or want to see it and again don't forget that this course is part of a full-length course on my website it's completely free just go to coding with Mitch comm go to courses and you can find that course registration takes 30 seconds again it's free so that's gonna be for this video and I'll see you the next one you
Info
Channel: CodingWithMitch
Views: 7,082
Rating: 4.9814816 out of 5
Keywords: android espresso, android espresso tutorial, espresso android tutorial, android ui testing, android ui testing with espresso, junit espresso, esspresso idling resource, espresso counting resource, espresso background threads, espresso asynchronous, espresso idling resource tutorial, espresso idling resource example
Id: _96FT7E6PL4
Channel Id: undefined
Length: 17min 39sec (1059 seconds)
Published: Thu Dec 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.