pivotal/robolectric — pull request #190 by waltsu (2012-02-04 09:45:09+00:00)
Title: ShadowSQLiteOpenHelper will keep same database if same Context is given in constructor

Right now, always when user calls getWritableDatabase() or getReadableDatabase() he gets fresh empty database and this is how ShadowSQLiteOpenHelper is intented to work. However I needed the way to look what my function, which was under the test, has written in to the database.

In Android everything is happening inside a Context so I made a minor change to ShadowSQLiteOpenHelper: If user gives Context-instance in the constructor, it will be saved. And if the same Context-instance is given next time the constructor is invoked, getWritableDatabase or getReadableDatabase will return the same SQLiteDatabase.

So then I'm able to give a Context to my function which is under the test. It will open SQLiteOpenHelper the same way it does on Android platform. But when the function's execution has ended I can create new SQLiteOpenHelper with the same Context I gave to the function. Then I can get a database with getWritableDatabase and fetch the data that function inserted. Also the same problem occurs if I want to setup database before executing the tests. This improvement will fix that issue also.

However there is one problem. I had to 'disable' (it will return before doing anything) ShadowSQLiteOpenHelper.close() if Context has been given in the constructor because finally close() will call Connection.close() which will wipe the database out of the memory.

I also created tests for modified parts of the SQLiteOpenHelper.