Skip to content

Commit 02c6810

Browse files
committed
Merge pull request #38 from maxme/unittests
Create a unittest sub-project
2 parents 93895a8 + 9d99934 commit 02c6810

File tree

11 files changed

+212
-5
lines changed

11 files changed

+212
-5
lines changed

readme.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ If you're just looking to install WordPress for Android, you can find it on
88

99
## Helpful Resources ##
1010

11-
Developer Blog: <http://dev.android.wordpress.org>
12-
Source Code: <http://android.svn.wordpress.org>
13-
Issue Tracker and Code Browser: <http://android.trac.wordpress.org/>
14-
WordPress Mobile Developer Handbook: <http://make.wordpress.org/mobile/handbook/>
11+
Developer Blog: <http://dev.android.wordpress.org>
12+
Source Code: <http://android.svn.wordpress.org>
13+
Issue Tracker and Code Browser: <http://android.trac.wordpress.org/>
14+
WordPress Mobile Developer Handbook: <http://make.wordpress.org/mobile/handbook/>
1515

1616

1717

@@ -46,3 +46,8 @@ Also see the full Android documentation, [Building and Running from the Command
4646
Line][command-line].
4747

4848
[command-line]: http://developer.android.com/tools/building/building-cmdline.html
49+
50+
## Run Unittests ##
51+
52+
cd tests
53+
ant debug && ant installd && ant test

src/org/wordpress/android/models/CategoryNode.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ public int getLevel() {
6060
}
6161

6262
public static CategoryNode createCategoryTreeFromDB(int blogId) {
63+
CategoryNode rootCategory = new CategoryNode(-1, -1, "");
64+
if (WordPress.wpDB == null) {
65+
return rootCategory;
66+
}
6367
List<String> stringCategories = WordPress.wpDB.loadCategories(blogId);
6468

6569
// First pass instantiate CategoryNode objects
6670
SparseArray<CategoryNode> categoryMap = new SparseArray<CategoryNode>();
67-
CategoryNode rootCategory = new CategoryNode(-1, -1, "");
6871
CategoryNode currentRootNode;
6972
for (String name : stringCategories) {
7073
int categoryId = WordPress.wpDB.getCategoryId(blogId, name);

tests/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/gen/
2+
/bin/
3+
build.xml
4+
local.properties

tests/AndroidManifest.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- package name must be unique so suffix with "tests" so package loader doesn't ignore us -->
3+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
4+
package="org.wordpress.android.tests"
5+
android:versionCode="1"
6+
android:versionName="1.0">
7+
<!-- We add an application tag here just so that we can indicate that
8+
this package needs to link against the android.test library,
9+
which is needed when building test cases. -->
10+
<application>
11+
<uses-library android:name="android.test.runner" />
12+
</application>
13+
<!--
14+
This declares that this application uses the instrumentation test runner targeting
15+
the package of org.wordpress.android. To run the tests use the command:
16+
"adb shell am instrument -w org.wordpress.android.tests/android.test.InstrumentationTestRunner"
17+
-->
18+
<instrumentation android:name="android.test.InstrumentationTestRunner"
19+
android:targetPackage="org.wordpress.android"
20+
android:label="Tests for org.wordpress.android"/>
21+
</manifest>

tests/ant.properties

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This file is used to override default values used by the Ant build system.
2+
#
3+
# This file must be checked into Version Control Systems, as it is
4+
# integral to the build system of your project.
5+
6+
# This file is only used by the Ant script.
7+
8+
# You can use this to override default values such as
9+
# 'source.dir' for the location of your java source folder and
10+
# 'out.dir' for the location of your output folder.
11+
12+
# You can also use it define how the release builds are signed by declaring
13+
# the following properties:
14+
# 'key.store' for the location of your keystore and
15+
# 'key.alias' for the name of the key to use.
16+
# The password will be asked during the build when you use the 'release' target.
17+
18+
tested.project.dir=../
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
PRAGMA foreign_keys=OFF;
2+
BEGIN TRANSACTION;
3+
CREATE TABLE android_metadata (locale TEXT);
4+
INSERT INTO "android_metadata" VALUES('en_US');
5+
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 '');
6+
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);
7+
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);
8+
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);
9+
CREATE TABLE cats (id integer primary key autoincrement, blog_id text, wp_id integer, category_name text not null, parent_id integer default 0);
10+
INSERT INTO cats VALUES(73,'1',78,'Test category - malformed parent', 999);
11+
CREATE TABLE quickpress_shortcuts (id integer primary key autoincrement, accountId text, name text);
12+
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);
13+
COMMIT;

tests/assets/one_category.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
PRAGMA foreign_keys=OFF;
2+
BEGIN TRANSACTION;
3+
CREATE TABLE android_metadata (locale TEXT);
4+
INSERT INTO "android_metadata" VALUES('en_US');
5+
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 '');
6+
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);
7+
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);
8+
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);
9+
CREATE TABLE cats (id integer primary key autoincrement, blog_id text, wp_id integer, category_name text not null, parent_id integer default 0);
10+
INSERT INTO cats VALUES(1,'1',1,'test category', 0);
11+
CREATE TABLE quickpress_shortcuts (id integer primary key autoincrement, accountId text, name text);
12+
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);
13+
COMMIT;

tests/proguard-project.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# To enable ProGuard in your project, edit project.properties
2+
# to define the proguard.config property as described in that file.
3+
#
4+
# Add project specific ProGuard rules here.
5+
# By default, the flags in this file are appended to flags specified
6+
# in ${sdk.dir}/tools/proguard/proguard-android.txt
7+
# You can edit the include path and order by changing the ProGuard
8+
# include property in project.properties.
9+
#
10+
# For more details, see
11+
# http://developer.android.com/guide/developing/tools/proguard.html
12+
13+
# Add any project specific keep options here:
14+
15+
# If your project uses WebView with JS, uncomment the following
16+
# and specify the fully qualified class name to the JavaScript interface
17+
# class:
18+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19+
# public *;
20+
#}

tests/readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# WordPress Android - Test Project #
2+
3+
## Run tests ##
4+
5+
$ ant debug && ant installd && ant test
6+
7+
## Dump a test database ##
8+
9+
$ adb shell su -c "echo .dump | sqlite3 /data/data/org.wordpress.android/databases/wordpress"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.wordpress.android;
2+
3+
import android.content.Context;
4+
import android.database.sqlite.SQLiteDatabase;
5+
import android.util.Log;
6+
import org.wordpress.android.WordPress;
7+
import org.wordpress.android.WordPressDB;
8+
9+
import java.io.BufferedReader;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.io.InputStreamReader;
13+
import java.lang.reflect.Field;
14+
15+
import static junit.framework.Assert.*;
16+
17+
public class TestUtils {
18+
private static String DATABASE_NAME = "wordpress";
19+
20+
public static SQLiteDatabase loadDBFromDump(Context targetContext, Context testContext, String filename) {
21+
WordPress.wpDB = new WordPressDB(targetContext);
22+
23+
Field dbField;
24+
try {
25+
dbField = WordPressDB.class.getDeclaredField("db");
26+
dbField.setAccessible(true);
27+
SQLiteDatabase db = (SQLiteDatabase) dbField.get(WordPress.wpDB);
28+
assertNotNull(db);
29+
30+
// delete and recreate DB
31+
targetContext.deleteDatabase(DATABASE_NAME);
32+
targetContext.openOrCreateDatabase(DATABASE_NAME, 0, null);
33+
34+
// Load file
35+
InputStream is = testContext.getAssets().open(filename);
36+
InputStreamReader inputStreamReader = new InputStreamReader(is);
37+
BufferedReader f = new BufferedReader(inputStreamReader);
38+
for (String line = f.readLine(); line != null; line = f.readLine()) {
39+
try {
40+
db.execSQL(line);
41+
} catch (android.database.sqlite.SQLiteException e ) {
42+
// ignore import errors
43+
}
44+
}
45+
f.close();
46+
return db;
47+
} catch (NoSuchFieldException e) {
48+
assertTrue(e.toString(), false);
49+
} catch (IllegalAccessException e) {
50+
assertTrue(e.toString(), false);
51+
} catch (IOException e) {
52+
assertTrue(e.toString(), false);
53+
}
54+
return null;
55+
}
56+
}

0 commit comments

Comments
 (0)