The ALMA spectroscopic survey in the HUDF: the cosmic dust and gas mass densities in galaxies up to z ~ 3