jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/361222 )

Change subject: Tests for Retrofit GalleryItemClient
......................................................................


Tests for Retrofit GalleryItemClient

Bug: T152404
Change-Id: I8a177aa34aeaa0b83f42a6818c8029923ad8cd34
---
A app/src/test/java/org/wikipedia/gallery/GalleryItemClientTest.java
A app/src/test/res/raw/gallery_item_image.json
A app/src/test/res/raw/gallery_item_video.json
3 files changed, 448 insertions(+), 0 deletions(-)

Approvals:
  jenkins-bot: Verified
  Mholloway: Looks good to me, approved



diff --git a/app/src/test/java/org/wikipedia/gallery/GalleryItemClientTest.java 
b/app/src/test/java/org/wikipedia/gallery/GalleryItemClientTest.java
new file mode 100644
index 0000000..73a1e51
--- /dev/null
+++ b/app/src/test/java/org/wikipedia/gallery/GalleryItemClientTest.java
@@ -0,0 +1,120 @@
+package org.wikipedia.gallery;
+
+import android.support.annotation.NonNull;
+
+import com.google.gson.stream.MalformedJsonException;
+
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.wikipedia.dataclient.WikiSite;
+import org.wikipedia.dataclient.mwapi.MwException;
+import org.wikipedia.dataclient.mwapi.MwQueryResponse;
+import org.wikipedia.dataclient.okhttp.HttpStatusException;
+import org.wikipedia.gallery.GalleryItemClient.Callback;
+import org.wikipedia.page.PageTitle;
+import org.wikipedia.test.MockWebServerTest;
+
+import retrofit2.Call;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.isA;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+public class GalleryItemClientTest extends MockWebServerTest {
+
+    private static final WikiSite WIKISITE_EN = WikiSite.forLanguageCode("en");
+    private static final PageTitle PAGE_TITLE_IMAGE = new PageTitle("File", 
"Kozanji_Kyoto_Kyoto11s5s4592", WIKISITE_EN);
+    private static final PageTitle PAGE_TITLE_VIDEO = new PageTitle("File", 
"Wood cleaving - 2016.webm", WIKISITE_EN);
+
+    @NonNull private final GalleryItemClient subject = new GalleryItemClient();
+
+    @Test public void testRequestSuccessForImage() throws Throwable {
+        enqueueFromFile("gallery_item_image.json");
+
+        Callback cb = mock(Callback.class);
+        Call<MwQueryResponse> call = request(cb, false);
+        server().takeRequest();
+        ArgumentCaptor<GalleryItem> captor = 
ArgumentCaptor.forClass(GalleryItem.class);
+
+        //noinspection unchecked
+        verify(cb).success(eq(call), captor.capture());
+        //noinspection unchecked
+        GalleryItem galleryItem = captor.getValue();
+
+        assertThat(galleryItem != null, is(true));
+        assertThat(String.valueOf(galleryItem.getHeight()), is("1489"));
+        assertThat(String.valueOf(galleryItem.getWidth()), is("2125"));
+        assertThat(galleryItem.getThumbUrl(), 
is("https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Kinkaku3402CBcropped.jpg/1280px-Kinkaku3402CBcropped.jpg";));
+        assertThat(galleryItem.getMimeType(), is("image/jpeg"));
+        assertThat(galleryItem.getUrl(), 
is("https://upload.wikimedia.org/wikipedia/commons/c/c9/Kinkaku3402CBcropped.jpg";));
+    }
+
+    @Test @SuppressWarnings("checkstyle:magicnumber")
+    public void testRequestSuccessForVideo() throws Throwable {
+        enqueueFromFile("gallery_item_video.json");
+
+        Callback cb = mock(Callback.class);
+        Call<MwQueryResponse> call = request(cb, true);
+        server().takeRequest();
+        ArgumentCaptor<GalleryItem> captor = 
ArgumentCaptor.forClass(GalleryItem.class);
+
+        //noinspection unchecked
+        verify(cb).success(eq(call), captor.capture());
+        //noinspection unchecked
+        GalleryItem galleryItem = captor.getValue();
+
+        assertThat(galleryItem != null, is(true));
+        assertThat(String.valueOf(galleryItem.getHeight()), is("720"));
+        assertThat(String.valueOf(galleryItem.getWidth()), is("400"));
+        assertThat(galleryItem.getThumbUrl(), 
is("https://upload.wikimedia.org/wikipedia/commons/thumb/e/eb/Wood_cleaving_-_2016.webm/400px--Wood_cleaving_-_2016.webm.jpg";));
+        assertThat(galleryItem.getMimeType(), is("video/webm"));
+        assertThat(galleryItem.getUrl(), 
is("https://upload.wikimedia.org/wikipedia/commons/e/eb/Wood_cleaving_-_2016.webm";));
+        assertThat(galleryItem.getDerivatives().size(), is(11));
+    }
+
+    @Test public void testRequestResponseMalformed() throws Throwable {
+        server().enqueue("'");
+
+        GalleryItemClient.Callback cb = mock(GalleryItemClient.Callback.class);
+        Call<MwQueryResponse> call = request(cb, false);
+        server().takeRequest();
+        assertCallbackFailure(call, cb, MalformedJsonException.class);
+    }
+
+    @Test public void testRequestResponseFailure() throws Throwable {
+        enqueue404();
+
+        GalleryItemClient.Callback cb = mock(GalleryItemClient.Callback.class);
+        Call<MwQueryResponse> call = request(cb, false);
+        server().takeRequest();
+        assertCallbackFailure(call, cb, HttpStatusException.class);
+    }
+
+    @Test public void testRequestResponseApiError() throws Throwable {
+        enqueueFromFile("api_error.json");
+
+        GalleryItemClient.Callback cb = mock(GalleryItemClient.Callback.class);
+        Call<MwQueryResponse> call = request(cb, false);
+
+        server().takeRequest();
+        assertCallbackFailure(call, cb, MwException.class);
+    }
+
+    private void assertCallbackFailure(@NonNull Call<MwQueryResponse> call,
+                                       @NonNull GalleryItemClient.Callback cb,
+                                       @NonNull Class<? extends Throwable> 
throwable) {
+        //noinspection unchecked
+        verify(cb, never()).success(any(Call.class), any(GalleryItem.class));
+        verify(cb).failure(eq(call), isA(throwable));
+    }
+
+    private Call<MwQueryResponse> request(@NonNull Callback cb, boolean 
isVideo) {
+        return subject.request(service(GalleryItemClient.Service.class), 
isVideo ? PAGE_TITLE_VIDEO
+                : PAGE_TITLE_IMAGE, cb, isVideo);
+    }
+}
diff --git a/app/src/test/res/raw/gallery_item_image.json 
b/app/src/test/res/raw/gallery_item_image.json
new file mode 100644
index 0000000..eb4c01e
--- /dev/null
+++ b/app/src/test/res/raw/gallery_item_image.json
@@ -0,0 +1,106 @@
+{
+  "batchcomplete": true,
+  "query": {
+    "pages": [
+      {
+        "imageinfo": [
+          {
+            "descriptionshorturl": 
"https://commons.wikimedia.org/w/index.php?curid=316138";,
+            "descriptionurl": 
"https://commons.wikimedia.org/wiki/File:Kinkaku3402CBcropped.jpg";,
+            "height": 1489,
+            "extmetadata": {
+              "Artist": {
+                "source": "commons-desc-page",
+                "value": "<a href=\"//commons.wikimedia.org/wiki/User:Fg2\" 
title=\"User:Fg2\">Fg2</a>"
+              },
+              "Assessments": {
+                "hidden": "",
+                "source": "commons-categories",
+                "value": "potd"
+              },
+              "AttributionRequired": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "false"
+              },
+              "Categories": {
+                "hidden": "",
+                "source": "commons-categories",
+                "value": "Featured pictures on Wikipedia, Hebrew|Featured 
pictures on Wikipedia, Indonesian|Featured pictures on Wikipedia, 
Spanish|Full-frame views of Kinkakuji|PD-self|PD-user|Self-published work"
+              },
+              "CommonsMetadataExtension": {
+                "hidden": "",
+                "source": "extension",
+                "value": "1.2"
+              },
+              "Copyrighted": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "False"
+              },
+              "Credit": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "<span class=\"int-own-work\" lang=\"en\">Own 
work</span>"
+              },
+              "DateTime": {
+                "hidden": "",
+                "source": "mediawiki-metadata",
+                "value": "2005-09-09 10:36:38"
+              },
+              "DateTimeOriginal": {
+                "source": "commons-desc-page",
+                "value": ""
+              },
+              "ImageDescription": {
+                "source": "commons-desc-page",
+                "value": "The building in this photograph is the Kinkaku, or 
Golden Pavilion, which is the shariden at Rokuonji, the Temple of the Golden 
Pavilion, in <a href=\"//commons.wikimedia.org/wiki/Kyoto\" 
class=\"mw-redirect\" title=\"Kyoto\">Kyoto</a>, <a 
href=\"//commons.wikimedia.org/wiki/Japan\" class=\"mw-redirect\" 
title=\"Japan\">Japan</a>. Same exposure as Image:Kinkaku3402.jpg and 
Image:Kinkaku3402CB.jpg (see gallery). I retouched the upper left corner to 
remove some pine needles."
+              },
+              "License": {
+                "hidden": "",
+                "source": "commons-templates",
+                "value": "pd"
+              },
+              "LicenseShortName": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "Public domain"
+              },
+              "ObjectName": {
+                "hidden": "",
+                "source": "mediawiki-metadata",
+                "value": "Kinkaku3402CBcropped"
+              },
+              "Permission": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "I took the photo and contribute my rights in it to 
the public domain."
+              },
+              "Restrictions": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": ""
+              },
+              "UsageTerms": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "Public domain"
+              }
+            },
+            "mime": "image/jpeg",
+            "url": 
"https://upload.wikimedia.org/wikipedia/commons/c/c9/Kinkaku3402CBcropped.jpg";,
+            "size": 1378812,
+            "thumbheight": 897,
+            "thumburl": 
"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Kinkaku3402CBcropped.jpg/1280px-Kinkaku3402CBcropped.jpg";,
+            "thumbwidth": 1280,
+            "width": 2125
+          }
+        ],
+        "index": 0,
+        "ns": 6,
+        "pageid": 0,
+        "title": "File:Kinkaku3402CBcropped.jpg"
+      }
+    ]
+  }
+}
\ No newline at end of file
diff --git a/app/src/test/res/raw/gallery_item_video.json 
b/app/src/test/res/raw/gallery_item_video.json
new file mode 100644
index 0000000..4171812
--- /dev/null
+++ b/app/src/test/res/raw/gallery_item_video.json
@@ -0,0 +1,222 @@
+{
+  "batchcomplete": false,
+  "continue": {
+    "vistart": "2016-02-26T20:49:32Z",
+    "continue": "||"
+  },
+  "query": {
+    "pages": [
+      {
+        "index": 0,
+        "ns": 6,
+        "pageid": 0,
+        "title": "File:Wood cleaving - 2016.webm",
+        "videoinfo": [
+          {
+            "derivatives": [
+              {
+                "bandwidth": 2446076,
+                "framerate": 1000.0,
+                "height": 720,
+                "shorttitle": "WebM source",
+                "src": 
"https://upload.wikimedia.org/wikipedia/commons/e/eb/Wood_cleaving_-_2016.webm";,
+                "title": "Original WebM file, 400 × 720 (2.45 Mbps)",
+                "type": "video/webm; codecs=\"vp8, vorbis\"",
+                "width": 400
+              },
+              {
+                "bandwidth": 194376,
+                "framerate": 1000.0,
+                "height": 160,
+                "shorttitle": "WebM 160P",
+                "src": 
"https://upload.wikimedia.org/wikipedia/commons/transcoded/e/eb/Wood_cleaving_-_2016.webm/Wood_cleaving_-_2016.webm.160p.webm";,
+                "title": "Low bandwidth WebM (160P)",
+                "type": "video/webm; codecs=\"vp8, vorbis\"",
+                "width": 88
+              },
+              {
+                "bandwidth": 306896,
+                "framerate": 1000.0,
+                "height": 240,
+                "shorttitle": "WebM 240P",
+                "src": 
"https://upload.wikimedia.org/wikipedia/commons/transcoded/e/eb/Wood_cleaving_-_2016.webm/Wood_cleaving_-_2016.webm.240p.webm";,
+                "title": "Small WebM (240P)",
+                "type": "video/webm; codecs=\"vp8, vorbis\"",
+                "width": 134
+              },
+              {
+                "bandwidth": 563216,
+                "framerate": 1000.0,
+                "height": 360,
+                "shorttitle": "WebM 360P",
+                "src": 
"https://upload.wikimedia.org/wikipedia/commons/transcoded/e/eb/Wood_cleaving_-_2016.webm/Wood_cleaving_-_2016.webm.360p.webm";,
+                "title": "WebM (360P)",
+                "type": "video/webm; codecs=\"vp8, vorbis\"",
+                "width": 200
+              },
+              {
+                "bandwidth": 1086976,
+                "framerate": 1000.0,
+                "height": 480,
+                "shorttitle": "WebM 480P",
+                "src": 
"https://upload.wikimedia.org/wikipedia/commons/transcoded/e/eb/Wood_cleaving_-_2016.webm/Wood_cleaving_-_2016.webm.480p.webm";,
+                "title": "SD WebM (480P)",
+                "type": "video/webm; codecs=\"vp8, vorbis\"",
+                "width": 266
+              },
+              {
+                "bandwidth": 2418696,
+                "framerate": 1000.0,
+                "height": 720,
+                "shorttitle": "WebM 720P",
+                "src": 
"https://upload.wikimedia.org/wikipedia/commons/transcoded/e/eb/Wood_cleaving_-_2016.webm/Wood_cleaving_-_2016.webm.720p.webm";,
+                "title": "HD WebM (720P)",
+                "type": "video/webm; codecs=\"vp8, vorbis\"",
+                "width": 400
+              },
+              {
+                "bandwidth": 210976,
+                "framerate": 15.0,
+                "height": 160,
+                "shorttitle": "Ogg 160P",
+                "src": 
"https://upload.wikimedia.org/wikipedia/commons/transcoded/e/eb/Wood_cleaving_-_2016.webm/Wood_cleaving_-_2016.webm.160p.ogv";,
+                "title": "Low bandwidth Ogg video (160P)",
+                "type": "video/ogg; codecs=\"theora, vorbis\"",
+                "width": 88
+              },
+              {
+                "bandwidth": 581296,
+                "framerate": 60.0,
+                "height": 240,
+                "shorttitle": "Ogg 240P",
+                "src": 
"https://upload.wikimedia.org/wikipedia/commons/transcoded/e/eb/Wood_cleaving_-_2016.webm/Wood_cleaving_-_2016.webm.240p.ogv";,
+                "title": "Small Ogg video (240P)",
+                "type": "video/ogg; codecs=\"theora, vorbis\"",
+                "width": 134
+              },
+              {
+                "bandwidth": 1121640,
+                "framerate": 60.0,
+                "height": 360,
+                "shorttitle": "Ogg 360P",
+                "src": 
"https://upload.wikimedia.org/wikipedia/commons/transcoded/e/eb/Wood_cleaving_-_2016.webm/Wood_cleaving_-_2016.webm.360p.ogv";,
+                "title": "Ogg video (360P)",
+                "type": "video/ogg; codecs=\"theora, vorbis\"",
+                "width": 200
+              },
+              {
+                "bandwidth": 2180928,
+                "framerate": 60.0,
+                "height": 480,
+                "shorttitle": "Ogg 480P",
+                "src": 
"https://upload.wikimedia.org/wikipedia/commons/transcoded/e/eb/Wood_cleaving_-_2016.webm/Wood_cleaving_-_2016.webm.480p.ogv";,
+                "title": "SD Ogg video (480P)",
+                "type": "video/ogg; codecs=\"theora, vorbis\"",
+                "width": 266
+              },
+              {
+                "bandwidth": 1977448,
+                "framerate": 60.0,
+                "height": 720,
+                "shorttitle": "Ogg 720P",
+                "src": 
"https://upload.wikimedia.org/wikipedia/commons/transcoded/e/eb/Wood_cleaving_-_2016.webm/Wood_cleaving_-_2016.webm.720p.ogv";,
+                "title": "HD Ogg video (720P)",
+                "type": "video/ogg; codecs=\"theora, vorbis\"",
+                "width": 400
+              }
+            ],
+            "descriptionshorturl": 
"https://commons.wikimedia.org/w/index.php?curid=47192480";,
+            "descriptionurl": 
"https://commons.wikimedia.org/wiki/File:Wood_cleaving_-_2016.webm";,
+            "height": 720,
+            "extmetadata": {
+              "Artist": {
+                "source": "commons-desc-page",
+                "value": "<a href=\"//commons.wikimedia.org/wiki/User:Jarekt\" 
title=\"User:Jarekt\">Jarek Tuszyński</a>"
+              },
+              "Assessments": {
+                "hidden": "",
+                "source": "commons-categories",
+                "value": ""
+              },
+              "AttributionRequired": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "true"
+              },
+              "Categories": {
+                "hidden": "",
+                "source": "commons-categories",
+                "value": "Photographs by Jarek Tuszyński from 
2015|Self-published work|Vertical videos|Videos of 2015 from the United 
States|Wood cleaving"
+              },
+              "CommonsMetadataExtension": {
+                "hidden": "",
+                "source": "extension",
+                "value": "1.2"
+              },
+              "Copyrighted": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "True"
+              },
+              "Credit": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "<span class=\"int-own-work\" lang=\"en\">Own 
work</span>"
+              },
+              "DateTime": {
+                "hidden": "",
+                "source": "mediawiki-metadata",
+                "value": "2016-02-27 09:36:40"
+              },
+              "DateTimeOriginal": {
+                "source": "commons-desc-page",
+                "value": "2015-08-27"
+              },
+              "ImageDescription": {
+                "source": "commons-desc-page",
+                "value": "Wood cleaving while camping in Adirondacks"
+              },
+              "License": {
+                "hidden": "",
+                "source": "commons-templates",
+                "value": "cc-by-sa-4.0"
+              },
+              "LicenseShortName": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "CC BY-SA 4.0"
+              },
+              "LicenseUrl": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "http://creativecommons.org/licenses/by-sa/4.0";
+              },
+              "ObjectName": {
+                "hidden": "",
+                "source": "mediawiki-metadata",
+                "value": "Wood cleaving - 2016"
+              },
+              "Restrictions": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": ""
+              },
+              "UsageTerms": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "Creative Commons Attribution-Share Alike 4.0"
+              }
+            },
+            "mime": "video/webm",
+            "url": 
"https://upload.wikimedia.org/wikipedia/commons/e/eb/Wood_cleaving_-_2016.webm";,
+            "size": 2490105,
+            "thumbheight": 720,
+            "thumburl": 
"https://upload.wikimedia.org/wikipedia/commons/thumb/e/eb/Wood_cleaving_-_2016.webm/400px--Wood_cleaving_-_2016.webm.jpg";,
+            "thumbwidth": 400,
+            "width": 400
+          }
+        ]
+      }
+    ]
+  }
+}
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/361222
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I8a177aa34aeaa0b83f42a6818c8029923ad8cd34
Gerrit-PatchSet: 4
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Yashasvi <yash.gird...@gmail.com>
Gerrit-Reviewer: Brion VIBBER <br...@wikimedia.org>
Gerrit-Reviewer: Dbrant <dbr...@wikimedia.org>
Gerrit-Reviewer: Mholloway <mhollo...@wikimedia.org>
Gerrit-Reviewer: Niedzielski <sniedziel...@wikimedia.org>
Gerrit-Reviewer: Yashasvi <yash.gird...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to