/*
 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Jean-Marc Orliaguet, Chalmers
 *
 * $Id$
 */

package org.nuxeo.theme.test.bank;

import java.io.IOException;
import java.util.List;

import org.nuxeo.runtime.test.NXRuntimeTestCase;
import org.nuxeo.theme.Manager;
import org.nuxeo.theme.bank.Utils;
import org.nuxeo.theme.themes.ThemeManager;
import org.nuxeo.theme.types.TypeRegistry;

public class TestUtils extends NXRuntimeTestCase {

    private ThemeManager themeManager;

    private TypeRegistry typeRegistry;

    private final String BANK_NAME = "test";

    private final String COLLECTION_NAME = "Test";

    @Override
    public void setUp() throws Exception {
        super.setUp();
        deployContrib("org.nuxeo.theme.core",
                "OSGI-INF/nxthemes-core-service.xml");
        deployContrib("org.nuxeo.theme.core",
                "OSGI-INF/nxthemes-core-contrib.xml");
        deployContrib("org.nuxeo.theme.bank.tests", "theme-bank-config.xml");
        themeManager = Manager.getThemeManager();
        typeRegistry = Manager.getTypeRegistry();
    }

    @Override
    public void tearDown() throws Exception {
        Manager.getTypeRegistry().clear();
        themeManager.clear();
        themeManager = null;
        typeRegistry.clear();
        typeRegistry = null;
        super.tearDown();
    }

    public void testGetCollections() throws IOException {
        final List<String> collections = Utils.getCollections(BANK_NAME);
        assertEquals("Test", collections.get(0));
    }

    public void testGetStyleItemsInCollections() throws IOException {
        final List<String> items = Utils.getItemsInCollection(BANK_NAME,
                COLLECTION_NAME, "style");
        assertEquals("base-skin.css", items.get(0));
        assertEquals("style.css", items.get(1));
        assertEquals("main.css", items.get(2));
        assertEquals("test.css", items.get(3));
        assertEquals("style-with-preview.css", items.get(4));
    }

    public void testGetPresetItemsInCollections() throws IOException {
        final List<String> items = Utils.getItemsInCollection(BANK_NAME,
                COLLECTION_NAME, "preset");
        assertEquals("font", items.get(0));
        assertEquals("color", items.get(1));
        assertEquals("background", items.get(2));
        assertEquals("border", items.get(3));
    }

    public void testGetImageItemsInCollections() throws IOException {
        final List<String> items = Utils.getItemsInCollection(BANK_NAME,
                COLLECTION_NAME, "image");
        assertEquals("emoticon_smile.png", items.get(0));
        assertEquals("photo.png", items.get(1));
    }

    public void testListSkinsInCollection() throws IOException {
        final List<String> skins = Utils.listSkinsInCollection(BANK_NAME,
                COLLECTION_NAME);
        assertEquals("test.css", skins.get(0));
        assertEquals("skin-without-preview.css", skins.get(1));
    }

    // JSON
    public void testListCollections() throws IOException {
        assertEquals("[\"Test\"]", Utils.listCollections(BANK_NAME));
    }

    public void testListBankSkins() throws IOException {
        final String expected = org.nuxeo.theme.Utils.readResourceAsString("skins.json");
        assertEquals(expected, Utils.listBankSkins(BANK_NAME));
    }

    public void testListImages() throws IOException {
        assertEquals("[\"Test/emoticon_smile.png\",\"Test/photo.png\"]",
                Utils.listImages(BANK_NAME));
    }

    public void testListStyles() throws IOException {
        assertEquals(org.nuxeo.theme.Utils.readResourceAsString("styles.json"),
                Utils.listBankStyles(BANK_NAME));
    }

    public void disabledTestGetNavTree() throws IOException {
        final String expected = org.nuxeo.theme.Utils.readResourceAsString("navtree.json");
        assertEquals(expected, Utils.getNavTree());
    }

}

@@ -2028,31 +2028,17 @@
 als(
-%22base-skin.css%22
+5
 , items.
 get(
@@ -2025,37 +2025,37 @@
 Equals(5, items.
-get(0
+size(
 ));%0A        asse
@@ -2060,158 +2060,224 @@
 sert
-Equals(%22style.css%22, items.get(1));%0A        assertEquals(%22main.css%22, items.get(2));%0A        assertEquals(%22test.css%22, items.get(3));%0A        assertEqual
+True(items.contains(%22base-skin.css%22));%0A        assertTrue(items.contains(%22style.css%22));%0A        assertTrue(items.contains(%22main.css%22));%0A        assertTrue(items.contains(%22test.css%22));%0A        assertTrue(items.contain
 s(%22s
@@ -2302,21 +2302,8 @@
 css%22
-, items.get(4
 ));%0A
@@ -2522,22 +2522,17 @@
 als(
-%22font%22
+4
 , items.
 get(
@@ -2519,37 +2519,37 @@
 Equals(4, items.
-get(0
+size(
 ));%0A        asse
@@ -2554,58 +2554,115 @@
 sert
-Equals(%22color%22, items.get(1));%0A        assertEqual
+True(items.contains(%22font%22));%0A        assertTrue(items.contains(%22color%22));%0A        assertTrue(items.contain
 s(%22b
@@ -2675,21 +2675,8 @@
 und%22
-, items.get(2
 ));%0A
@@ -2693,36 +2693,36 @@
 sert
-Equals(%22border%22, items.get(3
+True(items.contains(%22border%22
 ));%0A

test fix (list items are not ordered)