diff options
| author | Danilo M. <danix@danix.xyz> | 2026-10-22 11:09:59 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-10-22 11:09:59 +0200 |
| commit | 324b600f5795735c74eae81a27beb4953f40653e (patch) | |
| tree | 6de17d5bf33d1acfdbb09879c3c9c24e2474421d /app/src/test/java/mygram/droid/LogicTest.java | |
| parent | 3605f4e081283b2f32451c7075fe07648e76e99d (diff) | |
| download | mygram-droid-324b600f5795735c74eae81a27beb4953f40653e.tar.gz mygram-droid-324b600f5795735c74eae81a27beb4953f40653e.zip | |
Add post parsing, URL helpers, health check and double-OK logic with tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'app/src/test/java/mygram/droid/LogicTest.java')
| -rw-r--r-- | app/src/test/java/mygram/droid/LogicTest.java | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/app/src/test/java/mygram/droid/LogicTest.java b/app/src/test/java/mygram/droid/LogicTest.java new file mode 100644 index 0000000..e807725 --- /dev/null +++ b/app/src/test/java/mygram/droid/LogicTest.java @@ -0,0 +1,110 @@ +/* + * mygram-droid: Android TV client for Foldergram. + * Copyright (C) 2026 Danilo M. <danix@danix.xyz> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + + +package mygram.droid; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.json.JSONObject; +import org.junit.Test; + +import java.util.List; + +public class LogicTest { + private static final String BASE = "http://192.0.2.10:4141"; + + @Test + public void parsesSinglePhoto() throws Exception { + Post p = new Post(new JSONObject("{\"id\":7,\"folderSlug\":\"trips\",\"folderName\":\"Trips\"," + + "\"mediaType\":\"image\",\"width\":1080,\"height\":1920,\"durationMs\":null," + + "\"thumbnailUrl\":\"/thumbnails/ab/x.webp?v=1\",\"previewUrl\":\"/previews/ab/x.webp?v=1\"," + + "\"postType\":\"single\",\"itemCount\":1}"), BASE); + assertEquals(7, p.id); + assertEquals("trips", p.folderSlug); + assertEquals(1, p.media.size()); + Post.Media m = p.media.get(0); + assertFalse(m.video); + assertEquals(0, m.durationMs); + assertEquals(1080, m.width); + assertEquals(BASE + "/thumbnails/ab/x.webp?v=1", m.thumbnailUrl); + assertEquals(BASE + "/previews/ab/x.webp?v=1", m.previewUrl); + } + + @Test + public void parsesVideo() throws Exception { + Post p = new Post(new JSONObject("{\"id\":8,\"mediaType\":\"video\",\"durationMs\":41633," + + "\"thumbnailUrl\":\"/thumbnails/cd/y.webp\",\"previewUrl\":\"/previews/cd/y.mp4\"}"), BASE); + Post.Media m = p.media.get(0); + assertTrue(m.video); + assertEquals(41633, m.durationMs); + assertEquals(BASE + "/previews/cd/y.mp4", m.previewUrl); + } + + @Test + public void parsesCarouselSlidesInOrder() throws Exception { + Post p = new Post(new JSONObject("{\"id\":9,\"mediaType\":\"image\",\"postType\":\"carousel\"," + + "\"thumbnailUrl\":\"/thumbnails/a.webp\",\"previewUrl\":\"/previews/a.webp\",\"mediaItems\":[" + + "{\"imageId\":1,\"mediaType\":\"image\",\"thumbnailUrl\":\"/thumbnails/a.webp\",\"previewUrl\":\"/previews/a.webp\"}," + + "{\"imageId\":2,\"mediaType\":\"video\",\"durationMs\":5000,\"thumbnailUrl\":\"/thumbnails/b.webp\",\"previewUrl\":\"/previews/b.mp4\"}]}"), + BASE); + assertEquals(2, p.media.size()); + assertFalse(p.media.get(0).video); + assertTrue(p.media.get(1).video); + assertEquals(BASE + "/previews/b.mp4", p.media.get(1).previewUrl); + } + + @Test + public void parsesPageAndNullUrl() throws Exception { + List<Post> posts = Post.list(new JSONObject("{\"items\":[{\"id\":1,\"mediaType\":\"image\"," + + "\"thumbnailUrl\":null,\"previewUrl\":\"/previews/z.webp\"}],\"hasMore\":false}"), BASE); + assertEquals(1, posts.size()); + assertNull(posts.get(0).media.get(0).thumbnailUrl); + assertEquals(0, Post.list(new JSONObject("{}"), BASE).size()); + } + + @Test + public void normalizesServerUrls() { + assertEquals("http://192.0.2.10:4141", Api.normalize(" 192.0.2.10:4141/ ")); + assertEquals("http://foldergram.example.org:4141", Api.normalize("http://foldergram.example.org:4141//")); + assertEquals("https://foldergram.example.org", Api.normalize("https://foldergram.example.org")); + } + + @Test + public void resolvesMediaPaths() { + assertEquals(BASE + "/icon-192.png", Api.resolve(BASE, "/icon-192.png")); + assertNull(Api.resolve(BASE, null)); + } + + @Test + public void detectsFoldergramHealth() { + assertTrue(Discovery.isFoldergram("{\"ok\":true,\"timestamp\":\"t\",\"storage\":{\"available\":true}}")); + assertFalse(Discovery.isFoldergram("{\"ok\":true}")); + assertFalse(Discovery.isFoldergram("{\"ok\":false,\"storage\":{}}")); + assertFalse(Discovery.isFoldergram("<html>router login</html>")); + } + + @Test + public void tellsDoublePressFromSingle() { + DoubleOk d = new DoubleOk(); + assertFalse(d.press(1000)); + assertTrue(d.press(1250)); + assertFalse("a double press resets", d.press(1400)); + assertFalse(d.press(2000)); + assertFalse("301 ms is too slow", d.press(2301)); + } +} |
