# Path: ice/model.py
# ROM = collections.namedtuple('ROM', [
#   'name',
#   'path',
#   'console',
# ])
# 
# Path: ice/roms.py
# ICE_FLAG_TAG = "~ManagedByIce"
# def roms_directory(config):
# def rom_shortcut_name(rom):
# def rom_to_shortcut(rom):
# 
# Path: ice/steam_grid_updater.py
# class SteamGridUpdater(object):
#   def __init__(self, provider):
#   def update_rom_artwork(self, user, rom, dry_run=False):
#   def update_artwork_for_rom_collection(self, user, roms, dry_run=False):


import os
import tempfile
import unittest
from mockito import *
from pysteam import grid
from pysteam import shortcuts
from ice import model
from ice import roms
from ice import steam_grid_updater
from testinfra import fixtures

class SteamGridUpdaterTests(unittest.TestCase):

  def setUp(self):
    self.steam_fixture = fixtures.SteamFixture()
    self.user_fixture = fixtures.UserFixture(self.steam_fixture)

    self.mock_provider = mock()
    self.updater = steam_grid_updater.SteamGridUpdater(
        self.mock_provider,
    )

  def tearDown(self):
    self.user_fixture.tearDown()
    self.steam_fixture.tearDown()

  def test_updater_sets_image_if_provider_has_one(self):
    rom = model.ROM(name = 'Game1', path = '/Path/to/game1', console = fixtures.consoles.flagged)
    shortcut = roms.rom_to_shortcut(rom)

    (handle, path) = tempfile.mkstemp('.png')
    when(self.mock_provider).image_for_rom(rom).thenReturn(path)

    self.assertFalse(grid.has_custom_image(self.user_fixture.get_context(), shortcuts.shortcut_app_id(shortcut)))
    self.updater.update_rom_artwork(self.user_fixture.get_context(), rom)
    self.assertTrue(grid.has_custom_image(self.user_fixture.get_context(), shortcuts.shortcut_app_id(shortcut)))

    os.remove(path)

  def test_updater_does_nothing_if_provider_has_no_image(self):

# Path: ice/model.py
# ROM = collections.namedtuple('ROM', [
#   'name',
#   'path',
#   'console',
# ])
# 
# Path: ice/roms.py
# ICE_FLAG_TAG = "~ManagedByIce"
# def roms_directory(config):
# def rom_shortcut_name(rom):
# def rom_to_shortcut(rom):
# 
# Path: ice/steam_grid_updater.py
# class SteamGridUpdater(object):
#   def __init__(self, provider):
#   def update_rom_artwork(self, user, rom, dry_run=False):
#   def update_artwork_for_rom_collection(self, user, roms, dry_run=False):


# Path: tests/steam_grid_updater_tests.py
import os
import tempfile
import unittest
from mockito import *
from pysteam import grid
from pysteam import shortcuts
from ice import model
from ice import roms
from ice import steam_grid_updater
from testinfra import fixtures


class SteamGridUpdaterTests(unittest.TestCase):

  def setUp(self):
    self.steam_fixture = fixtures.SteamFixture()
    self.user_fixture = fixtures.UserFixture(self.steam_fixture)

    self.mock_provider = mock()
    self.updater = steam_grid_updater.SteamGridUpdater(
        self.mock_provider,
    )

  def tearDown(self):
    self.user_fixture.tearDown()
    self.steam_fixture.tearDown()

  def test_updater_sets_image_if_provider_has_one(self):
    rom = model.ROM(name = 'Game1', path = '/Path/to/game1', console = fixtures.consoles.flagged)
    shortcut = roms.rom_to_shortcut(rom)

    (handle, path) = tempfile.mkstemp('.png')
    when(self.mock_provider).image_for_rom(rom).thenReturn(path)

    self.assertFalse(grid.has_custom_image(self.user_fixture.get_context(), shortcuts.shortcut_app_id(shortcut)))
    self.updater.update_rom_artwork(self.user_fixture.get_context(), rom)
    self.assertTrue(grid.has_custom_image(self.user_fixture.get_context(), shortcuts.shortcut_app_id(shortcut)))

    os.remove(path)

  def test_updater_does_nothing_if_provider_has_no_image(self):

    rom = model.ROM(name = 'Game1', path = '/Path/to/game1', console = fixtures.consoles.flagged)