1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/************************************************************************
*
* Copyright (C) 2023-2025 IRCAD France
*
* This file is part of Sight.
*
* Sight 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 3 of the License, or
* (at your option) any later version.
*
* Sight 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 Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
// cspell:ignore bouboule
#include "extract_test.hpp"
#include <core/os/temp_path.hpp>
#include <core/progress/observer.hpp>
#include <io/vtk/vti_image_reader.hpp>
#include <service/base.hpp>
#include <service/op.hpp>
#include <ui/test/dialog/input.hpp>
#include <ui/test/dialog/location.hpp>
#include <ui/test/dialog/message.hpp>
#include <utest_data/data.hpp>
CPPUNIT_TEST_SUITE_REGISTRATION(sight::module::io::zip::ut::extract_test);
namespace sight::module::io::zip::ut
{
//------------------------------------------------------------------------------
void extract_test::basic_archive_test()
{
const core::os::temp_dir tmp_folder;
service::base::sptr extract = service::add("sight::module::io::zip::extract");
CPPUNIT_ASSERT(extract);
CPPUNIT_ASSERT_NO_THROW(extract->configure());
CPPUNIT_ASSERT_NO_THROW(extract->start().get());
// We select the archive we want to open.
ui::test::dialog::location::push_paths(
{utest_data::dir() / "sight/ui/archive_extractor/non-encrypted-archive.sample"
});
// We choose the output path.
ui::test::dialog::location::push_paths({tmp_folder});
CPPUNIT_ASSERT_NO_THROW(extract->update().get());
// There must be precisely one VTI file inside.
std::filesystem::path vti_path;
std::size_t nb_vti_files = 0;
std::ranges::for_each(
std::filesystem::recursive_directory_iterator {tmp_folder},
[&vti_path, &nb_vti_files](const std::filesystem::directory_entry& _entry)
{
if(_entry.path().extension() == ".vti")
{
nb_vti_files++;
vti_path = _entry;
}
});
CPPUNIT_ASSERT_EQUAL(std::size_t(1), nb_vti_files);
CPPUNIT_ASSERT(!vti_path.empty());
// Try to open the file using VTK to check if it is valid.
auto vti_reader = std::make_shared<sight::io::vtk::vti_image_reader>();
vti_reader->set_file(vti_path);
auto img = std::make_shared<data::image>();
vti_reader->set_object(img);
auto observer = std::make_shared<core::progress::observer>("Reading VTI image");
CPPUNIT_ASSERT_NO_THROW(vti_reader->read(observer));
ui::test::dialog::location::push_paths(
{utest_data::dir() / "sight/ui/archive_extractor/non-encrypted-archive.sample"
});
ui::test::dialog::location::push_paths({tmp_folder});
// Oops, we choose the same folder again! We get a warning. Let's try again.
ui::test::dialog::message::push_action(ui::test::dialog::message::retry);
ui::test::dialog::location::push_paths({tmp_folder});
// Ah, clumsy us, we chose the exact same folder! Let's try again later.
ui::test::dialog::message::push_action(ui::test::dialog::message::cancel);
CPPUNIT_ASSERT_NO_THROW(extract->update().get());
ui::test::dialog::location::push_paths(
{utest_data::dir() / "sight/ui/archive_extractor/non-encrypted-archive.sample"
});
ui::test::dialog::location::push_paths({tmp_folder});
// Well, well, the folder still isn't empty. Tough luck. Let's simply overwrite it.
ui::test::dialog::message::push_action(ui::test::dialog::message::yes);
CPPUNIT_ASSERT_NO_THROW(extract->update().get());
CPPUNIT_ASSERT(ui::test::dialog::location::clear());
CPPUNIT_ASSERT(ui::test::dialog::message::clear());
CPPUNIT_ASSERT_NO_THROW(extract->stop().get());
}
//------------------------------------------------------------------------------
void extract_test::encrypted_archive_test()
{
const core::os::temp_dir tmp_folder;
service::base::sptr extract = service::add("sight::module::io::zip::extract");
CPPUNIT_ASSERT(extract);
CPPUNIT_ASSERT_NO_THROW(extract->configure());
CPPUNIT_ASSERT_NO_THROW(extract->start().get());
// We select the archive we want to open.
ui::test::dialog::location::push_paths(
{utest_data::dir() / "sight/ui/archive_extractor/encrypted-archive.sample"
});
// We choose the output path.
ui::test::dialog::location::push_paths({tmp_folder});
// The archive is encrypted, let's input a password.
ui::test::dialog::input::push_input("tartare");
// Ah, wrong one. Let's try again.
ui::test::dialog::message::push_action(ui::test::dialog::message::retry);
ui::test::dialog::input::push_input("bouboule");
CPPUNIT_ASSERT_NO_THROW(extract->update().get());
// There must be precisely one VTI file inside.
std::filesystem::path vti_path;
std::size_t nb_vti_files = 0;
std::ranges::for_each(
std::filesystem::recursive_directory_iterator {tmp_folder},
[&vti_path, &nb_vti_files](const std::filesystem::directory_entry& _entry)
{
if(_entry.path().extension() == ".vti")
{
nb_vti_files++;
vti_path = _entry;
}
});
CPPUNIT_ASSERT_EQUAL(std::size_t(1), nb_vti_files);
CPPUNIT_ASSERT(!vti_path.empty());
// Try to open the file using VTK to check if it is valid.
auto vti_reader = std::make_shared<sight::io::vtk::vti_image_reader>();
vti_reader->set_file(vti_path);
auto img = std::make_shared<data::image>();
vti_reader->set_object(img);
auto observer = std::make_shared<core::progress::observer>("Reading VTI image");
CPPUNIT_ASSERT_NO_THROW(vti_reader->read(observer));
CPPUNIT_ASSERT(ui::test::dialog::location::clear());
CPPUNIT_ASSERT(ui::test::dialog::input::clear());
CPPUNIT_ASSERT(ui::test::dialog::message::clear());
CPPUNIT_ASSERT_NO_THROW(extract->stop().get());
}
//------------------------------------------------------------------------------
} // namespace sight::module::io::zip::ut
|