def testFunctions10_7(self): self.assertArgIsBOOL(CoreFoundation.CFRunLoopObserverCreateWithHandler, 2) self.assertArgIsBlock( CoreFoundation.CFRunLoopObserverCreateWithHandler, 4, b"v^{__CFRunLoopObserver=}" + objc._C_NSUInteger, ) lst = [] def record(observer, activity): lst.append((observer, activity)) ref = CoreFoundation.CFRunLoopObserverCreateWithHandler( None, CoreFoundation.kCFRunLoopAllActivities, False, 0, record ) self.assertIsInstance(ref, CoreFoundation.CFRunLoopObserverRef) runloop_mode = CoreFoundation.kCFRunLoopDefaultMode rl = CoreFoundation.CFRunLoopGetCurrent() CoreFoundation.CFRunLoopAddObserver(rl, ref, runloop_mode) res = CoreFoundation.CFRunLoopRunInMode(runloop_mode, 0.5, True) CoreFoundation.CFRunLoopRemoveObserver(rl, ref, runloop_mode) self.assertNotEqual(lst, []) for a, b in lst: self.assertEqual(a, ref) self.assertIsInstance(b, int) self.assertArgIsBlock( CoreFoundation.CFRunLoopTimerCreateWithHandler, 5, b"v^{__CFRunLoopTimer=}" ) lst = [] ref = CoreFoundation.CFRunLoopTimerCreateWithHandler( None, CoreFoundation.CFAbsoluteTimeGetCurrent() + 2.9, 0.0, 0, 0, lambda x: lst.append(x), ) self.assertIsInstance(ref, CoreFoundation.CFRunLoopTimerRef) CoreFoundation.CFRunLoopAddTimer(rl, ref, runloop_mode) res = CoreFoundation.CFRunLoopRunInMode(runloop_mode, 6.0, True) self.assertIsInstance(res, int) CoreFoundation.CFRunLoopRemoveTimer(rl, ref, runloop_mode) import __main__ # XXX: See issue #11 in the pyobjc tracker if "setup" in __main__.__file__: return self.assertNotEqual(lst, []) for a in lst: self.assertEqual(a, ref)
def testFunctions10_9(self): lst = [] ref = CoreFoundation.CFRunLoopTimerCreateWithHandler( None, CoreFoundation.CFAbsoluteTimeGetCurrent() + 2.9, 0.0, 0, 0, lambda x: lst.append(x), ) self.assertIsInstance(ref, CoreFoundation.CFRunLoopTimerRef) CoreFoundation.CFRunLoopTimerSetTolerance(ref, 5.0) v = CoreFoundation.CFRunLoopTimerGetTolerance(ref) self.assertIsInstance(v, float)
def testInspection(self): now = CoreFoundation.CFAbsoluteTimeGetCurrent() nowtm = time.localtime() dt = CoreFoundation.CFDateCreate(None, now) self.assertIsInstance(dt, NSDate) self.assertEqual(CoreFoundation.CFDateGetAbsoluteTime(dt), now) dt2 = CoreFoundation.CFDateCreate(None, now + 1000) self.assertEqual( CoreFoundation.CFDateGetTimeIntervalSinceDate(dt2, dt), 1000) cmp = CoreFoundation.CFDateCompare(dt, dt2, 0) self.assertLessThan(cmp, 0) dt = CoreFoundation.CFGregorianDate() dt.month = 12 self.assertIs( CoreFoundation.CFGregorianDateIsValid( dt, CoreFoundation.kCFGregorianUnitsMonths), True, ) dt.month = 99 self.assertIs( CoreFoundation.CFGregorianDateIsValid( dt, CoreFoundation.kCFGregorianUnitsMonths), False, ) tz = CoreFoundation.CFTimeZoneCopyDefault() dt.year = 2008 dt.day = 1 dt.month = 1 abstime = CoreFoundation.CFGregorianDateGetAbsoluteTime(dt, tz) self.assertIsInstance(abstime, float) dt = CoreFoundation.CFAbsoluteTimeGetGregorianDate(now, tz) self.assertEqual(dt.year, nowtm.tm_year) self.assertEqual(dt.month, nowtm.tm_mon) self.assertEqual(dt.day, nowtm.tm_mday) self.assertEqual(dt.hour, nowtm.tm_hour) self.assertEqual(dt.minute, nowtm.tm_min) units = CoreFoundation.CFGregorianUnits(days=1) stamp = CoreFoundation.CFAbsoluteTimeAddGregorianUnits(now, tz, units) self.assertEqual(stamp - now, 24 * 3600) units = CoreFoundation.CFAbsoluteTimeGetDifferenceAsGregorianUnits( stamp, now, tz, CoreFoundation.kCFGregorianAllUnits) self.assertEqual(units.days, 1) self.assertEqual(units.seconds, 0) v = CoreFoundation.CFAbsoluteTimeGetDayOfWeek(now, tz) self.assertEqual(v, nowtm.tm_wday + 1) v = CoreFoundation.CFAbsoluteTimeGetDayOfYear(now, tz) self.assertEqual(v, nowtm.tm_yday) v = CoreFoundation.CFAbsoluteTimeGetWeekOfYear(now, tz) self.assertIsInstance(v, int)
def testInspection(self): locale = CoreFoundation.CFLocaleCopyCurrent() self.assertIsInstance(locale, CoreFoundation.CFLocaleRef) date = CoreFoundation.CFDateCreate( None, CoreFoundation.CFAbsoluteTimeGetCurrent()) self.assertIsInstance(date, Foundation.NSDate) self.assertResultIsCFRetained(CoreFoundation.CFDateFormatterCreate) fmt = CoreFoundation.CFDateFormatterCreate( None, locale, CoreFoundation.kCFDateFormatterShortStyle, CoreFoundation.kCFDateFormatterLongStyle, ) self.assertIsInstance(fmt, CoreFoundation.CFDateFormatterRef) v = CoreFoundation.CFDateFormatterGetLocale(fmt) self.assertEqual( CoreFoundation.CFLocaleGetIdentifier(locale), CoreFoundation.CFLocaleGetIdentifier(v), ) v = CoreFoundation.CFDateFormatterGetDateStyle(fmt) self.assertEqual(v, CoreFoundation.kCFDateFormatterShortStyle) v = CoreFoundation.CFDateFormatterGetTimeStyle(fmt) self.assertEqual(v, CoreFoundation.kCFDateFormatterLongStyle) v = CoreFoundation.CFDateFormatterGetFormat(fmt) self.assertIsInstance(v, str) CoreFoundation.CFDateFormatterSetFormat(fmt, v[:-1]) v2 = CoreFoundation.CFDateFormatterGetFormat(fmt) self.assertEqual(v[:-1], v2) v = CoreFoundation.CFDateFormatterCreateStringWithDate(None, fmt, date) self.assertIsInstance(v, str) v = CoreFoundation.CFDateFormatterCreateStringWithAbsoluteTime( None, fmt, CoreFoundation.CFAbsoluteTimeGetCurrent()) self.assertIsInstance(v, str) dt, rng = CoreFoundation.CFDateFormatterCreateDateFromString( None, fmt, v, (0, len(v))) self.assertIsInstance(dt, Foundation.NSDate) self.assertIsInstance(rng, CoreFoundation.CFRange) ok, rng, abstime = CoreFoundation.CFDateFormatterGetAbsoluteTimeFromString( fmt, v, (0, len(v)), None) self.assertIs(ok, True) self.assertIsInstance(rng, CoreFoundation.CFRange) self.assertIsInstance(abstime, float) self.assertResultIsCFRetained( CoreFoundation.CFDateFormatterCopyProperty) v = CoreFoundation.CFDateFormatterCopyProperty( fmt, CoreFoundation.kCFDateFormatterCalendarName) self.assertIsInstance(v, str) CoreFoundation.CFDateFormatterSetProperty( fmt, CoreFoundation.kCFDateFormatterCalendarName, "gregorian") v = CoreFoundation.CFDateFormatterCopyProperty( fmt, CoreFoundation.kCFDateFormatterCalendarName) self.assertIsInstance(v, str) self.assertEqual(v, "gregorian") v = CoreFoundation.CFDateFormatterCopyProperty( fmt, CoreFoundation.kCFDateFormatterIsLenient) self.assertTrue(v is True or v is False) CoreFoundation.CFDateFormatterSetProperty( fmt, CoreFoundation.kCFDateFormatterIsLenient, True) v2 = CoreFoundation.CFDateFormatterCopyProperty( fmt, CoreFoundation.kCFDateFormatterIsLenient) self.assertIs(v2, True)
def testInspection(self): cal = CoreFoundation.CFCalendarCreateWithIdentifier( None, CoreFoundation.kCFGregorianCalendar ) self.assertIsInstance(cal, CoreFoundation.CFCalendarRef) name = CoreFoundation.CFCalendarGetIdentifier(cal) self.assertEqual(name, CoreFoundation.kCFGregorianCalendar) locale = CoreFoundation.CFCalendarCopyLocale(cal) self.assertIsInstance(locale, CoreFoundation.CFLocaleRef) timezone = CoreFoundation.CFCalendarCopyTimeZone(cal) self.assertIsInstance(timezone, CoreFoundation.CFTimeZoneRef) weekday = CoreFoundation.CFCalendarGetFirstWeekday(cal) self.assertLessEqual(0 <= weekday, 7) num = CoreFoundation.CFCalendarGetMinimumDaysInFirstWeek(cal) self.assertLessEqual(0 <= num, 7) rng = CoreFoundation.CFCalendarGetMinimumRangeOfUnit( cal, CoreFoundation.kCFCalendarUnitEra ) self.assertIsInstance(rng, CoreFoundation.CFRange) rng = CoreFoundation.CFCalendarGetMaximumRangeOfUnit( cal, CoreFoundation.kCFCalendarUnitEra ) self.assertIsInstance(rng, CoreFoundation.CFRange) m = datetime.date.today() if m.month in (1, 3, 5, 7, 8, 10, 12): monthLength = 31 elif m.month in (4, 6, 9, 11): monthLength = 30 else: if m.year % 4 == 0: # Yes this is wrong, but the next time this fails in # in 2100. monthLength = 29 else: monthLength = 28 rng = CoreFoundation.CFCalendarGetRangeOfUnit( cal, CoreFoundation.kCFCalendarUnitDay, CoreFoundation.kCFCalendarUnitMonth, CoreFoundation.CFAbsoluteTimeGetCurrent(), ) self.assertIsInstance(rng, CoreFoundation.CFRange) self.assertEqual(rng.location, 1) self.assertEqual(rng.length, monthLength) v = CoreFoundation.CFCalendarGetOrdinalityOfUnit( cal, CoreFoundation.kCFCalendarUnitDay, CoreFoundation.kCFCalendarUnitYear, CoreFoundation.CFAbsoluteTimeGetCurrent(), ) self.assertIsInstance(v, int) ok, startp, tip = CoreFoundation.CFCalendarGetTimeRangeOfUnit( cal, CoreFoundation.kCFCalendarUnitDay, CoreFoundation.CFAbsoluteTimeGetCurrent(), None, None, ) self.assertIs(ok, True) self.assertIsInstance(startp, float) self.assertIsInstance(tip, float) self.assertIn(tip, (86400.0, 90000.0, 82800)) # 1 day, remove DST, add DST
def testCreation(self): now = CoreFoundation.CFAbsoluteTimeGetCurrent() dt = CoreFoundation.CFDateCreate(None, now) self.assertIsInstance(dt, NSDate)
def testAbsoluteTime(self): v = CoreFoundation.CFAbsoluteTimeGetCurrent() self.assertIsInstance(v, float) self.assertLessThan( abs(v - time.time() + CoreFoundation.kCFAbsoluteTimeIntervalSince1970), 1.0)