int main(int argc, char * argv[]){

    char ns[32], name[32], path[32];
    int ans, delay;
    argo_setCommentId(argo_addString(name, STR("node-name"), STR("")),
                      STR("Specify the name of the node"));
    argo_setCommentId(argo_addString(ns, STR("namespace"), STR("")),
                      STR("Specify the namespace"));
    argo_setCommentId(argo_addInt(&ans, STR("auto-namespace"), 0, 0),
                      STR("Obtain the namespace from node_id"));
    argo_setCommentId(argo_addString(path, STR("force-path"), STR("")),
                      STR("Specify the path of the messages"));
    argo_setCommentId(argo_addIntMandatory(&node_id, STR("node-id"), 0, 1),
                      STR("Specify node address"));
    argo_setCommentId(argo_addIntMandatory(&num_of_nodes, STR("num-of-nodes"), 2, 1),
                      STR("Specify number of nodes"));
    argo_setCommentId(argo_addInt(&delay, STR("start-delay"), 0, 1),
                      STR("Delays the start of the node"));
    argo_setExample(argv[0],STR("--node-id 0 --num-of-nodes 3 --auto-namespace"));

    argo_doProcess(argc, argv, 0);

    std::ostringstream oss;
    oss << "R" << node_id;
    ros::init(argc, argv, oss.str());

    ros::NodeHandle n(oss.str());

    int port = 0;
    for (int i = 0; i< num_of_nodes; i++) {
        Manager * sm = new ServiceManager<libwrapper::Manage>(n, port, "manage", i, 10, 21000);
        std::ostringstream oss;
        oss << "manage (R" << i << ")";
        map[oss.str()] = sm;
        sms.push_back(sm);
        port+=2;
    }
    ros::ServiceServer service = n.advertiseService("manage", callback);
    map["ss_listener"] = new ShapeShifterListener(n, port);

    std::ostringstream oss1;
    std::string filename = wrapper_get_config_filename();

    oss1 << getenv("HOME") << "/" <<filename;
    std::cout << "Reading config file " << oss1.str() << "..." << std::endl;
    try{

        YAML::Node config = YAML::LoadFile(oss1.str());
        wrapper_setup_config(config, node_id, num_of_nodes, port);

        if (YAML::Node topics = config["topics"]){
            create_shapeshifter_topics(n, port, topics);
        }

        std::cout << "Done." << std::endl;
    }catch(...){
        std::cout << "Bad file or file not found." << std::endl;
    }
    port++;

    #include "config/config.h"

    wrapper_init(n, node_id, num_of_nodes, port);

    printf("\n%-40s PORTS  PRIORITY  PERIOD   CMP   DEST\n", "TOPICS");
    printf("-----------------------------------------------------------------------------\n");
    for (it = map.begin(); it != map.end(); it++){
        if (it->second->get_type()==Manager::TOPIC){
            printf("%-40s %5d       %3d   %5d   %3s  ", it->first.c_str(), it->second->get_port(), it->second->get_priority(), it->second->get_period(),it->second->get_compression()?"yes":"no");

            for (int i = 0; i< it->second->get_destinations().size(); i++){
                printf("%s", i != 0 ? ", " : " ");
                printf("%d", it->second->get_destinations().at(i));
            }
            printf("\n");
        }


    }

    printf("\n%-40s PORTS  PRIORITY  PERIOD   CMP\n", "SERVICES");
    printf("----------------------------------------------\n");
    for (it = map.begin(); it != map.end(); it++){
        if (it->second->get_type()==Manager::SERVICE){
            printf("%-40s %2d,%2d       %3d   %5s   %3s\n", it->first.c_str(), it->second->get_port(), it->second->get_port()+1, it->second->get_priority(), "n/a",it->second->get_compression()?"yes":"no");
        }
    }
    printf("\n");
    wrapper_run_bg();


    for (it = map.begin(); it != map.end(); it++){
        map[it->first]->run();
    }



///XXXXXXXXXXXXXXXXXXXXXXXX
    ros::AsyncSpinner spinner(4); // Use 4 threads
//    while (ros::ok()){
//        ros::spinOnce();
//        usleep(10);
//    }

    spinner.start();
    ros::waitForShutdown();
}
/* The main function */
int main(int arch, const char *argv[])
{
	int ifd;
	unsigned i;
	MafwRegistry *regi;

	/* Don't log debug messages. */
	mafw_log_init(":warning");

	/* Init wrapping */
	g_type_init();
	regi = MAFW_REGISTRY(mafw_registry_get_instance());
	mafw_shared_init(regi, NULL);
	wrapper_init();

	/* Load the plugins specified on the command line. */
	if ((ifd = inotify_init()) < 0)
		g_warning("inotify_init: %m");
	for (i = 1; argv[i]; i++) {
		DIR *hdir;
		gchar entry[PATH_MAX];
		const struct dirent *dent;

		strncpy(entry, argv[i], PATH_MAX);
		if (entry[PATH_MAX - 1] != '\0') {
			g_warning("Argument %d is too long, skipping", i-1);
			continue;
		}

		hdir = opendir(entry);
		if (hdir != NULL) {
			while ((dent = readdir(hdir)) != NULL) {
				gchar *path;

				/* Try to ignore non-shared object files. */
				if (!g_str_has_suffix(dent->d_name, ".so")
				    && !strstr(dent->d_name, ".so."))
					continue;
				path = g_strjoin("/", entry, dent->d_name,
						 NULL);
				if (load(regi, path))
					watch(ifd, path);
				g_free(path);
			} /* while */
			closedir(hdir);
			watch(ifd, entry);
		} else if (errno == ENOTDIR) {
			if (load(regi, entry))
				watch(ifd, entry);
		} else if (errno == ENOENT)
			load(regi, entry);
		else
			g_error("%s: %m", entry);
	} /* for */

	/* Watch $ifd. */
	if (ifd >= 0)
		g_io_add_watch(g_io_channel_unix_new(ifd), G_IO_IN,
			       (GIOFunc)renaissance, argv);

	/* The main loop should not return. */
	g_main_loop_run(g_main_loop_new(NULL, FALSE));
	return 1;
}
END_TEST

START_TEST(test_source_errors)
{
	ErrorSource *source;
	MafwRegistry *registry;
	DBusMessage *c;
	GError *error = NULL;
	const gchar *domain_str;
	GHashTable *metadata;
	GHashTable *metadata_empty;

	registry = mafw_registry_get_instance();
	mockbus_reset();
	wrapper_init();

	source = ERROR_SOURCE(error_source_new("mocksource", "mocksource",
                                               Loop));

	mock_appearing_extension(FAKE_SOURCE_SERVICE, FALSE);
	mafw_registry_add_extension(MAFW_REGISTRY(registry),
                                    MAFW_EXTENSION(source));

	g_set_error(&error, MAFW_EXTENSION_ERROR, MAFW_EXTENSION_ERROR_FAILED,
		    "Error source fails in everything it does.");
	domain_str = g_quark_to_string(error->domain);
	metadata = mockbus_mkmeta("title", "Easy", NULL);
	metadata_empty = mockbus_mkmeta(NULL);

	/* Browse */
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_BROWSE,
					  MAFW_DBUS_STRING("testobject"),
					  MAFW_DBUS_BOOLEAN(FALSE),
					  MAFW_DBUS_STRING("!(rating=sucks)"),
					  MAFW_DBUS_STRING("-year"),
					  MAFW_DBUS_C_STRVZ("title", "artist"),
					  MAFW_DBUS_UINT32(0),
					  MAFW_DBUS_UINT32(11)));
	mockbus_expect(mafw_dbus_gerror(c, error));

	mockbus_deliver(NULL);
	fail_unless(source->browse_called == 1);

	/* Cancel browse */
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_CANCEL_BROWSE,
					      MAFW_DBUS_UINT32(31337)));

	mockbus_expect(mafw_dbus_gerror(c, error));

	mockbus_deliver(NULL);
	fail_unless(source->cancel_browse_called == 1);

	/* Get metadata */
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_GET_METADATA,
				      MAFW_DBUS_STRING("testobject"),
				      MAFW_DBUS_C_STRVZ("title", "artist")));

	mockbus_expect(mafw_dbus_gerror(c, error));
	mockbus_deliver(NULL);
	fail_unless(source->get_metadata_called == 1);

	/* Set metadata */
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_SET_METADATA,
				      MAFW_DBUS_STRING("testobject"),
				      MAFW_DBUS_METADATA(metadata)));
	mockbus_expect(mafw_dbus_reply(c, MAFW_DBUS_STRING("testobject"),
				       MAFW_DBUS_C_STRVZ("pertti", "pasanen"),
				       MAFW_DBUS_STRING(domain_str),
				       MAFW_DBUS_INT32(error->code),
				       MAFW_DBUS_STRING(error->message)));
	mockbus_deliver(NULL);
	fail_unless(source->get_metadata_called == 1);

	/* Create object */
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_CREATE_OBJECT,
				      MAFW_DBUS_STRING("testobject"),
				      MAFW_DBUS_METADATA(metadata)));
	mockbus_expect(mafw_dbus_gerror(c, error));

	mockbus_deliver(NULL);
	fail_unless(source->create_object_called == 1);

	/* Destroy object */
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_DESTROY_OBJECT,
					      MAFW_DBUS_STRING("testobject")));

	mockbus_expect(mafw_dbus_gerror(c, error));

	mockbus_deliver(NULL);
	fail_unless(source->destroy_object_called == 1);

	mockbus_finish();
	g_error_free(error);
	mafw_metadata_release(metadata);
	mafw_metadata_release(metadata_empty);
}
END_TEST

START_TEST(test_source_wrapper)
{
	MockedSource *source;
	MafwRegistry *registry;
	GHashTable *metadata;
	DBusMessage *c;
	DBusMessage *replmsg;
	DBusMessageIter iter_array, iter_msg;
	gint i;
	const gchar *objlist[] = {"testobject", "testobject1", NULL};

	metadata = mockbus_mkmeta("title", "Easy", NULL);

	registry = mafw_registry_get_instance();
	mockbus_reset();
	wrapper_init();

	source = mocked_source_new("mocksource", "mocksource", Loop);

	mock_appearing_extension(FAKE_SOURCE_SERVICE, FALSE);
	mafw_registry_add_extension(MAFW_REGISTRY(registry),
                                    MAFW_EXTENSION(source));

	/* Browse */
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_BROWSE,
				      MAFW_DBUS_STRING("testobject"),
				      MAFW_DBUS_BOOLEAN(FALSE),
				      MAFW_DBUS_STRING("!(rating=sucks)"),
				      MAFW_DBUS_STRING("-year"),
				      MAFW_DBUS_C_STRVZ("title", "artist"),
				      MAFW_DBUS_UINT32(0),
				      MAFW_DBUS_UINT32(11)));
	replmsg = append_browse_res(NULL, &iter_msg, &iter_array, 1408, -1, 0,
                                    "testobject", metadata, "", 0, "");
	replmsg = append_browse_res(replmsg, &iter_msg, &iter_array, 1408, 0, 0,
                                    "", metadata, "", 0, "");
	dbus_message_iter_close_container(&iter_msg, &iter_array);
	mockbus_expect(replmsg);
	mockbus_expect(mafw_dbus_reply(c, MAFW_DBUS_UINT32(1408)));

	mockbus_deliver(NULL);
	fail_unless(source->browse_called == 1);

	source->repeat_browse = 25;
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_BROWSE,
				      MAFW_DBUS_STRING("testobject"),
				      MAFW_DBUS_BOOLEAN(FALSE),
				      MAFW_DBUS_STRING("!(rating=sucks)"),
				      MAFW_DBUS_STRING("-year"),
				      MAFW_DBUS_C_STRVZ("title", "artist"),
				      MAFW_DBUS_UINT32(0),
				      MAFW_DBUS_UINT32(11)));
	replmsg = NULL;
	for (i=0; i<25; i++)
		replmsg = append_browse_res(replmsg, &iter_msg, &iter_array,
                                            1408, -1, 0,
                                            "testobject", metadata, "", 0, "");
	dbus_message_iter_close_container(&iter_msg, &iter_array);
	mockbus_expect(replmsg);
	replmsg = append_browse_res(NULL, &iter_msg, &iter_array, 1408, 0, 0,
                                    "", metadata, "", 0, "");
	dbus_message_iter_close_container(&iter_msg, &iter_array);
	mockbus_expect(replmsg);
	mockbus_expect(mafw_dbus_reply(c, MAFW_DBUS_UINT32(1408)));

	mockbus_deliver(NULL);
	fail_unless(source->browse_called == 2);

	source->repeat_browse = 830;
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_BROWSE,
				      MAFW_DBUS_STRING("testobject"),
				      MAFW_DBUS_BOOLEAN(FALSE),
				      MAFW_DBUS_STRING("!(rating=sucks)"),
				      MAFW_DBUS_STRING("-year"),
				      MAFW_DBUS_C_STRVZ("title", "artist"),
				      MAFW_DBUS_UINT32(0),
				      MAFW_DBUS_UINT32(11)));
	replmsg = NULL;
	for (i=0; i<25; i++)
		replmsg = append_browse_res(replmsg, &iter_msg, &iter_array,
                                            1408, -1, 0,
                                            "testobject", metadata, "", 0, "");
	dbus_message_iter_close_container(&iter_msg, &iter_array);
	mockbus_expect(replmsg);
	replmsg = NULL;
	for (i=0; i<75; i++)
		replmsg = append_browse_res(replmsg, &iter_msg, &iter_array,
                                            1408, -1, 0,
                                            "testobject", metadata, "", 0, "");
	dbus_message_iter_close_container(&iter_msg, &iter_array);
	mockbus_expect(replmsg);
	replmsg = NULL;
	for (i=0; i<225; i++)
		replmsg = append_browse_res(replmsg, &iter_msg, &iter_array,
                                            1408, -1, 0,
                                            "testobject", metadata, "", 0, "");
	dbus_message_iter_close_container(&iter_msg, &iter_array);
	mockbus_expect(replmsg);
	replmsg = NULL;
	for (i=0; i<500; i++)
		replmsg = append_browse_res(replmsg, &iter_msg, &iter_array,
                                            1408, -1, 0,
                                            "testobject", metadata, "", 0, "");
	dbus_message_iter_close_container(&iter_msg, &iter_array);
	mockbus_expect(replmsg);
	replmsg = NULL;
	for (i=0; i<5; i++)
		replmsg = append_browse_res(replmsg, &iter_msg, &iter_array,
                                            1408, -1, 0,
                                            "testobject", metadata, "", 0, "");
	replmsg = append_browse_res(replmsg, &iter_msg, &iter_array, 1408, 0, 0,
				"", metadata, "", 0, "");
	dbus_message_iter_close_container(&iter_msg, &iter_array);
	mockbus_expect(replmsg);

	mockbus_expect(mafw_dbus_reply(c, MAFW_DBUS_UINT32(1408)));

	mockbus_deliver(NULL);
	fail_unless(source->browse_called == 3);

	/* Cancel browse */
	source->repeat_browse = 25;
	source->dont_send_last = TRUE;
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_BROWSE,
				      MAFW_DBUS_STRING("testobject"),
				      MAFW_DBUS_BOOLEAN(FALSE),
				      MAFW_DBUS_STRING(""),
				      MAFW_DBUS_STRING(""),
				      MAFW_DBUS_C_STRVZ("title", "artist"),
				      MAFW_DBUS_UINT32(0),
				      MAFW_DBUS_UINT32(11)));
	replmsg = NULL;
	for (i=0; i<25; i++)
		replmsg = append_browse_res(replmsg, &iter_msg, &iter_array,
                                            1408, -1, 0,
                                            "testobject", metadata, "", 0, "");
	dbus_message_iter_close_container(&iter_msg, &iter_array);
	mockbus_expect(replmsg);
	mockbus_expect(mafw_dbus_reply(c, MAFW_DBUS_UINT32(1408)));
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_CANCEL_BROWSE,
					      MAFW_DBUS_UINT32(1408)));

	mockbus_expect(mafw_dbus_reply(c));
	mockbus_deliver(NULL);
	mockbus_deliver(NULL);

	g_timeout_add(100, quit_mainloop_on_tout, NULL);
	g_main_loop_run(Loop);

	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_CANCEL_BROWSE,
					      MAFW_DBUS_UINT32(31337)));

	mockbus_expect(mafw_dbus_reply(c));

	mockbus_deliver(NULL);
	fail_unless(source->cancel_browse_called == 2);
	mockbus_finish();

	/* Get metadata */
	mockbus_incoming(c =
                         mafw_dbus_method(
                                 MAFW_SOURCE_METHOD_GET_METADATA,
                                 MAFW_DBUS_STRING("testobject"),
                                 MAFW_DBUS_C_STRVZ("title", "artist")));

	mockbus_expect(mafw_dbus_reply(c, MAFW_DBUS_METADATA(metadata)));

	mockbus_deliver(NULL);
	fail_unless(source->get_metadata_called == 1);

	/* Get metadatas */
	mockbus_incoming(c =
                         mafw_dbus_method(
                                 MAFW_SOURCE_METHOD_GET_METADATAS,
                                 MAFW_DBUS_C_STRVZ("testobject", "testobject2"),
                                 MAFW_DBUS_C_STRVZ("title", "artist")));

	mockbus_expect(mdatas_repl(c, objlist, metadata, FALSE));

	mockbus_deliver(NULL);
	fail_unless(source->get_metadata_called == 1);

	/* Set metadata */
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_SET_METADATA,
				      MAFW_DBUS_STRING("testobject"),
				      MAFW_DBUS_METADATA(metadata)));

	mockbus_expect(mafw_dbus_reply(c, MAFW_DBUS_STRING("testobject"),
				       MAFW_DBUS_C_STRVZ("pertti", "pasanen")));

	mockbus_deliver(NULL);
	fail_unless(source->set_metadata_called == 1);

	/* Create object */
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_CREATE_OBJECT,
				      MAFW_DBUS_STRING("testobject"),
				      MAFW_DBUS_METADATA(metadata)));

	mockbus_expect(mafw_dbus_reply(c, MAFW_DBUS_STRING("testobject")));

	mockbus_deliver(NULL);
	fail_unless(source->create_object_called == 1);

	/* Destroy object */
	mockbus_incoming(c = mafw_dbus_method(MAFW_SOURCE_METHOD_DESTROY_OBJECT,
					      MAFW_DBUS_STRING("testobject")));

	mockbus_expect(mafw_dbus_reply(c, MAFW_DBUS_STRING("testobject")));

	mockbus_deliver(NULL);
	fail_unless(source->destroy_object_called == 1);

	/* Signals */
	mockbus_expect(mafw_dbus_signal(MAFW_SOURCE_SIGNAL_METADATA_CHANGED,
					MAFW_DBUS_STRING("testobj")));
	g_signal_emit_by_name(source, "metadata-changed", "testobj");

	mockbus_expect(mafw_dbus_signal(MAFW_SOURCE_SIGNAL_CONTAINER_CHANGED,
				MAFW_DBUS_STRING("testobj")));
	g_signal_emit_by_name(source, "container-changed", "testobj");

        mockbus_expect(mafw_dbus_signal(MAFW_SOURCE_SIGNAL_UPDATING,
                                        MAFW_DBUS_INT32(25),
                                        MAFW_DBUS_INT32(4),
                                        MAFW_DBUS_INT32(6),
                                        MAFW_DBUS_INT32(12)));
        g_signal_emit_by_name(source, "updating", 25, 4, 6, 12);

	mockbus_finish();
	mafw_metadata_release(metadata);
}
END_TEST

#define MATCH_STR "type='signal',interface='org.freedesktop.DBus'," \
			"member='NameOwnerChanged',arg0='%s',arg2=''"

START_TEST(test_activate_settings)
{
	MockedSource *source;
	MafwRegistry *registry;
	DBusMessage *c;
	GValue v = {0};
	gchar *matchstr;

	registry = mafw_registry_get_instance();
	mockbus_reset();
	wrapper_init();

	source = mocked_source_new("mocksource", "mocksource", Loop);

	mock_appearing_extension(FAKE_SOURCE_SERVICE, FALSE);
	mafw_registry_add_extension(MAFW_REGISTRY(registry),
                                    MAFW_EXTENSION(source));
	g_value_init(&v, G_TYPE_BOOLEAN);

	// asking deactivity	
	g_value_set_boolean(&v, FALSE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == TRUE);

	// asking activity twice, and then deactivate it
	g_value_set_boolean(&v, TRUE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);

	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);

	g_value_set_boolean(&v, FALSE);

	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	matchstr = g_strdup_printf(MATCH_STR, msg_sender_id);
	mockbus_expect(mafw_dbus_method_full(DBUS_SERVICE_DBUS,
						     DBUS_PATH_DBUS,
						     DBUS_INTERFACE_DBUS,
						     "RemoveMatch",
						     MAFW_DBUS_STRING(matchstr)));
	g_free(matchstr);
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == TRUE);

	// asking activity, then activating by other one, deactivate by first one, deact by second one
	g_value_set_boolean(&v, TRUE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);

	msg_sender_id = ":1.104";
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);

	g_value_set_boolean(&v, FALSE);
	msg_sender_id = ":1.103";
	mockbus_incoming(c =
	mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	matchstr = g_strdup_printf(MATCH_STR, msg_sender_id);
	mockbus_expect(mafw_dbus_method_full(DBUS_SERVICE_DBUS,
						     DBUS_PATH_DBUS,
						     DBUS_INTERFACE_DBUS,
						     "RemoveMatch",
						     MAFW_DBUS_STRING(matchstr)));
	g_free(matchstr);
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);
	msg_sender_id = ":1.104";
	mockbus_incoming(c =
	mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	matchstr = g_strdup_printf(MATCH_STR, msg_sender_id);
	mockbus_expect(mafw_dbus_method_full(DBUS_SERVICE_DBUS,
						     DBUS_PATH_DBUS,
						     DBUS_INTERFACE_DBUS,
						     "RemoveMatch",
						     MAFW_DBUS_STRING(matchstr)));
	g_free(matchstr);
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == TRUE);

	// asking activity, then activating by other one, disconnect by first one, deact by second one
	msg_sender_id = ":1.103";
	g_value_set_boolean(&v, TRUE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);

	msg_sender_id = ":1.104";
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);

	g_value_set_boolean(&v, FALSE);
	msg_sender_id = ":1.103";
	mockbus_incoming(c =
	mafw_dbus_signal_full(NULL, DBUS_PATH_DBUS,
					      DBUS_INTERFACE_DBUS,
					      "NameOwnerChanged",
					      MAFW_DBUS_STRING(msg_sender_id),
					      MAFW_DBUS_STRING(msg_sender_id),
					      MAFW_DBUS_STRING("")));
	matchstr = g_strdup_printf(MATCH_STR, msg_sender_id);
	mockbus_expect(mafw_dbus_method_full(DBUS_SERVICE_DBUS,
						     DBUS_PATH_DBUS,
						     DBUS_INTERFACE_DBUS,
						     "RemoveMatch",
						     MAFW_DBUS_STRING(matchstr)));
	g_free(matchstr);
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);
	msg_sender_id = ":1.104";
	mockbus_incoming(c =
	mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	matchstr = g_strdup_printf(MATCH_STR, msg_sender_id);
	mockbus_expect(mafw_dbus_method_full(DBUS_SERVICE_DBUS,
						     DBUS_PATH_DBUS,
						     DBUS_INTERFACE_DBUS,
						     "RemoveMatch",
						     MAFW_DBUS_STRING(matchstr)));
	g_free(matchstr);
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == TRUE);
	
	// asking activity, then deactivating by other one, disconnect by first one
	msg_sender_id = ":1.103";
	g_value_set_boolean(&v, TRUE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);

	g_value_set_boolean(&v, FALSE);
	msg_sender_id = ":1.104";
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);

	msg_sender_id = ":1.103";
	mockbus_incoming(c =
	mafw_dbus_signal_full(NULL, DBUS_PATH_DBUS,
					      DBUS_INTERFACE_DBUS,
					      "NameOwnerChanged",
					      MAFW_DBUS_STRING(msg_sender_id),
					      MAFW_DBUS_STRING(msg_sender_id),
					      MAFW_DBUS_STRING("")));
	matchstr = g_strdup_printf(MATCH_STR, msg_sender_id);
	mockbus_expect(mafw_dbus_method_full(DBUS_SERVICE_DBUS,
						     DBUS_PATH_DBUS,
						     DBUS_INTERFACE_DBUS,
						     "RemoveMatch",
						     MAFW_DBUS_STRING(matchstr)));
	g_free(matchstr);
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == TRUE);

	/* Creating another source. Reffing both of them, and reffing one of
	them by another UIs, closing the first UI*/
	MockedSource *source2;
	source2 = mocked_source_new("mocksource2", "mocksource2", Loop);

	mock_appearing_extension(MAFW_SOURCE_SERVICE ".mockland." FAKE_SOURCE_NAME "2", FALSE);
	mafw_registry_add_extension(MAFW_REGISTRY(registry),
                                    MAFW_EXTENSION(source2));

	msg_sender_id = ":1.103";
	g_value_set_boolean(&v, TRUE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);

	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH "2",
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source2)->activate_state == FALSE);

	msg_sender_id = ":1.104";
	g_value_set_boolean(&v, TRUE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);

	msg_sender_id = ":1.103";
	mockbus_incoming(c =
	mafw_dbus_signal_full(NULL, DBUS_PATH_DBUS,
					      DBUS_INTERFACE_DBUS,
					      "NameOwnerChanged",
					      MAFW_DBUS_STRING(msg_sender_id),
					      MAFW_DBUS_STRING(msg_sender_id),
					      MAFW_DBUS_STRING("")));
	matchstr = g_strdup_printf(MATCH_STR, msg_sender_id);
	mockbus_expect(mafw_dbus_method_full(DBUS_SERVICE_DBUS,
						     DBUS_PATH_DBUS,
						     DBUS_INTERFACE_DBUS,
						     "RemoveMatch",
						     MAFW_DBUS_STRING(matchstr)));
	g_free(matchstr);
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);
	fail_if(MOCKED_SOURCE(source2)->activate_state == TRUE);

	msg_sender_id = ":1.104";
	g_value_set_boolean(&v, FALSE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	matchstr = g_strdup_printf(MATCH_STR, msg_sender_id);
	mockbus_expect(mafw_dbus_method_full(DBUS_SERVICE_DBUS,
						     DBUS_PATH_DBUS,
						     DBUS_INTERFACE_DBUS,
						     "RemoveMatch",
						     MAFW_DBUS_STRING(matchstr)));
	g_free(matchstr);
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == TRUE);

	/* Reffing both sources, and reffing one of
	them by another UIs, closing the last UI. Unreffing the first source,
	closing UI*/
	msg_sender_id = ":1.103";
	g_value_set_boolean(&v, TRUE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);
	fail_if(MOCKED_SOURCE(source2)->activate_state == TRUE);

	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH "2",
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source2)->activate_state == FALSE);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);

	msg_sender_id = ":1.104";
	g_value_set_boolean(&v, TRUE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);
	fail_if(MOCKED_SOURCE(source2)->activate_state == FALSE);

	msg_sender_id = ":1.104";
	mockbus_incoming(c =
	mafw_dbus_signal_full(NULL, DBUS_PATH_DBUS,
					      DBUS_INTERFACE_DBUS,
					      "NameOwnerChanged",
					      MAFW_DBUS_STRING(msg_sender_id),
					      MAFW_DBUS_STRING(msg_sender_id),
					      MAFW_DBUS_STRING("")));
	matchstr = g_strdup_printf(MATCH_STR, msg_sender_id);
	mockbus_expect(mafw_dbus_method_full(DBUS_SERVICE_DBUS,
						     DBUS_PATH_DBUS,
						     DBUS_INTERFACE_DBUS,
						     "RemoveMatch",
						     MAFW_DBUS_STRING(matchstr)));
	g_free(matchstr);
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);
	fail_if(MOCKED_SOURCE(source2)->activate_state == FALSE);

	msg_sender_id = ":1.103";
	g_value_set_boolean(&v, FALSE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == TRUE);
	fail_if(MOCKED_SOURCE(source2)->activate_state == FALSE);

	mockbus_incoming(c =
	mafw_dbus_signal_full(NULL, DBUS_PATH_DBUS,
					      DBUS_INTERFACE_DBUS,
					      "NameOwnerChanged",
					      MAFW_DBUS_STRING(msg_sender_id),
					      MAFW_DBUS_STRING(msg_sender_id),
					      MAFW_DBUS_STRING("")));
	matchstr = g_strdup_printf(MATCH_STR, msg_sender_id);
	mockbus_expect(mafw_dbus_method_full(DBUS_SERVICE_DBUS,
						     DBUS_PATH_DBUS,
						     DBUS_INTERFACE_DBUS,
						     "RemoveMatch",
						     MAFW_DBUS_STRING(matchstr)));
	g_free(matchstr);
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == TRUE);
	fail_if(MOCKED_SOURCE(source2)->activate_state == TRUE);


	/* Reffing both sources by a UI, reffing one of them by another UI,
	destroying this source */
	msg_sender_id = ":1.103";
	g_value_set_boolean(&v, TRUE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);
	fail_if(MOCKED_SOURCE(source2)->activate_state == TRUE);

	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH "2",
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source2)->activate_state == FALSE);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);

	msg_sender_id = ":1.104";
	g_value_set_boolean(&v, TRUE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH,
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source)->activate_state == FALSE);
	fail_if(MOCKED_SOURCE(source2)->activate_state == FALSE);

	mockbus_expect(mafw_dbus_method_full(
		DBUS_SERVICE_DBUS,
		DBUS_PATH_DBUS,
		DBUS_INTERFACE_DBUS,
		"ReleaseName",
		MAFW_DBUS_STRING("com.nokia.mafw.source.mockland.mocksource")
		));
	mockbus_reply(MAFW_DBUS_UINT32(4));
	matchstr = g_strdup_printf(MATCH_STR, msg_sender_id);
	mockbus_expect(mafw_dbus_method_full(DBUS_SERVICE_DBUS,
						     DBUS_PATH_DBUS,
						     DBUS_INTERFACE_DBUS,
						     "RemoveMatch",
						     MAFW_DBUS_STRING(matchstr)));
	g_free(matchstr);
	mafw_registry_remove_extension(mafw_registry_get_instance(),
					MAFW_EXTENSION(source));

	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source2)->activate_state == FALSE);
	
	msg_sender_id = ":1.103";
	g_value_set_boolean(&v, FALSE);
	mockbus_incoming(c =
		mafw_dbus_method_full(MAFW_DBUS_DESTINATION, MAFW_DBUS_PATH "2",
				      MAFW_EXTENSION_INTERFACE,
				      MAFW_EXTENSION_METHOD_SET_PROPERTY,
				      MAFW_DBUS_STRING(MAFW_PROPERTY_EXTENSION_ACTIVATE),
				      MAFW_DBUS_GVALUE(&v)));
	matchstr = g_strdup_printf(MATCH_STR, msg_sender_id);
	mockbus_expect(mafw_dbus_method_full(DBUS_SERVICE_DBUS,
						     DBUS_PATH_DBUS,
						     DBUS_INTERFACE_DBUS,
						     "RemoveMatch",
						     MAFW_DBUS_STRING(matchstr)));
	g_free(matchstr);
	mockbus_deliver(NULL);
	fail_if(MOCKED_SOURCE(source2)->activate_state == TRUE);


	mockbus_finish();
}