-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from maxme/unittests
Create a unittest sub-project
- Loading branch information
Showing
11 changed files
with
212 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/gen/ | ||
/bin/ | ||
build.xml | ||
local.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- package name must be unique so suffix with "tests" so package loader doesn't ignore us --> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="org.wordpress.android.tests" | ||
android:versionCode="1" | ||
android:versionName="1.0"> | ||
<!-- We add an application tag here just so that we can indicate that | ||
this package needs to link against the android.test library, | ||
which is needed when building test cases. --> | ||
<application> | ||
<uses-library android:name="android.test.runner" /> | ||
</application> | ||
<!-- | ||
This declares that this application uses the instrumentation test runner targeting | ||
the package of org.wordpress.android. To run the tests use the command: | ||
"adb shell am instrument -w org.wordpress.android.tests/android.test.InstrumentationTestRunner" | ||
--> | ||
<instrumentation android:name="android.test.InstrumentationTestRunner" | ||
android:targetPackage="org.wordpress.android" | ||
android:label="Tests for org.wordpress.android"/> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# This file is used to override default values used by the Ant build system. | ||
# | ||
# This file must be checked into Version Control Systems, as it is | ||
# integral to the build system of your project. | ||
|
||
# This file is only used by the Ant script. | ||
|
||
# You can use this to override default values such as | ||
# 'source.dir' for the location of your java source folder and | ||
# 'out.dir' for the location of your output folder. | ||
|
||
# You can also use it define how the release builds are signed by declaring | ||
# the following properties: | ||
# 'key.store' for the location of your keystore and | ||
# 'key.alias' for the name of the key to use. | ||
# The password will be asked during the build when you use the 'release' target. | ||
|
||
tested.project.dir=../ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
PRAGMA foreign_keys=OFF; | ||
BEGIN TRANSACTION; | ||
CREATE TABLE android_metadata (locale TEXT); | ||
INSERT INTO "android_metadata" VALUES('en_US'); | ||
CREATE TABLE accounts (id integer primary key autoincrement, url text, blogName text, username text, password text, imagePlacement text, centerThumbnail boolean, fullSizeImage boolean, maxImageWidth text, maxImageWidthId integer, lastCommentId integer, runService boolean, blogId integer, location boolean default false, dotcom_username text, dotcom_password text, api_key text, api_blogid text, dotcomFlag boolean default false, wpVersion text, httpuser text, httppassword text, postFormats text default '', isScaledImage boolean default false, scaledImgWidth integer default 1024, homeURL text default '', blog_options text default ''); | ||
CREATE TABLE eula (id integer primary key autoincrement, read integer not null, interval text, statsdate integer, sound boolean default false, vibrate boolean default false, light boolean default false, tagline text, tagline_flag boolean default false, uuid text, last_blog_id text); | ||
CREATE TABLE posts (id integer primary key autoincrement, blogID text, postid text, title text default '', dateCreated date, date_created_gmt date, categories text default '', custom_fields text default '', description text default '', link text default '', mt_allow_comments boolean, mt_allow_pings boolean, mt_excerpt text default '', mt_keywords text default '', mt_text_more text default '', permaLink text default '', post_status text default '', userid integer default 0, wp_author_display_name text default '', wp_author_id text default '', wp_password text default '', wp_post_format text default '', wp_slug text default '', mediaPaths text default '', latitude real, longitude real, localDraft boolean default 0, uploaded boolean default 0, isPage boolean default 0, wp_page_parent_id text, wp_page_parent_title text, isLocalChange boolean default 0); | ||
CREATE TABLE comments (blogID text, postID text, iCommentID integer, author text, comment text, commentDate text, commentDateFormatted text, status text, url text, email text, postTitle text); | ||
CREATE TABLE cats (id integer primary key autoincrement, blog_id text, wp_id integer, category_name text not null, parent_id integer default 0); | ||
INSERT INTO cats VALUES(73,'1',78,'Test category - malformed parent', 999); | ||
CREATE TABLE quickpress_shortcuts (id integer primary key autoincrement, accountId text, name text); | ||
CREATE TABLE media (id integer primary key autoincrement, postID integer not null, filePath text default '', fileName text default '', title text default '', description text default '', caption text default '', horizontalAlignment integer default 0, width integer default 0, height integer default 0, mimeType text default '', featured boolean default false, isVideo boolean default false, isFeaturedInPost boolean default false); | ||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
PRAGMA foreign_keys=OFF; | ||
BEGIN TRANSACTION; | ||
CREATE TABLE android_metadata (locale TEXT); | ||
INSERT INTO "android_metadata" VALUES('en_US'); | ||
CREATE TABLE accounts (id integer primary key autoincrement, url text, blogName text, username text, password text, imagePlacement text, centerThumbnail boolean, fullSizeImage boolean, maxImageWidth text, maxImageWidthId integer, lastCommentId integer, runService boolean, blogId integer, location boolean default false, dotcom_username text, dotcom_password text, api_key text, api_blogid text, dotcomFlag boolean default false, wpVersion text, httpuser text, httppassword text, postFormats text default '', isScaledImage boolean default false, scaledImgWidth integer default 1024, homeURL text default '', blog_options text default ''); | ||
CREATE TABLE eula (id integer primary key autoincrement, read integer not null, interval text, statsdate integer, sound boolean default false, vibrate boolean default false, light boolean default false, tagline text, tagline_flag boolean default false, uuid text, last_blog_id text); | ||
CREATE TABLE posts (id integer primary key autoincrement, blogID text, postid text, title text default '', dateCreated date, date_created_gmt date, categories text default '', custom_fields text default '', description text default '', link text default '', mt_allow_comments boolean, mt_allow_pings boolean, mt_excerpt text default '', mt_keywords text default '', mt_text_more text default '', permaLink text default '', post_status text default '', userid integer default 0, wp_author_display_name text default '', wp_author_id text default '', wp_password text default '', wp_post_format text default '', wp_slug text default '', mediaPaths text default '', latitude real, longitude real, localDraft boolean default 0, uploaded boolean default 0, isPage boolean default 0, wp_page_parent_id text, wp_page_parent_title text, isLocalChange boolean default 0); | ||
CREATE TABLE comments (blogID text, postID text, iCommentID integer, author text, comment text, commentDate text, commentDateFormatted text, status text, url text, email text, postTitle text); | ||
CREATE TABLE cats (id integer primary key autoincrement, blog_id text, wp_id integer, category_name text not null, parent_id integer default 0); | ||
INSERT INTO cats VALUES(1,'1',1,'test category', 0); | ||
CREATE TABLE quickpress_shortcuts (id integer primary key autoincrement, accountId text, name text); | ||
CREATE TABLE media (id integer primary key autoincrement, postID integer not null, filePath text default '', fileName text default '', title text default '', description text default '', caption text default '', horizontalAlignment integer default 0, width integer default 0, height integer default 0, mimeType text default '', featured boolean default false, isVideo boolean default false, isFeaturedInPost boolean default false); | ||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# To enable ProGuard in your project, edit project.properties | ||
# to define the proguard.config property as described in that file. | ||
# | ||
# Add project specific ProGuard rules here. | ||
# By default, the flags in this file are appended to flags specified | ||
# in ${sdk.dir}/tools/proguard/proguard-android.txt | ||
# You can edit the include path and order by changing the ProGuard | ||
# include property in project.properties. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# Add any project specific keep options here: | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# WordPress Android - Test Project # | ||
|
||
## Run tests ## | ||
|
||
$ ant debug && ant installd && ant test | ||
|
||
## Dump a test database ## | ||
|
||
$ adb shell su -c "echo .dump | sqlite3 /data/data/org.wordpress.android/databases/wordpress" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.wordpress.android; | ||
|
||
import android.content.Context; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.util.Log; | ||
import org.wordpress.android.WordPress; | ||
import org.wordpress.android.WordPressDB; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.lang.reflect.Field; | ||
|
||
import static junit.framework.Assert.*; | ||
|
||
public class TestUtils { | ||
private static String DATABASE_NAME = "wordpress"; | ||
|
||
public static SQLiteDatabase loadDBFromDump(Context targetContext, Context testContext, String filename) { | ||
WordPress.wpDB = new WordPressDB(targetContext); | ||
|
||
Field dbField; | ||
try { | ||
dbField = WordPressDB.class.getDeclaredField("db"); | ||
dbField.setAccessible(true); | ||
SQLiteDatabase db = (SQLiteDatabase) dbField.get(WordPress.wpDB); | ||
assertNotNull(db); | ||
|
||
// delete and recreate DB | ||
targetContext.deleteDatabase(DATABASE_NAME); | ||
targetContext.openOrCreateDatabase(DATABASE_NAME, 0, null); | ||
|
||
// Load file | ||
InputStream is = testContext.getAssets().open(filename); | ||
InputStreamReader inputStreamReader = new InputStreamReader(is); | ||
BufferedReader f = new BufferedReader(inputStreamReader); | ||
for (String line = f.readLine(); line != null; line = f.readLine()) { | ||
try { | ||
db.execSQL(line); | ||
} catch (android.database.sqlite.SQLiteException e ) { | ||
// ignore import errors | ||
} | ||
} | ||
f.close(); | ||
return db; | ||
} catch (NoSuchFieldException e) { | ||
assertTrue(e.toString(), false); | ||
} catch (IllegalAccessException e) { | ||
assertTrue(e.toString(), false); | ||
} catch (IOException e) { | ||
assertTrue(e.toString(), false); | ||
} | ||
return null; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
tests/src/org/wordpress/android/models/CategoryNodeInstrumentationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.wordpress.android.models; | ||
|
||
import android.content.Context; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.test.InstrumentationTestCase; | ||
import android.test.RenamingDelegatingContext; | ||
import android.util.Log; | ||
import org.wordpress.android.TestUtils; | ||
import org.wordpress.android.WordPress; | ||
import org.wordpress.android.WordPressDB; | ||
import org.wordpress.android.models.CategoryNode; | ||
|
||
public class CategoryNodeInstrumentationTest extends InstrumentationTestCase { | ||
protected Context testContext; | ||
protected Context targetContext; | ||
|
||
@Override | ||
protected void setUp() { | ||
// Run tests in an isolated context | ||
targetContext = new RenamingDelegatingContext(getInstrumentation().getTargetContext(), "test_"); | ||
testContext = getInstrumentation().getContext(); | ||
} | ||
|
||
public void testLoadDB() { | ||
SQLiteDatabase db = TestUtils.loadDBFromDump(targetContext, testContext, | ||
"one_category.sql"); | ||
CategoryNode node = CategoryNode.createCategoryTreeFromDB(1); | ||
// At least 1 category exists in test db: malformed_category_parent_id.sql | ||
assertTrue((node.getChildren().size() != 0)); | ||
db.close(); | ||
} | ||
|
||
public void testLoadDB_MalformedCategoryParentId() { | ||
SQLiteDatabase db = TestUtils.loadDBFromDump(targetContext, testContext, | ||
"malformed_category_parent_id.sql"); | ||
|
||
// This line failed before #36 was solved | ||
CategoryNode node = CategoryNode.createCategoryTreeFromDB(1); | ||
db.close(); | ||
} | ||
|
||
public void tearDown() throws Exception { | ||
super.tearDown(); | ||
} | ||
} |