/*
 * Copyright 2016 Eric Peterson
 *
 * 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 co.ericp.flashlight;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.MediumTest;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

@RunWith(AndroidJUnit4.class)
@MediumTest
public class MainActivityTest {

    Flashlight mockedFlashlight;

    @Rule
    public final ActivityTestRule<MainActivity> activityRule =
            new ActivityTestRule<>(MainActivity.class, false, false);

    @Before
    public void mockFlashlight() {
        mockedFlashlight = mock(Flashlight.class);
        FlashlightProvider.singleton = mockedFlashlight;
    }

    @Test
    public void callsToggle() throws Exception {
        activityRule.launchActivity(null);
        verify(mockedFlashlight).toggle();
    }

    @Test
    public void finishesAutomatically() throws Exception {
        MainActivity activity = activityRule.launchActivity(null);
        assertTrue(activity.isFinishing());
    }

    @After
    public void clearSingleton() {
        FlashlightProvider.clear();
    }

}

@@ -618,24 +618,115 @@
 lashlight;%0A%0A
+import android.hardware.Camera;%0Aimport android.os.Build;%0Aimport android.provider.Settings;%0A
 import andro
@@ -895,16 +895,41 @@
 .After;%0A
+import org.junit.Assert;%0A
 import o
@@ -1047,31 +1047,120 @@
 org.
-junit.Assert.assertTrue
+hamcrest.Matchers.lessThan;%0Aimport static org.junit.Assert.assertTrue;%0Aimport static org.junit.Assume.assumeThat
 ;%0Aim
@@ -1990,24 +1990,486 @@
 ());%0A    %7D%0A%0A
+    @Test%0A    public void retainsCameraAfterGC() throws Exception %7B%0A        assumeThat(Build.VERSION.SDK_INT, lessThan(Build.VERSION_CODES.M)); // Legacy test only%0A%0A        activityRule.launchActivity(null);%0A        Thread.sleep(500);%0A%0A        System.gc();%0A        Thread.sleep(1500);%0A%0A        try %7B%0A            Camera.open();%0A            Assert.fail(%22Camera should not be available%22);%0A        %7D catch (RuntimeException e) %7B%0A            // Pass%0A        %7D%0A    %7D%0A%0A
     @After%0A 

Add test to retain camera on legacy devices