package com.codeaffine.extras.jdt.internal.junitstatus;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Shell;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.codeaffine.eclipse.swt.test.util.DisplayHelper;
import com.codeaffine.eclipse.swt.test.util.SWTIgnoreConditions.NonWindowsPlatform;
import com.codeaffine.test.util.junit.ConditionalIgnoreRule;
import com.codeaffine.test.util.junit.ConditionalIgnoreRule.ConditionalIgnore;


public class JUnitProgressBarTest {

  @Rule
  public final DisplayHelper displayHelper = new DisplayHelper();
  @Rule
  public final ConditionalIgnoreRule ignoreRule = new ConditionalIgnoreRule();

  private JUnitProgressBar progressBar;

  @Before
  public void setUp() {
    Shell parent = displayHelper.createShell();
    parent.setLayout( new FillLayout() );
    progressBar = new JUnitProgressBar( parent );
  }

  @Test
  public void testInitialValues() {
    assertThat( progressBar.getText() ).isEmpty();
    assertThat( progressBar.getTextAlignment() ).isEqualTo( SWT.LEFT );
    assertThat( progressBar.getBarColor() ).isNull();
    assertThat( progressBar.getSelection() ).isEqualTo( 0 );
    assertThat( progressBar.getMaximum() ).isEqualTo( 0 );
  }

  @ConditionalIgnore(condition=NonWindowsPlatform.class)
  @Test
  public void testStyleNoBackground() {
    assertThat( progressBar.getStyle() & SWT.NO_BACKGROUND ).isNotZero();
  }

  @Test
  public void testStyleDoubleBuffered() {
    assertThat( progressBar.getStyle() & SWT.DOUBLE_BUFFERED ).isNotZero();
  }

  @Test
  public void testStyleNoFocus() {
    assertThat( progressBar.getStyle() & SWT.NO_FOCUS ).isNotZero();
  }

  @Test
  public void testSetText() {
    String expected = "text";
    progressBar.setText( expected );

    String text = progressBar.getText();

    assertThat( text ).isEqualTo( expected );
  }

  @Test
  public void testSetTextAlignment() {
    int expected = SWT.CENTER;
    progressBar.setTextAlignment( expected );

    assertThat( progressBar.getTextAlignment() ).isEqualTo( expected );
  }

  @Test
  public void testSetBarColor() {
    Color expected = displayHelper.getDisplay().getSystemColor( SWT.COLOR_RED );
    progressBar.setBarColor( expected );

    assertThat( progressBar.getBarColor() ).isEqualTo( expected );
  }

  @Test
  public void testSetSelection() {
    int expected = 2;
    progressBar.setSelection( expected );

    assertThat( progressBar.getSelection() ).isEqualTo( expected );
  }

  @Test
  public void testSetMaximum() {
    int expected = 2;
    progressBar.setMaximum( expected );

    assertThat( progressBar.getMaximum() ).isEqualTo( expected );
  }

  @Test
  public void testSetValue() {
    String text = "text";
    int textAlignment = SWT.CENTER;
    Color barColor = displayHelper.getDisplay().getSystemColor( SWT.COLOR_RED );
    int selection = 1;
    int maximum = 100;

    progressBar.setValues( text, textAlignment, barColor, selection, maximum );

    assertThat( progressBar.getText() ).isEqualTo( text );
    assertThat( progressBar.getTextAlignment() ).isEqualTo( textAlignment );
    assertThat( progressBar.getBarColor() ).isEqualTo( barColor );
    assertThat( progressBar.getSelection() ).isEqualTo( selection );
    assertThat( progressBar.getMaximum() ).isEqualTo( maximum );
  }

  @Test
  public void testSetValueWithUnchangedArguments() {
    progressBar = spy( progressBar );
    String text = "text";
    int textAlignment = SWT.CENTER;
    Color barColor = displayHelper.getDisplay().getSystemColor( SWT.COLOR_RED );
    int selection = 1;
    int maximum = 100;
    progressBar.setValues( text, textAlignment, barColor, selection, maximum );

    progressBar.setValues( text, textAlignment, barColor, selection, maximum );

    verify( progressBar ).redraw();
  }

  @Test
  public void testGetBarWidth() {
    setProgressBarWidth( 100 );
    progressBar.setMaximum( 100 );

    progressBar.setSelection( 50 );

    assertThat( progressBar.getBarWidth() ).isEqualTo( 50 );
  }

  @Test
  public void testGetBarWidthForSelectionLargetThanWidth() {
    setProgressBarWidth( 100 );
    progressBar.setMaximum( 200 );

    progressBar.setSelection( 100 );

    assertThat( progressBar.getBarWidth() ).isEqualTo( 50 );
  }

  @Test
  public void testGetBarWidthForSelectionExceedingMaximum() {
    setProgressBarWidth( 100 );
    progressBar.setMaximum( 100 );

    progressBar.setSelection( 200 );

    assertThat( progressBar.getBarWidth() ).isEqualTo( 100 );
  }

  @Test
  public void testGetBarWidthForZeroSelection() {
    setProgressBarWidth( 100 );
    progressBar.setMaximum( 100 );

    progressBar.setSelection( 0 );

    assertThat( progressBar.getBarWidth() ).isEqualTo( 0 );
  }

  @Test
  public void testGetBarWidthForZeroMaximum() {
    setProgressBarWidth( 100 );
    progressBar.setMaximum( 0 );

    progressBar.setSelection( 10 );

    assertThat( progressBar.getBarWidth() ).isEqualTo( 0 );
  }

  @Test
  public void testRedrawWithInitialValues() {
    progressBar.getParent().setVisible( true );

    try {
      progressBar.redraw();
    } catch( Exception notExpected ) {
      fail();
    }
  }

  @Test
  public void testRedrawWithCustomValues() {
    progressBar.getParent().setVisible( true );
    Color barColor = displayHelper.getSystemColor( SWT.COLOR_BLUE );
    progressBar.setValues( "text", SWT.CENTER, barColor, 4, 5 );

    try {
      progressBar.redraw();
    } catch( Exception notExpected ) {
      fail();
    }
  }

  private void setProgressBarWidth( int width ) {
    Rectangle trim = progressBar.computeTrim( 0, 0, width, 10 );
    progressBar.setSize( trim.width + 2, 10 );
  }

}

@@ -112,45 +112,8 @@
 at;%0A
-import static org.junit.Assert.fail;%0A
 impo
@@ -735,16 +735,70 @@
 Ignore;%0A
+import com.codeaffine.test.util.lang.ThrowableCaptor;%0A
 %0A%0Apublic
@@ -5376,35 +5376,77 @@
 rue );%0A%0A    
-try %7B%0A     
+Throwable throwable = ThrowableCaptor.thrownBy( () -%3E
  progressBar
@@ -5458,68 +5458,50 @@
 aw()
-;
+ );%0A
 %0A    
-%7D catch( Exception notExpected ) %7B%0A      fail();%0A    %7D
+assertThat( throwable ).isNull();
 %0A  %7D
@@ -5746,19 +5746,61 @@
     
-try %7B%0A     
+Throwable throwable = ThrowableCaptor.thrownBy( () -%3E
  pro
@@ -5820,68 +5820,50 @@
 aw()
-;
+ );%0A
 %0A    
-%7D catch( Exception notExpected ) %7B%0A      fail();%0A    %7D
+assertThat( throwable ).isNull();
 %0A  %7D

Use ThrowableCaptor instead of try/catch block