Testing for memory usage

Sometimes, memory consumption is an important factor to measure the good behavior of the test target, be it an Activity, Service, Content Provider, or another component.

To test for this condition, we can use a utility test that you can invoke from other tests mainly after having run a test loop:

public void assertNotInLowMemoryCondition() {
//Verification: check if it is in low memory
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
 ((ActivityManager)getActivity()
.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryInfo(mi);
assertFalse("Low memory condition", mi.lowMemory);
}

This assertion can be called from other tests. At the beginning, it obtains MemoryInfo from ActivityManager using getMemoryInfo() ...

Get Learning Android Application Testing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.