def tearDown(self): final_paths = self._device.GetApplicationPaths(self.PACKAGE) if self._original_paths != final_paths: try: self._device.Uninstall(self.PACKAGE) except Exception: # pylint: disable=broad-except pass with system_app.EnableSystemAppModification(self._device): for cached_apk, install_path in self._cached_apks.iteritems(): try: with device_temp_file.DeviceTempFile( self._device.adb) as tmp: self._device.adb.Push(cached_apk, tmp.name) self._device.RunShellCommand( ['mv', tmp.name, install_path], as_root=True, check_return=True) except base_error.BaseError: logger.warning('Failed to reinstall %s', os.path.basename(cached_apk)) try: shutil.rmtree(self._apk_cache_dir) except IOError: logger.error('Unable to remove app cache directory.') super(SystemAppDeviceTest, self).tearDown()
def testDoubleEnableModification(self): """Ensures that system app modification logic isn't repeated. If EnableSystemAppModification uses are nested, inner calls should not need to perform any of the expensive modification logic. """ # pylint: disable=no-self-use,protected-access mock_device = mock.Mock(spec=device_utils.DeviceUtils) mock_device.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) type(mock_device).build_version_sdk = mock.PropertyMock( return_value=version_codes.LOLLIPOP) system_props = {} def dict_setprop(prop_name, value): system_props[prop_name] = value def dict_getprop(prop_name): return system_props.get(prop_name, '') mock_device.SetProp.side_effect = dict_setprop mock_device.GetProp.side_effect = dict_getprop with system_app.EnableSystemAppModification(mock_device): mock_device.EnableRoot.assert_called_once_with() mock_device.GetProp.assert_called_once_with( system_app._ENABLE_MODIFICATION_PROP) mock_device.SetProp.assert_called_once_with( system_app._ENABLE_MODIFICATION_PROP, '1') mock_device.reset_mock() with system_app.EnableSystemAppModification(mock_device): self.assertFalse( mock_device.EnableRoot.mock_calls) # assert not called mock_device.GetProp.assert_called_once_with( system_app._ENABLE_MODIFICATION_PROP) self.assertFalse( mock_device.SetProp.mock_calls) # assert not called mock_device.reset_mock() mock_device.SetProp.assert_called_once_with( system_app._ENABLE_MODIFICATION_PROP, '0')