| 1 | // SPDX-FileCopyrightText: 2019 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "filtercontent.h" |
| 6 | #include "logperiodbutton.h" |
| 7 | #include "structdef.h" |
| 8 | #include "logcombox.h" |
| 9 | #include "qtcompat.h" |
| 10 | |
| 11 | #include <QPaintEvent> |
| 12 | #include <QEvent> |
| 13 | #include <QStandardItemModel> |
| 14 | #include <QStandardItem> |
| 15 | #include <QDebug> |
| 16 | |
| 17 | #include <stub.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | |
| 20 | bool setSelection_IsVisible() |
| 21 | { |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | class FilterContent_UT : public testing::Test |
| 26 | { |
| 27 | public: |
| 28 | //添加日志 |
| 29 | static void SetUpTestCase() |
| 30 | { |
| 31 | qDebug() << "SetUpTestCase" << ENDL; |
| 32 | } |
| 33 | static void TearDownTestCase() |
| 34 | { |
| 35 | qDebug() << "TearDownTestCase" << ENDL; |
| 36 | } |
| 37 | void SetUp() //TEST跑之前会执行SetUp |
| 38 | { |
| 39 | m_filter = new FilterContent; |
| 40 | qDebug() << "SetUp" << ENDL; |
| 41 | } |
| 42 | void TearDown() //TEST跑完之后会执行TearDown |
| 43 | { |
| 44 | delete m_filter; |
| 45 | } |
| 46 | FilterContent *m_filter; |
| 47 | }; |
| 48 | |
| 49 | TEST_F(FilterContent_UT, shortCutExport_UT) |
| 50 | { |
| 51 | m_filter->shortCutExport(); |
| 52 | EXPECT_NE(m_filter, nullptr)<<"check the status after shortCutExport()"; |
| 53 | EXPECT_NE(m_filter->exportBtn, nullptr)<<"check the status after shortCutExport()"; |
| 54 | } |
| 55 | |
| 56 | TEST_F(FilterContent_UT, setAppComboBoxItem_UT) |
| 57 | { |
| 58 | m_filter->setAppComboBoxItem(); |
| 59 | EXPECT_NE(m_filter->cbx_app,nullptr)<<"check the status after setAppComboBoxItem()"; |
| 60 | } |
| 61 | |
| 62 | TEST_F(FilterContent_UT, setSelection_UT) |
| 63 | { |
| 64 | Stub stub; |
| 65 | stub.set(ADDR(QWidget, isVisible), setSelection_IsVisible); |
| 66 | FILTER_CONFIG fitler; |
| 67 | m_filter->setSelection(fitler); |
| 68 | EXPECT_NE(m_filter->cbx_app,nullptr)<<"check the status after setSelection()"; |
| 69 | EXPECT_NE(m_filter->cbx_lv,nullptr)<<"check the status after setSelection()"; |
| 70 | } |
| 71 | |
| 72 | TEST_F(FilterContent_UT, paintEvent_UT) |
| 73 | { |
| 74 | QPaintEvent repaint(m_filter->rect()); |
| 75 | m_filter->paintEvent(&repaint); |
| 76 | EXPECT_NE(m_filter, nullptr)<<"check the status after paintEvent()"; |
| 77 | } |
| 78 | |
| 79 | TEST_F(FilterContent_UT, eventFilter_UT) |
| 80 | { |
| 81 | QKeyEvent keyEvent(QEvent::KeyPress, Qt::Key_Right, Qt::NoModifier); |
| 82 | m_filter->eventFilter(m_filter->m_allBtn, &keyEvent); |
| 83 | m_filter->eventFilter(m_filter->m_todayBtn, &keyEvent); |
| 84 | m_filter->eventFilter(m_filter->m_threeDayBtn, &keyEvent); |
| 85 | m_filter->eventFilter(m_filter->m_lastWeekBtn, &keyEvent); |
| 86 | m_filter->eventFilter(m_filter->m_lastMonthBtn, &keyEvent); |
| 87 | m_filter->eventFilter(m_filter->m_threeMonthBtn, &keyEvent); |
| 88 | |
| 89 | QKeyEvent keyLeftEvent(QEvent::KeyPress, Qt::Key_Left, Qt::NoModifier); |
| 90 | m_filter->eventFilter(m_filter->m_allBtn, &keyLeftEvent); |
| 91 | m_filter->eventFilter(m_filter->m_todayBtn, &keyLeftEvent); |
| 92 | m_filter->eventFilter(m_filter->m_threeDayBtn, &keyLeftEvent); |
| 93 | m_filter->eventFilter(m_filter->m_lastWeekBtn, &keyLeftEvent); |
| 94 | m_filter->eventFilter(m_filter->m_lastMonthBtn, &keyLeftEvent); |
| 95 | m_filter->eventFilter(m_filter->m_threeMonthBtn, &keyLeftEvent); |
| 96 | EXPECT_NE(m_filter, nullptr)<<"check the status after paintEvent()"; |
| 97 | } |
| 98 | |
| 99 | TEST_F(FilterContent_UT, updateWordWrap_UT) |
| 100 | { |
| 101 | m_filter->updateWordWrap(); |
| 102 | m_filter->rect().setWidth(500); |
| 103 | LogPeriodButton btn("test", m_filter); |
| 104 | m_filter->hLayout_period->addWidget(&btn); |
| 105 | m_filter->updateWordWrap(); |
| 106 | EXPECT_NE(m_filter, nullptr)<<"check the status after updateWordWrap()"; |
| 107 | } |
| 108 | |
| 109 | TEST_F(FilterContent_UT, slot_logCatelogueClicked_UT) |
| 110 | { |
| 111 | QStandardItemModel m_model; |
| 112 | QStandardItem *item; |
| 113 | |
| 114 | for (int i = 0; i < 10; i++) { |
| 115 | item = new QStandardItem; |
| 116 | m_model.appendRow(item); |
| 117 | } |
| 118 | m_model.setData(m_model.index(0, 0), QString("application"), Qt::UserRole + 66); |
| 119 | m_model.setData(m_model.index(1, 0), QString("journalctl"), Qt::UserRole + 66); |
| 120 | m_model.setData(m_model.index(2, 0), QString("/var/log/boot.log"), Qt::UserRole + 66); |
| 121 | m_model.setData(m_model.index(3, 0), QString("/var/log/kern.log"), Qt::UserRole + 66); |
| 122 | m_model.setData(m_model.index(4, 0), QString("/var/log/dpkg.log"), Qt::UserRole + 66); |
| 123 | m_model.setData(m_model.index(5, 0), QString("/var/log/Xorg.0.log"), Qt::UserRole + 66); |
| 124 | m_model.setData(m_model.index(6, 0), QString("last"), Qt::UserRole + 66); |
| 125 | m_model.setData(m_model.index(7, 0), QString("bootklu"), Qt::UserRole + 66); |
| 126 | m_model.setData(m_model.index(8, 0), QString("/var/log/dnf.log"), Qt::UserRole + 66); |
| 127 | m_model.setData(m_model.index(9, 0), QString("dmesg"), Qt::UserRole + 66); |
| 128 | m_model.setData(m_model.index(10, 0), QString(Utils::homePath + "/.kwin.log"), Qt::UserRole + 66); |
| 129 | for (int i = 0; i < m_model.rowCount(); i++) { |
| 130 | QModelIndex modelindex = m_model.index(i, 0, QModelIndex()); |
| 131 | m_filter->slot_logCatelogueClicked(modelindex); |
| 132 | EXPECT_EQ(m_filter->m_curTreeIndex, modelindex)<<"check the status after slot_logCatelogueClicked()"; |
| 133 | } |
| 134 | delete item; |
| 135 | } |
| 136 | |
| 137 | TEST_F(FilterContent_UT, slot_exportButtonClicked_UT) |
| 138 | { |
| 139 | m_filter->slot_exportButtonClicked(); |
| 140 | EXPECT_NE(m_filter, nullptr)<<"check the status after slot_exportButtonClicked()"; |
| 141 | } |
| 142 | |
| 143 | TEST_F(FilterContent_UT, slot_cbxLvIdxChanged_UT) |
| 144 | { |
| 145 | m_filter->slot_cbxLvIdxChanged(2); |
| 146 | EXPECT_EQ(m_filter->m_curLvCbxId, 1)<<"check the status after slot_cbxLvIdxChanged()"; |
| 147 | } |
| 148 | |
| 149 | TEST_F(FilterContent_UT, slot_cbxLogTypeChanged_UT) |
| 150 | { |
| 151 | m_filter->slot_cbxLogTypeChanged(2); |
| 152 | EXPECT_NE(m_filter, nullptr)<<"check the status after slot_cbxLogTypeChanged()"; |
| 153 | } |
| 154 | |
| 155 | TEST_F(FilterContent_UT, slot_logCatelogueRefresh_UT) |
| 156 | { |
| 157 | QStandardItemModel m_model; |
| 158 | QStandardItem item; |
| 159 | m_model.appendRow(&item); |
| 160 | m_model.setData(m_model.index(0, 0), QString("application"), Qt::UserRole + 66); |
| 161 | m_filter->slot_logCatelogueRefresh(m_model.index(0, 0)); |
| 162 | EXPECT_NE(m_filter->cbx_app,nullptr)<<"check the status after slot_logCatelogueRefresh()"; |
| 163 | } |
| 164 | |
| 165 | TEST_F(FilterContent_UT, slot_buttonClicked_UT) |
| 166 | { |
| 167 | m_filter->slot_buttonClicked(BUTTONID::RESET); |
| 168 | EXPECT_EQ(m_filter->m_curBtnId,ALL)<<"check the status after slot_buttonClicked()"; |
| 169 | } |