Refactor image load code, add unit tests.

The code to load and set the image was semi-duplicated and
scattered in various places. This was the source of defects
as this code is called asynchronously and was hard to debug.
This change attemps ot consolidate all the image load logic
in one place. It also adds sorely needed unit tests.

Change-Id: I92eb1c48e2ff0808134e56b4b150e22254eb2d6e