/*
 * Copyright 2018 mk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mk.gdx.firebase.html;


import com.badlogic.gdx.Application;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.Constructor;
import com.badlogic.gdx.utils.reflect.ReflectionException;
import com.google.gwt.core.client.ScriptInjector;
import com.google.gwtmockito.GwtMockitoTestRunner;

import org.junit.Before;
import org.junit.Rule;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.rule.PowerMockRule;

@RunWith(GwtMockitoTestRunner.class)
@PrepareForTest({ClassReflection.class, Constructor.class, ScriptInjector.class})
@SuppressStaticInitializationFor({"com.google.gwt.core.client.ScriptInjector"})
public abstract class GdxHtmlAppTest {

    @Rule
    public PowerMockRule powerMockRule = new PowerMockRule();

    @Before
    public void setup() {
        Application application = Mockito.mock(Application.class);
        Mockito.when(application.getType()).thenReturn(Application.ApplicationType.WebGL);
        FileHandle configFile = Mockito.mock(FileHandle.class);
        Mockito.when(configFile.exists()).thenReturn(true);
        Mockito.when(configFile.readString()).thenReturn("<script src=\"https://www.gstatic.com/firebasejs/123/firebase.js\"></script>\n" +
                "<script>\n" +
                "  // Initialize Firebase\n" +
                "  var config = {\n" +
                "    apiKey: \"abc\",\n" +
                "    authDomain: \"abc\",\n" +
                "    databaseURL: \"abc\",\n" +
                "    projectId: \"abc\",\n" +
                "    storageBucket: \"abc\",\n" +
                "    messagingSenderId: \"123\"\n" +
                "  };\n" +
                "  firebase.initializeApp(config);\n" +
                "</script>");

        Files files = Mockito.mock(Files.class);
        Mockito.when(files.internal(Mockito.eq("firebase-config.html"))).thenReturn(configFile);

        PowerMockito.mockStatic(ScriptInjector.class);
        Mockito.when(ScriptInjector.fromUrl(Mockito.anyString())).thenCallRealMethod();

        PowerMockito.mockStatic(ClassReflection.class);
        try {
            Mockito.when(ClassReflection.forName(Mockito.anyString())).thenAnswer(new Answer<Object>() {
                @Override
                public Object answer(InvocationOnMock invocation) throws Throwable {
                    return Class.forName((String) invocation.getArgument(0));
                }
            });
            Mockito.when(ClassReflection.getConstructor(Mockito.any(Class.class))).thenAnswer(new Answer<Object>() {
                @Override
                public Object answer(InvocationOnMock invocation) throws Throwable {
                    Constructor constructor = PowerMockito.mock(Constructor.class);
                    Mockito.when(constructor.newInstance()).thenReturn(((Class) invocation.getArgument(0)).getConstructors()[0].newInstance());
                    return constructor;
                }
            });
        } catch (ReflectionException e) {
            e.printStackTrace();
        }

        Gdx.app = application;
        Gdx.files = files;
    }
}

@@ -1811,16 +1811,32 @@
 setup() 
+throws Exception
 %7B%0A      
@@ -3120,26 +3120,8 @@
 s);%0A
-        try %7B%0A    
     
@@ -3221,36 +3221,32 @@
 ) %7B%0A            
-  
-  
 @Override%0A      
@@ -3231,36 +3231,32 @@
       @Override%0A
-    
             publ
@@ -3312,36 +3312,32 @@
 ows Throwable %7B%0A
-    
                 
@@ -3402,34 +3402,26 @@
             
-    %7D%0A    
+%7D%0A
         %7D);%0A
@@ -3420,36 +3420,32 @@
     %7D);%0A        
- 
-   
 Mockito.when(Cla
@@ -3537,36 +3537,32 @@
 ) %7B%0A            
-    
 @Override%0A      
@@ -3559,36 +3559,32 @@
 ide%0A            
-   
- 
 public Object an
@@ -3628,36 +3628,32 @@
 ows Throwable %7B%0A
-    
                 
@@ -3728,28 +3728,24 @@
             
-    
 Mockito.when
@@ -3868,28 +3868,24 @@
             
- 
-   
 return const
@@ -3900,34 +3900,26 @@
             
-    %7D%0A    
+%7D%0A
         %7D);%0A
@@ -3921,93 +3921,8 @@
  %7D);
-%0A        %7D catch (ReflectionException e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A%0A  

refactor GdxHtmlAppTest