Learning about XPCShell & Unit Tests – Part Deux

My goals for this week (even though it’s just Monday, I had time to just try it out today) were to create a simple XPCShell unit test that has access to nsIBrowserHistory through XPConnect, and perform three objectives:

  1. Grants access to the history manager through the service nsIBrowserHistory. For the sake of simplicity, I used the variable _HMSvc, in essence, “history manager service”.
  2. Creates a history entry with details, using the function addPageWithDetails. For the sake of simplicity, I used the URI, http://www.mozilla.org”.
  3. Check the count of the history entries in the manager by using the XPCShell unit test function, do_check_eq do_check_eq(1,_HMSvc.count); – Literally comparing the number of entries against my check of size 1, “compare if both objects equal 1”.

Full script here

var _HMSvc = null;
function get_historyManager() {
 if (_HMSvc)
   return _HMSvc;
   return _HMSvc = Components.classes["@mozilla.org/browser/global-history;2"].
                   getService(Components.interfaces.nsIBrowserHistory);
}

function run_test()
{
  var testURI = uri("http://mozilla.com");

  try{
    // Get the history Manager
       var HM = get_historyManager();
       do_check_neq(HM, null, "Retrieving the history manager failed");
    // Add a page into history using the URI, http://mozilla.com
       _HMSvc.addPageWithDetails(testURI,"testURI",Date.now()*1000);
    // Check if any pages exist in the history
       do_check_eq(1,_HMSvc.count);
   }catch (e){
       do_throw("Unexpected exception: " + e);
  }
}

The code is simple to the trained eye. What I mean by this is I had to look at a lot of examples of unit tests before I could see a pattern of how they run, for example each unit test needs a function run_test().

Running the code involves selecting the single unit test file, which I did by running:

make SOLO_FILE=”test_historySimple.js” -C toolkit/components/places/tests/ check-one

My code runs with the output:

test_historySimple.js: PASS.

So I know my script is testing correctly.

If I pass the value of 0, I receive output of

*** TEST-UNEXPECTED-FAIL | test_historySimple.js | Unexpected exception: 2147500036.

Log Output here: http://mozilla.pastebin.com/f710dd2c5

I’m starting to get the hang of writing unit tests even though they are simple. The process to learn will continue. Example, today, I spent a few hours trying to figure out which directory the script needs to be situated in. Searching MXR, I was looking at the location of other tests, and discovered that this script specifically, would wind up (after build & relearning how to symlink) in obj-i686-pc-linux-gnu/_tests/xpcshell-simple/test_places/unit, with the original file in /toolkit/components/places/tests/unit. Hours passed and all I was reading were , “File Not Found” and “Service not available” messages. Magic happened when I placed the files in their correct spots. Im assuming by doing that, my script found the nsIBrowserHistory interface fine – I guess from a specific location it knew where to look.

Not sure how to conclude this posting but, http://mxr.mozilla.org/ – Mozilla Cross Reference and http://developer.mozilla.org/ Mozilla Developer Wiki are your friends!

Cheers,

– AaronMT

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: