@UsesMocks({SocializeEntityLoader.class, EntityLoaderUtils.class})
  public void testShowEntityById() {
    final Entity entity = new Entity();

    SocializeEntityUtils utils =
        new SocializeEntityUtils() {
          @Override
          public void getEntity(Activity context, long id, EntityGetListener listener) {
            listener.onGet(entity);
          }
        };

    SocializeEntityLoader entityLoader = AndroidMock.createMock(SocializeEntityLoader.class);
    EntityLoaderUtils entityLoaderUtils = AndroidMock.createMock(EntityLoaderUtils.class);

    AndroidMock.expect(entityLoaderUtils.initEntityLoader()).andReturn(entityLoader);
    AndroidMock.expect(entityLoader.canLoad(TestUtils.getActivity(this), entity)).andReturn(true);

    entityLoader.loadEntity(TestUtils.getActivity(this), entity);

    AndroidMock.replay(entityLoaderUtils, entityLoader);

    utils.setEntityLoaderUtils(entityLoaderUtils);
    utils.showEntity(TestUtils.getActivity(this), 123, null);

    AndroidMock.verify(entityLoaderUtils, entityLoader);
  }
  public void testEntityMetaDataWithUrl() throws Exception {

    final String entityKey = "testEntityMetaDataWithUrl" + Math.random();

    Entity entity = Entity.newInstance(entityKey, "testAddEntity");

    String url = "http://www.getsocialize.com";

    JSONObject metaData = new JSONObject();
    metaData.put("url", url);

    String jsonString = metaData.toString();

    entity.setMetaData(jsonString);

    final CountDownLatch latch = new CountDownLatch(1);

    // Force no config
    ConfigUtils.getConfig(getContext())
        .setProperty(SocializeConfig.SOCIALIZE_REQUIRE_AUTH, "false");

    EntityUtils.saveEntity(
        TestUtils.getActivity(this),
        entity,
        new EntityAddListener() {

          @Override
          public void onError(SocializeException error) {
            error.printStackTrace();
            addResult(0, error);
            latch.countDown();
          }

          @Override
          public void onCreate(Entity entity) {
            addResult(0, entity);
            latch.countDown();
          }
        });

    latch.await(20, TimeUnit.SECONDS);

    Object result = getResult(0);

    assertNotNull(result);
    assertTrue("Result is not a entity object", (result instanceof Entity));

    Entity entityAfter = (Entity) result;

    String metaDataAfter = entityAfter.getMetaData();

    assertNotNull(metaDataAfter);

    JSONObject json = new JSONObject(metaDataAfter);
    String urlAfter = json.getString("url");

    assertEquals(url, urlAfter);
  }
  public void testGetEntities() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);

    EntityUtils.getEntities(
        getContext(),
        0,
        100,
        new EntityListListener() {

          @Override
          public void onList(ListResult<Entity> entities) {
            addResult(0, entities);
            latch.countDown();
          }

          @Override
          public void onError(SocializeException error) {
            error.printStackTrace();
            latch.countDown();
          }
        });

    latch.await(20, TimeUnit.SECONDS);

    ListResult<Entity> after = getResult(0);

    assertNotNull(after);
    assertTrue(after.size() >= 2);

    // Make sure it contains what we expect.
    JSONObject json = TestUtils.getJSON(getContext(), "entities.json");
    JSONArray jsonArray = json.getJSONArray("items");

    JSONObject jsonObject0 = (JSONObject) jsonArray.get(0);
    JSONObject jsonObject1 = (JSONObject) jsonArray.get(1);

    String key0 = jsonObject0.getString("key");
    String key1 = jsonObject1.getString("key");

    List<Entity> items = after.getItems();

    int found = 0;

    for (Entity entity : items) {
      if (entity.getKey().equals(key0) || entity.getKey().equals(key1)) {
        found++;
      }

      if (found >= 2) {
        break;
      }
    }

    assertEquals(2, found);
  }
  public void testOnActionBarShareEventListenerIsCalledOnDialogDisplay() throws Throwable {

    Activity activity = TestUtils.getActivity(this);

    ConfigUtils.getConfig(activity).setProperty(SocializeConfig.FACEBOOK_APP_ID, "");
    ConfigUtils.getConfig(activity).setProperty(SocializeConfig.TWITTER_CONSUMER_KEY, "");
    ConfigUtils.getConfig(activity).setProperty(SocializeConfig.TWITTER_CONSUMER_SECRET, "");
    ConfigUtils.getConfig(activity).setProperty(SocializeConfig.SOCIALIZE_REQUIRE_AUTH, "false");

    ShareUtils.preloadShareDialog(TestUtils.getActivity(this));
    ShareUtils.preloadLinkDialog(TestUtils.getActivity(this));

    final ActionBarView actionBar = TestUtils.findView(activity, ActionBarView.class, 5000);
    final ActionBarLayoutView actionBarView =
        TestUtils.findView(activity, ActionBarLayoutView.class, 10000);

    assertNotNull(actionBar);

    final Like like = new Like();

    like.setId(-1L);
    like.setEntity(entity);

    final CountDownLatch shareLatch = new CountDownLatch(1);
    final CountDownLatch dialogLatch = new CountDownLatch(1);

    final SocializeLikeUtils mockLikeUtils =
        new SocializeLikeUtils() {
          @Override
          public void getLike(Activity context, String entityKey, LikeGetListener listener) {
            listener.onGet(like);
          }
        };

    actionBar.setOnActionBarEventListener(
        new OnActionBarShareEventListener() {

          @Override
          public void onShow(Dialog dialog, SharePanelView dialogView) {
            TestUtils.addResult(dialogView);
            dialogLatch.countDown();
          }
        });

    SocializeAccess.setLikeUtilsProxy(mockLikeUtils);

    // Simulate share

    this.runTestOnUiThread(
        new Runnable() {
          @Override
          public void run() {
            assertTrue(actionBarView.getShareButton().performClick());
            shareLatch.countDown();
          }
        });

    assertTrue(shareLatch.await(5, TimeUnit.SECONDS));
    dialogLatch.await(90, TimeUnit.SECONDS);

    SharePanelView dialogView = TestUtils.getResult(0);

    assertNotNull(dialogView);
  }
  public void testAddEntity() throws Exception {
    final String entityKey = "testAddEntity" + Math.random();
    Entity entity = Entity.newInstance(entityKey, "testAddEntity");

    final CountDownLatch latch = new CountDownLatch(1);

    // Force no config
    ConfigUtils.getConfig(getContext())
        .setProperty(SocializeConfig.SOCIALIZE_REQUIRE_AUTH, "false");

    EntityUtils.saveEntity(
        TestUtils.getActivity(this),
        entity,
        new EntityAddListener() {

          @Override
          public void onError(SocializeException error) {
            error.printStackTrace();
            addResult(0, error);
            latch.countDown();
          }

          @Override
          public void onCreate(Entity entity) {
            addResult(0, entity);
            latch.countDown();
          }
        });

    latch.await(20, TimeUnit.SECONDS);

    Object result = getResult(0);

    assertNotNull(result);
    assertTrue("Result is not a entity object", (result instanceof Entity));

    Entity entityAfter = (Entity) result;
    assertTrue("Entity ID is not greater than 0", entityAfter.getId() > 0);

    // Now use the normal UI to retrieve the entity
    clearResults();
    final CountDownLatch latch2 = new CountDownLatch(1);

    EntityUtils.getEntity(
        TestUtils.getActivity(this),
        entityAfter.getId(),
        new EntityGetListener() {

          @Override
          public void onGet(Entity entity) {
            addResult(0, entity);
            latch2.countDown();
          }

          @Override
          public void onError(SocializeException error) {
            error.printStackTrace();
            latch2.countDown();
          }
        });

    latch2.await(20, TimeUnit.SECONDS);

    Entity after = getResult(0);

    assertNotNull(after);
    assertEquals(entityAfter.getId(), after.getId());
    assertEquals(entityAfter.getKey(), after.getKey());
  }