reader/android: add Robolectric and the first unit test for the initial page assignment.

Change-Id: I368d327576a5ab25770dbd3f98302fa5285631cb
diff --git a/android/app/build.gradle b/android/app/build.gradle
index 5e476c3..4612664 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -95,8 +95,10 @@
     compile 'io.v:vanadium-android:1.0'
     compile 'io.v:baku-toolkit:0.3.0'
 
+    testCompile 'org.robolectric:robolectric:3.0'
+
     // Required by baku-toolkit.
-    apk ('org.slf4j:slf4j-android:1.7.13')
+    compile 'org.slf4j:slf4j-android:1.7.13'
 }
 
 vdl {
diff --git a/android/app/src/main/java/io/v/android/apps/reader/PdfViewerActivity.java b/android/app/src/main/java/io/v/android/apps/reader/PdfViewerActivity.java
index ad48a0d..eb3663f 100644
--- a/android/app/src/main/java/io/v/android/apps/reader/PdfViewerActivity.java
+++ b/android/app/src/main/java/io/v/android/apps/reader/PdfViewerActivity.java
@@ -302,16 +302,10 @@
 
         Log.i(TAG, "Joining device set: " + ds.getId());
 
-        // Get the last page number of the first consecutive set of pages in the device set.
-        int lastPage = StreamSupport.stream(ds.getDevices().values())
-                .map(DeviceMeta::getPage)
-                .sorted()
-                .reduce((x, y) -> (y - x) > 1 ? x : y)
-                .orElse(0);
+        int initialPage = determineInitialPage(ds);
 
         // Create a new device meta, and update the device set with it.
-        // Set the initial page as lastPage + 1.
-        DeviceMeta dm = createDeviceMeta(lastPage + 1);
+        DeviceMeta dm = createDeviceMeta(initialPage);
 
         // Load the pdf file.
         try {
@@ -329,6 +323,18 @@
         mCurrentDS = ds;
     }
 
+    static int determineInitialPage(final DeviceSet ds) {
+        // Get the last page number of the first consecutive set of pages in the device set.
+        int lastPage = StreamSupport.stream(ds.getDevices().values())
+                .map(DeviceMeta::getPage)
+                .sorted()
+                .reduce((x, y) -> (y - x) > 1 ? x : y)
+                .orElse(0);
+
+        // Set the initial page as lastPage + 1.
+        return lastPage + 1;
+    }
+
     private void leaveDeviceSet() {
         if (mCurrentDS == null) {
             return;
diff --git a/android/app/src/test/java/io/v/android/apps/reader/PdfViewerActivityTest.java b/android/app/src/test/java/io/v/android/apps/reader/PdfViewerActivityTest.java
new file mode 100644
index 0000000..dec8686
--- /dev/null
+++ b/android/app/src/test/java/io/v/android/apps/reader/PdfViewerActivityTest.java
@@ -0,0 +1,59 @@
+// Copyright 2015 The Vanadium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package io.v.android.apps.reader;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricGradleTestRunner;
+import org.robolectric.annotation.Config;
+
+import java.util.HashMap;
+
+import io.v.android.apps.reader.model.IdFactory;
+import io.v.android.apps.reader.vdl.DeviceMeta;
+import io.v.android.apps.reader.vdl.DeviceSet;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(RobolectricGradleTestRunner.class)
+@Config(constants = BuildConfig.class, sdk=21)
+public class PdfViewerActivityTest {
+
+    @Test
+    public void testDetermineInitialPage() {
+        int actual;
+
+        actual = PdfViewerActivity.determineInitialPage(createDeviceSetWithPages());
+        assertEquals(1, actual);
+
+        actual = PdfViewerActivity.determineInitialPage(createDeviceSetWithPages(1));
+        assertEquals(2, actual);
+
+        actual = PdfViewerActivity.determineInitialPage(createDeviceSetWithPages(1, 2));
+        assertEquals(3, actual);
+
+        actual = PdfViewerActivity.determineInitialPage(createDeviceSetWithPages(2, 1));
+        assertEquals(3, actual);
+
+        actual = PdfViewerActivity.determineInitialPage(createDeviceSetWithPages(3, 4));
+        assertEquals(5, actual);
+
+        actual = PdfViewerActivity.determineInitialPage(createDeviceSetWithPages(5, 2, 3));
+        assertEquals(4, actual);
+
+    }
+
+    private DeviceSet createDeviceSetWithPages(int... pages) {
+        DeviceSet ds = new DeviceSet(null, null, new HashMap<>());
+
+        for (int page : pages) {
+            DeviceMeta dm = new DeviceMeta(IdFactory.getRandomId(), page, 0, true);
+            ds.getDevices().put(dm.getDeviceId(), dm);
+        }
+
+        return ds;
+    }
+
+}