/** Remove cache from DB and cache to ensure that the cache is not loaded from the database */
  private static void deleteCacheFromDBAndLogout(String geocode) {
    cgeoapplication.getInstance().removeCache(geocode, LoadFlags.REMOVEALL);

    cgBase.logout();
    // Modify login data to avoid an automatic login again
    Settings.setLogin("c:geo", "c:geo");
  }
  /** Test {@link cgBase#searchByGeocode(String, String, int, boolean, CancellableHandler)} */
  @MediumTest
  public static void testSearchByGeocodeNotLoggedIn() {
    ImmutablePair<String, String> login = Settings.getLogin();

    try {
      // non premium cache
      MockedCache cache = new GC1ZXX2();

      deleteCacheFromDBAndLogout(cache.getGeocode());

      SearchResult search =
          cgBase.searchByGeocode(
              cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null);
      assertNotNull(search);
      assertEquals(1, search.getGeocodes().size());
      assertTrue(search.getGeocodes().contains(cache.getGeocode()));
      cgCache searchedCache = search.getFirstCacheFromResult(LoadFlags.LOADCACHEORDB);
      // coords must be null if the user is not logged in
      assertNull(searchedCache.getCoords());

      // premium cache. Not visible to guests
      cache = new GC2JVEH();

      deleteCacheFromDBAndLogout(cache.getGeocode());

      search =
          cgBase.searchByGeocode(
              cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null);
      assertNotNull(search);
      assertEquals(0, search.getGeocodes().size());

    } finally {
      // restore user and password
      Settings.setLogin(login.left, login.right);
      cgBase.login();
    }
  }
  /** Test {@link cgBase#searchByViewport(String, Viewport)} */
  @MediumTest
  public static void testSearchByViewportNotLoggedIn() {

    if (LIVEMAPENABLED) {
      ImmutablePair<String, String> login = Settings.getLogin();

      try {

        final String token = null; // without a valid token we are "logged off"

        // non premium cache
        MockedCache cache = new GC2CJPF();
        deleteCacheFromDBAndLogout(cache.getGeocode());

        Viewport viewport = new Viewport(cache.getCoords(), 0.003, 0.003);
        SearchResult search = cgBase.searchByViewport(token, viewport);

        assertNotNull(search);
        assertTrue(search.getGeocodes().contains(cache.getGeocode()));
        // coords differ
        Log.d(
            Settings.tag,
            "cgeoApplicationTest.testSearchByViewportNotLoggedIn: Coords expected = "
                + cache.getCoords());
        Log.d(
            Settings.tag,
            "cgeoApplicationTest.testSearchByViewportNotLoggedIn: Coords actual = "
                + cgeoapplication
                    .getInstance()
                    .loadCache(cache.getGeocode(), LoadFlags.LOADCACHEORDB)
                    .getCoords());
        assertFalse(
            cache
                .getCoords()
                .isEqualTo(
                    cgeoapplication
                        .getInstance()
                        .loadCache(cache.getGeocode(), LoadFlags.LOADCACHEORDB)
                        .getCoords(),
                    1e-3));
        assertFalse(
            cgeoapplication
                .getInstance()
                .loadCache(cache.getGeocode(), LoadFlags.LOADCACHEORDB)
                .isReliableLatLon());

        // premium cache
        cache = new GC2JVEH();
        deleteCacheFromDBAndLogout(cache.getGeocode());

        viewport = new Viewport(cache.getCoords(), 0.003, 0.003);
        search = cgBase.searchByViewport(token, viewport);

        assertNotNull(search);
        // It's a premium member cache only and thus not visible to guests
        assertFalse(search.getGeocodes().contains(cache.getGeocode()));

      } finally {
        // restore user and password
        Settings.setLogin(login.left, login.right);
        cgBase.login();
      }
    }
  }