{"id":493,"date":"2015-10-19T14:02:03","date_gmt":"2015-10-19T14:02:03","guid":{"rendered":"http:\/\/installingcats.com\/?p=493"},"modified":"2015-10-19T14:02:03","modified_gmt":"2015-10-19T14:02:03","slug":"robolectric-reference","status":"publish","type":"post","link":"https:\/\/installingcats.com\/2015\/10\/19\/robolectric-reference\/","title":{"rendered":"Robolectric reference"},"content":{"rendered":"

Robolectric Android testing Fragments and more<\/p>\n

private ReminderManagerFragment startReminderManagerFragment() {\n    final ReminderManagerFragment fragment = new ReminderManagerFragment();\n    SupportFragmentTestUtil.startVisibleFragment(fragment);\n    return fragment;\n}<\/pre>\n
<\/pre>\n
@Test\npublic void reminderManagerTest() throws Exception {\n    \/\/ by default there should be 3 Reminder rows\n    ReminderManagerFragment fragment = startReminderManagerFragment();\n    assertThat(fragment).isNotNull();\n    Button addButton = (Button) fragment.getView().findViewById(R.id.reminder_add_button);\n    assertThat(addButton).isNotNull();\n\n    List<Reminder> reminders = getReminders(TestValues.BillyUser);\n    assertThat(reminders.size()).isEqualTo(3);\n\n    \/\/ add a reminder, assert reminders = 4\n    addButton.performClick();\n\n    \/\/assertThat(reminders.size()).isEqualTo(4);\n    TimePickerFragment pickerFragment =\n            (TimePickerFragment) fragment.getFragmentManager()\n                    .findFragmentByTag(ReminderManagerFragment.TIME_DIALOG);\n    assertThat(pickerFragment).isNotNull();\n\n    reminders = getReminders(TestValues.BillyUser);\n    assertThat(reminders.size()).isEqualTo(4);\n\n    AlertDialog pickerDialog = (AlertDialog) pickerFragment.getDialog();\n\n    assertThat(pickerDialog).isNotNull();\n    assertThat(pickerDialog.getButton(AlertDialog.BUTTON_POSITIVE)).isNotNull();\n\n    Button pickerOKButton = (Button) pickerDialog.getButton(AlertDialog.BUTTON_POSITIVE);\n    pickerOKButton.performClick();\n\n    \/\/ check that an alarm was set\n    ShadowAlarmManager shadowAlarmManager = shadowOf(AlarmFactory.getAlarmManager(mCtx));\n    \/\/ shadow AlarmManager should return 0 alarms\n    List<ShadowAlarmManager.ScheduledAlarm> shadowAlarms = shadowAlarmManager.getScheduledAlarms();\n    Log.i(TAG, \"alarms currently set: \" + shadowAlarms.size());\n    assertThat(shadowAlarms.size()).isGreaterThan(0);<\/pre>\n

 <\/p>\n

Try changing TimePicker Hour and Minutes and resubmit AlertDialog (TimePickerDialog), they key method being ((TimePickerDialog) pickerDialog).updateTime(hh,mm);<\/p>\n

\/\/ try adding a second reminder & alarm\naddButton.performClick();\n\npickerFragment =\n        (TimePickerFragment) fragment.getFragmentManager()\n                .findFragmentByTag(ReminderManagerFragment.TIME_DIALOG);\n\nassertThat(pickerFragment).isNotNull();\n\nreminders = getReminders(TestValues.BillyUser);\nassertThat(reminders.size()).isEqualTo(5);\n\npickerDialog = (AlertDialog) pickerFragment.getDialog();\n\n\nassertThat(pickerDialog).isNotNull();\n\n((TimePickerDialog) pickerDialog).updateTime(18, 30);\n\npickerOKButton = (Button) pickerDialog.getButton(AlertDialog.BUTTON_POSITIVE);\npickerOKButton.performClick();\n\nshadowAlarms = shadowAlarmManager.getScheduledAlarms();\nfor (ShadowAlarmManager.ScheduledAlarm sAlarm :\n        shadowAlarms) {\n    Log.i(TAG, Dates.getBasicDate(new Date(sAlarm.triggerAtTime)) + \" \" +\n            sAlarm.operation.toString());\n}\nLog.i(TAG, \"alarms currently set: \" + shadowAlarms.size());\nassertThat(shadowAlarms.size()).isGreaterThan(1);<\/pre>\n

More to come<\/p>\n","protected":false},"excerpt":{"rendered":"

Robolectric Android testing Fragments and more private ReminderManagerFragment startReminderManagerFragment() { final ReminderManagerFragment fragment = new ReminderManagerFragment(); SupportFragmentTestUtil.startVisibleFragment(fragment); return fragment; } @Test public void reminderManagerTest() throws Exception { \/\/ by default there should be 3 Reminder rows ReminderManagerFragment fragment = startReminderManagerFragment(); assertThat(fragment).isNotNull(); Button addButton = (Button) fragment.getView().findViewById(R.id.reminder_add_button); assertThat(addButton).isNotNull(); List<Reminder> reminders = getReminders(TestValues.BillyUser); assertThat(reminders.size()).isEqualTo(3); \/\/ add a […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,246],"tags":[245],"_links":{"self":[{"href":"https:\/\/installingcats.com\/wp-json\/wp\/v2\/posts\/493"}],"collection":[{"href":"https:\/\/installingcats.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/installingcats.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/installingcats.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/installingcats.com\/wp-json\/wp\/v2\/comments?post=493"}],"version-history":[{"count":0,"href":"https:\/\/installingcats.com\/wp-json\/wp\/v2\/posts\/493\/revisions"}],"wp:attachment":[{"href":"https:\/\/installingcats.com\/wp-json\/wp\/v2\/media?parent=493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/installingcats.com\/wp-json\/wp\/v2\/categories?post=493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/installingcats.com\/wp-json\/wp\/v2\/tags?post=493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}