[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a unittest sub-project #38

Merged
merged 3 commits into from
Aug 9, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create a unittest sub-project.
Add a testcase for malformed category parent id (test #36).
  • Loading branch information
maxme committed Aug 9, 2013
commit d26cbb2fe6ab2ab8c063f690df80c216ed9c1511
13 changes: 9 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ If you're just looking to install WordPress for Android, you can find it on

## Helpful Resources ##

Developer Blog: <http://dev.android.wordpress.org>
Source Code: <http://android.svn.wordpress.org>
Issue Tracker and Code Browser: <http://android.trac.wordpress.org/>
WordPress Mobile Developer Handbook: <http://make.wordpress.org/mobile/handbook/>
Developer Blog: <http://dev.android.wordpress.org>
Source Code: <http://android.svn.wordpress.org>
Issue Tracker and Code Browser: <http://android.trac.wordpress.org/>
WordPress Mobile Developer Handbook: <http://make.wordpress.org/mobile/handbook/>



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

[command-line]: http://developer.android.com/tools/building/building-cmdline.html

## Run Unittests ##

cd tests
ant debug && ant installd && ant test
5 changes: 4 additions & 1 deletion src/org/wordpress/android/models/CategoryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ public int getLevel() {
}

public static CategoryNode createCategoryTreeFromDB(int blogId) {
CategoryNode rootCategory = new CategoryNode(-1, -1, "");
if (WordPress.wpDB == null) {
return rootCategory;
}
List<String> stringCategories = WordPress.wpDB.loadCategories(blogId);

// First pass instantiate CategoryNode objects
SparseArray<CategoryNode> categoryMap = new SparseArray<CategoryNode>();
CategoryNode rootCategory = new CategoryNode(-1, -1, "");
CategoryNode currentRootNode;
for (String name : stringCategories) {
int categoryId = WordPress.wpDB.getCategoryId(blogId, name);
Expand Down
4 changes: 4 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/gen/
/bin/
build.xml
local.properties
21 changes: 21 additions & 0 deletions tests/AndroidManifest.xml
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>
18 changes: 18 additions & 0 deletions tests/ant.properties
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=../
13 changes: 13 additions & 0 deletions tests/assets/malformed_category_parent_id.sql
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;
13 changes: 13 additions & 0 deletions tests/assets/one_category.sql
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;
20 changes: 20 additions & 0 deletions tests/proguard-project.txt
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 *;
#}
9 changes: 9 additions & 0 deletions tests/readme.md
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"
56 changes: 56 additions & 0 deletions tests/src/org/wordpress/android/TestUtils.java
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;
}
}
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();
}
}