1

I am writing a multi-language android app and I would like to test if the labels and others texts are correct. For example, in the English version the button will have the label "Download", in Germany "Nachladen", in Czech "Stahnout".

My test code work fine for the language which is set in the testing device, but how do I programmatically run the test for every language on one device?

import android.content.Intent;
import android.test.ActivityUnitTestCase;
import android.widget.Button;

public class MenuActivityTest extends ActivityUnitTestCase<MenuActivity> {

  private Intent launchIntent;
  private MenuActivity activity;

  public MenuActivityTest() {
    super(MenuActivity.class);
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();

    launchIntent = new Intent(getInstrumentation().getTargetContext(),
        MenuActivity.class);
  }

  public void testSettingsButtonText() {
    startActivity(launchIntent, null, null);
    activity = getActivity();
    final String expected = activity
        .getText(R.string.menu_button_text_settings).toString();
    final Button button = (Button) activity
        .findViewById(R.id.menu_button_settings);
    final String actual = button.getText().toString();

    assertEquals("Button text for Settings is wrong", expected, actual);
  }
}    
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Pauli
  • 538
  • 8
  • 22
  • To be honest, I don't think it actually make sense to test individual translations. What if your terminology change? Just like that you'll end up with multiple test failures. And your code will be correct. You should test behaviours, not specifics. And the behaviour here is, your screen got translated. The whole screen, not the individual words for translation may be the same word in a target language... – Paweł Dyda Jul 04 '14 at 06:55
  • I know that is not usual, but I have reasons (not IT reasons),ok I take, that maybe it doesn't make sense. But I just want know how do I programmatically run the test for every language on one device? – Pauli Jul 06 '14 at 10:31
  • You may try to assign the default Locale (`Locale.setDefault(Locale)`) and run the test. I am not sure if that would work, and it is likely that you would need to create test suite rather than single test, but that may be the way to go. – Paweł Dyda Jul 07 '14 at 14:11
  • Thanks for the right way :) I found solutions there http://stackoverflow.com/questions/2900023/change-language-programatically-in-android – Pauli Jul 08 '14 at 13:42

0 Answers0