The one thing I will say is absolutely easy to decide is this: if a foreigner comes up to you and starts speaking Chinese, you should respond in Chinese. If a foreigner comes up to you and starts speaking English, you should respond in English.
As for other cases, I disagree with the other answers here, but of course you are welcome to make up your own mind. I think you should speak in Chinese first, and try English if they don't understand you. The reason I say this is because it can be a little bit racist if you assume that no foreigner speaks Chinese, and every foreigner speaks English. I think it's more polite to assume that any person, no matter what they look like, has put in the work to try and learn the language of the country where they're living.
I personally am studying Korean, because I want to live in Korea, and I get really nervous about this. I worry that every Korean I meet will speak to me only in English, and that would make me feel sad. It would show me that they think I don't belong, or they think I'm not smart enough to speak Korean. I think that foreigners in China will feel the same way.