Roboletric v4.2 Release Notes
Release Date: 2019-02-14 // about 5 years ago-
๐ This release introduces a new extension mechanism for Robolectric, fixes a major performance regression in 4.x, and includes numerous SDK support improvements and bug fixes.
๐ Features
- ๐ Robolectric 4.2 introduces a new plugin system, replacing the previous ad-hoc customization mechanisms (subclassing
RobolectricTestRunner
, providing system properties, etc.). The old mechanisms are deprecated and will eventually be removed; let us know if there are missing extension points you need. See org.robolectric.pluginapi for more details [issue #4362]. - ๐ Fixed a significant performance regression in 4.x. Thanks @cesar1000! [issues #4142, #4585].
- For developers of custom shadows, Robolectric 4.2 has a new typesafe and more performant way to access fields and methods on @RealObjects using the Reflector API [issues #4361, #4315, #4351, #4456]
โ Android SDK support and Test API changes
- ๐ Activities must now be explicitly declared in a manifest (as is the case for normal Android). See #4736 for more context.
- ๐ฆ PackageManager
PackageManager
is moving to returning defensive immutable copies of its internal data (as on a real device). A series of new testing APIs have been added to modify the package under test; seePackageManager.addOrUpdateActivity()
andremoveActivity()
, plusService
,ContentProvider
, andBroadcastReceiver
equivalents [issue #4489].- Implement
PackageManager.isSafeMode()
[issue #4526].
ShadowDisplayManager.getSaturationLevel()
is now an instance method [issue #4522].- Telephony
- Add
slotId
basedgetImei()
andgetMeid()
toTelephonyManager
[issue #4521]. - Add the ability to prevent certain networks from being updated in tests [issue #4515].
- Implement
TelephonyManager.getListenersForFlags
in tests [issue #4469]. - Implement
TelephonyManager.getVisualVoicemailPackageName()
[issue #4444].
- Add
- Bluetooth
- Test
APIs
forBluetoothSocket
andBluetoothServerSocket
connections [issue #4510]. - Implement
BluetoothLeAdvertiser
[issues #4478 and #4454]. - Implement
BluetoothHeadset.getConnectedDevices()
,getConnectionState()
, andsendVendorSpecificResultCode()
, with test APIs [issue #4442]. - Add ability to send BluetoothGattCallback connection state change updates [issue #4584].
- Test
- Implement
ActivityManager#getCurrentUser()
[issue #4483]. UserManager
- Implement
Context.sendBroadcastAsUser
[issue #4486]. - ๐ Implement
WindowManagerImpl#removeViewImmediate(View)
[issue #4447]. - Implement
SensorManager.registerListener()
: [issue #4446]. - Implement
NetworkScoreManager.disableScoring()
andisScoringEnabled()
to [issue #4436]. DevicePolicyManager
now enforces password restrictions [issues #4532, #4545, and #4539].- Implement
UiModeManager
[issue #4548]. - Implement
AccessibilityManager#addAccessibilityStateChangeListener(AccessibilityStateChangeListener
, Handler) [issue #4448]. - Implement
ParcelFileDescriptor.createReliablePipe
method [issue #4562]. - ๐ฆ Move intent filters to their own lists and out of the Package [issue #4488].
- Implement
WifiRttManager
andRangingResult
[issue #4561]. - โ Add test API to toggle a
Sensor
wake-up mode [issue #4558]. - โ Add
PowerManager.WakeLock
getTimesHeld()
test API to count number of times the wakelock was held [issue #4551]. - โ Adds basic
MediaCodec
andMediaMuxer
shadows to robolectric. TheMediaCodec
shadow implements basic passthrough asynchronous encoding, and theMediaMuxer
shadow muxes input bytes directly to the file without modification [issue #4550]. - โก๏ธ Update to
ShadowDevicePolicyManager
to allow uses-policy granting [issue #4546]. - Modify
ShadowParcel
to throw an Error instead ofRuntimeException
to discourage [issue #4528]. - Don't autocreate activities in
LocalActivityInvoker
anymore [issue #4582]. - 0๏ธโฃ Don't automatically generate default
ActivityInfo
whenever asked [issue #4580]. - ๐ Implements
WebView.removeJavascriptInterface
[issue #4575]. - โก๏ธ Update to
ShadowDevicePolicyManager
to set storage encryption [issue #4563].
๐ง Configuration
- ๐ There are some small changes to the way Android SDK jars are located; see (
LegacyDependencyResolver
javadoc)[http://robolectric.org/javadoc/4.2/org/robolectric/plugins/LegacyDependencyResolver.html] for details [issue #4599].
๐ Bug Fixes
- The
android
system context is no longer a parent of the application context [issue #4481]. - ๐ Fix
LocalActivityInvoker.recreateActivity.
Before this change, it calledActivity.onRetainNonConfigurationInstance()
directly to obtainnonConfigInstance
for the activity. This should've called indirectly fromActivity.retainNonConfigurationInstances().
retainNonConfigurationInstances()
also createsnonConfigInstances
for fragments to prepare for the host activity recreation [issue #4511]. - ๐ Fix timeout in
FontsContract.getFontSync()
caused becauseContext.canLoadUnsafeResources()
now returns false since the context isn't a system context [issue #4517]. - ๐ Fix handling of persisted preferred activities [issue #4480].
- ๐ Fix
processName
setting and add few more reasonable defaults when installing new package [issue #4465]. - โ Add permission check to
LocationManager.setLocationEnabledForUser
[issue #4500]. - No longer reset the data position after unmarshalling a
ShadowParcel
[issue #4468]. - ๐ Fix
NPE
Robolectric when a failure occurs before shadows are set up [issue #4467]. - Set
AppBindData#processName
consistently withApplicationInfo#processName
[issue #4537]. - ๐ฆ Throw
NameNotFoundException
from Context.createPackageContext
for non-existing packages [issue #4527]. - ๐ Synchronize access to
ShadowPendingIntent
andShadowUsageStatsManager
fields [issue #4520]. - ๐ Fix
Activity#isChangingConfigurations()
returning false during the recreation [issue #4540]. - ๐ Fix equality in
ShadowNetwork
[issue #4464].
๐ Deprecations and Removals
- ๐
FsFile
et al. have been removed in favor of java.nio.file.Path [issue #4265]. - โก๏ธ
ShadowPackageManager.addResolveInfoForIntent
is deprecated; add explicit activity declarations in your manifest, or useShadowPackageManager.addOrUpdateActivity(ActivityInfo)
[issue #4489]. - ๐ Deprecate Robolectric#
setupActivity
in favor ofActivityScenario
[issue #4564].
Internal Changes
- โ Tests are now run on a dedicated thread for each SDK [issue #4559].
- ๐ Resolve
AARs
like the android plugins do (kind of) [issue #4279]. - Reflector objects should unwrap invocation exceptions [issue #4361].
- Fast private method invocation [issue #4315].
- โ Add support for static methods and fields to Reflector [issue #4351].
- ๐ Fix Reflector class definition in Java 11 [issue #4456].
- ๐ Support Java 9 [issue #4400].
- ๐ Robolectric now builds successfully on Windows, though a few tests still fail [issue #4293].
- ๐ง Extensible configuration system [issue #4519].
- Switch circleci to
API
28 image. FixSDK
install license fail [issue #4541]. - ๐ Changes to support Java 9 [issue #4452].
TypedArray
doesn't need a shadow in binary mode [issue #4470].- Extract
Sdk
fromSdkConfig
[issue #4394]. - Clean up
SdkProvider
interface [issue #4513]. - ๐ Move
PerfStatsReporter
to pluginapi [issue #4471]. PerfStatsCollector
retains min and max timing info for events [issue #4552].- โ Remove
DefaultConfigMerger
[issue #4549].
Known Issues
- ๐ Activities must now be explicitly declared in a manifest (as is the case for normal Android); however, Android Gradle Plugin doesn't currently merge a test manifest. See #4736 for more context and #4727 for a workaround.
- ๐ Robolectric 4.2 doesn't work on Windows in some cases [issue #4603 et al.].
- โ
Android Gradle Plugin 3.2.1 and 3.3 report the following warning, which may be safely ignored:
WARNING: The option setting 'android.enableUnitTestBinaryResources=true' is experimental and unsupported.
. Android Gradle Plugin 3.4 will resolve this issue. - ๐ Android Gradle Plugin 3.2.1 does not work on Windows; use 3.3 instead [issue #3955].
Compatibility
- ๐ Android Studio/Android Gradle Plugin 3.3 or 3.4 Beta
- โ Android SDK 28 (includes support for testing against SDKs from 16 on)
๐ Use Robolectric:
testCompile "org.robolectric:robolectric:4.2"
Find more details here. Report issues here. Enjoy!
- ๐ Robolectric 4.2 introduces a new plugin system, replacing the previous ad-hoc customization mechanisms (subclassing