A few weeks ago, I landed a new capability to the testing framework to make it 
easier to write tests where you need chrome Javascript to cause changes in the 
Native UI. For instance, my initial goal was to use this to test the 
nsIPromptService. margaret has also used it to test the new Home banner API. 
Its in some ways a much less polished version of the JavscriptTest framework 
nalexander wrote. Nick's stuff is great if you're just testing some JS. This is 
helpful if you've got chrome JS that will trigger/interact with Java UI. 

Usage is pretty simple, just put a file in 
mobile/android/base/tests/roboextender and it will be installed as an addon in 
the test framework's profile. To load the file just load a chrome url  
"chrome://roboextender/content/myTestPage.html". That page loads with full 
chrome privileges, and can load JSM's or access XPCOM components, making it 
simple to send messages from the page back to java (where you can assert). It 
also runs locally on the device, which is nice if you're having difficulties 
connecting to the js-http server on your local computer. Sending messages to 
the page SHOULD be possible with some reflection, but is left as an exercise to 
the reader :)

Margaret's home banner test is a pretty concise example:

http://mxr.mozilla.org/mozilla-central/source/mobile/android/base/tests/testHomeBanner.java
http://mxr.mozilla.org/mozilla-central/source/mobile/android/base/tests/roboextender/robocop_home_banner.html?force=1

testHomeBanner.java in this case loads the 
chrome://roboextender/content/robocop_home_banner.html#test url. Note the hash 
there tells the chrome to run a particular function. You don't really need to 
do that if you're only running one function in this test, but its a nifty way 
to put multiple different tests in one html file (just load the page with a 
different hash).

The test page can then send data back to the java ui, so Java should register 
for the events it expects:

// robocop_home_banner.html
Services.androidBridge.handleGeckoMessage(JSON.stringify({type: 
"TestHomeBanner:MessageShown"}));

// testHomeBanner.java
eventExpecter = mActions.expectGeckoEvent("TestHomeBanner:MessageShown");
// do something  (load the page?)
eventExpecter.blockForEvent();

you can also send data back with the event by using:
String data = eventExpecter.blockForEventDataWithTimeout(MAX_WAIT_MS);

and then assert based on the message contents.

Happy testing!
Wes
_______________________________________________
mobile-firefox-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/mobile-firefox-dev

Reply via email to