Search in sources :

Example 6 with Manifest

use of com.chimbori.hermitcrab.schema.manifest.Manifest in project lite-apps by chimbori.

the class LiteAppsValidator method testManifestIsValid.

@Test
public void testManifestIsValid() throws UnsupportedEncodingException {
    File manifestFile = new File(liteApp, FilePaths.MANIFEST_JSON_FILE_NAME);
    Manifest manifest = readManifest(manifestFile);
    String tag = liteApp.getName();
    assertNotNull(manifest);
    assertFieldExists(tag, "name", manifest.name);
    assertFieldExists(tag, "start_url", manifest.startUrl);
    assertFieldExists(tag, "lang", manifest.lang);
    assertFieldExists(tag, "manifest_url", manifest.manifestUrl);
    assertFieldExists(tag, "theme_color", manifest.themeColor);
    assertFieldExists(tag, "secondary_color", manifest.secondaryColor);
    assertFieldExists(tag, "manifest_version", manifest.manifestVersion);
    assertFieldExists(tag, "icon", manifest.icon);
    assertNotEquals(String.format("priority not defined for %s", liteApp), 0, manifest.priority.longValue());
    // Test that the "manifest_url" field contains a valid URL.
    try {
        URL manifestUrl = new URL(manifest.manifestUrl);
        assertEquals("https", manifestUrl.getProtocol());
        assertEquals("hermit.chimbori.com", manifestUrl.getHost());
        assertTrue(manifestUrl.getPath().startsWith("/lite-apps/"));
        assertTrue(manifestUrl.getPath().endsWith(".hermit"));
        assertEquals(liteApp.getName() + ".hermit", new File(URLDecoder.decode(manifestUrl.getFile(), "UTF-8")).getName());
    } catch (MalformedURLException e) {
        fail(e.getMessage());
    }
    // Test that colors are valid hex colors.
    assertTrue(String.format("[%s] theme_color should be a valid hex color", tag), HEX_COLOR_PATTERN.matcher(manifest.themeColor).matches());
    assertTrue(String.format("[%s] secondary_color should be a valid hex color", tag), HEX_COLOR_PATTERN.matcher(manifest.secondaryColor).matches());
    // Test that the name of the icon file is "icon.png" & that the file exists.
    // Although any filename should work, having it be consistent in the library can let us
    // avoid a filename lookup in automated tests and refactors.
    assertEquals(IconFile.FAVICON_FILE, manifest.icon);
    File iconsDirectory = new File(liteApp, FilePaths.ICONS_DIR_NAME);
    assertTrue(new File(iconsDirectory, IconFile.FAVICON_FILE.fileName).exists());
    // Test Endpoints for basic parseability.
    validateEndpoints(tag, manifest.hermitBookmarks, EndpointRole.BOOKMARK);
    validateEndpoints(tag, manifest.hermitFeeds, EndpointRole.FEED);
    validateEndpoints(tag, manifest.hermitShare, EndpointRole.SHARE);
    validateEndpoints(tag, manifest.hermitSearch, EndpointRole.SEARCH);
    validateEndpoints(tag, manifest.hermitMonitors, EndpointRole.MONITOR);
    // Test all Settings to see whether they belong to our whitelisted set of allowable strings.
    validateSettings(tag, manifestFile);
    // Test "related_apps" for basic sanity, that if one exists, then it’s pointing to a Play Store app.
    if (manifest.relatedApplications != null) {
        for (RelatedApp relatedApplication : manifest.relatedApplications) {
            assertEquals(GOOGLE_PLAY, relatedApplication.platform);
            assertFalse(relatedApplication.id.isEmpty());
            assertTrue(relatedApplication.url.startsWith("https://play.google.com/store/apps/details?id="));
            assertTrue(relatedApplication.url.endsWith(relatedApplication.id));
        }
    }
    // Test that if any localization files are present, then they are well-formed.
    File localesDirectory = new File(liteApp, FilePaths.LOCALES_DIR_NAME);
    if (localesDirectory.exists()) {
        File[] localizations = localesDirectory.listFiles(File::isDirectory);
        if (localizations != null) {
            for (File localization : localizations) {
                File messagesFile = new File(localization, FilePaths.MESSAGES_JSON_FILE_NAME);
                // With no specific field checks, we at least validate that the file is well-formed JSON.
                try {
                    TestHelpers.assertJsonIsWellFormedAndReformat(messagesFile);
                } catch (IOException e) {
                    fail(e.getMessage());
                }
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) RelatedApp(com.chimbori.hermitcrab.schema.manifest.RelatedApp) IOException(java.io.IOException) Manifest(com.chimbori.hermitcrab.schema.manifest.Manifest) File(java.io.File) IconFile(com.chimbori.hermitcrab.schema.manifest.IconFile) URL(java.net.URL) TestHelpers.assertIsURL(com.chimbori.liteapps.TestHelpers.assertIsURL) Test(org.junit.Test)

Aggregations

Manifest (com.chimbori.hermitcrab.schema.manifest.Manifest)6 File (java.io.File)5 IconFile (com.chimbori.hermitcrab.schema.manifest.IconFile)4 FileReader (java.io.FileReader)4 Gson (com.google.gson.Gson)3 ColorExtractor (com.chimbori.common.ColorExtractor)2 LibraryTagsList (com.chimbori.hermitcrab.schema.library.LibraryTagsList)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 Library (com.chimbori.hermitcrab.schema.library.Library)1 LibraryApp (com.chimbori.hermitcrab.schema.library.LibraryApp)1 LibraryTag (com.chimbori.hermitcrab.schema.library.LibraryTag)1 RelatedApp (com.chimbori.hermitcrab.schema.manifest.RelatedApp)1 TestHelpers.assertIsURL (com.chimbori.liteapps.TestHelpers.assertIsURL)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1