Exemplo n.º 1
1
  @Implementation
  public void setTo(Configuration o) {
    // commented out lines are coming in a newer version of Android SDK

    realConfiguration.fontScale = o.fontScale;
    realConfiguration.mcc = o.mcc;
    realConfiguration.mnc = o.mnc;
    if (o.locale != null) {
      realConfiguration.locale = (Locale) o.locale.clone();
      // realConfiguration.textLayoutDirection = o.textLayoutDirection;
    }
    // realConfiguration.userSetLocale = o.userSetLocale;
    realConfiguration.touchscreen = o.touchscreen;
    realConfiguration.keyboard = o.keyboard;
    realConfiguration.keyboardHidden = o.keyboardHidden;
    realConfiguration.hardKeyboardHidden = o.hardKeyboardHidden;
    realConfiguration.navigation = o.navigation;
    realConfiguration.navigationHidden = o.navigationHidden;
    realConfiguration.orientation = o.orientation;
    realConfiguration.screenLayout = o.screenLayout;
    realConfiguration.uiMode = o.uiMode;
    realConfiguration.screenWidthDp = o.screenWidthDp;
    realConfiguration.screenHeightDp = o.screenHeightDp;
    realConfiguration.smallestScreenWidthDp = o.smallestScreenWidthDp;
    // realConfiguration.compatScreenWidthDp = o.compatScreenWidthDp;
    // realConfiguration.compatScreenHeightDp = o.compatScreenHeightDp;
    // realConfiguration.compatSmallestScreenWidthDp = o.compatSmallestScreenWidthDp;
    // realConfiguration.seq = o.seq;
  }
  /** Change the configuration used when retrieving resources. Not for use by applications. */
  @Override
  public void setConfiguration(
      int mcc,
      int mnc,
      String locale,
      int orientation,
      int touchscreen,
      int density,
      int keyboard,
      int keyboardHidden,
      int navigation,
      int screenWidth,
      int screenHeight,
      int version) {

    Configuration c = new Configuration();
    c.mcc = mcc;
    c.mnc = mnc;
    c.locale = new Locale(locale);
    c.touchscreen = touchscreen;
    c.keyboard = keyboard;
    c.keyboardHidden = keyboardHidden;
    c.navigation = navigation;
    c.orientation = orientation;
  }
 @Test
 public void testForceMakeKeyboardsOnOrientationChange() {
   final Configuration configuration =
       RuntimeEnvironment.application.getResources().getConfiguration();
   configuration.orientation = Configuration.ORIENTATION_LANDSCAPE;
   mAnySoftKeyboardUnderTest.onConfigurationChanged(configuration);
   Mockito.verify(mAnySoftKeyboardUnderTest.getSpiedKeyboardSwitcher()).flushKeyboardsCache();
   // sanity
   Mockito.reset(mAnySoftKeyboardUnderTest.getSpiedKeyboardSwitcher());
   mAnySoftKeyboardUnderTest.onConfigurationChanged(configuration);
   Mockito.verify(mAnySoftKeyboardUnderTest.getSpiedKeyboardSwitcher(), Mockito.never())
       .flushKeyboardsCache();
 }
  @SmallTest
  public void testClientSidePreferenceSharedPreferencesConfiguration() {
    SharedPreferences sharedPreferences =
        MozcPreferenceUtil.getSharedPreferences(
            getInstrumentation().getContext(), "CLIENT_SIDE_PREF");
    MozcPreferenceUtil.updateSharedPreference(
        sharedPreferences,
        PreferenceUtil.PREF_PORTRAIT_KEYBOARD_LAYOUT_KEY,
        PORTRAIT_KEYBOARD_LAYOUT.name());
    MozcPreferenceUtil.updateSharedPreference(
        sharedPreferences,
        PreferenceUtil.PREF_PORTRAIT_INPUT_STYLE_KEY,
        PORTRAIT_INPUT_STYLE.name());
    MozcPreferenceUtil.updateSharedPreference(
        sharedPreferences,
        PreferenceUtil.PREF_PORTRAIT_QWERTY_LAYOUT_FOR_ALPHABET_KEY,
        PORTRAIT_QWERTY_LAYOUT_FOR_ALPHABET);
    MozcPreferenceUtil.updateSharedPreference(
        sharedPreferences,
        PreferenceUtil.PREF_PORTRAIT_FLICK_SENSITIVITY_KEY,
        PORTRAIT_FLICK_SENSITIVITY);

    MozcPreferenceUtil.updateSharedPreference(
        sharedPreferences,
        PreferenceUtil.PREF_LANDSCAPE_KEYBOARD_LAYOUT_KEY,
        LANDSCAPE_KEYBOARD_LAYOUT.name());
    MozcPreferenceUtil.updateSharedPreference(
        sharedPreferences,
        PreferenceUtil.PREF_LANDSCAPE_INPUT_STYLE_KEY,
        LANDSCAPE_INPUT_STYLE.name());
    MozcPreferenceUtil.updateSharedPreference(
        sharedPreferences,
        PreferenceUtil.PREF_LANDSCAPE_QWERTY_LAYOUT_FOR_ALPHABET_KEY,
        LANDSCAPE_QWERTY_LAYOUT_FOR_ALPHABET);
    MozcPreferenceUtil.updateSharedPreference(
        sharedPreferences,
        PreferenceUtil.PREF_LANDSCAPE_FLICK_SENSITIVITY_KEY,
        LANDSCAPE_FLICK_SENSITIVITY);

    MozcPreferenceUtil.updateSharedPreference(
        sharedPreferences, PreferenceUtil.PREF_PORTRAIT_FULLSCREEN_KEY, PORTRAIT_FULLSCREEN_MODE);
    MozcPreferenceUtil.updateSharedPreference(
        sharedPreferences, PreferenceUtil.PREF_LANDSCAPE_FULLSCREEN_KEY, LANDSCAPE_FULLSCREEN_MODE);

    MozcPreferenceUtil.updateSharedPreference(
        sharedPreferences,
        PreferenceUtil.PREF_PORTRAIT_LAYOUT_ADJUSTMENT_KEY,
        PORTRAIT_LAYOUT_ADJUSTMENT.name());
    MozcPreferenceUtil.updateSharedPreference(
        sharedPreferences,
        PreferenceUtil.PREF_LANDSCAPE_LAYOUT_ADJUSTMENT_KEY,
        LANDSCAPE_LAYOUT_ADJUSTMENT.name());

    MozcPreferenceUtil.updateSharedPreference(
        sharedPreferences, PreferenceUtil.PREF_SKIN_TYPE, SkinType.BLUE_LIGHTGRAY.name());

    class TestData extends Parameter {
      final int orientation;
      final boolean usePortraitKeyboardSettingsForLandscape;

      final KeyboardLayout expectedKeyboardLayout;
      final InputStyle expectedInputStyle;
      final boolean expectedQwertyLayoutForAlphabet;
      final int expectedFlickSensitivity;
      final boolean expectedFullscreenMode;
      final LayoutAdjustment expectedLayoutAdjustment;
      final SkinType expectedSkinType;

      TestData(
          int orientation,
          boolean usePortraitKeyboardSettingsForLandscape,
          KeyboardLayout expectedKeyboardLayout,
          InputStyle expectedInputStyle,
          boolean expectedQwertyLayoutForAlphabet,
          int expectedFlickSensitivity,
          LayoutAdjustment expectedLayoutAdjustment,
          boolean expectedFullscreenMode,
          SkinType expectedSkinType) {
        this.orientation = orientation;
        this.usePortraitKeyboardSettingsForLandscape = usePortraitKeyboardSettingsForLandscape;

        this.expectedKeyboardLayout = expectedKeyboardLayout;
        this.expectedInputStyle = expectedInputStyle;
        this.expectedQwertyLayoutForAlphabet = expectedQwertyLayoutForAlphabet;
        this.expectedFlickSensitivity = expectedFlickSensitivity;
        this.expectedLayoutAdjustment = expectedLayoutAdjustment;
        this.expectedFullscreenMode = expectedFullscreenMode;
        this.expectedSkinType = expectedSkinType;
      }
    }

    TestData[] testDataList = {
      new TestData(
          Configuration.ORIENTATION_PORTRAIT,
          false,
          PORTRAIT_KEYBOARD_LAYOUT,
          PORTRAIT_INPUT_STYLE,
          PORTRAIT_QWERTY_LAYOUT_FOR_ALPHABET,
          PORTRAIT_FLICK_SENSITIVITY,
          PORTRAIT_LAYOUT_ADJUSTMENT,
          PORTRAIT_FULLSCREEN_MODE,
          SkinType.BLUE_LIGHTGRAY),
      new TestData(
          Configuration.ORIENTATION_LANDSCAPE,
          false,
          LANDSCAPE_KEYBOARD_LAYOUT,
          LANDSCAPE_INPUT_STYLE,
          LANDSCAPE_QWERTY_LAYOUT_FOR_ALPHABET,
          LANDSCAPE_FLICK_SENSITIVITY,
          LANDSCAPE_LAYOUT_ADJUSTMENT,
          LANDSCAPE_FULLSCREEN_MODE,
          SkinType.BLUE_LIGHTGRAY),
      new TestData(
          Configuration.ORIENTATION_PORTRAIT,
          true,
          PORTRAIT_KEYBOARD_LAYOUT,
          PORTRAIT_INPUT_STYLE,
          PORTRAIT_QWERTY_LAYOUT_FOR_ALPHABET,
          PORTRAIT_FLICK_SENSITIVITY,
          PORTRAIT_LAYOUT_ADJUSTMENT,
          PORTRAIT_FULLSCREEN_MODE,
          SkinType.BLUE_LIGHTGRAY),
      new TestData(
          Configuration.ORIENTATION_LANDSCAPE,
          true,
          PORTRAIT_KEYBOARD_LAYOUT,
          PORTRAIT_INPUT_STYLE,
          PORTRAIT_QWERTY_LAYOUT_FOR_ALPHABET,
          PORTRAIT_FLICK_SENSITIVITY,
          PORTRAIT_LAYOUT_ADJUSTMENT,
          LANDSCAPE_FULLSCREEN_MODE,
          SkinType.BLUE_LIGHTGRAY),
    };

    for (TestData testData : testDataList) {
      MozcPreferenceUtil.updateSharedPreference(
          sharedPreferences,
          PreferenceUtil.PREF_USE_PORTRAIT_KEYBOARD_SETTINGS_FOR_LANDSCAPE_KEY,
          testData.usePortraitKeyboardSettingsForLandscape);

      Configuration configuration = new Configuration();
      configuration.orientation = testData.orientation;

      ClientSidePreference preference = new ClientSidePreference(sharedPreferences, configuration);
      assertEquals(
          testData.toString(), testData.expectedKeyboardLayout, preference.getKeyboardLayout());
      assertEquals(testData.toString(), testData.expectedInputStyle, preference.getInputStyle());
      assertEquals(
          testData.toString(),
          testData.expectedQwertyLayoutForAlphabet,
          preference.isQwertyLayoutForAlphabet());
      assertEquals(
          testData.toString(), testData.expectedFlickSensitivity, preference.getFlickSensitivity());
      assertEquals(
          testData.toString(), testData.expectedLayoutAdjustment, preference.getLayoutAdjustment());
      assertEquals(
          testData.toString(), testData.expectedFullscreenMode, preference.isFullscreenMode());
      assertEquals(testData.toString(), testData.expectedSkinType, preference.getSkinType());
    }
  }
  private Configuration getConfiguration() {
    Configuration config = new Configuration();

    HardwareConfig hardwareConfig = mParams.getHardwareConfig();

    ScreenSize screenSize = hardwareConfig.getScreenSize();
    if (screenSize != null) {
      switch (screenSize) {
        case SMALL:
          config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_SMALL;
          break;
        case NORMAL:
          config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_NORMAL;
          break;
        case LARGE:
          config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_LARGE;
          break;
        case XLARGE:
          config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_XLARGE;
          break;
      }
    }

    Density density = hardwareConfig.getDensity();
    if (density == null) {
      density = Density.MEDIUM;
    }

    config.screenWidthDp = hardwareConfig.getScreenWidth() / density.getDpiValue();
    config.screenHeightDp = hardwareConfig.getScreenHeight() / density.getDpiValue();
    if (config.screenHeightDp < config.screenWidthDp) {
      //noinspection SuspiciousNameCombination
      config.smallestScreenWidthDp = config.screenHeightDp;
    } else {
      config.smallestScreenWidthDp = config.screenWidthDp;
    }
    config.densityDpi = density.getDpiValue();

    // never run in compat mode:
    config.compatScreenWidthDp = config.screenWidthDp;
    config.compatScreenHeightDp = config.screenHeightDp;

    ScreenOrientation orientation = hardwareConfig.getOrientation();
    if (orientation != null) {
      switch (orientation) {
        case PORTRAIT:
          config.orientation = Configuration.ORIENTATION_PORTRAIT;
          break;
        case LANDSCAPE:
          config.orientation = Configuration.ORIENTATION_LANDSCAPE;
          break;
        case SQUARE:
          //noinspection deprecation
          config.orientation = Configuration.ORIENTATION_SQUARE;
          break;
      }
    } else {
      config.orientation = Configuration.ORIENTATION_UNDEFINED;
    }

    // TODO: fill in more config info.

    return config;
  }