def test_changing(self):
        """Test whether the changes are displayed correctly"""
        w = prefs.GuiRcParamsWidget()
        gui_rcParams['console.auto_set_mp'] = True
        w.initialize()
        items = list(w.tree.top_level_items)
        for item in items:
            if item.text(0) == 'console.auto_set_mp':
                iw = w.tree.itemWidget(item.child(0), 2)
                iw.setPlainText('f')
        QTest.mouseClick(w.bt_select_changed, Qt.LeftButton)
        selected_rc = dict(w.tree.selected_rc())
        self.assertEqual(len(selected_rc), 1, msg=selected_rc)
        self.assertIn('console.auto_set_mp', selected_rc)
        self.assertEqual(selected_rc['console.auto_set_mp'], False)

        for item in items:
            if item.text(0) == 'console.auto_set_mp':
                iw = w.tree.itemWidget(item.child(0), 2)
                iw.setPlainText('t')

        QTest.mouseClick(w.bt_select_none, Qt.LeftButton)
        self.assertFalse(dict(w.tree.selected_rc()))

        QTest.mouseClick(w.bt_select_changed, Qt.LeftButton)
        self.assertFalse(w.tree.selectedItems())
    def test_update(self):
        """Test updating the rcParams"""
        w = prefs.GuiRcParamsWidget()
        w.initialize()

        fname = tempfile.NamedTemporaryFile(
            prefix='psyplot_gui_test', suffix='.yml').name
        self._created_files.add(fname)
        gui_rcParams.find_all('console').dump(fname)

        keys = []

        for item in w.tree.top_level_items:
            if asstring(item.text(0)).startswith('help_explorer'):
                item.setSelected(True)
                keys.append(asstring(item.text(0)))

        self.assertEqual(len(w.tree.selectedItems()), len(keys))

        action = w.save_settings_action(update=True, target=fname)
        action.trigger()
        self.assertTrue(osp.exists(fname))
        rc = GuiRcParams(defaultParams=gui_rcParams.defaultParams)
        rc.load_from_file(fname)

        self.assertEqual(
            dict(rc),
            {key: gui_rcParams[key] for key in gui_rcParams
             if key.startswith('console') or key.startswith('help_explorer')})
    def test_save_01_all(self):
        """Test saving the rcParams"""
        w = prefs.GuiRcParamsWidget()
        w.initialize()

        QTest.mouseClick(w.bt_select_all, Qt.LeftButton)
        self.assertEqual(len(w.tree.selectedItems()), len(gui_rcParams))

        fname = tempfile.NamedTemporaryFile(
            prefix='psyplot_gui_test', suffix='.yml').name
        self._created_files.add(fname)
        action = w.save_settings_action(target=fname)
        action.trigger()
        self.assertTrue(osp.exists(fname))
        rc = GuiRcParams(defaultParams=gui_rcParams.defaultParams)
        rc.load_from_file(fname)

        self.assertEqual(rc, gui_rcParams)
    def test_loading(self):
        """Test whether the rcParams are loaded correctly"""
        # create the preferences window
        w = prefs.GuiRcParamsWidget()
        w.initialize()
        items = list(w.tree.top_level_items)
        self.assertEqual(len(items), len(gui_rcParams))
        # test keys
        keys = set(gui_rcParams)
        for item in items:
            keys.remove(asstring(item.text(0)))
        self.assertFalse(keys)

        # test values
        for item in items:
            key = asstring(item.text(0))
            s_val = asstring(w.tree.itemWidget(item.child(0), 2).toPlainText())
            val = yaml.load(s_val, Loader=yaml.Loader)
            self.assertEqual(val, gui_rcParams[key],
                             msg='Failed item %s: %s' % (key, s_val))
    def test_save_02_some(self):
        """Test saving some parts the rcParams"""
        w = prefs.GuiRcParamsWidget()
        w.initialize()

        keys = []

        for item in islice(w.tree.top_level_items, 0, None, 2):
            item.setSelected(True)
            keys.append(asstring(item.text(0)))

        self.assertEqual(len(w.tree.selectedItems()), len(keys))

        fname = tempfile.NamedTemporaryFile(
            prefix='psyplot_gui_test', suffix='.yml').name
        self._created_files.add(fname)
        action = w.save_settings_action(target=fname)
        action.trigger()
        self.assertTrue(osp.exists(fname))
        rc = GuiRcParams(defaultParams=gui_rcParams.defaultParams)
        rc.load_from_file(fname)

        self.assertEqual(dict(rc), {key: gui_rcParams[key] for key in keys})
    def test_validation(self):
        """Test whether the validation works correctly"""
        self.window.showMaximized()
        w = prefs.GuiRcParamsWidget()
        w.initialize()

        # choose an item
        for i, item in enumerate(w.tree.top_level_items):
            if asstring(item.text(0)) == 'console.auto_set_mp':
                break

        self.assertTrue(w.is_valid, msg=w.tree.valid)
        # set an invalid value
        w.tree.itemWidget(item.child(0), 2).setPlainText('tg')
        self.assertFalse(w.tree.valid[i])
        self.assertFalse(w.is_valid)
        w.tree.itemWidget(item.child(0), 2).setPlainText('t')
        self.assertTrue(w.tree.valid[i])
        self.assertTrue(w.is_valid)

        # set a value that cannot be loaded by yaml
        w.tree.itemWidget(item.child(0), 2).setPlainText('"t')
        self.assertFalse(w.tree.valid[i])
        self.assertFalse(w.is_valid)