<commit_before>package se.chalmers.watchmetest.ui;

import java.util.ArrayList;

import se.chalmers.watchme.R;
import se.chalmers.watchme.activity.AddMovieActivity;
import se.chalmers.watchme.activity.MainActivity;
import se.chalmers.watchme.activity.MovieDetailsActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;
import android.widget.TextView;

import com.jayway.android.robotium.solo.Solo;

public class MovieListFragmentTest extends
		ActivityInstrumentationTestCase2<MainActivity> {

	Solo solo;

	public MovieListFragmentTest() {
		super(MainActivity.class);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		solo = new Solo(getInstrumentation(), getActivity());
	}

	@Override
	public void tearDown() throws Exception {
		// finish all activities that have been opened during test execution.
		solo.finishOpenedActivities();
	}
	
	public void testAddMovie() {
		solo.assertCurrentActivity("MainActivity expected", MainActivity.class);
		solo.clickOnText("Movies");
		solo.clickOnActionBarItem(R.id.menu_add_movie);
		solo.assertCurrentActivity("AddMovieActivity expected",
				AddMovieActivity.class);
		solo.enterText(0, "Batman");
		// TODO: Set Raiting
		solo.clickOnText("Pick");
		solo.setDatePicker(0, 2013, 12, 24);
		solo.clickOnText("Done");
		solo.enterText(1, "Action");
		solo.enterText(2, "Mum said I'd like this");
		solo.clickOnButton(1);
		solo.assertCurrentActivity("MainActivity expected", MainActivity.class);
		boolean movieFound = solo.searchText("Batman");
		assertTrue(movieFound);
	}

	// TODO: Can you be sure that the application is in the state as when the
	// last test. In that case you can remove first lines.
	public void testRemoveMovie() {
		final int ADD_MOVIE_BUTTON = 1;

		solo.assertCurrentActivity("MainActivity expected", MainActivity.class);
		solo.clickOnText("Movies");
		solo.clickOnActionBarItem(R.id.menu_add_movie);
		solo.enterText(0, "TEST_MOVIE");
		solo.clickOnButton(ADD_MOVIE_BUTTON);
		solo.assertCurrentActivity("MainActivity expected", MainActivity.class);
		solo.clickLongOnText("TEST_MOVIE");

		boolean dialogAppeared = solo
				.searchText("Are you sure you want to delete \"TEST_MOVIE\"?");
		assertTrue(dialogAppeared);

		solo.clickOnText("Cancel");
		boolean expected = true;
		boolean actual = solo.searchText("TEST_MOVIE");
		assertEquals("TEST_MOVIE was not found", expected, actual);

		solo.clickLongOnText("TEST_MOVIE");
		solo.clickOnText("Yes");
		
		// Delay. Gives TEST_MOVIE time to disappear 
		solo.sleep(5000);

		expected = false;
		actual = solo.searchText("TEST_MOVIE");
		assertEquals("TEST_MOVIE was found", expected, actual);
	}

	public void testClickOnMovie() {
		solo.assertCurrentActivity("MainActivity expected", MainActivity.class);
		solo.clickOnText("Movies");
		solo.clickOnActionBarItem(R.id.menu_add_movie);
		solo.enterText(0, "TEST_MOVIE_2");
		solo.clickOnButton(1);
		solo.assertCurrentActivity("MainActivity expected", MainActivity.class);
		solo.clickOnText("TEST_MOVIE_2");
		solo.assertCurrentActivity("MovieDetailsActivity expected",
				MovieDetailsActivity.class);
	}
	
}<commit_msg>Add new test in MovieListFragmentTest: testSort()<commit_after>package se.chalmers.watchmetest.ui;

import java.util.ArrayList;

import se.chalmers.watchme.R;
import se.chalmers.watchme.activity.AddMovieActivity;
import se.chalmers.watchme.activity.MainActivity;
import se.chalmers.watchme.activity.MovieDetailsActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.jayway.android.robotium.solo.Solo;

public class MovieListFragmentTest extends
		ActivityInstrumentationTestCase2<MainActivity> {

	Solo solo;

	public MovieListFragmentTest() {
		super(MainActivity.class);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		solo = new Solo(getInstrumentation(), getActivity());
	}

	@Override
	public void tearDown() throws Exception {
		// finish all activities that have been opened during test execution.
		solo.finishOpenedActivities();
	}

	// TODO: solo.sleep(n) ugly code? better with .waitFor___() method?
	public void testAddMovie() {
		solo.assertCurrentActivity("MainActivity expected", MainActivity.class);
		solo.clickOnText("Movies");
		solo.clickOnActionBarItem(R.id.menu_add_movie);
		solo.assertCurrentActivity("AddMovieActivity expected",
				AddMovieActivity.class);
		solo.enterText(0, "Batman");
		solo.setProgressBar(0, 1);
		solo.clickOnText("Pick");
		solo.setDatePicker(0, 2013, 12, 24);
		solo.clickOnText("Done");
		solo.enterText(1, "Action");
		solo.enterText(2, "Mum said I'd like this");
		solo.clickOnButton(1);
		solo.assertCurrentActivity("MainActivity expected", MainActivity.class);
		boolean movieFound = solo.searchText("Batman");
		assertTrue(movieFound);
	}

	// TODO: Can you be sure that the application is in the state as when the
	// last test was done? In that case, first rows can be removed.
	public void testRemoveMovie() {
		final int ADD_MOVIE_BUTTON = 1;

		solo.assertCurrentActivity("MainActivity expected", MainActivity.class);
		solo.clickOnText("Movies");
		solo.clickOnActionBarItem(R.id.menu_add_movie);
		solo.enterText(0, "1_TEST_MOVIE");
		solo.clickOnButton(ADD_MOVIE_BUTTON);
		solo.assertCurrentActivity("MainActivity expected", MainActivity.class);
		solo.clickLongOnText("1_TEST_MOVIE");

		boolean dialogAppeared = solo
				.searchText("Are you sure you want to delete \"1_TEST_MOVIE\"?");
		assertTrue(dialogAppeared);

		solo.clickOnText("Cancel");
		boolean expected = true;
		boolean actual = solo.searchText("1_TEST_MOVIE");
		assertEquals("1_TEST_MOVIE was not found", expected, actual);

		solo.clickLongOnText("1_TEST_MOVIE");
		solo.clickOnText("Yes");

		// Delay. Gives 1_TEST_MOVIE time to disappear 
		solo.sleep(5000);

		expected = false;
		actual = solo.searchText("1_TEST_MOVIE");
		assertEquals("1_TEST_MOVIE was found", expected, actual);
	}

	public void testClickOnMovie() {
		solo.assertCurrentActivity("MainActivity expected", MainActivity.class);
		solo.clickOnText("Movies");
		solo.clickOnActionBarItem(R.id.menu_add_movie);
		solo.enterText(0, "2_TEST_MOVIE");
		solo.clickOnButton(1);
		solo.assertCurrentActivity("MainActivity expected", MainActivity.class);
		solo.clickOnText("2_TEST_MOVIE");
		solo.assertCurrentActivity("MovieDetailsActivity expected",
				MovieDetailsActivity.class);
	}

	// TODO: add date sorting. hard to compare dates..
	public void testSort() {
		final int ADD_MOVIE_BUTTON = 1;

		solo.assertCurrentActivity("MainActivity expected", MainActivity.class);
		solo.clickOnText("Movies");

		// Add three movies with different titles, rating and dates
		char movieFirstCharacter = 'A';
		int year = 2012;
		int rating = 1;
		for (int i = 0; i < 3; i++) {
			solo.clickOnActionBarItem(R.id.menu_add_movie);
			solo.enterText(0, movieFirstCharacter + "_MOVIE");
			solo.setProgressBar(0, rating);
			solo.clickOnText("Pick");
			solo.setDatePicker(0, year, 12, 24);
			solo.clickOnText("Done");
			solo.clickOnButton(ADD_MOVIE_BUTTON);
			solo.sleep(2000);

			movieFirstCharacter++;
			year++;
			rating++;
		}

		solo.clickOnActionBarItem(R.id.menu_sort_button);
		boolean dialogAppeared = solo.searchText("Order by");
		assertTrue(dialogAppeared);

		solo.clickOnText("Title");
		solo.sleep(2000);

		// Extract movie titles from text fields found in the
		// MovieListFragment's list.
		View listView = solo.getView(android.R.id.list);
		ArrayList<TextView> textViews = solo.getCurrentTextViews(listView);
		ArrayList<String> movieTitleList = new ArrayList<String>();

		// Every second text view holds a movie title.
		for (int i = 2; i < textViews.size(); i += 2) {
			String movieTitle = textViews.get(i).getText().toString();
			movieTitleList.add(movieTitle);
		}

		// Check if movies really are sorted by title
		boolean areMoviesInCorrectOrder = true;
		for (int i = 1; i < movieTitleList.size(); i++) {
			String thisTitle = movieTitleList.get(i);
			String lastTitle = movieTitleList.get(i - 1);
			if (thisTitle.compareTo(lastTitle) < 0) {
				areMoviesInCorrectOrder = false;
			}
		}

		assertTrue(areMoviesInCorrectOrder);

		solo.clickOnActionBarItem(R.id.menu_sort_button);
		solo.clickOnText("Rating");
		solo.sleep(2000);

		// Extract movie's rating from rating bars found in the
		// MovieListFragment's list.
		ArrayList<ProgressBar> ratingBarList = solo.getCurrentProgressBars();
		ArrayList<Float> movieRatingList = new ArrayList<Float>();

		for (int i = 0; i < ratingBarList.size(); i++) {
			float ratingTemp = ((RatingBar) ratingBarList.get(i)).getRating();
			movieRatingList.add(ratingTemp);
		}

		// Check if movies really are sorted by rating
		areMoviesInCorrectOrder = true;
		for (int i = 1; i < movieRatingList.size(); i++) {
			float thisRating = movieRatingList.get(i);
			float lastRating = movieRatingList.get(i - 1);
			if (thisRating > lastRating) {
				areMoviesInCorrectOrder = false;
			}
		}

		assertTrue(areMoviesInCorrectOrder);

	}

}
{'fraction_non_alphanumeric': 0.12045951859956236, 'fraction_numerical': 0.01050328227571116, 'mean_word_length': 5.12257200267917, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2562112', 'n_tokens_mistral': 3340, 'n_tokens_neox': 2918, 'n_words': 624}