<commit_before>/*
 * Copyright 2016 <NAME>
 *
 * 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.mkjensen.dml.ondemand;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.support.membermodification.MemberMatcher.field;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.support.v17.leanback.widget.ImageCardView;
import android.support.v17.leanback.widget.Presenter.ViewHolder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.github.mkjensen.dml.PowerMockRobolectricTest;

import org.junit.Before;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.robolectric.Robolectric;

/**
 * Tests for {@link VideoPresenter}.
 */
@PrepareForTest(Video.class)
public class VideoPresenterTest extends PowerMockRobolectricTest {

  private VideoPresenter videoPresenter;

  @Before
  public void before() {
    videoPresenter = new VideoPresenter();
  }

  @Test
  public void onCreateViewHolder_shouldCreateView() {

    // When
    ViewHolder viewHolder = createViewHolder();

    // Then
    assertNotNull(viewHolder.view);
    assertEquals(ImageCardView.class, viewHolder.view.getClass());
  }

  @Test
  public void onBindViewHolder_shouldSetImageAndTitleOfView() throws IllegalAccessException {

    // Given
    ViewHolder viewHolder = createViewHolder();
    ImageCardView viewMock = (ImageCardView) spy(viewHolder.view);
    setViewHolderView(viewHolder, viewMock);
    Video videoMock = mock(Video.class);
    String title = "title";
    when(videoMock.getTitle()).thenReturn(title);

    // When
    videoPresenter.onBindViewHolder(viewHolder, videoMock);

    // Then
    verify(viewMock).setMainImage(isA(Drawable.class));
    assertEquals(title, viewMock.getTitleText());
  }

  @Test
  public void onUnbindViewHolder_shouldUnsetViewMainImage() throws IllegalAccessException {

    // Given
    ViewHolder viewHolderMock = mock(ViewHolder.class);
    ImageCardView viewMock = mock(ImageCardView.class);
    setViewHolderView(viewHolderMock, viewMock);

    // When
    videoPresenter.onUnbindViewHolder(viewHolderMock);

    // Then
    verify(viewMock).setMainImage(null);
  }

  private ViewHolder createViewHolder() {
    Activity activity = Robolectric.buildActivity(Activity.class).create().get();
    ViewGroup layout = new FrameLayout(activity);
    return videoPresenter.onCreateViewHolder(layout);
  }

  private void setViewHolderView(ViewHolder viewHolder, View view) throws IllegalAccessException {
    field(ViewHolder.class, "view").set(viewHolder, view);
  }
}<commit_msg>Make setViewHolderView method static<commit_after>/*
 * Copyright 2016 <NAME>
 *
 * 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.mkjensen.dml.ondemand;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.support.membermodification.MemberMatcher.field;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.support.v17.leanback.widget.ImageCardView;
import android.support.v17.leanback.widget.Presenter.ViewHolder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.github.mkjensen.dml.PowerMockRobolectricTest;

import org.junit.Before;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.robolectric.Robolectric;

/**
 * Tests for {@link VideoPresenter}.
 */
@PrepareForTest(Video.class)
public class VideoPresenterTest extends PowerMockRobolectricTest {

  private VideoPresenter videoPresenter;

  @Before
  public void before() {
    videoPresenter = new VideoPresenter();
  }

  @Test
  public void onCreateViewHolder_shouldCreateView() {

    // When
    ViewHolder viewHolder = createViewHolder();

    // Then
    assertNotNull(viewHolder.view);
    assertEquals(ImageCardView.class, viewHolder.view.getClass());
  }

  @Test
  public void onBindViewHolder_shouldSetImageAndTitleOfView() throws IllegalAccessException {

    // Given
    ViewHolder viewHolder = createViewHolder();
    ImageCardView viewMock = (ImageCardView) spy(viewHolder.view);
    setViewHolderView(viewHolder, viewMock);
    Video videoMock = mock(Video.class);
    String title = "title";
    when(videoMock.getTitle()).thenReturn(title);

    // When
    videoPresenter.onBindViewHolder(viewHolder, videoMock);

    // Then
    verify(viewMock).setMainImage(isA(Drawable.class));
    assertEquals(title, viewMock.getTitleText());
  }

  @Test
  public void onUnbindViewHolder_shouldUnsetViewMainImage() throws IllegalAccessException {

    // Given
    ViewHolder viewHolderMock = mock(ViewHolder.class);
    ImageCardView viewMock = mock(ImageCardView.class);
    setViewHolderView(viewHolderMock, viewMock);

    // When
    videoPresenter.onUnbindViewHolder(viewHolderMock);

    // Then
    verify(viewMock).setMainImage(null);
  }

  private ViewHolder createViewHolder() {
    Activity activity = Robolectric.buildActivity(Activity.class).create().get();
    ViewGroup layout = new FrameLayout(activity);
    return videoPresenter.onCreateViewHolder(layout);
  }

  private static void setViewHolderView(ViewHolder viewHolder, View view) throws IllegalAccessException {
    field(ViewHolder.class, "view").set(viewHolder, view);
  }
}
{'fraction_non_alphanumeric': 0.09503424657534247, 'fraction_numerical': 0.003424657534246575, 'mean_word_length': 4.721632653061224, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '535406', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1984, 'n_words': 476}