/*
 * Copyright (C) 2015 Pedro Vicente Gomez Sanchez.
 *
 * 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 com.github.pedrovgs.lynx.presenter;

import com.github.pedrovgs.lynx.LynxConfig;
import com.github.pedrovgs.lynx.model.Lynx;
import com.github.pedrovgs.lynx.model.Trace;
import com.github.pedrovgs.lynx.model.TraceLevel;
import java.util.LinkedList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * @author Pedro Vicente Gómez Sánchez.
 */
public class LynxPresenterTest {

  private static final int MAX_NUMBER_OF_TRACES = 10;
  private static final String ANY_FILTER = "filter";

  @Mock private Lynx lynx;
  @Mock private LynxPresenter.View view;

  private LynxPresenter presenter;

  @Before public void setUp() {
    MockitoAnnotations.initMocks(this);
    presenter = new LynxPresenter(lynx, view, MAX_NUMBER_OF_TRACES);
  }

  @Test public void shouldRegisterItselfAsLynxListenerOnResume() {
    presenter.resume();

    verify(lynx).registerListener(presenter);
  }

  @Test public void shouldUnregisterItselfAsLynxListenerOnPause() {
    presenter.resume();

    presenter.pause();

    verify(lynx).unregisterListener(presenter);
  }

  @Test public void shouldStartLynxOnResume() {
    presenter.resume();

    verify(lynx).startReading();
  }

  @Test public void shouldStopLynxOnPause() {
    presenter.resume();

    presenter.pause();

    verify(lynx).stopReading();
  }

  @Test public void shouldShowTracesNotifiedFromLynx() {
    List<Trace> traces = generateTraces(MAX_NUMBER_OF_TRACES);

    presenter = new LynxPresenter(lynx, view, MAX_NUMBER_OF_TRACES);
    presenter.resume();
    presenter.onNewTraces(traces);

    verify(view).showTraces(eq(traces), eq(0));
  }

  @Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullLynxConfigs() {
    presenter.setLynxConfig(null);
  }

  @Test public void shouldUpdateLynxConfigOnSetLynxConfig() {
    LynxConfig lynxConfig = new LynxConfig();

    presenter.setLynxConfig(lynxConfig);

    verify(lynx).setConfig(lynxConfig);
  }

  @Test
  public void shouldDisableAutoScrollOnScrollCalledWithLastVisiblePositionDifferentFromThreeLastPositions() {
    List<Trace> traces = generateTraces(10);

    presenter.resume();
    presenter.onNewTraces(traces);
    presenter.onScrollToPosition(5);

    verify(view).disableAutoScroll();
  }

  @Test public void shouldEnableAutoScrollOnScrollCalledWithOneOfTheLastThreePositions() {
    List<Trace> traces = generateTraces(10);

    presenter.resume();
    presenter.onNewTraces(traces);
    presenter.onScrollToPosition(9);

    verify(view).enableAutoScroll();
  }

  @Test public void shouldApplyNewConfigToLynxOnFilterUpdated() {
    givenAPreviusLynxConfig();
    ArgumentCaptor<LynxConfig> lynxConfigArgumentCaptor = ArgumentCaptor.forClass(LynxConfig.class);

    presenter.resume();
    presenter.onFilterUpdated(ANY_FILTER);

    verify(lynx).setConfig(lynxConfigArgumentCaptor.capture());
    LynxConfig lynxConfig = lynxConfigArgumentCaptor.getValue();
    assertEquals(ANY_FILTER, lynxConfig.getFilter());
  }

  @Test public void shouldClearViewOnFilterUpdated() {
    givenAPreviusLynxConfig();

    presenter.resume();
    presenter.onFilterUpdated(ANY_FILTER);

    verify(view).clear();
  }

  @Test public void shouldShowNewTracesAfterOnFilterUpdated() {
    givenAPreviusLynxConfig();
    List<Trace> traces = generateTraces(3);

    presenter.resume();
    presenter.onNewTraces(traces);
    presenter.onFilterUpdated(ANY_FILTER);
    List<Trace> newTraces = generateTraces(5);
    presenter.onNewTraces(newTraces);

    verify(view, times(2)).showTraces(newTraces, 0);
  }

  private void givenAPreviusLynxConfig() {
    when(lynx.getConfig()).thenReturn(new LynxConfig());
  }

  private List<Trace> generateTraces(int numberOfTraces) {
    List<Trace> traces = new LinkedList<Trace>();
    for (int i = 0; i < numberOfTraces; i++) {
      Trace dummyTrace = new Trace(TraceLevel.VERBOSE, String.valueOf(i));
      traces.add(dummyTrace);
    }
    return traces;
  }
}

@@ -4475,32 +4475,987 @@
 races, 0);%0A  %7D%0A%0A
+  @Test public void shouldShareAPlainRepresentationOfTheCurrentBufferTraces() %7B%0A    List%3CTrace%3E traces = generateTraces(30);%0A%0A    presenter.resume();%0A    presenter.onNewTraces(traces);%0A    presenter.onShareButtonClicked();%0A%0A    traces = removeFirstTraces(20, traces);%0A    String expectedTraces = generatePlainTracesToShare(traces);%0A    verify(view).shareTraces(expectedTraces);%0A  %7D%0A%0A  private List%3CTrace%3E removeFirstTraces(int tracesToRemove, List%3CTrace%3E traces) %7B%0A    for (int i = 0; i %3C tracesToRemove; i++) %7B%0A      traces.remove(0);%0A    %7D%0A    return traces;%0A  %7D%0A%0A  private String generatePlainTracesToShare(List%3CTrace%3E traces) %7B%0A    StringBuilder sb = new StringBuilder();%0A    for (Trace trace : traces) %7B%0A      String traceLevel = trace.getLevel().getValue();%0A      String traceMessage = trace.getMessage();%0A      sb.append(traceLevel);%0A      sb.append(%22/ %22);%0A      sb.append(traceMessage);%0A      sb.append(%22%5Cn%22);%0A    %7D%0A    return sb.toString();%0A  %7D%0A%0A
   private void g

Add test to check if onShareButtonClicked method is working as expected