#!/usr/bin/python3
#
# Copyright (C) 2022 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.

import os
import sys

# import Cockpit's machinery for test VMs and its browser test API
TEST_DIR = os.path.dirname(__file__)
sys.path.append(os.path.join(TEST_DIR, "common"))
sys.path.append(os.path.join(TEST_DIR, "helpers"))
sys.path.append(os.path.join(os.path.dirname(TEST_DIR), "bots/machine"))

from installer import Installer
from storage import Storage  # pylint: disable=import-error
from review import Review
from testlib import MachineCase, nondestructive, test_main  # pylint: disable=import-error
from machine_install import VirtInstallMachine

@nondestructive
class TestReview(MachineCase):
    MachineCase.machine_class = VirtInstallMachine

    def testBasic(self):
        b = self.browser
        m = self.machine
        i = Installer(b, m)
        s = Storage(b, m)
        r = Review(b)

        i.open()
        i.next()

        # After clicking 'Next' on the storage step, partitioning is done, thus changing the available space on the disk
        # Since this is a non-destructive test we need to make sure the we reset partitioning to how it was before the test started
        self.addCleanup(s.dbus_reset_partitioning)
        i.next()

        # check language is shown
        r.check_language("English (United States)")

        # check selected disks are shown
        r.check_disk_label("vda", "Local standard disk")
        r.check_disk_description("vda", "0x1af4 (vda)")

        # Pixel test the review step
        b.assert_pixels("#app", "review-step-basic", ignore=["#betanag-icon"])

    def testNoConfirmation(self):
        b = self.browser
        m = self.machine
        i = Installer(b, m)
        s = Storage(b, m)

        self.addCleanup(s.dbus_reset_partitioning)

        i.open()

        for _ in i.steps[:-1]:
            if i.steps[i.get_current_page_id()] != i.steps.REVIEW:
                i.next()
            else:
                i.begin_installation(should_fail=True, confirm_erase=False)

if __name__ == '__main__':
    test_main()
