from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils import unittest

from wirecloud.plugins import clear_cache, get_active_features, get_plugins, \
    get_extra_javascripts, get_gadget_api_extensions, WirecloudPlugin

try:
    from djangosanetesting.cases import UnitTestCase
except:
    class UnitTestCase(unittest.TestCase):
        pass


# Avoid nose to repeat these tests (they are run through wirecloud/tests/__init__.py)
__test__ = False


class WirecloudTestPlugin1(WirecloudPlugin):

    features = {
        'feature1': '0.1',
    }

    def get_scripts(view):
        if view == 'index':
            return ('a.js', 'b.js')
        else:
            return ('a.js',)

    def get_gadget_api_extensions(view):
        if view == 'index':
            return ('d.js',)
        else:
            return ()


class WirecloudTestPlugin2(WirecloudPlugin):

    features = {
        'feature2': '0.2',
    }


class WirecloudTestConflictingPlugin(WirecloudPlugin):

    features = {
        'feature1': '0.1',
        'feature2': '0.2',
    }


class WirecloudPluginTestCase(UnitTestCase):

    def setUp(self):
        self.OLD_WIRECLOUD_PLUGINS = getattr(settings, 'WIRECLOUD_PLUGINS', ())
        super(WirecloudPluginTestCase, self).setUp()

    def tearDown(self):
        settings.WIRECLOUD_PLUGINS = self.OLD_WIRECLOUD_PLUGINS
        super(WirecloudPluginTestCase, self).tearDown()

    def test_basic_conf(self):
        settings.WIRECLOUD_PLUGINS = (
            'WirecloudTestPlugin1',
            'WirecloudTestPlugin2',
        )
        clear_cache()

        self.assertEqual(len(get_plugins()), 2)
        self.assertEqual(len(get_active_features()), 3)
        self.assertEqual(len(get_extra_javascripts('index')), 2)
        self.assertEqual(len(get_extra_javascripts('iphone')), 1)
        self.assertEqual(len(get_gadget_api_extensions('index')), 2)
        self.assertEqual(len(get_gadget_api_extensions('iphone')), 1)

    def test_several_plugins_with_the_same_feature(self):
        settings.WIRECLOUD_PLUGINS = (
            'WirecloudTestPlugin1',
            'WirecloudTestConflictingPlugin',
        )
        clear_cache()

        self.assertRaises(ImproperlyConfigured, get_plugins)

@@ -613,16 +613,22 @@
 scripts(
+self, 
 view):%0A 
@@ -768,16 +768,22 @@
 ensions(
+self, 
 view):%0A 
@@ -1478,32 +1478,33 @@
 sic_conf(self):%0A
+%0A
         settings
@@ -1517,32 +1517,33 @@
 LOUD_PLUGINS = (
+)
 %0A            'Wi
@@ -1527,37 +1527,503 @@
 NS = ()%0A        
-    '
+clear_cache()%0A%0A        core_plugins = len(get_plugins())%0A        core_features = len(get_active_features())%0A        core_index_javascripts = len(get_extra_javascripts('index'))%0A        core_iphone_javascripts = len(get_extra_javascripts('iphone'))%0A        core_index_extensions = len(get_gadget_api_extensions('index'))%0A        core_iphone_extensions = len(get_gadget_api_extensions('iphone'))%0A%0A        settings.WIRECLOUD_PLUGINS = (%0A            'wirecloud.tests.plugins.
 WirecloudTestPlu
@@ -2034,32 +2034,56 @@
 ',%0A            '
+wirecloud.tests.plugins.
 WirecloudTestPlu
@@ -2166,16 +2166,31 @@
 gins()),
+ core_plugins +
  2)%0A    
@@ -2242,9 +2242,25 @@
 )), 
-3
+core_features + 2
 )%0A  
@@ -2310,32 +2310,57 @@
 ripts('index')),
+ core_index_javascripts +
  2)%0A        self
@@ -2405,24 +2405,50 @@
 ('iphone')),
+ core_iphone_javascripts +
  1)%0A        
@@ -2505,17 +2505,41 @@
 dex')), 
-2
+core_index_extensions + 1
 )%0A      
@@ -2599,17 +2599,42 @@
 one')), 
-1
+core_iphone_extensions + 0
 )%0A%0A    d
@@ -2730,32 +2730,56 @@
  (%0A            '
+wirecloud.tests.plugins.
 WirecloudTestPlu
@@ -2798,16 +2798,40 @@
        '
+wirecloud.tests.plugins.
 Wireclou

Fix WirecloudPluginTestCase bugs