package org.commcare.android.tests.activities;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.widget.TextView;

import org.commcare.CommCareApplication;
import org.commcare.CommCareNoficationManager;
import org.commcare.CommCareTestApplication;
import org.commcare.activities.InstallArchiveActivity;
import org.commcare.activities.UpdateActivity;
import org.commcare.android.CommCareTestRunner;
import org.commcare.android.util.TestAppInstaller;
import org.commcare.dalvik.R;
import org.javarosa.core.services.locale.Localization;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

/**
 * Test performing updates through the update activity
 *
 * @author Phillip Mates (pmates@dimagi.com)
 */
@Config(application = CommCareTestApplication.class)
@RunWith(CommCareTestRunner.class)
public class UpdateActivityTest {
    @Before
    public void setup() {
        TestAppInstaller.installAppAndLogin(
                "jr://resource/commcare-apps/update_tests/base_app/profile.ccpr",
                "test", "123");
    }

    /**
     * Use the update activity to update to a ccz with an invalid suite file.
     * Assert that an error is shown and pinned notification is created w/ more details
     */
    @Test
    public void invalidUpdateTest() {
        String invalidUpdateReference = "jr://resource/commcare-apps/update_tests/invalid_suite_update/profile.ccpr";

        // start the update activity
        Intent updateActivityIntent =
                new Intent(RuntimeEnvironment.application, UpdateActivity.class);

        UpdateActivity updateActivity =
                Robolectric.buildActivity(UpdateActivity.class)
                        .withIntent(updateActivityIntent).setup().get();

        // click the 'offline install' menu item
        ShadowActivity shadowActivity = Shadows.shadowOf(updateActivity);
        shadowActivity.clickMenuItem(UpdateActivity.MENU_UPDATE_FROM_CCZ);

        // make sure there are no pinned notifications
        NotificationManager notificationManager =
                (NotificationManager)RuntimeEnvironment.application.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = Shadows.shadowOf(notificationManager).getNotification(CommCareNoficationManager.MESSAGE_NOTIFICATION);
        assertNull(notification);

        // mock receiving the offline app reference and start the update
        Intent referenceIntent = new Intent();
        referenceIntent.putExtra(InstallArchiveActivity.ARCHIVE_JR_REFERENCE, invalidUpdateReference);
        shadowActivity.receiveResult(shadowActivity.getNextStartedActivity(), Activity.RESULT_OK, referenceIntent);

        Robolectric.flushBackgroundThreadScheduler();
        Robolectric.flushForegroundThreadScheduler();

        // assert that we get the right error message
        String errorMessage = (String)((TextView)updateActivity.findViewById(R.id.update_progress_text)).getText();
        assertEquals(Localization.get("updates.check.failed"), errorMessage);

        // check that a pinned notification was created for invalid update
        // NOTE: it is way more work to assert the notification body is correct, so skip over that
        notification = Shadows.shadowOf(notificationManager).getNotification(CommCareNoficationManager.MESSAGE_NOTIFICATION);
        assertNotNull(notification);
    }
}

@@ -1781,16 +1781,32 @@
 erence =
+%0A               
  %22jr://r
@@ -2423,17 +2423,17 @@
      // 
-m
+M
 ake sure
@@ -2466,16 +2466,32 @@
 ications
+ before we start
 %0A       
@@ -2596,24 +2596,49 @@
 .application
+%0A                        
 .getSystemSe
@@ -2674,16 +2674,100 @@
 RVICE);%0A
+        notificationManager.cancel(CommCareNoficationManager.MESSAGE_NOTIFICATION);%0A
         
@@ -2823,32 +2823,49 @@
 ficationManager)
+%0A                
 .getNotification
@@ -3269,16 +3269,32 @@
 SULT_OK,
+%0A               
  referen
@@ -3531,16 +3531,33 @@
 Activity
+%0A                
 .findVie
@@ -3914,16 +3914,33 @@
 Manager)
+%0A                
 .getNoti

fix failing test