package com.thebluealliance.androidclient.notifications;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.thebluealliance.androidclient.DefaultTestRunner;
import com.thebluealliance.androidclient.R;
import com.thebluealliance.androidclient.activities.ViewEventActivity;
import com.thebluealliance.androidclient.adapters.ViewEventFragmentPagerAdapter;
import com.thebluealliance.androidclient.datafeed.framework.ModelMaker;
import com.thebluealliance.androidclient.gcm.notifications.NotificationTypes;
import com.thebluealliance.androidclient.gcm.notifications.ScheduleUpdatedNotification;
import com.thebluealliance.androidclient.helpers.MyTBAHelper;
import com.thebluealliance.androidclient.models.StoredNotification;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import java.text.DateFormat;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(DefaultTestRunner.class)
public class ScheduleUpdatedNotificationTest {
private Context mContext;
private ScheduleUpdatedNotification mNotification;
private JsonObject mData;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application.getApplicationContext();
mData = ModelMaker.getModel(JsonObject.class, "notification_schedule_updated");
mNotification = new ScheduleUpdatedNotification(mData.toString());
}
@Test
public void testParseData() {
mNotification.parseMessageData();
assertEquals(mNotification.getEventKey(), "2015ausy");
assertEquals(mNotification.getEventName(), "Australia Regional");
assertNotNull(mNotification.getMatchTime());
assertEquals(mNotification.getMatchTime().getAsInt(), 1397330280);
}
@Test(expected = JsonParseException.class)
public void testNoEventKey() {
mData.remove("event_key");
mNotification = new ScheduleUpdatedNotification(mData.toString());
mNotification.parseMessageData();
}
@Test(expected = JsonParseException.class)
public void testNoEventName() {
mData.remove("event_name");
mNotification = new ScheduleUpdatedNotification(mData.toString());
mNotification.parseMessageData();
}
@Test
public void testBuildNotification() {
mNotification.parseMessageData();
Notification notification = mNotification.buildNotification(mContext, null);
assertNotNull(notification);
long scheduledStartTimeUNIX = mNotification.getMatchTime().getAsLong();
Date scheduledStartTime = new Date(scheduledStartTimeUNIX * 1000);
DateFormat format = android.text.format.DateFormat.getTimeFormat(mContext);
String startTime = format.format(scheduledStartTime);
StoredNotification stored = mNotification.getStoredNotification();
assertNotNull(stored);
assertEquals(stored.getType(), NotificationTypes.SCHEDULE_UPDATED);
assertEquals(stored.getTitle(), mContext.getString(R.string.notification_schedule_updated_title, "AUSY"));
assertEquals(stored.getBody(), mContext.getString(R.string.notification_schedule_updated_with_time, "Australia", startTime));
assertEquals(stored.getMessageData(), mData.toString());
assertEquals(stored.getIntent(), MyTBAHelper.serializeIntent(mNotification.getIntent(mContext)));
assertNotNull(stored.getTime());
}
@Test
public void testGetIntent() {
mNotification.parseMessageData();
Intent intent = mNotification.getIntent(mContext);
assertNotNull(intent);
assertEquals(intent.getComponent().getClassName(), "com.thebluealliance.androidclient.activities.ViewEventActivity");
assertEquals(intent.getStringExtra(ViewEventActivity.EVENTKEY), mNotification.getEventKey());
assertEquals(intent.getIntExtra(ViewEventActivity.TAB, -1), ViewEventFragmentPagerAdapter.TAB_MATCHES);
}
}