Java tutorial
/* * Copyright 2013 Devaholic, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). */ package com.devaholic.android.test.activities.camera; import android.widget.Button; import android.widget.ImageView; import com.devaholic.android.R; import com.devaholic.android.activities.camera.CameraThroughAPI; import com.devaholic.android.cameraControls.CameraSurface; import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowImageView; import org.robolectric.shadows.ShadowTextView; import org.robolectric.util.ActivityController; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; /** * Test for class for CameraThroughAPITest */ @RunWith(RobolectricTestRunner.class) public class CameraThroughAPITest { private ActivityController controller = Robolectric.buildActivity(CameraThroughAPI.class); private void checkCameraPreviewIsNotStarted(CameraThroughAPI cameraThroughAPI) throws IllegalAccessException { Boolean isCameraStarted = (Boolean) FieldUtils.readDeclaredField(cameraThroughAPI, "isCameraStarted", true); assertThat("the Camera preview should not be started at this point", !isCameraStarted); } private void checkCameraPreviewIsStarted(CameraThroughAPI cameraThroughAPI) throws IllegalAccessException { boolean isCameraStarted = (Boolean) FieldUtils.readDeclaredField(cameraThroughAPI, "isCameraStarted", true); assertThat("the Camera preview should be started at this point", isCameraStarted); } private void clickStartPreviewModeButton(CameraThroughAPI cameraThroughAPI) { Button button = (Button) cameraThroughAPI.findViewById(R.id.startCameraAPI); assertThat("There is no button to trigger the Camera preview", button, notNullValue()); Robolectric.clickOn(button); } /** * Test to check the layout of the Home screen activity */ @Test public void layoutTest() { CameraThroughAPI cameraThroughAPI = (CameraThroughAPI) controller.create().get(); ShadowTextView text = (ShadowTextView) Robolectric .shadowOf(cameraThroughAPI.findViewById(R.id.takePicture)); assertThat("There is no button to trigger the Camera through API", text, notNullValue()); String expected = "Take a picture"; assertThat(String.format("Text for Button 'Take a picture' is wrong. Expected [%s] but found [%s]", expected, text.innerText()), text.innerText(), equalTo(expected)); text = (ShadowTextView) Robolectric.shadowOf(cameraThroughAPI.findViewById(R.id.startCameraAPI)); assertThat("There is no button to start the preview of the Camera", text, notNullValue()); expected = "Start Preview"; assertThat(String.format("Text for Button 'Start Preview' is wrong. Expected [%s] but found [%s]", expected, text.innerText()), text.innerText(), equalTo(expected)); ShadowImageView img = (ShadowImageView) Robolectric .shadowOf(cameraThroughAPI.findViewById(R.id.imgPreviewApi)); assertThat("There is image viewer to show the picture taken", img, notNullValue()); } /** * Open Camera test */ @Test public void openCameraTest() throws IllegalAccessException { CameraThroughAPI cameraThroughAPI = (CameraThroughAPI) controller.get(); checkCameraPreviewIsNotStarted(cameraThroughAPI); ImageView imgView = (ImageView) FieldUtils.readDeclaredField(cameraThroughAPI, "imgView", true); assertThat("Image viewer for the picture should be null at this point", imgView, nullValue()); CameraSurface cameraSurface = (CameraSurface) FieldUtils.readDeclaredField(cameraThroughAPI, "cameraSurface", true); assertThat("CameraSurface should not be started", cameraSurface, nullValue()); cameraThroughAPI = (CameraThroughAPI) controller.create().get(); checkCameraPreviewIsNotStarted(cameraThroughAPI); imgView = (ImageView) FieldUtils.readDeclaredField(cameraThroughAPI, "imgView", true); assertThat("There is no image viewer for the picture", imgView, notNullValue()); cameraSurface = (CameraSurface) FieldUtils.readDeclaredField(cameraThroughAPI, "cameraSurface", true); assertThat("CameraSurface did not start", cameraSurface, notNullValue()); } /** * Start Preview Test */ @Test public void startPreviewTest() throws IllegalAccessException { CameraThroughAPI cameraThroughAPI = (CameraThroughAPI) controller.create().start().visible().get(); checkCameraPreviewIsNotStarted(cameraThroughAPI); clickStartPreviewModeButton(cameraThroughAPI); checkCameraPreviewIsStarted(cameraThroughAPI); } /** * HomeActivity English Test */ @Test @Config(qualifiers = "en") public void mainActivityEnglishTest() { String expected = "Custom Camera"; String found = Robolectric.application.getString(R.string.custom_camera); assertThat(String.format("English Text for 'custom_camera' is wrong. Expected [%s] but Found [%s]", expected, found), found, equalTo(expected)); expected = "Take a picture"; found = Robolectric.application.getString(R.string.triggerCameraIntent); assertThat(String.format("English Text for 'triggerCameraIntent' is wrong. Expected [%s] but Found [%s]", expected, found), found, equalTo(expected)); expected = "You need to start the preview mode First"; found = Robolectric.application.getString(R.string.startPreviewMode); assertThat(String.format("English Text for 'startPreviewMode' is wrong. Expected [%s] but Found [%s]", expected, found), found, equalTo(expected)); expected = "Image viewer that shows the taken picture"; found = Robolectric.application.getString(R.string.imageViewerDesc); assertThat(String.format("English Text for 'imageViewerDesc' is wrong. Expected [%s] but Found [%s]", expected, found), found, equalTo(expected)); } /** * HomeActivity Spanish Test */ @Test @Config(qualifiers = "es") public void mainActivitySpanishTest() { String expected = "Cmara Customizada"; String found = Robolectric.application.getString(R.string.custom_camera); assertThat(String.format("English Text for 'custom_camera' is wrong. Expected [%s] but Found [%s]", expected, found), found, equalTo(expected)); expected = "Sacar foto"; found = Robolectric.application.getString(R.string.triggerCameraIntent); assertThat(String.format("English Text for 'triggerCameraIntent' is wrong. Expected [%s] but Found [%s]", expected, found), found, equalTo(expected)); expected = "Necesitar iniciar el modo privisualizacin primero"; found = Robolectric.application.getString(R.string.startPreviewMode); assertThat(String.format("English Text for 'startPreviewMode' is wrong. Expected [%s] but Found [%s]", expected, found), found, equalTo(expected)); expected = "Visualizador de la imagen tomada"; found = Robolectric.application.getString(R.string.imageViewerDesc); assertThat(String.format("English Text for 'imageViewerDesc' is wrong. Expected [%s] but Found [%s]", expected, found), found, equalTo(expected)); } }