package openfoodfacts.github.scrachx.openfood.utils;

import android.content.Context;
import android.content.pm.PackageManager;

import org.junit.Test;

import openfoodfacts.github.scrachx.openfood.R;
import openfoodfacts.github.scrachx.openfood.models.Product;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class UtilsTest {
    @Test
    public void getImageGrade() {
        final Product mockProduct = mock(Product.class);

        when(mockProduct.getNutritionGradeFr()).thenReturn("a");
        assertEquals(R.drawable.nnc_a, Utils.getImageGrade(mockProduct));

        when(mockProduct.getNutritionGradeFr()).thenReturn("b");
        assertEquals(R.drawable.nnc_b, Utils.getImageGrade(mockProduct));

        when(mockProduct.getNutritionGradeFr()).thenReturn("c");
        assertEquals(R.drawable.nnc_c, Utils.getImageGrade(mockProduct));

        when(mockProduct.getNutritionGradeFr()).thenReturn("d");
        assertEquals(R.drawable.nnc_d, Utils.getImageGrade(mockProduct));

        when(mockProduct.getNutritionGradeFr()).thenReturn("e");
        assertEquals(R.drawable.nnc_e, Utils.getImageGrade(mockProduct));

        when(mockProduct.getNutritionGradeFr()).thenReturn("");
        assertEquals(Utils.NO_DRAWABLE_RESOURCE, Utils.getImageGrade(mockProduct));

        when(mockProduct.getNutritionGradeFr()).thenReturn(null);
        assertEquals(Utils.NO_DRAWABLE_RESOURCE, Utils.getImageGrade(mockProduct));
    }

    @Test
    public void getRoundNumber() {
        /* TODO: Fix method before testing
        assertEquals("?", Utils.getRoundNumber(""));
        assertEquals("?", Utils.getRoundNumber(null));
        // TODO: Something for string
        // assertEquals("?", Utils.getRoundNumber("test"));
        assertEquals("0", Utils.getRoundNumber("0"));
        assertEquals("0", Utils.getRoundNumber(0));
        assertEquals("1", Utils.getRoundNumber(1));
        assertEquals("1.7", Utils.getRoundNumber(1.7f));
        assertEquals("1.75", Utils.getRoundNumber(1.75f));
        assertEquals("1.75", Utils.getRoundNumber(1.754f));
        assertEquals("1.76", Utils.getRoundNumber(1.756f));
        assertEquals("1.76", Utils.getRoundNumber(1.756f));

         */
    }

    @Test
    public void isHardwareCameraInstalled() {
        final Context mockCtx = mock(Context.class);
        final PackageManager mockPM = mock(PackageManager.class);

        when(mockCtx.getPackageManager()).thenReturn(mockPM);

        // Test for best condition
        when(mockPM.hasSystemFeature(PackageManager.FEATURE_CAMERA))
            .thenReturn(true);
        assertTrue(Utils.isHardwareCameraInstalled(mockCtx));

        // False condition
        when(mockPM.hasSystemFeature(PackageManager.FEATURE_CAMERA))
            .thenReturn(false);

        assertFalse(Utils.isHardwareCameraInstalled(mockCtx));
    }
}
@@ -2900,16 +2900,514 @@
 ckCtx));
+%0A%0A    String servingSize;%0A%0A    @Test%0A    public void getServingInOz_from_ml()%7B%0A        servingSize = %22100 ml%22;%0A%0A        assertEquals(%223.38 oz%22, Utils.getServingInOz(servingSize));%0A    %7D%0A%0A    @Test%0A    public void getServingInOz_from_cl()%7B%0A        servingSize = %22250 cl%22;%0A%0A        assertEquals(%2284.54 oz%22, Utils.getServingInOz(servingSize));%0A    %7D%0A%0A    @Test%0A    public void getServingInOz_from_l()%7B%0A        servingSize = %223 l%22;%0A%0A        assertEquals(%22101.44 oz%22, Utils.getServingInOz(servingSize));
 %0A    %7D%0A%7D

Test for Utils.getServingInOz