END_TEST

START_TEST (test_empathy_chatroom_manager_remove)
{
  EmpathyChatroomManager *mgr;
  gchar *file;
  EmpathyAccount *account;
  struct chatroom_t chatrooms[] = {
        { "name2", "room2", FALSE, TRUE }};
  EmpathyChatroom *chatroom;
  EmpathyAccountManager *account_mgr;

  account_mgr = empathy_account_manager_dup_singleton ();
  account = get_test_account ();

  copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);

  file = get_user_xml_file (CHATROOM_FILE);

  /* change the chatrooms XML file to use the account we just created */
  fail_unless (change_account_name_in_file (account, file));

  mgr = empathy_chatroom_manager_dup_singleton (file);

  /* remove room1 */
  chatroom = empathy_chatroom_manager_find (mgr, account, "room1");
  fail_if (chatroom == NULL);
  empathy_chatroom_manager_remove (mgr, chatroom);

  check_chatrooms_list (mgr, account, chatrooms, 1);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_dup_singleton (file);

  check_chatrooms_list (mgr, account, chatrooms, 1);

  /* remove room1 */
  chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
  fail_if (chatroom == NULL);

  empathy_chatroom_manager_remove (mgr, chatroom);

  check_chatrooms_list (mgr, account, chatrooms, 0);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_dup_singleton (file);

  check_chatrooms_list (mgr, account, chatrooms, 0);

  g_object_unref (mgr);
  g_free (file);
  g_object_unref (account);
  g_object_unref (account_mgr);
}
END_TEST

START_TEST (test_empathy_chatroom_manager_change_chatroom)
{
  EmpathyChatroomManager *mgr;
  gchar *file;
  EmpathyAccount *account;
  EmpathyAccountManager *account_manager;
  struct chatroom_t chatrooms[] = {
        { "name1", "room1", TRUE, TRUE },
        { "name2", "room2", FALSE, TRUE }};
  EmpathyChatroom *chatroom;

  account_manager = empathy_account_manager_dup_singleton ();
  account = get_test_account ();

  copy_xml_file (CHATROOM_SAMPLE, "foo.xml");

  file = get_user_xml_file ("foo.xml");

  /* change the chatrooms XML file to use the account we just created */
  fail_unless (change_account_name_in_file (account, file));

  mgr = empathy_chatroom_manager_dup_singleton (file);

  check_chatrooms_list (mgr, account, chatrooms, 2);

  /* change room2 name */
  chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
  fail_if (chatroom == NULL);
  empathy_chatroom_set_name (chatroom, "new_name");

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_dup_singleton (file);

  chatrooms[1].name = "new_name";
  check_chatrooms_list (mgr, account, chatrooms, 2);

  /* change room2 auto-connect status */
  chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
  fail_if (chatroom == NULL);
  empathy_chatroom_set_auto_connect (chatroom, TRUE);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_dup_singleton (file);

  chatrooms[1].auto_connect = TRUE;
  check_chatrooms_list (mgr, account, chatrooms, 2);

  /* change room2 room */
  chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
  fail_if (chatroom == NULL);
  empathy_chatroom_set_room (chatroom, "new_room");

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_dup_singleton (file);

  chatrooms[1].room = "new_room";
  check_chatrooms_list (mgr, account, chatrooms, 2);

  g_object_unref (mgr);
  g_free (file);
  g_object_unref (account);
  g_object_unref (account_manager);
}
END_TEST

START_TEST (test_empathy_chatroom_manager_change_favorite)
{
  EmpathyChatroomManager *mgr;
  gchar *file;
  EmpathyAccount *account;
  EmpathyAccountManager *account_manager;
  struct chatroom_t chatrooms[] = {
        { "name1", "room1", TRUE, TRUE },
        { "name2", "room2", FALSE, FALSE }};
  EmpathyChatroom *chatroom;

  account_manager = empathy_account_manager_dup_singleton ();
  account = get_test_account ();

  copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);

  file = get_user_xml_file (CHATROOM_FILE);

  /* change the chatrooms XML file to use the account we just created */
  fail_unless (change_account_name_in_file (account, file));

  mgr = empathy_chatroom_manager_dup_singleton (file);

  /* room2 is not favorite anymore */
  chatroom = empathy_chatroom_manager_find (mgr, account, "room2");
  fail_if (chatroom == NULL);
  g_object_set (chatroom, "favorite", FALSE, NULL);

  check_chatrooms_list (mgr, account, chatrooms, 2);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_dup_singleton (file);

  /* room2 is not present in the XML file anymore as it's not a favorite */
  check_chatrooms_list (mgr, account, chatrooms, 1);

  /* re-add room2 */
  chatroom = empathy_chatroom_new_full (account, "room2", "name2", FALSE);
  empathy_chatroom_manager_add (mgr, chatroom);

  check_chatrooms_list (mgr, account, chatrooms, 2);

  /* set room2 as favorite */
  g_object_set (chatroom, "favorite", TRUE, NULL);

  chatrooms[1].favorite = TRUE;
  check_chatrooms_list (mgr, account, chatrooms, 2);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_dup_singleton (file);

  /* room2 is back in the XML file now */
  check_chatrooms_list (mgr, account, chatrooms, 2);

  g_object_unref (mgr);
  g_object_unref (chatroom);
  g_free (file);
  g_object_unref (account_manager);
  g_object_unref (account);
}
END_TEST

START_TEST (test_empathy_chatroom_manager_add)
{
  EmpathyChatroomManager *mgr;
  gchar *file;
  EmpathyAccount *account;
  EmpathyAccountManager *account_manager;
  struct chatroom_t chatrooms[] = {
        { "name1", "room1", TRUE, TRUE },
        { "name2", "room2", FALSE, TRUE },
        { "name3", "room3", FALSE, TRUE },
        { "name4", "room4", FALSE, FALSE }};
  EmpathyChatroom *chatroom;

  account_manager = empathy_account_manager_dup_singleton ();

  account = get_test_account ();

  copy_xml_file (CHATROOM_SAMPLE, CHATROOM_FILE);

  file = get_user_xml_file (CHATROOM_FILE);

  /* change the chatrooms XML file to use the account we just created */
  fail_unless (change_account_name_in_file (account, file));

  mgr = empathy_chatroom_manager_dup_singleton (file);

  /* add a favorite chatroom */
  chatroom = empathy_chatroom_new_full (account, "room3", "name3", FALSE);
  g_object_set (chatroom, "favorite", TRUE, NULL);
  empathy_chatroom_manager_add (mgr, chatroom);
  g_object_unref (chatroom);

  check_chatrooms_list (mgr, account, chatrooms, 3);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_dup_singleton (file);

  /* chatroom has been added to the XML file as it's a favorite */
  check_chatrooms_list (mgr, account, chatrooms, 3);

  /* add a non favorite chatroom */
  chatroom = empathy_chatroom_new_full (account, "room4", "name4", FALSE);
  g_object_set (chatroom, "favorite", FALSE, NULL);
  empathy_chatroom_manager_add (mgr, chatroom);
  g_object_unref (chatroom);

  check_chatrooms_list (mgr, account, chatrooms, 4);

  /* reload chatrooms file */
  g_object_unref (mgr);
  mgr = empathy_chatroom_manager_dup_singleton (file);

  /* chatrooms has not been added to the XML file */
  check_chatrooms_list (mgr, account, chatrooms, 3);

  g_object_unref (mgr);
  g_free (file);
  g_object_unref (account_manager);
  g_object_unref (account);
}
END_TEST

START_TEST (test_modify_both_files)
{
  EmpathyIrcNetworkManager *mgr;
  EmpathyIrcNetwork *network;
  EmpathyIrcServer *server;
  gchar *global_file, *user_file;
  GSList *networks, *l;
  struct server_t gimpnet_servers[] = {
    { "irc.gimp.org", 6667, TRUE },
    { "irc.us.gimp.org", 6668, FALSE }};
  struct server_t great_server[] = {
    { "irc.greatserver.com", 7873, TRUE }};
  struct server_t another_server[] = {
    { "irc.anothersrv.be", 6660, FALSE }};
  struct server_t undernet_servers[] = {
    { "eu.undernet.org", 6667, FALSE },
    { "us.undernet.org", 6667, FALSE }};
  gboolean network_modified[4];
  gboolean network_checked[4];
  gchar *global_file_orig, *user_file_orig;

  copy_xml_file (USER_SAMPLE, USER_FILE);
  global_file_orig = get_xml_file (GLOBAL_SAMPLE);
  user_file_orig = get_user_xml_file (USER_FILE);
  mgr = empathy_irc_network_manager_new (global_file_orig, user_file_orig);

  g_object_get (mgr,
      "global-file", &global_file,
      "user-file", &user_file,
      NULL);
  fail_if (global_file == NULL || strcmp (global_file, global_file_orig) != 0);
  fail_if (user_file == NULL || strcmp (user_file, user_file_orig) != 0);
  g_free (global_file);
  g_free (global_file_orig);
  g_free (user_file);
  g_free (user_file_orig);

  networks = empathy_irc_network_manager_get_networks (mgr);
  fail_if (g_slist_length (networks) != 5);

  network_modified[0] = network_modified[1] = network_modified[2] =
    network_modified[3] = FALSE;
  /* check networks and servers */
  for (l = networks; l != NULL; l = g_slist_next (l))
    {
      EmpathyIrcNetwork *network;
      gchar *name;

      network = l->data;
      g_object_get (network, "name", &name, NULL);
      fail_if (name == NULL);

      if (strcmp (name, "GIMPNet") == 0)
        {
          /* Modify user network */
          GSList *servers, *ll;

          servers = empathy_irc_network_get_servers (network);
          for (ll = servers; ll != NULL; ll = g_slist_next (ll))
            {
              EmpathyIrcServer *server;
              gchar *address;

              server = ll->data;
              g_object_get (server, "address", &address, NULL);
              if (strcmp (address, "irc.gimp.org") == 0)
                {
                  /* change SSL */
                  g_object_set (server, "ssl", TRUE, NULL);
                }
              else if (strcmp (address, "irc.us.gimp.org") == 0)
                {
                  /* change port */
                  g_object_set (server, "port", 6668, NULL);
                }
              else if (strcmp (address, "irc.au.gimp.org") == 0)
                {
                  /* remove this server */
                  empathy_irc_network_remove_server (network, server);
                }
              else
                {
                  fail_if (TRUE);
                }

              g_free (address);
            }

          network_modified[0] = TRUE;

          g_slist_foreach (servers, (GFunc) g_object_unref, NULL);
          g_slist_free (servers);
        }
      else if (strcmp (name, "My Server") == 0)
        {
          /* remove user network */
          empathy_irc_network_manager_remove (mgr, network);
          network_modified[1] = TRUE;
        }
      else if (strcmp (name, "Freenode") == 0)
        {
          /* remove global network */
          empathy_irc_network_manager_remove (mgr, network);
          network_modified[2] = TRUE;
        }
      else if (strcmp (name, "Undernet") == 0)
        {
          /* modify global network */
          EmpathyIrcServer *server;

          server = empathy_irc_server_new ("us.undernet.org", 6667, FALSE);
          empathy_irc_network_append_server (network, server);
          g_object_unref (server);

          network_modified[3] = TRUE;
        }
      else if (strcmp (name, "Another Server") == 0)
        {
          /* Don't change this one */
        }
      else
        {
          fail_if (TRUE);
        }

      g_free (name);
    }
  fail_if (!network_modified[0] || !network_modified[1] || !network_modified[2]
      || !network_modified[3]);

  /* Add a new network */
  network = empathy_irc_network_new ("Great Server");
  server = empathy_irc_server_new ("irc.greatserver.com", 7873, TRUE);
  empathy_irc_network_append_server (network, server);
  empathy_irc_network_manager_add (mgr, network);
  g_object_unref (server);
  g_object_unref (network);

  g_slist_foreach (networks, (GFunc) g_object_unref, NULL);
  g_slist_free (networks);
  g_object_unref (mgr);


  /* Now let's reload the file and check its contain */
  global_file_orig = get_xml_file (GLOBAL_SAMPLE);
  user_file_orig = get_user_xml_file (USER_FILE);
  mgr = empathy_irc_network_manager_new (global_file_orig, user_file_orig);
  g_free (global_file_orig);
  g_free (user_file_orig);

  networks = empathy_irc_network_manager_get_networks (mgr);
  fail_if (g_slist_length (networks) != 4);

  network_checked[0] = network_checked[1] = network_checked[2] =
    network_checked[3] = FALSE;
  /* check networks and servers */
  for (l = networks; l != NULL; l = g_slist_next (l))
    {
      gchar *name;

      g_object_get (l->data, "name", &name, NULL);
      fail_if (name == NULL);

      if (strcmp (name, "GIMPNet") == 0)
        {
          check_network (l->data, "GIMPNet", "UTF-8", gimpnet_servers, 2);
          network_checked[0] = TRUE;
        }
      else if (strcmp (name, "Great Server") == 0)
        {
          check_network (l->data, "Great Server", "UTF-8", great_server, 1);
          network_checked[1] = TRUE;
        }
      else if (strcmp (name, "Another Server") == 0)
        {
          check_network (l->data, "Another Server", "UTF-8", another_server, 1);
          network_checked[2] = TRUE;
        }
      else if (strcmp (name, "Undernet") == 0)
        {
          check_network (l->data, "Undernet", "UTF-8", undernet_servers, 2);
          network_checked[3] = TRUE;
        }
      else
        {
          fail_if (TRUE);
        }

      g_free (name);
    }
  fail_if (!network_checked[0] || !network_checked[1] || !network_checked[2] ||
      !network_checked[3]);

  g_slist_foreach (networks, (GFunc) g_object_unref, NULL);
  g_slist_free (networks);
  g_object_unref (mgr);
}
END_TEST

START_TEST (test_load_user_file)
{
  EmpathyIrcNetworkManager *mgr;
  gchar *global_file, *user_file;
  GSList *networks, *l;
  struct server_t gimpnet_servers[] = {
    { "irc.gimp.org", 6667, FALSE },
    { "irc.us.gimp.org", 6667, FALSE },
    { "irc.au.gimp.org", 6667, FALSE }};
  struct server_t my_server[] = {
    { "irc.mysrv.net", 7495, TRUE }};
  struct server_t another_server[] = {
    { "irc.anothersrv.be", 6660, FALSE }};
  gboolean network_checked[3];
  gchar *user_file_orig;

  copy_xml_file (USER_SAMPLE, USER_FILE);
  user_file_orig = get_user_xml_file (USER_FILE);
  mgr = empathy_irc_network_manager_new (NULL, user_file_orig);

  g_object_get (mgr,
      "global-file", &global_file,
      "user-file", &user_file,
      NULL);
  fail_if (global_file != NULL);
  fail_if (user_file == NULL || strcmp (user_file, user_file_orig) != 0);
  g_free (global_file);
  g_free (user_file);
  g_free (user_file_orig);

  networks = empathy_irc_network_manager_get_networks (mgr);
  fail_if (g_slist_length (networks) != 3);

  network_checked[0] = network_checked[1] = network_checked[2] = FALSE;
  /* check networks and servers */
  for (l = networks; l != NULL; l = g_slist_next (l))
    {
      gchar *name;

      g_object_get (l->data, "name", &name, NULL);
      fail_if (name == NULL);

      if (strcmp (name, "GIMPNet") == 0)
        {
          check_network (l->data, "GIMPNet", "UTF-8", gimpnet_servers, 3);
          network_checked[0] = TRUE;
        }
      else if (strcmp (name, "My Server") == 0)
        {
          check_network (l->data, "My Server", "UTF-8", my_server, 1);
          network_checked[1] = TRUE;
        }
      else if (strcmp (name, "Another Server") == 0)
        {
          check_network (l->data, "Another Server", "UTF-8", another_server, 1);
          network_checked[2] = TRUE;
        }
      else
        {
          fail_if (TRUE);
        }

      g_free (name);
    }
  fail_if (!network_checked[0] || !network_checked[1] || !network_checked[2]);

  g_slist_foreach (networks, (GFunc) g_object_unref, NULL);
  g_slist_free (networks);
  g_object_unref (mgr);
}