/*
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.dmdirc.config.prefs;

import com.dmdirc.actions.ActionManager;
import com.dmdirc.actions.CoreActionType;
import com.dmdirc.harness.TestPreferencesInterface;
import com.dmdirc.config.IdentityManager;
import com.dmdirc.harness.TestActionListener;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

public class PreferencesManagerTest extends junit.framework.TestCase {
    
    @Before
    public void setUp() throws Exception {        
        IdentityManager.load();
    }    

    @Test
    public void testDefaults() {
        final PreferencesManager pm = new PreferencesManager();
        assertNotNull(pm.getCategory("General"));
        assertNotNull(pm.getCategory("Connection"));
        assertNotNull(pm.getCategory("Messages"));
        assertNotNull(pm.getCategory("Advanced"));
        assertNotNull(pm.getCategory("GUI"));
        assertNotNull(pm.getCategory("Plugins"));
        assertNotNull(pm.getCategory("Updates"));
        assertNotNull(pm.getCategory("URL Handlers"));
    }
    
    @Test
    public void testGetCategory() {
        final PreferencesManager pm = new PreferencesManager();
        assertNull(pm.getCategory("unittest123"));
    }
    
    @Test
    public void testGetCategories() {
        final PreferencesManager pm = new PreferencesManager();
        assertNotNull(pm.getCategories());
        assertFalse(pm.getCategories().isEmpty());
        
        for (PreferencesCategory cat : pm.getCategories()) {
            assertNotNull(pm.getCategory(cat.getTitle()));
        }
    }
    
    @Test
    public void testSaveListener() {        
        final PreferencesManager pm = new PreferencesManager();
        final TestPreferencesInterface tpi = new TestPreferencesInterface();
        
        pm.registerSaveListener(tpi);
        pm.fireSaveListeners();
        assertTrue(tpi.saved);
    }
    
    @Test
    public void testOpenAction() {
        final TestActionListener tal = new TestActionListener();
        ActionManager.init();
        ActionManager.addListener(tal, CoreActionType.CLIENT_PREFS_OPENED);
        
        new PreferencesManager();
        
        assertNotNull(tal.events.get(CoreActionType.CLIENT_PREFS_OPENED));
        assertEquals(1, tal.events.get(CoreActionType.CLIENT_PREFS_OPENED).size());
    }
    
    @Test
    public void testCloseAction() {
        final TestActionListener tal = new TestActionListener();
        ActionManager.init();
        ActionManager.addListener(tal, CoreActionType.CLIENT_PREFS_CLOSED);
        
        new PreferencesManager().close();
        
        assertNotNull(tal.events.get(CoreActionType.CLIENT_PREFS_CLOSED));
        assertEquals(1, tal.events.get(CoreActionType.CLIENT_PREFS_CLOSED).size());
    }

}
@@ -1182,16 +1182,40 @@
 prefs;%0A%0A
+import com.dmdirc.Main;%0A
 import c
@@ -1289,16 +1289,67 @@
 onType;%0A
+import com.dmdirc.addons.ui_swing.SwingController;%0A
 import c
@@ -1503,16 +1503,21 @@
 t.Before
+Class
 ;%0Aimport
@@ -1608,41 +1608,8 @@
 est 
-extends junit.framework.TestCase 
 %7B%0A  
@@ -1622,16 +1622,21 @@
  @Before
+Class
 %0A    pub
@@ -1635,24 +1635,31 @@
 %0A    public 
+static 
 void setUp()
@@ -1677,24 +1677,16 @@
 eption %7B
-        
 %0A       
@@ -1706,24 +1706,67 @@
 ger.load();%0A
+        Main.setUI(new SwingController());%0A
     %7D    %0A%0A 

Fix prefs manager test, maybe