Skip to content

Commit

Permalink
First tests for PositronicContentProvider --- getting content types r…
Browse files Browse the repository at this point in the history
…ight.
  • Loading branch information
rst committed Jun 1, 2012
1 parent a909619 commit ecaabce
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/main/scala/content/ContentExporter.scala
Expand Up @@ -69,8 +69,7 @@ abstract class PositronicContentProvider
res
}

def delete( uri: Uri, vals: ContentValues,
where: String, whereArgs: Array[String] ) =
def delete( uri: Uri, where: String, whereArgs: Array[String] ) =
{
val res: Int = getRepository( uri, where, whereArgs ).delete
notifyChange( uri )
Expand Down
102 changes: 102 additions & 0 deletions src/test/scala/ContentExporterSpec.scala
@@ -0,0 +1,102 @@
package org.positronicnet.test

import org.positronicnet.content._

import android.net.Uri

import org.scalatest._
import org.scalatest.matchers.ShouldMatchers
import com.xtremelabs.robolectric.Robolectric

// I guess in "real life" you'd want to make this a shim Java class
// with statics, for the sake of other apps on the stock technology
// stack.
//
// Definitions here still distressingly verbose.
//
// (BTW, when vals here are declared "lazy" for no obvious reason,
// it's usually because the code that supplies the value would hit
// a "Stub!" exception unless loaded through the Robolectric class
// loader.)

object TodoProvider {

val TODO_PREFIX = "content://org.positronicnet.test/"
val TODO_LISTS_PREFIX = TODO_PREFIX + "lists"
lazy val TODO_LISTS_URI = Uri.parse( TODO_LISTS_PREFIX )

def todoListUri( id: Long ) =
Uri.withAppendedPath( TODO_LISTS_URI, id.toString )

lazy val TODO_LIST_ITEMS_PATTERN = TODO_LISTS_PREFIX + "/=/items"

def todoListItemsUri( listId: Long ) =
Uri.withAppendedPath( TODO_LISTS_URI, listId.toString + "/items" )

val TODO_LIST_ITEM_PATTERN = TODO_LISTS_PREFIX + "/=/items/="

def todoListItemUri( listId: Long, itemId: Long ) =
Uri.withAppendedPath( TODO_LISTS_URI, listId.toString +"/items/"+ itemId )

val TODO_LIST_TYPE = "vnd.org.positronicnet.todolist"
val TODO_ITEM_TYPE = "vnd.org.positronicnet.todoitem"

// Might want these somewhere where they'd be more generally useful

def dirContentType(s: String) = "vnd.android.cursor.dir/"+s
def rowContentType(s: String) = "vnd.android.cursor.item/"+s
}

class TodoProvider extends PositronicContentProvider
{
import TodoProvider._ // make 'static' stuff visible w/o prefix

def onCreate = { TodoDb.openInContext( getContext ); true }

matchUriObj( TODO_LISTS_URI, dirContentType( TODO_LIST_TYPE )){
noArgs => TodoDb("todo_lists")
}
matchUriStr( TODO_LISTS_PREFIX+"/=", rowContentType( TODO_LIST_TYPE )){
seq => TodoDb("todo_lists").whereEq( "_id" -> seq(0) )
}
matchUriStr( TODO_LISTS_PREFIX+"/=/items", dirContentType( TODO_ITEM_TYPE )) {
seq => TodoDb("todo_items").whereEq( "todo_list_id" -> seq(0) )
}
matchUriStr(TODO_LISTS_PREFIX+"/=/items/=", rowContentType( TODO_ITEM_TYPE )){
seq => TodoDb("todo_items").whereEq("todo_list_id"->seq(0), "_id"->seq(1))
}
}

class ContentExporterSpec
extends Spec
with ShouldMatchers
with DbTestFixtures
{
override def afterEach = TodoDb.close // each test opens it...
def makeTodosProvider = {
val it = new TodoProvider
it.onCreate
it
}

import TodoProvider._

implicit def string2uri( s: String ) = Uri.parse( s ) // gaaaaaah!!!!

describe( "content types" ) {

it ("should yield correct values for single table") {
val todos = makeTodosProvider
todos.getType( TODO_LISTS_URI ) should be( dirContentType(TODO_LIST_TYPE))
todos.getType( todoListUri(3) ) should be( rowContentType(TODO_LIST_TYPE))
}

it ("should yield correct values for nested table") {
val todos = makeTodosProvider
todos.getType( todoListItemsUri( 3 ) ) should be(
dirContentType(TODO_ITEM_TYPE))
todos.getType( todoListItemUri( 3, 3 ) ) should be(
rowContentType(TODO_ITEM_TYPE))
}
}
}

0 comments on commit ecaabce

Please sign in to comment.