@Test
 public void testEntityBecomesTemporaryIfForceBlockActiveComponentRemoved() {
   EntityRef blockEntity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
   blockEntity.addComponent(new ForceBlockActiveComponent());
   worldProvider.update(1.0f);
   blockEntity.removeComponent(ForceBlockActiveComponent.class);
   worldProvider.update(1.0f);
   assertFalse(blockEntity.exists());
   assertFalse(blockEntity.isActive());
 }
 @Test
 public void testEntityBecomesTemporaryWhenChangedFromAKeepActiveBlock() {
   worldProvider.setBlock(Vector3i.zero(), keepActiveBlock);
   EntityRef blockEntity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
   worldProvider.setBlock(Vector3i.zero(), BlockManager.getAir());
   worldProvider.update(1.0f);
   assertFalse(blockEntity.isActive());
 }
 @Test
 public void testEntityNotRemovedIfForceBlockActiveComponentAdded() {
   EntityRef blockEntity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
   blockEntity.addComponent(new ForceBlockActiveComponent());
   worldProvider.update(1.0f);
   assertTrue(blockEntity.exists());
   assertTrue(blockEntity.isActive());
 }
 @Test
 public void testEntityCeasesToBeTemporaryIfBlockChangedToKeepActive() {
   worldProvider.setBlock(Vector3i.zero(), keepActiveBlock);
   worldProvider.update(1.0f);
   LifecycleEventChecker checker =
       new LifecycleEventChecker(entityManager.getEventSystem(), StringComponent.class);
   worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
   assertTrue(checker.receivedEvents.isEmpty());
 }
 @Test
 public void testTemporaryCleanedUpWithNoEvent() {
   BlockEventChecker checker = new BlockEventChecker();
   entityManager.getEventSystem().registerEventHandler(checker);
   EntityRef blockEntity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
   worldProvider.update(1.0f);
   assertFalse(blockEntity.exists());
   assertFalse(checker.addedReceived);
   assertFalse(checker.activateReceived);
   assertFalse(checker.deactivateReceived);
   assertFalse(checker.removedReceived);
 }
  @Test
  public void testEntityExtraComponentsRemovedBeforeCleanUp() {
    EntityRef entity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
    entity.addComponent(new StringComponent("test"));

    LifecycleEventChecker checker =
        new LifecycleEventChecker(entityManager.getEventSystem(), StringComponent.class);

    worldProvider.update(1.0f);
    assertEquals(
        Lists.newArrayList(
            new EventInfo(BeforeDeactivateComponent.newInstance(), entity),
            new EventInfo(BeforeRemoveComponent.newInstance(), entity)),
        checker.receivedEvents);
  }
  @Test
  public void testChangedComponentsRevertedBeforeCleanUp() {
    worldStub.setBlock(Vector3i.zero(), blockWithString);
    EntityRef entity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
    StringComponent comp = entity.getComponent(StringComponent.class);
    comp.value = "Moo";
    entity.saveComponent(comp);

    LifecycleEventChecker checker =
        new LifecycleEventChecker(entityManager.getEventSystem(), StringComponent.class);

    worldProvider.update(1.0f);
    assertEquals(
        Lists.newArrayList(new EventInfo(OnChangedComponent.newInstance(), entity)),
        checker.receivedEvents);
  }
  @Test
  public void testEntityMissingComponentsAddedBeforeCleanUp() {
    worldStub.setBlock(Vector3i.zero(), blockWithString);
    EntityRef entity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
    entity.removeComponent(StringComponent.class);

    LifecycleEventChecker checker =
        new LifecycleEventChecker(entityManager.getEventSystem(), StringComponent.class);

    worldProvider.update(1.0f);
    assertEquals(
        Lists.newArrayList(
            new EventInfo(OnAddedComponent.newInstance(), entity),
            new EventInfo(OnActivatedComponent.newInstance(), entity)),
        checker.receivedEvents);
  }
  @Test
  public void testEntityExtraComponentsRemovedBeforeCleanUpForBlocksWithPrefabs() {
    worldStub.setBlock(Vector3i.zero(), blockWithString);
    EntityRef entity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
    entity.addComponent(new IntegerComponent(1));

    LifecycleEventChecker checker =
        new LifecycleEventChecker(entityManager.getEventSystem(), IntegerComponent.class);

    worldProvider.update(1.0f);
    assertEquals(
        Lists.newArrayList(
            new EventInfo(BeforeDeactivateComponent.newInstance(), entity),
            new EventInfo(BeforeRemoveComponent.newInstance(), entity)),
        checker.receivedEvents);
  }
  @Test
  public void networkComponentRemovedWhenTemporaryCleanedUp() {
    EntityRef entity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
    entity.addComponent(new RetainedOnBlockChangeComponent(2));

    LifecycleEventChecker checker =
        new LifecycleEventChecker(entityManager.getEventSystem(), NetworkComponent.class);
    entity.removeComponent(RetainedOnBlockChangeComponent.class);

    worldProvider.update(1.0f);

    assertEquals(
        Lists.newArrayList(
            new EventInfo(BeforeDeactivateComponent.newInstance(), entity),
            new EventInfo(BeforeRemoveComponent.newInstance(), entity)),
        checker.receivedEvents);
  }
  @Test
  public void testActiveBlockNotCleanedUp() {
    Block testBlock = new Block();
    testBlock.setKeepActive(true);
    BlockFamily blockFamily = new SymmetricFamily(new BlockUri("test:keepActive"), testBlock);
    blockManager.addBlockFamily(blockFamily, true);
    worldStub.setBlock(Vector3i.zero(), testBlock);

    BlockEventChecker checker = new BlockEventChecker();
    entityManager.getEventSystem().registerEventHandler(checker);

    EntityRef blockEntity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
    worldProvider.update(1.0f);
    assertTrue(blockEntity.exists());
    assertTrue(blockEntity.isActive());
    assertTrue(checker.addedReceived);
    assertTrue(checker.activateReceived);
  }