Chase Brammer Thinking in Code

16Jan/090

Alex Uhlmann on Agile Testing and Unit Testing in Flex

I got an email from Alex Uhlmann answering some questions about Flex Unit testing, and pointed me to a presentation he gave at max on the topic!  I guess that he had to twist some fingers to get the people at Adobe TV to post it ;)

Here is the link

And embedded for your viewing pleasure.

15Jan/090

Flex Unit Testing

Unit testing rocks.  It makes big projects less buggy, takes costs down, and in general is best practice for RIA development.  On top of all that, the hours spent searching through thousands of lines of code to eliminate a simple error all but goes away!  In the end testing is like source control.  You don't know how much you need until you start using it.

So what is unit testing?

"It's about figuring out what you are trying to do before you run off to try to do it. You write a specification that nails down a small aspect of behavior in a concise, unambiguous, and executable form. It's that simple. Does that mean you write tests? No. It means you write specifications of what your code will have to do. It means you specify the behavior of your code ahead of time. But not far ahead of time. In fact, just before you write the code is best because that's when you have as much information at hand as you will up to that point."

Who uses it?

“The fact of the matter is, not only is Adobe Consulting leveraging FlexUnit each and every day on our projects, but we continue to be advocates to our customers of using FlexUnit on their projects.”

Steven Webster
Adobe Consulting
cairngorm framework core contributor

What are the benefits of using TDD?

  • Faster overall development
  • Less expensive (development and maintenance cycles)
  • Less complicated code bases
  • More independent and reusable code
  • Less time and problems with regression testing
  • Break new ground

What are some of the cons of TDD?

  • Developer learning curve
  • Slower initial development speed
  • Problems with a MVC type framework (Nimbus, Cairngorm, PureMVC)
  • Poor Documentation

Implementation

Ok, so by now you should be convinced that Unit Testing will save you time, money, and sleepless nights.  But does FlexUnit play well with existing frameworks like Nimbus, Cairngorm, and PureMVC?  In short.  Not really.  FlexUnit can do it, but in a hackish way.  Adobe is trying to fix this though.  They are integrating FlexUnit into the next release of Flex, Flex 4!  Flex 4's release date is sometime in the 4th quater of 2009.

Until then, what do we do? Wait for more docs from Adobe!  Alex Uhlmann, a really nice guy who also happens to be a Flex Ninja at Adobe consulting responded to an email saying:

Yes I and many others within AC use TDD. I understand your frustration with the lack of information out there. I've talked at the last MAX conference about it and AFAIK this will be on Adobe TV soon. I'll ask when exactly. Also, I plan on publishing a lot more around this area.  [...]  I've sent some emails to Adobe folks to find out when my video will be released.

Current Course of Action

In lieu of Flex 4 and better integrated Unit Testing in the Flex Builder, what should we do?  Keep making your code testable wherever possible!  In projects that are leveraging frameworks like Nimbus or Cairngorm most of the business logic will be executed in the Command classes.  But that does not mean that the business logic of an application should reside in the commands themselves.  There are many views on where business logic should reside (commands, model, helpers), but for making testable and getting more code coverage, the best practice for now will most likely be putting as much code into self contained utility classes and objects.  This makes for better testing, and gets more code coverage!

TDD and Unit Testing

View SlideShare presentation or Upload your own. (tags: 3 flex)
9Jan/090

Unknown classes, and classes to XML

In working on preparing some stuff for a training I am doing on Unit Testing in Flex, I wanted to find a way that I could reference any class, and any method in that class without ever knowning the actual class name or method names.  This is a pretty slick solution that will go through and add all of my test case methods to my FlexUnit testing suite.

1) I needed to call the class, but do it without ever knowing its name.  The way to do this is using getQualifiedClassName to get a string of the class.  Fully qualified means that it is the full path, so something like "com.chasebrammer.samples.TestClass"

2) I needed to call the class based off of its qualifiedName.  To do this, I used a little used native method called getDefinitionByName

3) After getting what class I am calling from, I wanted to get all of the method names in that class, and find any that have the name "test" on the front, and add them to my testSuite.  To get the xml of an object (or class), it is actually really simple, here is an example.  Then it was just a matter of looping the code to find the right methods and add them to the test suite.  So, below is the code, I find code talks better than description of code.

private function createSuite():TestSuite {
   var ts:TestSuite = new TestSuite();
   var className:String = getQualifiedClassName(this);
   addTests(ts, getDefinitionByName(className) as Class);
   return ts;
}
 
private function addTests(suite:TestSuite, testerClass:Class):void {
   var desc:XML = describeType(new testerClass("method"));
   for each(var method:XML in desc.method) {
      if (method.@name.substr(0, 4) == "test")
      suite.addTest(new testerClass(method.@name));
   }
}
17Dec/080

Mormonism and Actionscript as religions

A friend emailed me a humorous link that was posted on slashdot about what religions would be in the programming world.    I like it that Mormonism was included on the list.

C# would be Mormonism - At first glance, it's the same as Java, but at a closer look you realize that it's controlled by a single corporation (which many Java followers believe to be evil), and that many theological concepts are quite different. You suspect that it'd probably be nice, if only all the followers of Java wouldn't discriminate so much against you for following it.

But where is my other religion, Actionscript?  If I were to make the same analogies to actionscript...

Actionscript would be the Church of England - Actionscripts dominant cousin, via the ECMA family , is javascript - the most commonly used language on the web and like the Catholic church has millions of adherants, of which almost none really know the language.  When Macromedia needed a little more som'n som'n like Henry the VIII did, we ended up with a divergent, but very similar language.

10Dec/081

Data Encryption in Adobe AIR

Daniel Dura - a really cool AIR Platform evangelist from Utah that I have met a few times at different flash user group meetings - just posted a great video on encrypting data in AIR.

I just wanted to point out how awesome AIR is that there is a single method in Flex to encrypt data - the EncryptedLocalStore feature, and it will switch between the different most secure encryption methods for different operating systems.  For Macs it will default to using the Keychain, and for Windows it will default to using the DPAPI.

What is cool too, is because EncryptedLocalStore uses just a simple ByteArray to store data, you can encrypt almost anything you can load into the Flash Player - images, swf's, text ... anything really.

Now, my question of curiousity, does anyone know what it uses for Linux?

8Dec/080

Caching the Flex Framework – using a RSL

In a project recently, I was having a bug where fonts would load into the Flex Framework, but would never be rendered in the text field. The strange thing about this though is that it was only happening on select computers. This is a strange thing for Flex/Flash developers to run into because we pride ourselves in developing a ubiquitous platform that doesn’t change from OS to OS, and from browser to browser (I am looking at you CSS).

Tracking down why the fonts weren’t loading was a pain, because every computer I would get my hands on (thanks to VMware) would show the problem, and then it would magically disappear. The problem wasn’t in the code, however hard I looked and wanted to find it there. The problem was in the Flex framework.

The flex framework is usually compiled into the .SWF that a Flex project outputs, which adds about 500k to a project depending on what parts of the framework you use. But when you compile the framework into a project, you may not be getting the most recent or best framework for your application. But 99% of the time this doesn’t matter because you are getting what you need out of the framework. But getting the wrong framework, like what happened to me, causes some very frustrating problems.

Even though I was using the wrong framework, this didn’t affect most users, because Flex was defaulting to a cached Flex framework that the user had picked up somewhere else on the web. So, to force users to use the right framework, all that was needed was to use a RSL (runtime-shared-library).

What is an RSL? It is basically a package of code that has been put into a file by adobe.  This file can then be loaded in and cached IF the user doesn't already have it, but the great this is is that if the User does have it your download to the user is significanlty less..  These files however need to be placed on the local server and consist of the same file twice.  The default is an Adobe signed secure version called a .swz (pronounced swiz) and the second is swf that is a failsafe backup that contains the same code, but is not signed.

A nice side note on using and RSL is that file size will decrease by about 500k!

There are lots of ways to enable and choose an RSL. The easiest though is to just go to the project properties in flex, and choose your RSL. Then when you compile your project you will notice some extra files like framework_3.0.0.477.swf and framework_3.0.0.477.swz. These are adobe signed frameworks that are used in case the framework isn’t cached and needs to be downloaded.

One thing to note is that the flash player 9 framework that was used in the original shipment of Flex Builder 3 is the 3.0.0.477 framework.  But if you have updated your Flex SDK or download the latest version of flex you will notice that your framework files will be the updated to 3.2.0.3958

On one last note, the thing that helped to find this bug the most was the Flash Settings manager. Most people don’t realize that the Flex framework is not cached in the browser cache, it is cached in the Flash Player cache. To every computer to show the problem, I just had to disable the flash cache.





   

Pages

Categories

Blogroll

Archive

Meta