More on testing automation in Android
Test automation has been a particularly interesting topic for me in the past few weeks. I wanted to share some of the info I've gathered hoping that it will be useful to someone else. In my previous post I talked about Testdroid which makes use of robotium and also can make MonkeyRunner tests The plugin provided by Testdroid is essentially a way to record and script UI events.
It will generate test cases for you where you can then go in and add assertions. It's pretty neat.
Now i'd like to talk a little bit about Robolectric. Robolectric is created and maintained by folks at Pivotal labs. Robolectric runs within your VM which makes it very cool.
This means you don't need the emulator to run tests. That's pretty cool! I definitely recommend giving this a try. I'm going to deep dive into Robolectric to use on my current personal project.
I'll post some of those results later.
How to ensure views are set below the fold on a device.
Did you ever have a view that you needed to make sure started off below the screen so the user could scroll to it later?
Sounds weird but I had to do that for a designer's crazy ass UI.
Wasn't as simple as I first thought so I wanted to share the code.
Turns out that onLayout() doesn't always work. you sometimes don't get all of the dimensions for the views you're interested in and it's just a mess. I guess all the measures() for each view haven't happened yet. Who knows, or rather who cares. It didn't work so I had to find a way that did.
Turns out you can listen for layout change events by adding a listener to the ViewTreeObserver of a view, which notifies of many layout changes on your view. If you add a OnGlobalLayoutListener to the ViewTreeObserver of the view you're interested in you are guaranteed to have gotten the measure() values at that point.
You can then use any way you want to set the position of your view. In my case I chose to use padding. Anyone have a better suggestion?
ViewTreeObserver vto = bottomContainer.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Display display = getWindowManager().getDefaultDisplay();
LinearLayout bottomContainer = (LinearLayout) findViewById(R.id.footer_container);
ViewGroup profileBar = (ViewGroup) findViewById(R.id.profileBar);
RelativeLayout sv = (RelativeLayout) findViewById(R.id.main);
bottomContainer.setPadding(0, display.getHeight(), 0, 0);
bottomContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
bottomContainer.forceLayout();
}
});
Testdroid – a great test solution for Testing Automation in Android
Because of fragmentation in Android it is very important to test you apps in as many devices as you can get your hands on. You've heard it all before right?
So have I. Testing mobile apps still remains quite a daunting task. For larger projects such as the one I'm currently working on Test Automation becomes a necessity very quickly. It is simply too time consuming, error prone and difficult to test manually. So what can you do? What tools are available to help you automate UI testing?
Luckily, Android comes to the rescue. Android's testing framework is top notch and easy to work with. They make life worth living.
Android's testing toolset is based on JUnit and is very well integrated for android projects into Eclipse. Android instrumentation provides a way to hook into android components and load applications.
The testing framework also provides a way to create Mock objects to help you isolate parts of your application for specific tests.
You also get the monkeyrunner tool, a python framework for executing android apps.
All in all you get a very powerful set of tools that can enable you to produce detailed and efficient testing solutions.
But wait! there's more! Since of course this framework is so great, there are developers building great testing solutions based on these tools. One that i'm starting to dig into is robotium. Robotium is a test framework aimed at test case developers to write function, system and acceptance tests scenarios for apps.
Adding yet more value to this are companies like bitbar who provide a plugin for Eclipse called testdroid which makes recording a UI test of your app, child's play. Very cool and worth checking out!
Sandip Chitale’s Blog: LinearLayout gravity and layout_gravity explained
Sandip Chitale's Blog: LinearLayout gravity and layout_gravity explained.
Very helpful visualization of how gravity and layout_gravity play with each other.
Android bluetooth example
I'm starting more detailed work on Bluetooth sharing.
I found this snippet of code on the dev guide that looked very similar to a TCP socket which I did a while ago.
The code below is taken from :
http://developer.android.com/guide/topics/wireless/bluetooth.html
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main Activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main Activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
Lawrys Digital Dinner Bell – Android Market
My first major android app!
Over 10k downloads already!
android and JDK compliance – how to fix @override errors
From time to time when working with other people's projects I run into build errors that are caused by the @override annotation. It took me a while to understand why that was happening as I was usually able to solve it by simply deleting the @override annotation or adding it where needed.
Turns out the problem is because of of a mismatch in the JDK different developers are using.
Starting in Java 6 the @override annotation no longer applies to interface methods. So most methods derived from an interface no longer need them. They are however needed with Java 5. The problem arises if a dev with Java 5 creates an android project, By default the project will get set with whatever JDK compliance is valid in their environment, in this Java 1.5. If a developer with java 6 opens the same project all @override annotations will be flagged as errors when appearing above interface methods.
Thankfully, this is easy to fix.
You simply need to go to your android project properties, choose the java compiler and select the appropriate JDK compliance level for your environment.
Click enable project specific settings to be able to select the appropriate JDK version to use.
Don't know which hava version you have? No problem. Simply type java -version in your command line to get that info. It will look like below
Don't want to have to do this every time you and another developer have mismatching JDKs? You can configure it as a default on your workspace.
From the same Java Compiler properties window, click "Configure Workspace Settings"
That will bring you to the window below where you can choose your default Compiler compliance level for your JDK Compliance.
As per usual i'd like to thank the fine folks @ stackoverflow for their help.
Android units – pixels, density, dpi, dip, dp, dps, sp, sip | Edwin Evans
A bit more on dealing with android units.
Android units – pixels, density, dpi, dip, dp, dps, sp, sip | Edwin Evans.
adb command line: How to launch an app from adb
If you ever need to launch an app from the adb command line here's how.
First, if you haven't added the platform tools to you %PATH% environment you should. It's a great time saving shortcut.
Ok now that you're cool like us on to the adb command:
You need to use the am command which is an adb shell command. adb has its own set of commands, adb shell has its own other set.
So what does the syntax for the am command look like? First i'll show you the command and then i'll explain it.
Ok so there it is. adb shell am start -a android.intent.action.MAIN -n com.rga.sony/.ZeusMain
Here is also a pretty great explanation:
http://www.anddev.org/using_the_am-tool_start_activities-intens_from_a_shell-t368.html





