/** * @return This component's key or null if none is set. */ @Nullable public String getKey() { return mNode.getComponents().get(mComponentIndex).getKey(); }
private void checkIsDuplicateKey(Component component) { if (mKnownGlobalKeys.contains(component.getGlobalKey())) { final String message = "Found another " + component.getSimpleName() + " Component with the same key: " + component.getKey(); final String errorMessage = mLogger == null ? message : getDuplicateKeyMessage(); if (component.hasState()) { throw new RuntimeException(message + "\n" + errorMessage); } if (mLogger != null) { mLogger.emitMessage(ComponentsLogger.LogLevel.ERROR, message + "\n" + errorMessage); } } }
builder.append('['); if (component.hasManualKey()) { builder.append("manual.key=\"").append(component.getKey()).append("\";");
private String generateKey(ComponentContext parentContext) { final Component parentScope = parentContext.getComponentScope(); final String key = getKey(); final String globalKey; if (parentScope == null) { globalKey = key; } else { if (parentScope.getGlobalKey() == null) { final ComponentsLogger logger = parentContext.getLogger(); if (logger != null) { logger.emitMessage( ComponentsLogger.LogLevel.ERROR, "Trying to generate parent-based key for component " + getSimpleName() + " , but parent " + parentScope.getSimpleName() + " has a null global key \"." + " This is most likely a configuration mistake, check the value of ComponentsConfiguration.useGlobalKeys."); } globalKey = "null" + key; } else { globalKey = parentScope.generateUniqueGlobalKeyForChild(this, key); } } return globalKey; }
@Test public void testComponentManualKey() { Component component = TestDrawableComponent.create(mContext).key("someKey").build(); Assert.assertEquals(component.getKey(), "someKey"); Assert.assertNull(component.getGlobalKey()); }
@Test public void testComponentKey() { Component component = TestDrawableComponent.create(mContext).build(); Assert.assertEquals(component.getKey(), component.getTypeId() + ""); Assert.assertNull(component.getGlobalKey()); }
@Test public void testComponentKey() { Component component = TestDrawableComponent.create(mContext).build(); Assert.assertEquals(component.getKey(), component.getTypeId() + ""); Assert.assertNull(component.getGlobalKey()); }
@Test public void testComponentManualKey() { Component component = TestDrawableComponent.create(mContext).key("someKey").build(); Assert.assertEquals(component.getKey(), "someKey"); Assert.assertNull(component.getGlobalKey()); }
@Test public void testRootComponentGlobalKey() { final Component component = TestDrawableComponent.create(mContext).widthDip(10).heightDip(10).build(); final LithoView lithoView = getLithoView(component); Assert.assertEquals( lithoView.getMountItemAt(0).getComponent().getGlobalKey(), component.getKey()); }
@Test public void testRootComponentGlobalKey() { final Component component = TestDrawableComponent.create(mContext).widthDip(10).heightDip(10).build(); final LithoView lithoView = getLithoView(component); Assert.assertEquals( lithoView.getMountItemAt(0).getComponent().getGlobalKey(), component.getKey()); }
@Before public void setup() { MockitoAnnotations.initMocks(this); ComponentsConfiguration.isNestedTreeResolutionExperimentEnabled = true; mContext = new ComponentContext(RuntimeEnvironment.application); mComponent = new InlineLayoutSpec() { @Override @OnCreateLayout protected Component onCreateLayout(ComponentContext c) { return Row.create(c) .marginPx(YogaEdge.LEFT, MARGIN_LEFT) .marginPx(YogaEdge.TOP, MARGIN_TOP) .child( TestDrawableComponent.create(c) .key(KEY_ANCHOR) .widthPx(ANCHOR_WIDTH) .heightPx(ANCHOR_HEIGHT)) .build(); } }; mComponentTree = ComponentTree.create(mContext, mComponent).build(); Whitebox.setInternalState(mComponent, "mGlobalKey", mComponent.getKey()); mContext = ComponentContext.withComponentTree(mContext, mComponentTree); mContext = ComponentContext.withComponentScope(mContext, mComponent); mLithoView = getLithoView(mComponentTree); mAnchorGlobalKey = ComponentKeyUtils.getKeyWithSeparator(Row.create(mContext).build().getTypeId(), KEY_ANCHOR); }