# Path: ubuntucleaner/utils/icon.py
# def get_from_name(name='gtk-execute',
#                   alter='gtk-execute',
#                   size=DEFAULT_SIZE,
#                   force_reload=False,
#                   only_path=False):
#     pixbuf = None
# 
#     if force_reload:
#         global icontheme
#         icontheme = Gtk.IconTheme.get_default()
# 
#     if only_path:
#         path = icontheme.lookup_icon(name, size, Gtk.IconLookupFlags.USE_BUILTIN)
#         return path
# 
#     try:
#         pixbuf = icontheme.load_icon(name, size, 0)
#     except Exception as e:
#         log.warning(e)
#         # if the alter name isn't here, so use random icon
# 
#         while not pixbuf:
#             try:
#                 pixbuf = icontheme.load_icon(alter, size, 0)
#             except Exception as e:
#                 log.error(e)
#                 icons = icontheme.list_icons(None)
#                 alter = icons[random.randint(0, len(icons) - 1)]
# 
#     if pixbuf.get_height() != size:
#         return pixbuf.scale_simple(size, size, GdkPixbuf.InterpType.BILINEAR)
# 
#     return pixbuf
# 
# def get_from_list(list, size=DEFAULT_SIZE):
#     pixbuf = None
#     for name in list:
#         try:
#             pixbuf = icontheme.load_icon(name,
#                                          size,
#                                          Gtk.IconLookupFlags.USE_BUILTIN)
#         except Exception:
#             log.warning('get_from_list for %s failed, try next' % name)
#             continue
# 
#     return pixbuf or get_from_name('application-x-executable', size=size)
# 
# def get_from_mime_type(mime, size=DEFAULT_SIZE):
#     try:
#         gicon = Gio.content_type_get_icon(mime)
# 
#         return get_from_list(gicon.get_names(), size=size)
#     except Exception as e:
#         log.error('get_from_mime_type failed: %s' % e)
#         return get_from_name(size=size)
# 
# def get_from_file(file, size=DEFAULT_SIZE, only_path=False):
#     try:
#         return GdkPixbuf.Pixbuf.new_from_file_at_size(file, size, size)
#     except Exception as e:
#         log.error('get_from_file failed: %s' % e)
#         return get_from_name(size=size, only_path=only_path)
# 
# DEFAULT_SIZE = 24


import unittest
import mock
from gi.repository import Gtk, GdkPixbuf
from ubuntucleaner.utils.icon import get_from_name, get_from_list, get_from_mime_type, get_from_file, DEFAULT_SIZE
        "ubuntucleaner.utils.icon.Gtk.IconTheme.load_icon",
        side_effect=side_effect)


def patch_log():
    return mock.patch("ubuntucleaner.utils.icon.log")


def patch_get_from_name():
    return mock.patch("ubuntucleaner.utils.icon.get_from_name")


def patch_get_from_list():
    return mock.patch("ubuntucleaner.utils.icon.get_from_list")


def patch_new_from_file_at_size():
    return mock.patch("ubuntucleaner.utils.icon.GdkPixbuf.Pixbuf.new_from_file_at_size")


class TestIconModule(unittest.TestCase):

    def test_get_from_name(self):
        """
        Calling with no parameters should return the pixbuf object through `load_icon()`
        and default parameters.
        """
        m_pixbuf = mock.Mock(spec=GdkPixbuf.Pixbuf, get_height=lambda: DEFAULT_SIZE)
        with patch_load_icon() as m_load_icon:
            m_load_icon.return_value = m_pixbuf

# Path: ubuntucleaner/utils/icon.py
# def get_from_name(name='gtk-execute',
#                   alter='gtk-execute',
#                   size=DEFAULT_SIZE,
#                   force_reload=False,
#                   only_path=False):
#     pixbuf = None
# 
#     if force_reload:
#         global icontheme
#         icontheme = Gtk.IconTheme.get_default()
# 
#     if only_path:
#         path = icontheme.lookup_icon(name, size, Gtk.IconLookupFlags.USE_BUILTIN)
#         return path
# 
#     try:
#         pixbuf = icontheme.load_icon(name, size, 0)
#     except Exception as e:
#         log.warning(e)
#         # if the alter name isn't here, so use random icon
# 
#         while not pixbuf:
#             try:
#                 pixbuf = icontheme.load_icon(alter, size, 0)
#             except Exception as e:
#                 log.error(e)
#                 icons = icontheme.list_icons(None)
#                 alter = icons[random.randint(0, len(icons) - 1)]
# 
#     if pixbuf.get_height() != size:
#         return pixbuf.scale_simple(size, size, GdkPixbuf.InterpType.BILINEAR)
# 
#     return pixbuf
# 
# def get_from_list(list, size=DEFAULT_SIZE):
#     pixbuf = None
#     for name in list:
#         try:
#             pixbuf = icontheme.load_icon(name,
#                                          size,
#                                          Gtk.IconLookupFlags.USE_BUILTIN)
#         except Exception:
#             log.warning('get_from_list for %s failed, try next' % name)
#             continue
# 
#     return pixbuf or get_from_name('application-x-executable', size=size)
# 
# def get_from_mime_type(mime, size=DEFAULT_SIZE):
#     try:
#         gicon = Gio.content_type_get_icon(mime)
# 
#         return get_from_list(gicon.get_names(), size=size)
#     except Exception as e:
#         log.error('get_from_mime_type failed: %s' % e)
#         return get_from_name(size=size)
# 
# def get_from_file(file, size=DEFAULT_SIZE, only_path=False):
#     try:
#         return GdkPixbuf.Pixbuf.new_from_file_at_size(file, size, size)
#     except Exception as e:
#         log.error('get_from_file failed: %s' % e)
#         return get_from_name(size=size, only_path=only_path)
# 
# DEFAULT_SIZE = 24


# Path: tests/utils/test_icon.py
import unittest
import mock
from gi.repository import Gtk, GdkPixbuf
from ubuntucleaner.utils.icon import get_from_name, get_from_list, get_from_mime_type, get_from_file, DEFAULT_SIZE

        "ubuntucleaner.utils.icon.Gtk.IconTheme.load_icon",
        side_effect=side_effect)


def patch_log():
    return mock.patch("ubuntucleaner.utils.icon.log")


def patch_get_from_name():
    return mock.patch("ubuntucleaner.utils.icon.get_from_name")


def patch_get_from_list():
    return mock.patch("ubuntucleaner.utils.icon.get_from_list")


def patch_new_from_file_at_size():
    return mock.patch("ubuntucleaner.utils.icon.GdkPixbuf.Pixbuf.new_from_file_at_size")


class TestIconModule(unittest.TestCase):

    def test_get_from_name(self):
        """
        Calling with no parameters should return the pixbuf object through `load_icon()`
        and default parameters.
        """
        m_pixbuf = mock.Mock(spec=GdkPixbuf.Pixbuf, get_height=lambda: DEFAULT_SIZE)
        with patch_load_icon() as m_load_icon:
            m_load_icon.return_value = m_pixbuf

            pixbuf = get_from_name()