/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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 org.jetbrains.plugins.gradle.execution.test.runner.events;

import com.intellij.execution.testframework.sm.runner.SMTestProxy;
import org.jetbrains.plugins.gradle.util.XmlXpathHelper;
import org.jetbrains.plugins.gradle.execution.test.runner.GradleTestsExecutionConsoleManager;

/**
 * @author Vladislav.Soroka
 * @since 2/28/14
 */
public class AfterSuiteEvent extends AbstractTestEvent {
  public AfterSuiteEvent(GradleTestsExecutionConsoleManager consoleManager) {
    super(consoleManager);
  }

  @Override
  public void process(XmlXpathHelper eventXml) throws XmlXpathHelper.XmlParserException {
    final String testId = getTestId(eventXml);

    final SMTestProxy testProxy = getConsoleManager().getTestsMap().get(testId);
    if (testProxy == null) return;

    final TestEventResult result = getTestEventResultType(eventXml);
    switch (result) {
      case SUCCESS:
        testProxy.setFinished();
        break;
      case FAILURE:
        testProxy.setTestFailed("", null, false);
        break;
      case SKIPPED:
        testProxy.setTestIgnored(null, null);
        break;
      case UNKNOWN_RESULT:
        break;
    }

    if (testProxy.isEmptySuite() && !(testProxy instanceof SMTestProxy.SMRootTestProxy)) {
      testProxy.getParent().getChildren().remove(testProxy);
    }
    addToInvokeLater(new Runnable() {
      @Override
      public void run() {
        getResultsViewer().onSuiteFinished(testProxy);
      }
    });
  }
}

@@ -1251,17 +1251,170 @@
 ntXml);%0A
-%0A
+    final TestEventResult result = getTestEventResultType(eventXml);%0A%0A    addToInvokeLater(new Runnable() %7B%0A      @Override%0A      public void run() %7B%0A    
     fina
@@ -1486,16 +1486,20 @@
 estId);%0A
+    
     if (
@@ -1534,73 +1534,8 @@
     
-final TestEventResult result = getTestEventResultType(eventXml);%0A
     
@@ -1554,24 +1554,28 @@
 lt) %7B%0A      
+ 
+   
 case SUCCESS
@@ -1576,16 +1576,20 @@
 UCCESS:%0A
+    
         
@@ -1613,16 +1613,20 @@
 shed();%0A
+    
         
@@ -1634,24 +1634,28 @@
 reak;%0A      
+    
 case FAILURE
@@ -1656,16 +1656,20 @@
 AILURE:%0A
+    
         
@@ -1710,39 +1710,47 @@
 false);%0A        
+   
+ 
 break;%0A
+    
       case SKIPP
@@ -1753,16 +1753,20 @@
 KIPPED:%0A
+    
         
@@ -1807,31 +1807,39 @@
 l);%0A        
+  
+  
 break;%0A
+    
       case U
@@ -1853,32 +1853,36 @@
 RESULT:%0A        
+    
 break;%0A    %7D%0A%0A  
@@ -1876,19 +1876,27 @@
 ak;%0A    
+ 
-%7D%0A%0A
+   %7D%0A%0A    
     if (
@@ -1980,24 +1980,28 @@
 y)) %7B%0A      
+    
 testProxy.ge
@@ -2051,89 +2051,13 @@
     
-%7D%0A
     
-addToInvokeLater(new Runnable() %7B%0A      @Override%0A      public void run() %7B
+%7D
 %0A   

access SMTestProxy in EDT only (possible fix for https://ea.jetbrains.com/browser/ea_problems/59241 - CME: SMTestProxy.calcHasErrors)