March 2, 2021
Hot Topics:

Building a Simple BlackBerry Application Interface

  • By Jeff Langr
  • Send Email »
  • More Articles »

Listing 3: TestListFieldModel.

package com.langrsoft.app;

import java.util.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import com.langrsoft.bbtest.*;
import com.langrsoft.ui.util.*;

public class TestListFieldModel implements ListFieldCallback {
   private Vector tests = new Vector();

   public void drawListRow(
      ListField listField, Graphics graphics, int index,
         int y, int width) {
         MicroTest test = (MicroTest)get(listField, index);
         graphics.setColor(test.passed() ? Color.GREEN :
         graphics.drawText(toString(test), 0, y);

   public Object get(ListField listField, int index) {
      return tests.elementAt(index);

   public int getPreferredWidth(ListField listField) {
      return listField.getWidth();

   private String toString(MicroTest test) {
      return test.name() + " [" + test.className() + "]";

   public int indexOfList(ListField listField, String prefix,
      int start) {
      return listField.getSelectedIndex();

   public void insert(MicroTest test) {
      tests.insertElementAt(test, 0);

Wrapping Up

As part of building this rudimentary user interface for the unit testing framework, I made some minor improvements to the core BBTest code. I also learned that you do not want to code a catch block to trap Throwable instances; otherwise, the BlackBerry generates some odd exceptions. I changed the BBTest code to catch RuntimeException instead. The RIM (Research In Motion, the company that makes the BlackBerry) Javadoc for Throwable discusses the details of this special implementation.

Overall, the very small amount of code I've presented here points out a significant number of interesting BlackBerry development challenges. It took me a good bit of time to search for solutions and otherwise dig out of the pitfalls; hopefully, this information can help you avoid wasting similar time.

The simple BBTest user interface does the job of showing which tests passed and failed. I hope to make this a robust open source unit testing tool for the BlackBerry, so it needs a bit more work. I intend to add some nicer graphics, replace the list with a tree widget, make the font smaller, and introduce a few more usability features to make the test tool more effective.

Coding user interface applications in any environment, BlackBerry included, is a risky proposition: it's always too easy to allow the model, view, and controller code to be highly intermingled, making for an unmaintainable mess. Because I'm a proponent of TDD, I'll be using this current rudimentary BBTest tool to rebuild the testing tool, but in a test-first manner, to help ensure that it ends up with the right design.

Download the Code

You can download the sample code for agent.zip and bbtest.zip here.

About the Author

Jeff Langr is a veteran software developer with over a quarter century of professional software development experience. He's written two books, including Agile Java: Crafting Code With Test-Driven Development (Prentice Hall) in 2005. Jeff has contributed a couple chapters to Uncle Bob Martin's upcoming book, Clean Code. Jeff has written over 80 articles on software development, with over forty appearing at Developer.com. You can learn more about Jeff at his site, http://langrsoft.com, or you can contact him via email at jeff at langrsoft dot com.

Page 3 of 3

This article was originally published on August 14, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date