@NonNull @Override
  public Object instantiateItem(@NonNull ViewGroup container, int position) {
    View root =
        mLayoutInflater.inflate(R.layout.quick_text_popup_autorowkeyboard_view, container, false);
    ScrollViewWithDisable scrollViewWithDisable =
        root.findViewById(R.id.scroll_root_for_quick_test_keyboard);
    scrollViewWithDisable.setPadding(
        scrollViewWithDisable.getPaddingLeft(),
        scrollViewWithDisable.getPaddingTop(),
        scrollViewWithDisable.getPaddingRight(),
        scrollViewWithDisable.getPaddingBottom() + mBottomPadding);
    container.addView(root);

    final QuickKeysKeyboardView keyboardView = root.findViewById(R.id.keys_container);
    keyboardView.setKeyboardTheme(mKeyboardTheme);
    keyboardView.setOnPopupShownListener(
        new PopupKeyboardShownHandler(mViewPager, scrollViewWithDisable));
    keyboardView.setOnKeyboardActionListener(mKeyboardActionListener);
    QuickTextKey addOn = mAddOns[position];
    AnyPopupKeyboard keyboard = mPopupKeyboards[position];
    if (keyboard == null || position == 0 /*ALWAYS re-create history, in case it has changed*/) {
      if (addOn.isPopupKeyboardUsed()) {
        keyboard =
            new AnyPopupKeyboard(
                addOn,
                mContext,
                addOn.getPopupKeyboardResId(),
                keyboardView.getThemedKeyboardDimens(),
                addOn.getName(),
                mDefaultSkinTonePrefTracker.getDefaultSkinTone(),
                mDefaultGenderPrefTracker.getDefaultGender());
      } else {
        keyboard =
            new PopupListKeyboard(
                mDefaultLocalAddOn,
                mContext,
                keyboardView.getThemedKeyboardDimens(),
                addOn.getPopupListNames(),
                addOn.getPopupListValues(),
                addOn.getName());
      }
      mPopupKeyboards[position] = keyboard;
      final int keyboardViewMaxWidth = keyboardView.getThemedKeyboardDimens().getKeyboardMaxWidth();
      mIsAutoFitKeyboards[position] =
          keyboard.getMinWidth() > keyboardViewMaxWidth || addOn instanceof HistoryQuickTextKey;
      if (mIsAutoFitKeyboards[position]) {
        // fixing up the keyboard, so it will fit nicely in the width
        int currentY = 0;
        int xSub = 0;
        for (Keyboard.Key key : keyboard.getKeys()) {
          key.y = currentY;
          key.x -= xSub;
          if (Keyboard.Key.getEndX(key) > keyboardViewMaxWidth) {
            currentY += key.height;
            xSub += key.x;
            key.y = currentY;
            key.x = 0;
          }
        }
        keyboard.resetDimensions();
      }
    }
    keyboardView.setKeyboard(keyboard);
    return root;
  }
@Test
  public void testInstantiateItem() throws Exception {
    ViewGroup container = new LinearLayout(getApplicationContext());
    Object instance0 = mUnderTest.instantiateItem(container, 0);
    Assert.assertNotNull(instance0);
    Assert.assertTrue(instance0 instanceof ScrollViewWithDisable);
    Assert.assertEquals(1, container.getChildCount());
    Assert.assertSame(instance0, container.getChildAt(0));
    //noinspection
    Mockito.verify(mSkinTonePrefTracker).getDefaultSkinTone();
    Mockito.verify(mGenderTracker).getDefaultGender();

    final QuickKeysKeyboardView keyboardView0 =
        ((View) instance0).findViewById(R.id.keys_container);
    Assert.assertNotNull(keyboardView0);

    Object instance1 = mUnderTest.instantiateItem(container, 1);
    Assert.assertNotNull(instance1);
    Assert.assertNotSame(instance0, instance1);
    final QuickKeysKeyboardView keyboardView1 =
        ((View) instance1).findViewById(R.id.keys_container);
    Assert.assertNotNull(keyboardView1);

    Assert.assertNotEquals(
        keyboardView0.getKeyboard().getKeyboardAddOn().getId(),
        keyboardView1.getKeyboard().getKeyboardAddOn().getId());

    Object instance0Again = mUnderTest.instantiateItem(container, 0);
    Assert.assertNotNull(instance0Again);
    Assert.assertNotSame(instance0, instance0Again);
    final QuickKeysKeyboardView keyboardView0Again =
        ((View) instance0Again).findViewById(R.id.keys_container);
    Assert.assertNotNull(keyboardView0Again);
    // the history is always recreated!
    Assert.assertNotSame(keyboardView0.getKeyboard(), keyboardView0Again.getKeyboard());
    // making sure the keyboard DOES NOT have a background - this is because we want the
    // background to be used in the pager container.
    Assert.assertNull(keyboardView0.getBackground());
    Assert.assertNull(null, keyboardView1.getBackground());

    // adds padding
    Assert.assertEquals(11, ((View) instance0).getPaddingBottom());

    // the other views (not history) ARE NOT RECREATED!
    Object instance1Again = mUnderTest.instantiateItem(container, 1);
    Assert.assertNotNull(instance1Again);
    Assert.assertNotSame(instance1, instance1Again);
    final QuickKeysKeyboardView keyboardView1Again =
        ((View) instance1Again).findViewById(R.id.keys_container);
    Assert.assertNotNull(keyboardView1Again);
    // non history is not recreated!
    Assert.assertSame(keyboardView1.getKeyboard(), keyboardView1Again.getKeyboard());
  }