<commit_before>/**
 * Copyright 2011 <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 ca.marcmeszaros.papyrus.test;

import java.net.URL;
import java.util.LinkedList;
import java.util.concurrent.ExecutionException;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.test.ActivityUnitTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import ca.marcmeszaros.papyrus.remote.DownloadThumbnails;

public class DownloadThumbnailsTest extends ActivityUnitTestCase<MockActivity> {

	private DownloadThumbnails download;
	private URL testUrl;
	private Intent startIntent;
	
	public DownloadThumbnailsTest() {
		super(MockActivity.class);
	}
	
    @Override
    protected void setUp() throws Exception {
    	super.setUp();
    	download = new DownloadThumbnails();
    	testUrl = new URL("http://developer.android.com/assets/images/bg_logo.png");
    	
    	// only start the intent in each test
    	startIntent = new Intent(Intent.ACTION_MAIN);
    }
    
    /**
     * The name 'test preconditions' is a convention to signal that if this
     * test doesn't pass, the test case was not set up properly and it might
     * explain any and all failures in other tests.  This is not guaranteed
     * to run before other tests, as junit uses reflection to find the tests.
     */
    @MediumTest
    public void testPreconditions(){
    	// start the activity
    	startActivity(startIntent, null, null);
    	
    	// check some stuff isn't null
    	assertNotNull(download);
    	assertNotNull(testUrl);
    	assertNotNull(getActivity());
    }
    
    /**
     * Make sure a download can start.
     */
    @MediumTest
    public void testStartDownload(){
    	// make sure it's pending (not started)
    	assertEquals(AsyncTask.Status.PENDING, download.getStatus());
    	
    	// use the background header logo in the Android Documentation as a sample image
    	download.execute(testUrl);
    	
    	// make sure it started
    	assertEquals(AsyncTask.Status.RUNNING, download.getStatus());
    }
    
    /**
     * See if the downloaded image is the same as the one saved locally.
     * 
     * @throws InterruptedException
     * @throws ExecutionException
     */
    @MediumTest
    public void testDownloadImage() throws InterruptedException, ExecutionException {
    	// use the background header logo in the Android Documentation as a sample image
    	download.execute(testUrl);
    	LinkedList<Bitmap> images = download.get();
    	
    	// make sure there are image
    	assertNotNull(images);
    	
    	MockActivity activity = startActivity(startIntent, null, null);
    	
    	// need a "real" activity to get a context (to get resources)	
    	// load up an image from the app
    	Bitmap bm = BitmapFactory.decodeResource(activity.getResources(), R.drawable.bg_logo);
    	
   	
    	// check images are the same
    	// TODO this test fails; figure out why (theory: the resource image and the
    	// one downloaded are not binary equivalent - last modified date etc.) 
    	//assertEquals(bm, images.getFirst());

    	assertEquals(bm, bm);
    	assertEquals(images.getFirst(), images.getFirst());
    
    }
    
}<commit_msg>tests now check for network connection first<commit_after>/**
 * Copyright 2011 <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 ca.marcmeszaros.papyrus.test;

import java.net.URL;
import java.util.LinkedList;
import java.util.concurrent.ExecutionException;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.test.ActivityUnitTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import ca.marcmeszaros.papyrus.remote.DownloadThumbnails;

public class DownloadThumbnailsTest extends ActivityUnitTestCase<MockActivity> {

	private DownloadThumbnails download;
	private URL testUrl;
	private Intent startIntent;
	
	public DownloadThumbnailsTest() {
		super(MockActivity.class);
	}
	
    @Override
    protected void setUp() throws Exception {
    	super.setUp();
    	download = new DownloadThumbnails();
    	testUrl = new URL("http://developer.android.com/assets/images/bg_logo.png");
    	
    	// only start the intent in each test
    	startIntent = new Intent(Intent.ACTION_MAIN);
    }
    
    /**
     * Used to test the network connection inside tests.
     */
    private void checkNetworkConnection() {
		// check network connection
    	assertNotNull(getActivity().getSystemService(Context.CONNECTIVITY_SERVICE));
    	ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    	assertNotNull(connectivityManager.getActiveNetworkInfo());
    	NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    	assertTrue(activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED);
	}
    
    /**
     * The name 'test preconditions' is a convention to signal that if this
     * test doesn't pass, the test case was not set up properly and it might
     * explain any and all failures in other tests.  This is not guaranteed
     * to run before other tests, as junit uses reflection to find the tests.
     */
    @MediumTest
    public void testPreconditions() {
    	// start the activity
    	startActivity(startIntent, null, null);
    	
    	// check some stuff isn't null
    	assertNotNull(download);
    	assertNotNull(testUrl);
    	assertNotNull(getActivity());
    	
    	// make sure we have a network connection
    	checkNetworkConnection();
    }
    
    /**
     * Make sure a download can start.
     */
    @MediumTest
    public void testStartDownload() {
    	// start the activity
    	startActivity(startIntent, null, null);
    	
    	// make sure we have a network connection
    	checkNetworkConnection();
    	
    	// make sure it's pending (not started)
    	assertEquals(AsyncTask.Status.PENDING, download.getStatus());
    	
    	// use the background header logo in the Android Documentation as a sample image
    	download.execute(testUrl);
    	
    	// make sure it started
    	assertEquals(AsyncTask.Status.RUNNING, download.getStatus());
    }
    
    /**
     * See if the downloaded image is the same as the one saved locally.
     * 
     * @throws InterruptedException
     * @throws ExecutionException
     */
    @MediumTest
    public void testDownloadImage() throws InterruptedException, ExecutionException {
    	// start the activity
    	MockActivity activity = startActivity(startIntent, null, null);
    	
    	// make sure we have a network connection
    	checkNetworkConnection();
    	
    	// use the background header logo in the Android Documentation as a sample image
    	download.execute(testUrl);
    	LinkedList<Bitmap> images = download.get();
    	
    	// make sure there are image
    	assertNotNull(images);
    	
    	// need a "real" activity to get a context (to get resources)	
    	// load up an image from the app
    	Bitmap bm = BitmapFactory.decodeResource(activity.getResources(), R.drawable.bg_logo);
   	
    	// check images are the same
    	// TODO this test fails; figure out why (theory: the resource image and the
    	// one downloaded are not binary equivalent - last modified date etc.) 
    	//assertEquals(bm, images.getFirst());

    	assertEquals(bm, bm);
    	assertEquals(images.getFirst(), images.getFirst());
    
    }
    
}
{'fraction_non_alphanumeric': 0.08324661810613944, 'fraction_numerical': 0.0018499248468030985, 'mean_word_length': 3.1686746987951806, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6000105', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2317, 'n_words': 788}