import pytest
from PyQt5 import QtCore
import os
from nixview.ui.mainwindow import NixView

test_file = os.path.join(".", "nix_test.h5")

@pytest.fixture
def app(qtbot):
    if not os.path.exists(test_file):
        from nixview.test.create_test_file import create_test_file
        create_test_file(test_file)
    test_nv = NixView()
    qtbot.addWidget(test_nv)
    yield test_nv
    test_nv._file_close_action.trigger()

def test_file_open(app, qtbot):
    assert app._help_action.isEnabled()
    assert app._about_action.isEnabled()
    assert app._quit_action.isEnabled()
    assert not app._table_action.isEnabled()
    assert not app._plot_action.isEnabled()

    assert not app._file_handler.is_valid
   
    app.open_file(test_file)
    assert app._file_handler.is_valid
    assert app._file_open_action.isEnabled()
    assert app._file_close_action.isEnabled()

    app._file_close_action.trigger()
    assert not app._file_handler.is_valid
    assert app._file_open_action.isEnabled()
    assert not app._file_close_action.isEnabled()