Learning JavaScript Design Patterns by Addy Osmani

6. September, 2013

Learning JavaScript Design Patterns” is a free eBook (also available in print via O’Reilly Media) by Addy Osmani which explains how to apply various design patterns to JavaScript.

It’s not only a great resource to improve your JavaScript skills or an easily understandable refresher of design patterns (if you didn’t understand the C/C++ or Java versions, this is the place to go!).

It also lists some of the latest patterns that you might have missed like the MVVM pattern.

Last but not least, it can serve as a test for huge HTML pages 😉


Wine on OpenSUSE Without Sound

4. September, 2013

Yesterday, I tried to install the Windows game Homeworld on my openSUSE 12.3. After a couple of problems, the no-cd patch, I could start the game with:

/opt/cxoffice/bin/cxrun --bottle Homeworld homeworld.exe /1600 /enable3DNow /enableSSE /device gl /heap 1073741824

Note: For /device gl, you must install DirectX 9.

The main problem: No sound. Starting the Wine Configuration, I saw that it was using winealsa.drv in the Audio tab.

Looking into the terminal finally solved the mystery:

ALSA lib dlmisc.c:236:(snd1_dlobj_cache_get) Cannot open shared library /usr/lib/alsa-lib/libasound_module_pcm_pulse.so
ALSA lib dlmisc.c:236:(snd1_dlobj_cache_get) Cannot open shared library /usr/lib/alsa-lib/libasound_module_pcm_pulse.so

My system is 64bit but Crossover is probably a 32bit application.

Installing alsa-plugins-pulse-32bit finally solved the problem.

Also make sure you have this in your ~/.asoundrc:

pcm.!default {
    type pulse
}
ctl.!default {
    type pulse
}

Related:


Selenium vs. ZK

30. August, 2013

Testing ZK applications using Selenium can be a drag. Selenium offers a lot of tool to test traditional request-response cycle applications. But it relies heavily on stable element IDs and submitting whole forms.

ZK, on the other hand, sends just a skeleton page to the browser and from then, builds everything with JavaScript. Instead of posting a whole form, it submits individual field values (to trigger validation). And it assigns random IDs to reach element.

At first glance, the two don’t seem to be a perfect match. But there are a couple of simple tools that will make your life much more simple.

Getting Stable IDs for Tests

One solution here is to write an ID generator which always returns the same IDs for each element. But this is tedious, error-prone and sometimes impossible.

A better solution is to attach a custom attribute to some elements which doesn’t change. A beacon. If you have a central content area, then being able to find that will make your life much more simple because whatever else you might seek – it must be a child of the main content.

To do that, add a XML namespace:

    <zk xmlns:cl="client" xmlns:ca="client/attribute">

to the top of your ZUL file, you can now use a new attribute ca:data-test-id="xxx" to set a fixed attribute on an element. In Selenium code, you can now locate this element using this code:

    By.xpath( "//div[@data-test-id = 'xxx']" )

I suggest to use this sparingly in order not to bloat your DOM. But a few of them for fast moving targets will make your tests much more stable.

Debugging the DOM

Sometimes, your life would be much more simple if you could see what the DOM was when your test failed. Here is a simple trick to get a HTML fragment from the browser:

    protected JavascriptLibrary javascript = new JavascriptLibrary();

    public String dump( WebElement element ) {
        return (String) javascript.executeScript( driver,
            "return arguments[0].innerHTML", element );
    }

You can now use JTidy and JDOM to convert the fragment first into W3C XML nodes and then into JDOM elements:

    public org.w3c.dom.Document asDom( WebElement parent ) {
        String html = dump( parent );
        Tidy tidy = new Tidy();
        tidy.setShowWarnings( false );
        tidy.setErrout( new PrintWriter( new NullOutputStream() ) );
        org.w3c.dom.Document dom = tidy.parseDOM(
           new StringReader( html ), null );
        return dom;
    }

    public static org.jdom2.Document asJDOM( org.w3c.dom.Document content ) {

        // JDOM chokes on: org.jdom2.IllegalNameException: The name "html PUBLIC "-//W3C//DTD HTML 4.01//EN"" is not legal for JDOM/XML DocTypes: XML names cannot contain the character " ".
        Node docType = content.getDoctype();
        if( null != docType ) {
            content.removeChild( docType );
        }

        DOMBuilder builder = new DOMBuilder();
        org.jdom2.Document jdomDoc = builder.build( content );

        return jdomDoc;
    }

Another great use case for this is a default exception handler for tests which saves a screenshot and a copy of the DOM at the time a test fails. No more guessing why something didn’t happen.


Debugging proxy.pac Files

29. August, 2013

I just wrote a small utility to debug proxy.pac files using the excellent pacparser library.

You can find it here (gist).


Google Shares Your WLAN Passwords with NSA

17. July, 2013

If you “Back up my data” is enabled on your Android phone, then Google keeps a clear-text, unencrypted copy of your WLAN passwords on its servers. Since Google is an US company, the government and its agencies have access to this data. Google also keeps a database with the location of all WLANs (for their location service) so it’s trivial for them to gain access (even though someone must physically walk/drive into the range of the WLAN router).

Solution: Disable this function, use a local backup program (disable cloud backup for them as well) and change all your passwords.

Related articles:


Installing Kubuntu 13.04 or Luckily, I Get Paid for This

27. June, 2013

I have now spent many hours trying to install Kubuntu 13.04. As you can see by the time spent, it didn’t exactly go smooth. But before I begin the barrage, let me assure you that I’m a Linux fan, I’m using Linux for many years now, I’m using it at home and even after this ordeal, I’m not going back to either Windows or Mac.

Let’s start with the installer. It’s fast, looks slick … Why are there no keyboard shortcuts? 😦

Harddisk partitioning. No keyboard shortcuts. Why can’t I format the swap partition? The button is dead but it doesn’t look disabled.

Okay. Let’s put / on my SSD, add some swap, /home on my normal harddisk.

I live in Switzerland, so let’s select that … “ubi-console-setup failed with exit code 141” 😦 Googling quickly got me solution. Okay, let’s go back. Exit code 141 again. Grrr…. Close … Why do I get a desktop now?

Oh, nice an installer icon. Click … why is it asking be *again* for the harddisk layout? Still no keyboard shortcuts … and error 141 is back. Great. Reboot.

Trying again … okay, selecting the time zone triggers the ubi-console-setup bug. Thanks, I just entered my harddisk layout a third time. Reboot.

Germany. No, leave my ***** keyboard layout alone. Just shut up and … harddisk layout again … Maybe I can save some time by not formatting the partitions again?

Yes. Err … no. Now it tried to “backup installed packages” ARGH!!! Okay, that shouldn’t take long … 1 hour …. 2 hours …. Power off.

Harddisk again, yes, format everything. German keyboard layout. Waiting for everything to install from the USB stick.

Creating this stick was an adventure of it’s own. What is the difference between Kubuntu, Kubuntu HDmedia, Kubuntu active and Kubuntu 13.04?

Yay, a desktop.

Let’s switch the keyboard layout. Good.

Where is Chrome or Firefox? Ah, nice, there is a one-click Firefox installer. Click … wait … wait some more … is that thing doing anything?

How do I install software here? Where is aptiude? Where did the Software installer in the System Settings go??? Okay, it’s “Muon”, now. Well, let’s try that.

First, set the proxy. Checking the proxy in rekonq … works.

Starting Muon. Search for “Firefox”. No results. “Chrome”? Nope. “Vim”? Anything? What’s wrong with this software?

I added “Canonical partners” as a repository but there is no indication whatsoever that it accepts this change, that it downloads the new repo or anything. My first impression with Muon: Stay away.

Back to apt-get. Installing chrome works. Starting it … suddenly, my proxy settings in System Settings are gone? Okay, put them back and also add them to Chrome. Good, that seems to work.

Trying Muon one last time. Aha, when I prepare everything with apt from the command line (like downloading the repositories), Muon can suddenly find packages. But it can’t install anything. I click on “Install”, it flickers and that’s it. No error message, the UI gets corrupted and no software installed.

So. I’m now officially on 13.04. The UI looks nice enough.

Trying to upgrade the system. “Muon Package Manager” (what’s the difference between this thing and “Muon Software Center”?) seems to work and it shows me 289 packages ready for upgrades. Apply … wtf!? “The following software can’t be verified. WARNING: Installing such software is a security risk …” Okay, if the official Ubuntu updates are a security risk, why even bother???

*sigh* Go on. ARGH!! “Could not download packages” :-((( Details: “Unable to connect to …” Apparently, Muon doesn’t care whether there is a proxy or not … or … ah, of course, the system has again forgotten my proxy settings. Okay, f*** that. apt-get upgrade. Yes, install all those trojans on my computer, I don’t care anymore. Yes, yes, yes, yes, just do it already.

*Deep breathing*

How do I make the proxy settings stick???

Oh well, let’s just add them to /etc/environment – which is probably the worst way to do it but hey, if the obvious ways don’t work and Google doesn’t turn up anything useful, what’s a man to do?

Conclusion: That was my adventure with installing Kubuntu 13.04. Tomorrow, I’ll try to do some actual work on it.

Overall, I feel that many, many people put a lot of work into the system and all this was ruined by a few small bugs (Muon, proxy settings, Swiss keyboard in the installer).


Quick: How Does Quicksort Work?

24. June, 2013

Every now and then, we need to remember how some complex algorithm works. For some, code works well. Or puzzles. Others prefer an explanation with examples. A third group likes to see what is going on.

The AlgoViz.org portal contains visualizations of all kinds of algorithms for the visually inclined.

 


Google Reader is Going Away

5. June, 2013

I’m looking for a replacement of Google Reader because it’s being shut down July 1, 2013. People don’t seem to be interested in the petition.

So far, I haven’t found a useful replacement.

Feedly has no bearable support for tagging (the tag editor is a joke, tags can’t be sorted with drag&drop, adding new tags is a pain, you have only a single tag per article and no tagging via the keyboard).

But the worst bug is that Feedly will randomly refresh the page. This means: You are reading an article and in the middle of it, Feedly will throw it away and replace it with some random, new article. To add insult to injury, there is no easy way to get the lost article back since it will now be in the “has been read” state. Even when you read articles in the headlines-only mode, you can lose an interesting article because Feedly sometimes refreshes the list before you can open it.

From the horde of online readers, Feedly was one of the best. The Old Reader? Great … if you don’t need tagging. I have five years worth of tags.

NewsBlur is … I don’t know. After half an hour, I still haven’t figured out how to add feeds, how to navigate them, let alone how to read articles.

Bloglovin. I need a RSS reader, not a fashion statement.

*sigh*


Windows

28. May, 2013

It’s time again for a rant.

There are people out there who believe that Windows is a “professional” OS.

Okaaaayyyy …

Let’s not bother to discuss what “professional” might mean but I can guarantee you, Windows is anything but “professional.” The reason is quite simple: It’s an OS for everyone. For the average computer user. Professionals use professional tools. They know their stuff, they don’t play with average stuff. You think anything about, say, a Formula One car is average? Even the finish is optimized for weight. Average cars clog the streets, professional cars can haul 400 tons and each tire costs $35’000 alone.

I have a friend who works in a garage. He has a set of tool that costs more than I ever spent on tools my whole life. One day, he had to get nut off. It was already in bad shape from previous attempts by “enthusiastic amateurs”. So he took one of his metric hexagon nut sockets that was one number too small and a hammer and hammered it on. Afterwards, he screwed it loose, forced the nut out of it’s too tight housing and the tool was still intact – barely a scratch. That’s the difference between what local DIY sells you and “professional.”

Windows is optimized to run without complaints. Errors are deliberately hidden from the user since the average computer user simply can’t deal with them anyway.

I turned away from Windows almost 20 years ago and never looked back. Sure, not something that everyone could or should do. Linux still isn’t an OS for the average user (even though we have come a long way – I can almost always set up my twin-monitor system without having to grab a text editor). But then, I need professional tools.

My fellow students wrote 100 page master theses with Word. I remember them cursing all the time. And we were studying computer science. I did mine in LaTeX: 400 pages, 0 problems. Oh, and I had everything under version control. Not that I would recommend it for everyone. But maybe, just maybe you feel like Windows and other M$ products are wasting your time: Have a look at professional tools.


Helicopter Management

23. April, 2013

Helicopter managers

  • Suddenly appear out of nowhere after longer periods of (felt) absence
  • Create huge gusts of dust
  • Make high and immediate demands for change
  • Vanish as fast as they appeared

As a friend of mine once said, “managers are a service.” I don’t bother my manager with my daily work (that’s what I’m paid to do) but for things that I can’t solve myself. When I need feedback and someone else starves me. When I need a tool but the company doesn’t give me budget or the authority to sign it. Things like that.

Helicopter managers don’t help. Most of the time, you don’t notice that they’re there. When the suddenly jump to action, a lot of dust gets blown around. But dust isn’t important. Usually, you can safely ignore dust. Dust only becomes a problem when it’s disturbed. As soon as it gets airborne, it can blind or suffocate you.

Since the helicopter manager is always in a hurry, they don’t have time to see change happen. They just start it. But they’re not there when the flaws and shortcomings of the change become apparent.

When you suffer from one of those, always keep a bucket with dust around to clog their engines.