July 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Building a Simple BlackBerry Application Interface

  • August 14, 2008
  • By Jeff Langr
  • Send Email »
  • More Articles »

As I dig deep into building applications for the BlackBerry, I'd like to employ test-driven development (TDD). I've constructed a simple assertion-based framework, BBTest, designed for unit testing BlackBerry applications, but so far it is only a domain model. A testing framework isn't of much value without a way of viewing its output, so I am building an as-simple-as-possible user interface for the framework. By using BBTest, you'll have a way to test-drive your applications as well.

Building BlackBerry applications is much like building Java Swing applications. The same general concepts apply, but there also are some very interesting differences, from how list controls work all the way up to how you support multiple entry points in an application.

The design of the BlackBerry means that I need to use a single application entry point (in other words, the main method) to trigger both the given application that I'm testing, as well as the unit tests for that application. In Eclipse, both the application source code and test framework source code need to sit side-by-side in one Eclipse project. (I'm using a build script to copy over the source from the BBTest project.)

To trigger the tests separately requires a second Eclipse project. This second project represents an alternate entry point for the primary application. When the BlackBerry (or BlackBerry simulator) executes this alternate entry point, the primary application gets triggered with a different argument to its main method. That will allow me to code the main method to take a different action—in this case, to execute the primary application's unit tests.

The steps to configure the alternate entry point are as follows:

  1. Open the project properties for the secondary (testing) project.
  2. Select BlackBerry Project Properties.
  3. Select the Application tab.
  4. From the Project type: dropdown, select Alternate CLDC Application Entry Point.
  5. Ensure that the Alternate entry point for: dropdown shows the primary project as being selected.
  6. In the field marked Argument passed to "static public void main(String args[])":, enter unittest.

Figure 1 shows the BlackBerry Project Properties dialog.



Click here for a larger image.

Figure 1: The Eclipse BlackBerry Project Properties dialog.

BlackBerry Application Fundamentals

The main class for my primary application is shown is Listing 1. BlackBerry applications extend the class net.rim.device.api.ui.UiApplication. For applications with a user interface, the primary job of this main class is to create a screen, push it onto the stack of screens that the BlackBerry tracks, and then enter the event dispacher. If the application requires additional screens, they get pushed onto or popped from the stack. The BlackBerry routes input events, including trackwheel events and key press events, to the screen on the top of the stack.

Programming BlackBerry interfaces is similar to programming Java Swing applications. Per the Javadoc, "Only one thread at a time (generally, the event-dispatching thread) may gain access to an interface component at once." Other interactions with user interface components must be executed by using invokeLater or invokeAndWait.

Listing 1 shows that I execute the unit test framework if the first argument passed to main is the String "unittest"—to correspond with the property setting for the alternate entry point. Otherwise, the primary application's main screen gets pushed onto the stack.

The TestApp class, shown in Listing 2, is the simple user interface I created for purposes of quickly displaying some test results. SampleTest is a MicroTestGroup subclass with a couple simple passing tests and one failing test.

Listing 1: A main application class.

package com.langrsoft.app;

import net.rim.device.api.ui.*;
import com.langrsoft.agent.*;

public class AgentApp extends UiApplication {
   public static void main(String args[]) {
      AgentApp app = new AgentApp();
      if (isInTestMode(args))
         app.test();
      else
         app.go();
      app.enterEventDispatcher();
   }

   private void test() {
      pushScreen(new TestApp(new SampleTest()));
   }

   private static boolean isInTestMode(String[] args) {
      return args.length > 0 && "unittest".equals(args[0]);
   }

   public void go() {
      pushScreen(new MainApp());
   }
}




Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel