Java tutorial
/* * ================================================================================================= * Copyright (C) 2015 Martin Albedinsky * ================================================================================================= * Licensed under the Apache License, Version 2.0 or later (further "License" only). * ------------------------------------------------------------------------------------------------- * You may use this file only in compliance with the License. More details and copy of this License * you may obtain at * * http://www.apache.org/licenses/LICENSE-2.0 * * You can redistribute, modify or publish any part of the code written within this file but as it * is described in the License, the software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND. * * See the License for the specific language governing permissions and limitations under the License. * ================================================================================================= */ package com.albedinsky.android.support.intent; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Environment; import android.support.annotation.NonNull; import android.support.v4.app.Fragment; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Locale; /** * @author Martin Albedinsky */ @SuppressWarnings("ResultOfMethodCallIgnored") public final class ContentIntentTest extends IntentBaseTest<ContentIntentTest.ContentIntentImpl> { @SuppressWarnings("unused") private static final String TAG = "ContentIntentTest"; public ContentIntentTest() { super(ContentIntentTest.ContentIntentImpl.class); } public void testCreateContentFileTimeStamp() { assertEquals(new SimpleDateFormat(ContentIntent.CONTENT_FILE_TIME_STAMP_FORMAT, Locale.getDefault()) .format(new Date()), ContentIntent.createContentFileTimeStamp()); } public void testCreateContentFileNameInExternalDirectory() { final File file = ContentIntent.createContentFile("test-file.tmp", Environment.DIRECTORY_DOWNLOADS); assertNotNull(file); assertTrue(file.exists()); assertFileRelativePath("/Download/", file); assertTrue(file.delete()); } public void testCreateContentFileNameInDirectory() { final File externalFilesDir = getActivity().getExternalFilesDir(null); final File file = ContentIntent.createContentFile("test-file.tmp", externalFilesDir); assertNotNull(file); assertTrue(file.exists()); assertFileRelativePath("/Android/data/com.albedinsky.android.support.intent.test/files/", file); assertTrue(file.delete()); } public void testAppendDefaultFileSuffixIfNotPresented() { assertEquals("lion.png", ContentIntent.appendDefaultFileSuffixIfNotPresented("lion.png", ".jpg")); assertEquals("elephant.jpg", ContentIntent.appendDefaultFileSuffixIfNotPresented("elephant", ".jpg")); assertEquals("cat.1", ContentIntent.appendDefaultFileSuffixIfNotPresented("cat.1", ".jpg")); } public void testDefaultHandlers() { assertEquals(Collections.EMPTY_LIST, mIntent.handlers()); } public void testWithHandlers() { mIntent.withHandlers(new ContentIntent.ContentHandler("TestHandler1", new Intent()), new ContentIntent.ContentHandler("TestHandler2", new Intent())); final List<ContentIntent.ContentHandler> handlers = mIntent.handlers(); assertNotNull(handlers); assertTrue(handlers.size() == 2); assertEquals("TestHandler1", handlers.get(0).name()); assertEquals("TestHandler2", handlers.get(1).name()); } public void testWithEmptyHandlers() { mIntent.withHandlers(new ArrayList<ContentIntent.ContentHandler>(0)); assertEquals(Collections.EMPTY_LIST, mIntent.handlers()); } public void testWithHandler() { mIntent.withHandler(new ContentIntent.ContentHandler("TestHandler", new Intent())); final List<ContentIntent.ContentHandler> handlers = mIntent.handlers(); assertNotNull(handlers); assertTrue(handlers.size() == 1); assertEquals("TestHandler", handlers.get(0).name()); } public void testClearHandlers() { mIntent.withHandler(new ContentIntent.ContentHandler("TestHandler", new Intent())); mIntent.clearHandlers(); assertEquals(Collections.EMPTY_LIST, mIntent.handlers()); mIntent.clearHandlers(); assertEquals(Collections.EMPTY_LIST, mIntent.handlers()); } public void testDefaultUri() { assertNull(mIntent.uri()); } public void testInputFile() { mIntent.input(new File("TestFile")); final Uri input = mIntent.uri(); assertNotNull(input); assertEquals(Uri.fromFile(new File("TestFile")), input); } public void testInputNullFile() { mIntent.input((File) null); assertNull(mIntent.uri()); } public void testInputUri() { mIntent.dataType(MimeType.IMAGE_JPEG); mIntent.input(Uri.parse("content://android/data/images/lion.jpg")); final Uri input = mIntent.uri(); assertNotNull(input); assertEquals(Uri.parse("content://android/data/images/lion.jpg"), input); // Data type is null as we did not specify it after the input. assertNull(mIntent.dataType()); } public void testInputNullUri() { mIntent.input((Uri) null); assertNull(mIntent.uri()); } public void testOutputFile() { mIntent.output(new File("TestFile")); final Uri output = mIntent.uri(); assertNotNull(output); assertEquals(Uri.fromFile(new File("TestFile")), output); } public void testOutputNullFile() { mIntent.output((File) null); assertNull(mIntent.uri()); } public void testOutputUri() { mIntent.output(Uri.parse("content://android/data/images/lion.jpg")); final Uri output = mIntent.uri(); assertNotNull(output); assertEquals(Uri.parse("content://android/data/images/lion.jpg"), output); assertNull(mIntent.dataType()); } public void testOutputNullUri() { mIntent.output((Uri) null); assertNull(mIntent.uri()); } public void testDefaultDataType() { assertNull(mIntent.dataType()); } public void testDataType() { mIntent.dataType(MimeType.AUDIO_MP3); assertEquals(MimeType.AUDIO_MP3, mIntent.dataType()); } public void testBuildWithInputUri() { mIntent.input(Uri.parse("content://android/data/images/lion.jpg")); mIntent.dataType(MimeType.IMAGE_JPEG); final Intent intent = mIntent.build(); assertNotNull(intent); assertEquals(Intent.ACTION_VIEW, intent.getAction()); assertEquals(Uri.parse("content://android/data/images/lion.jpg"), intent.getData()); assertEquals(MimeType.IMAGE_JPEG, intent.getType()); } public void testBuildWithOutputUri() { mIntent.output(Uri.parse("content://android/data/images/lion.jpg")); assertBuildThrowsExceptionWithCause(mIntent, "No input Uri specified."); } public void testBuildWithoutUri() { assertBuildThrowsExceptionWithCause(mIntent, "No input Uri specified."); } public void testBuildWithoutDataType() { mIntent.input(Uri.parse("content://android/data/images/lion.jpg")); assertBuildThrowsExceptionWithCause(mIntent, "No MIME type specified for input Uri."); } static void assertFileRelativePath(String expected, File file) { assertEquals(TestsConfig.STORAGE_BASE_PATH + expected, file.getPath().replace(file.getName(), "")); } static void assertFilePath(String expected, File file) { assertEquals(TestsConfig.STORAGE_BASE_PATH + expected, file.getPath()); } static final class ContentIntentImpl extends ContentIntent<ContentIntentImpl> { public ContentIntentImpl(@NonNull Activity activity) { super(activity); } public ContentIntentImpl(@NonNull Fragment fragment) { super(fragment); } @Override public ContentIntentImpl withDefaultHandlers() { return this; } } }