@Test
 public void testNullInitArguments() throws Exception {
   final Date validDate = today.getTime();
   try {
     view.init(validDate, validDate, locale) //
         .inMode(SINGLE) //
         .withSelectedDate(null);
     fail("Should not have been able to pass in a null startDate");
   } catch (IllegalArgumentException expected) {
   }
   try {
     view.init(null, validDate, locale) //
         .inMode(SINGLE) //
         .withSelectedDate(validDate);
     fail("Should not have been able to pass in a null minDate");
   } catch (IllegalArgumentException expected) {
   }
   try {
     view.init(validDate, null, locale) //
         .inMode(SINGLE) //
         .withSelectedDate(validDate);
     fail("Should not have been able to pass in a null maxDate");
   } catch (IllegalArgumentException expected) {
   }
   try {
     view.init(validDate, validDate, null) //
         .inMode(SINGLE) //
         .withSelectedDate(validDate);
     fail("Should not have been able to pass in a null locale");
   } catch (IllegalArgumentException expected) {
   }
 }
  @Test
  public void testOnDateConfiguredListener() {
    final Calendar testCal = Calendar.getInstance(locale);
    view.setDateSelectableFilter(
        new CalendarPickerView.DateSelectableFilter() {
          @Override
          public boolean isDateSelectable(Date date) {
            testCal.setTime(date);
            int dayOfWeek = testCal.get(DAY_OF_WEEK);
            return dayOfWeek > 1 && dayOfWeek < 7;
          }
        });
    view.init(minDate, maxDate, locale) //
        .inMode(SINGLE) //
        .withSelectedDate(today.getTime());
    Calendar jumpToCal = Calendar.getInstance(locale);
    jumpToCal.setTime(today.getTime());
    jumpToCal.add(MONTH, 2);
    jumpToCal.set(DAY_OF_WEEK, 1);
    boolean wasAbleToSetDate = view.selectDate(jumpToCal.getTime());
    assertThat(wasAbleToSetDate).isFalse();

    jumpToCal.set(DAY_OF_WEEK, 2);
    wasAbleToSetDate = view.selectDate(jumpToCal.getTime());
    assertThat(wasAbleToSetDate).isTrue();
  }
  @Test
  public void testInitWithoutHighlightingCells() {
    view.init(minDate, maxDate, locale) //
        .inMode(SINGLE);

    assertThat(view.highlightedCals).hasSize(0);
    assertThat(view.highlightedCells).hasSize(0);
  }
 @Test
 public void selectDateReturnsTrueForDateInRange() {
   view.init(minDate, maxDate, locale) //
       .inMode(SINGLE) //
       .withSelectedDate(today.getTime());
   Calendar inRange = buildCal(2013, FEBRUARY, 1);
   boolean wasAbleToSetDate = view.selectDate(inRange.getTime());
   assertThat(wasAbleToSetDate).isTrue();
 }
 @Test
 public void testLocaleSetting() throws Exception {
   view.init(minDate, maxDate, Locale.GERMAN);
   MonthView monthView = (MonthView) view.getAdapter().getView(1, null, null);
   CalendarRowView header = (CalendarRowView) monthView.grid.getChildAt(0);
   TextView firstDay = (TextView) header.getChildAt(0);
   assertThat(firstDay).hasTextString("Mo"); // Montag = Monday
   assertThat(monthView.title).hasTextString("Dezember 2012");
 }
 @Test
 public void testInitMidyear() throws Exception {
   Calendar may2012 = buildCal(2012, MAY, 1);
   Calendar may2013 = buildCal(2013, MAY, 1);
   view.init(may2012.getTime(), may2013.getTime(), locale) //
       .inMode(SINGLE) //
       .withSelectedDate(may2012.getTime());
   assertThat(view.months).hasSize(12);
 }
 @Test
 public void testInitDecember() throws Exception {
   Calendar dec2012 = buildCal(2012, DECEMBER, 1);
   Calendar dec2013 = buildCal(2013, DECEMBER, 1);
   view.init(dec2012.getTime(), dec2013.getTime(), locale) //
       .inMode(SINGLE) //
       .withSelectedDate(dec2012.getTime());
   assertThat(view.months).hasSize(12);
 }
 @Test
 public void testShowingOnlyOneMonth() throws Exception {
   Calendar feb1 = buildCal(2013, FEBRUARY, 1);
   Calendar mar1 = buildCal(2013, MARCH, 1);
   view.init(feb1.getTime(), mar1.getTime(), locale) //
       .inMode(SINGLE) //
       .withSelectedDate(feb1.getTime());
   assertThat(view.months).hasSize(1);
 }
 @Test
 public void testInitJanuary() throws Exception {
   Calendar jan2012 = buildCal(2012, JANUARY, 1);
   Calendar jan2013 = buildCal(2013, JANUARY, 1);
   view.init(jan2012.getTime(), jan2013.getTime(), locale) //
       .inMode(SINGLE) //
       .withSelectedDate(jan2012.getTime());
   assertThat(view.months).hasSize(12);
 }
 @Test
 public void selectDateDoesntSelectDisabledCell() {
   view.init(minDate, maxDate, locale) //
       .inMode(SINGLE) //
       .withSelectedDate(today.getTime());
   Calendar jumpToCal = buildCal(2013, FEBRUARY, 1);
   boolean wasAbleToSetDate = view.selectDate(jumpToCal.getTime());
   assertThat(wasAbleToSetDate).isTrue();
   assertThat(view.selectedCells.get(0).isSelectable()).isTrue();
 }
  @Test
  public void testInitSingleWithMultipleSelections() throws Exception {
    List<Date> selectedDates = new ArrayList<Date>();
    selectedDates.add(minDate);
    // This one should work.
    view.init(minDate, maxDate, locale) //
        .inMode(SINGLE) //
        .withSelectedDates(selectedDates);

    // Now add another date and try init'ing again in SINGLE mode.
    Calendar secondSelection = buildCal(2012, NOVEMBER, 17);
    selectedDates.add(secondSelection.getTime());
    try {
      view.init(minDate, maxDate, locale) //
          .inMode(SINGLE) //
          .withSelectedDates(selectedDates);
      fail("Should not have been able to init() with SINGLE mode && multiple selected dates");
    } catch (IllegalArgumentException expected) {
    }
  }
  @Test
  public void testRangeWithTwoInitialSelections() throws Exception {
    Calendar nov18 = buildCal(2012, NOVEMBER, 18);
    Calendar nov24 = buildCal(2012, NOVEMBER, 24);
    List<Date> selectedDates = Arrays.asList(nov18.getTime(), nov24.getTime());
    view.init(minDate, maxDate, locale) //
        .inMode(RANGE) //
        .withSelectedDates(selectedDates);
    assertRangeSelected();

    assertRangeSelectionBehavior();
  }
 @Test
 public void selectDateThrowsExceptionForDatesOutOfRange() {
   view.init(minDate, maxDate, locale) //
       .inMode(SINGLE) //
       .withSelectedDate(today.getTime());
   Calendar outOfRange = buildCal(2015, FEBRUARY, 1);
   try {
     view.selectDate(outOfRange.getTime());
     fail("selectDate should've blown up with an out of range date");
   } catch (IllegalArgumentException expected) {
   }
 }
 @Test
 public void testMinAndMaxMixup() throws Exception {
   final Date minDate = today.getTime();
   today.add(YEAR, -1);
   final Date maxDate = today.getTime();
   try {
     view.init(minDate, maxDate, locale) //
         .inMode(SINGLE) //
         .withSelectedDate(minDate);
     fail("Should not have been able to pass in a maxDate < minDate");
   } catch (IllegalArgumentException expected) {
   }
 }
  @Test
  public void testHighlightingCells() {
    final Calendar highlightedCal = buildCal(2012, NOVEMBER, 20);

    view.init(minDate, maxDate, locale)
        .inMode(SINGLE)
        .withHighlightedDate(highlightedCal.getTime());

    assertThat(view.highlightedCals).hasSize(1);
    assertThat(view.highlightedCells).hasSize(1);

    List<List<MonthCellDescriptor>> cells = getCells(NOVEMBER, 2012);
    assertThat(cells.get(3).get(2).isHighlighted()).isTrue();
  }
  @Test
  public void testMultiselectWithNoInitialSelections() throws Exception {
    view.init(minDate, maxDate, locale) //
        .inMode(MULTIPLE);
    assertThat(view.selectionMode).isEqualTo(MULTIPLE);
    assertThat(view.getSelectedDates()).isEmpty();

    view.selectDate(minDate);
    assertThat(view.getSelectedDates()).hasSize(1);

    Calendar secondSelection = buildCal(2012, NOVEMBER, 17);
    view.selectDate(secondSelection.getTime());
    assertThat(view.getSelectedDates()).hasSize(2);
    assertThat(view.getSelectedDates().get(1)).hasTime(secondSelection.getTimeInMillis());
  }
 @Test
 public void testRightToLeftLocale() throws Exception {
   view.init(minDate, maxDate, new Locale("iw", "IL"));
   MonthView monthView = (MonthView) view.getAdapter().getView(1, null, null);
   CalendarRowView header = (CalendarRowView) monthView.grid.getChildAt(0);
   TextView firstDay = (TextView) header.getChildAt(0);
   assertThat(firstDay).hasTextString("ש"); // Last day of the week (Saturday) is the first cell.
   CalendarRowView firstWeek = (CalendarRowView) monthView.grid.getChildAt(1);
   TextView firstDate = (TextView) firstWeek.getChildAt(0);
   assertThat(firstDate).hasTextString("1");
   CalendarRowView secondWeek = (CalendarRowView) monthView.grid.getChildAt(2);
   TextView secondDate = (TextView) secondWeek.getChildAt(6);
   assertThat(secondDate).hasTextString("2");
   assertThat(monthView.title).hasTextString("דצמבר 2012");
 }
 @Test
 public void testSelectedNotInRange() throws Exception {
   final Date minDate = today.getTime();
   today.add(YEAR, 1);
   final Date maxDate = today.getTime();
   today.add(YEAR, 1);
   Date selectedDate = today.getTime();
   try {
     view.init(minDate, maxDate, locale) //
         .inMode(SINGLE) //
         .withSelectedDate(selectedDate);
     fail("Should not have been able to pass in a selectedDate > maxDate");
   } catch (IllegalArgumentException expected) {
   }
   today.add(YEAR, -5);
   selectedDate = today.getTime();
   try {
     view.init(minDate, maxDate, locale) //
         .inMode(SINGLE) //
         .withSelectedDate(selectedDate);
     fail("Should not have been able to pass in a selectedDate < minDate");
   } catch (IllegalArgumentException expected) {
   }
 }
  @Test
  public void testRangeSelectionWithNoInitialSelection() throws Exception {
    view.init(minDate, maxDate, locale) //
        .inMode(RANGE);
    assertThat(view.selectedCals).hasSize(0);
    assertThat(view.selectedCells).hasSize(0);

    Calendar nov18 = buildCal(2012, NOVEMBER, 18);
    view.selectDate(nov18.getTime());
    assertOneDateSelected();

    Calendar nov24 = buildCal(2012, NOVEMBER, 24);
    view.selectDate(nov24.getTime());
    assertRangeSelected();

    assertRangeSelectionBehavior();
  }
 @Test
 public void testInitSeveralWithRangeSelections() throws Exception {
   List<Date> selectedDates = new ArrayList<Date>();
   Calendar firstSelection = buildCal(2012, NOVEMBER, 17);
   selectedDates.add(firstSelection.getTime());
   Calendar secondSelection = buildCal(2012, NOVEMBER, 21);
   selectedDates.add(secondSelection.getTime());
   Calendar thirdSelection = buildCal(2012, NOVEMBER, 25);
   selectedDates.add(thirdSelection.getTime());
   try {
     view.init(minDate, maxDate, locale) //
         .inMode(RANGE) //
         .withSelectedDates(selectedDates);
     fail("Should not have been able to init() with RANGE mode && three selected dates");
   } catch (IllegalArgumentException expected) {
   }
 }
  @Test
  public void testCellClickInterceptor() throws Exception {
    view.init(minDate, maxDate, Locale.getDefault());
    view.setCellClickInterceptor(
        new CalendarPickerView.CellClickInterceptor() {
          Calendar cal = Calendar.getInstance(locale);

          @Override
          public boolean onCellClicked(Date date) {
            cal.setTime(date);
            return cal.get(MONTH) == NOVEMBER && cal.get(DAY_OF_MONTH) == 18;
          }
        });
    Calendar jumpToCal = Calendar.getInstance(locale);
    jumpToCal.setTime(today.getTime());
    jumpToCal.set(DAY_OF_MONTH, 17);
    MonthCellDescriptor cellToClick =
        new MonthCellDescriptor(
            jumpToCal.getTime(),
            true,
            true,
            true,
            true,
            true,
            0,
            MonthCellDescriptor.RangeState.NONE);
    view.listener.handleClick(cellToClick);

    assertThat(view.selectedCals.get(0).get(DATE)).isEqualTo(17);

    jumpToCal.set(DAY_OF_MONTH, 18);
    cellToClick =
        new MonthCellDescriptor(
            jumpToCal.getTime(),
            true,
            true,
            true,
            true,
            true,
            0,
            MonthCellDescriptor.RangeState.NONE);
    view.listener.handleClick(cellToClick);

    assertThat(view.selectedCals.get(0).get(DATE)).isEqualTo(17);
  }
  @Test
  public void testSetShortWeekdays() throws Exception {
    String[] capitalDays = {"", "S", "M", "T", "W", "T", "F", "S"};

    Calendar cal = Calendar.getInstance(Locale.getDefault());
    assertThat(cal.getFirstDayOfWeek()).isEqualTo(Calendar.MONDAY);

    view.init(minDate, maxDate, Locale.getDefault()) //
        .setShortWeekdays(capitalDays);
    MonthView monthView = (MonthView) view.getAdapter().getView(1, null, null);
    CalendarRowView header = (CalendarRowView) monthView.grid.getChildAt(0);
    TextView firstDay = (TextView) header.getChildAt(0);
    assertThat(firstDay).hasTextString("M"); // Monday!
    TextView secondDay = (TextView) header.getChildAt(1);
    assertThat(secondDay).hasTextString("T"); // Tuesday!
    TextView thirdDay = (TextView) header.getChildAt(2);
    assertThat(thirdDay).hasTextString("W"); // Wednesday!
  }
  @Before
  public void setUp() throws Exception {
    activity = Robolectric.buildActivity(Activity.class).create().start().resume().get();
    locale = Locale.US;
    view = new CalendarPickerView(activity, null);
    today = Calendar.getInstance(locale);
    today.set(2012, NOVEMBER, 16, 0, 0);
    minDate = today.getTime();
    today.set(2013, NOVEMBER, 16, 0, 0);
    maxDate = today.getTime();
    today.set(2012, NOVEMBER, 16, 0, 0);
    Date startDate = today.getTime();
    view.init(minDate, maxDate, locale) //
        .inMode(SINGLE) //
        .withSelectedDate(startDate);

    // Do not change the internal state of the CalendarPickerView until init() has run.
    view.today.setTime(startDate);
  }
  @Test
  public void testFirstDayOfWeekIsMonday() throws Exception {
    Locale greatBritain = new Locale("en", "GB");

    // Verify that firstDayOfWeek is actually Monday.
    Calendar cal = Calendar.getInstance(greatBritain);
    assertThat(cal.getFirstDayOfWeek()).isEqualTo(Calendar.MONDAY);

    view.init(minDate, maxDate, greatBritain);
    MonthView monthView = (MonthView) view.getAdapter().getView(1, null, null);
    CalendarRowView header = (CalendarRowView) monthView.grid.getChildAt(0);
    TextView firstDay = (TextView) header.getChildAt(0);
    assertThat(firstDay).hasTextString("Mon"); // Monday!

    List<List<MonthCellDescriptor>> cells = getCells(SEPTEMBER, 2013);
    assertThat(cells).hasSize(6);
    assertCell(cells, 0, 0, 26, false, false, false, false, NONE);
    assertCell(cells, 1, 0, 2, true, false, false, true, NONE);
    assertCell(cells, 5, 0, 30, true, false, false, true, NONE);
  }
  @Nullable
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_daterange_picker, container, false);
    mDoneButton = (Button) view.findViewById(R.id.saveButton);
    mCancelButton = (Button) view.findViewById(R.id.cancelButton);
    mCalendarPickerView = (CalendarPickerView) view.findViewById(R.id.calendar_view);

    Calendar nextYear = Calendar.getInstance();
    nextYear.add(Calendar.YEAR, 1);

    Date today = new Date();
    mCalendarPickerView
        .init(mStartRange, mEndRange)
        .inMode(CalendarPickerView.SelectionMode.RANGE)
        .withSelectedDate(today);

    mDoneButton.setText(R.string.confirm);
    mDoneButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            List<Date> selectedDates = mCalendarPickerView.getSelectedDates();
            Date startDate = selectedDates.get(0);
            Date endDate = selectedDates.size() == 2 ? selectedDates.get(1) : new Date();
            mDateRangeSetListener.onDateRangeSet(startDate, endDate);
            dismiss();
          }
        });

    mCancelButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            dismiss();
          }
        });
    return view;
  }
  @Test
  public void testWithoutDateSelectedListener() throws Exception {
    view.init(minDate, maxDate, locale) //
        .inMode(SINGLE) //
        .withSelectedDate(today.getTime());

    Calendar jumpToCal = Calendar.getInstance(locale);
    jumpToCal.setTime(today.getTime());
    jumpToCal.add(DATE, 1);
    MonthCellDescriptor cellToClick =
        new MonthCellDescriptor(
            jumpToCal.getTime(),
            true,
            true,
            true,
            true,
            true,
            0,
            MonthCellDescriptor.RangeState.NONE);
    view.listener.handleClick(cellToClick);

    assertThat(view.selectedCals.get(0).get(DATE)).isEqualTo(jumpToCal.get(DATE));
  }
  @Test
  public void testRangeStateOnDateSelections() {
    Calendar startCal = buildCal(2012, NOVEMBER, 17);
    Calendar endCal = buildCal(2012, NOVEMBER, 24);

    view.init(minDate, maxDate, locale) //
        .inMode(RANGE);

    boolean wasAbleToSetDate = view.selectDate(startCal.getTime());
    assertThat(wasAbleToSetDate).isTrue();

    wasAbleToSetDate = view.selectDate(endCal.getTime());
    assertThat(wasAbleToSetDate).isTrue();

    List<List<MonthCellDescriptor>> cells = getCells(NOVEMBER, 2012);
    assertCell(cells, 2, 6, 17, true, true, false, true, FIRST);
    assertCell(cells, 3, 0, 18, true, false, false, true, MIDDLE);
    assertCell(cells, 3, 1, 19, true, false, false, true, MIDDLE);
    assertCell(cells, 3, 2, 20, true, false, false, true, MIDDLE);
    assertCell(cells, 3, 3, 21, true, false, false, true, MIDDLE);
    assertCell(cells, 3, 4, 22, true, false, false, true, MIDDLE);
    assertCell(cells, 3, 5, 23, true, false, false, true, MIDDLE);
    assertCell(cells, 3, 6, 24, true, true, false, true, LAST);
  }
示例#28
0
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    switch (id) {
      case R.id.action_refresh:
        browsingFragment.refresh(getApplicationContext());
        break;
      case R.id.action_pick_day:
        // DialogFragment newFragment = new DatePickerFragment();
        // newFragment.show(getFragmentManager(), "datePicker");

        View view = getLayoutInflater().inflate(R.layout.calendar_date_picker, null);
        final CalendarPickerView calendarPickerView =
            (CalendarPickerView) view.findViewById(R.id.calendar);
        final Date today = new Date();
        Calendar tmo = Calendar.getInstance();
        tmo.add(Calendar.DAY_OF_MONTH, 1);
        Calendar janFirst = Calendar.getInstance();
        janFirst.set(Calendar.MONTH, Calendar.JANUARY);
        janFirst.set(Calendar.DAY_OF_MONTH, 1);
        janFirst.set(Calendar.YEAR, 2014);
        calendarPickerView.init(janFirst.getTime(), tmo.getTime()).withSelectedDate(currDate);

        // Build the dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(view); // Set the view of the dialog to your custom layout
        builder.setTitle("View Products posted on...");
        builder.setPositiveButton(
            "OK",
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date selected = calendarPickerView.getSelectedDate();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(selected);
                Calendar current = Calendar.getInstance();
                current.setTime(currDate);
                if (calendar.get(Calendar.MONTH) == current.get(Calendar.MONTH)
                    && calendar.get(Calendar.DAY_OF_MONTH) == current.get(Calendar.DAY_OF_MONTH)
                    && calendar.get(Calendar.YEAR) == current.get(Calendar.YEAR)) {
                  dialog.dismiss();
                  return;
                }
                currDate = selected;
                String dateString = format.format(selected);
                browsingFragment.dateString = dateString;
                MainActivity.this.dateString = dateString;
                browsingFragment.refresh(getApplicationContext());

                if (today.equals(selected)) {
                  getActionBar().setTitle("Today's Hunts");
                } else {
                  getActionBar()
                      .setTitle(
                          "Hunts for "
                              + (calendar.get(Calendar.MONTH) + 1)
                              + "/"
                              + calendar.get(Calendar.DAY_OF_MONTH));
                }

                dialog.dismiss();
              }
            });

        // Create and show the dialog
        builder.create().show();
        break;
    }
    return super.onOptionsItemSelected(item);
  }
 /**
  * Verify the expectation that the set of dates excludes the max. In other words, the date
  * interval is [minDate, maxDate)
  */
 @Test(expected = IllegalArgumentException.class)
 public void testSelectedNotInRange_maxDateExcluded() throws Exception {
   view.init(minDate, maxDate, locale) //
       .inMode(SINGLE) //
       .withSelectedDate(maxDate);
 }