@Override
 protected void tearDown() throws Exception {
   UpdateService.setCrawlerToUse(null);
   PreferenceManager.getDefaultSharedPreferences(getActivity())
       .edit()
       .clear()
       .commit(); // Reset default preferences!!
   PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, true);
   super.tearDown();
 }
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   populateCourseList();
   UpdateService.setCrawlerToUse(SimpleMockCrawler.class);
   solo = new Solo(getInstrumentation(), getActivity());
   DailyListener.setNewPendingIntentAndCancelOld(getActivity(), null); // Cancel schedule
   CourseContainer.instance().deleteObservers();
   PreferenceManager.getDefaultSharedPreferences(getActivity())
       .edit()
       .clear()
       .commit(); // Reset default preferences!!
   PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, true);
 }
 public void testAddCourseCrawlerError() {
   UpdateService.setCrawlerToUse(NullCrawler.class);
   solo.clickOnView(getActivity().findViewById(R.id.add));
   solo.enterText(0, "Course");
   solo.clickOnEditText(0);
   solo.sendKey(Solo.ENTER);
   assertTrue("Error dialog not shown", solo.searchText(solo.getString(R.string.error)));
   assertTrue("Error message not shown", solo.searchText(solo.getString(R.string.search_error)));
   solo.clickOnButton(0);
   assertFalse(
       "Error shows even after clicking ok", solo.searchText(solo.getString(R.string.error)));
   solo.goBack();
   solo.goBack();
 }
  public void testAddCourse() {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
      PreferenceManager.getDefaultSharedPreferences(getActivity())
          .edit()
          .putBoolean("pref_use_android_calendar", true)
          .putString("pref_android_calendar_to_use", "1")
          .commit();
    }
    UpdateService.setCrawlerToUse(SimpleMockCrawler.class);
    solo.clickOnView(getActivity().findViewById(R.id.add));
    int oldsize = CourseContainer.instance().size();
    solo.clickOnEditText(0);
    solo.enterText(0, "Course");
    solo.clickOnEditText(0);
    solo.sendKey(Solo.ENTER);
    solo.waitForText("THE COURSE #2", 1, 5);
    solo.clickOnText("THE COURSE #2");
    solo.waitForText("Courses", 1, 5);
    assertEquals(oldsize + 1, CourseContainer.instance().size());
    assertTrue(solo.searchText("THE COURSE #2", 1, true));
    Exam exam = CourseContainer.instance().get(CourseContainer.instance().size() - 1).exams.first();
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
      assertFalse("Exam of added course should have a created event", exam.event_id == -1);
    }

    oldsize = CourseContainer.instance().size();
    solo.clickOnView(getActivity().findViewById(R.id.add));
    solo.enterText(0, "Course");
    solo.clickOnEditText(0);
    solo.sendKey(Solo.ENTER);
    solo.waitForText("THE COURSE #2", 1, 5);
    solo.clickOnText("THE COURSE #2");
    assertEquals(
        "You should not be able to add the same course twice",
        oldsize,
        CourseContainer.instance().size());

    solo.goBack();
    solo.goBack();
  }