yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
tile16_editor_test.cc
Go to the documentation of this file.
2
3#include <iostream>
4#include <memory>
5#include <vector>
6
7#include <gtest/gtest.h>
8#include "app/rom.h"
9#include "app/gfx/arena.h"
11#include "app/gfx/bitmap.h"
12#include "app/gfx/tilemap.h"
14#include "app/core/window.h"
15
16namespace yaze {
17namespace editor {
18namespace test {
19
20class Tile16EditorIntegrationTest : public ::testing::Test {
21 protected:
22 static void SetUpTestSuite() {
23 // Initialize SDL and rendering system once for all tests
25 }
26
27 static void TearDownTestSuite() {
28 // Clean up SDL
30 auto shutdown_result = core::ShutdownWindow(test_window_);
31 (void)shutdown_result; // Suppress unused variable warning
32 window_initialized_ = false;
33 }
34 }
35
36 void SetUp() override {
37#ifdef YAZE_ENABLE_ROM_TESTS
39 GTEST_SKIP() << "Failed to initialize graphics system";
40 }
41
42 // Load the test ROM
43 rom_ = std::make_unique<Rom>();
44 auto load_result = rom_->LoadFromFile(YAZE_TEST_ROM_PATH);
45 ASSERT_TRUE(load_result.ok()) << "Failed to load test ROM: " << load_result.message();
46
47 // Load overworld data
48 overworld_ = std::make_unique<zelda3::Overworld>(rom_.get());
49 auto overworld_load_result = overworld_->Load(rom_.get());
50 ASSERT_TRUE(overworld_load_result.ok()) << "Failed to load overworld: " << overworld_load_result.message();
51
52 // Create tile16 blockset
53 auto tile16_data = overworld_->tile16_blockset_data();
54 auto palette = overworld_->current_area_palette();
55
56 tile16_blockset_ = std::make_unique<gfx::Tilemap>(
57 gfx::CreateTilemap(nullptr, tile16_data, 0x80, 0x2000, 16,
59
60 // Create graphics bitmap
61 current_gfx_bmp_ = std::make_unique<gfx::Bitmap>();
62 current_gfx_bmp_->Create(0x80, 512, 0x40, overworld_->current_graphics());
63 current_gfx_bmp_->SetPalette(palette);
66
67 // Create tile16 blockset bitmap
68 tile16_blockset_bmp_ = std::make_unique<gfx::Bitmap>();
69 tile16_blockset_bmp_->Create(0x80, 0x2000, 0x08, tile16_data);
70 tile16_blockset_bmp_->SetPalette(palette);
73
74 // Initialize the tile16 editor
75 editor_ = std::make_unique<Tile16Editor>(rom_.get(), tile16_blockset_.get());
76 auto init_result = editor_->Initialize(*tile16_blockset_bmp_, *current_gfx_bmp_,
77 *overworld_->mutable_all_tiles_types());
78 ASSERT_TRUE(init_result.ok()) << "Failed to initialize editor: " << init_result.message();
79
80 rom_loaded_ = true;
81#else
82 // Fallback for non-ROM tests
83 rom_ = std::make_unique<Rom>();
84 tilemap_ = std::make_unique<gfx::Tilemap>();
85 editor_ = std::make_unique<Tile16Editor>(rom_.get(), tilemap_.get());
86 rom_loaded_ = false;
87#endif
88 }
89
90protected:
92 // Create renderer for test
93 test_renderer_ = std::make_unique<gfx::SDL2Renderer>();
94 auto window_result = core::CreateWindow(test_window_, test_renderer_.get(), SDL_WINDOW_HIDDEN);
95 if (window_result.ok()) {
97 } else {
98 window_initialized_ = false;
99 // Log the error but don't fail test setup
100 std::cerr << "Failed to initialize test window: " << window_result.message() << std::endl;
101 }
102 }
103
106 static std::unique_ptr<gfx::SDL2Renderer> test_renderer_;
107
108 bool rom_loaded_ = false;
109 std::unique_ptr<Rom> rom_;
110 std::unique_ptr<gfx::Tilemap> tilemap_;
111 std::unique_ptr<gfx::Tilemap> tile16_blockset_;
112 std::unique_ptr<gfx::Bitmap> current_gfx_bmp_;
113 std::unique_ptr<gfx::Bitmap> tile16_blockset_bmp_;
114 std::unique_ptr<zelda3::Overworld> overworld_;
115 std::unique_ptr<Tile16Editor> editor_;
116};
117
118// Static member definitions
121std::unique_ptr<gfx::SDL2Renderer> Tile16EditorIntegrationTest::test_renderer_;
122
123// Basic validation tests (no ROM required)
125 // Test with invalid tile ID
126 EXPECT_FALSE(editor_->IsTile16Valid(-1));
127 EXPECT_FALSE(editor_->IsTile16Valid(9999));
128
129 // Test scratch space operations with invalid slots
130 auto save_invalid = editor_->SaveTile16ToScratchSpace(-1);
131 EXPECT_FALSE(save_invalid.ok());
132 EXPECT_EQ(save_invalid.code(), absl::StatusCode::kInvalidArgument);
133
134 auto load_invalid = editor_->LoadTile16FromScratchSpace(5);
135 EXPECT_FALSE(load_invalid.ok());
136 EXPECT_EQ(load_invalid.code(), absl::StatusCode::kInvalidArgument);
137
138 // Test valid scratch space clearing
139 auto clear_valid = editor_->ClearScratchSpace(0);
140 EXPECT_TRUE(clear_valid.ok());
141}
142
143// ROM-dependent tests
144TEST_F(Tile16EditorIntegrationTest, ValidateTile16DataWithROM) {
145#ifdef YAZE_ENABLE_ROM_TESTS
146 if (!rom_loaded_) {
147 GTEST_SKIP() << "ROM not loaded, skipping integration test";
148 }
149
150 // Test validation with properly loaded ROM
151 auto status = editor_->ValidateTile16Data();
152 EXPECT_TRUE(status.ok()) << "Validation failed: " << status.message();
153#else
154 GTEST_SKIP() << "ROM tests disabled";
155#endif
156}
157
158TEST_F(Tile16EditorIntegrationTest, SetCurrentTileWithROM) {
159#ifdef YAZE_ENABLE_ROM_TESTS
160 if (!rom_loaded_) {
161 GTEST_SKIP() << "ROM not loaded, skipping integration test";
162 }
163
164 // Test setting a valid tile
165 auto valid_tile_result = editor_->SetCurrentTile(0);
166 EXPECT_TRUE(valid_tile_result.ok()) << "Failed to set tile 0: " << valid_tile_result.message();
167
168 auto valid_tile_result2 = editor_->SetCurrentTile(100);
169 EXPECT_TRUE(valid_tile_result2.ok()) << "Failed to set tile 100: " << valid_tile_result2.message();
170
171 // Test invalid ranges still fail
172 auto invalid_low = editor_->SetCurrentTile(-1);
173 EXPECT_FALSE(invalid_low.ok());
174 EXPECT_EQ(invalid_low.code(), absl::StatusCode::kOutOfRange);
175
176 auto invalid_high = editor_->SetCurrentTile(10000);
177 EXPECT_FALSE(invalid_high.ok());
178 EXPECT_EQ(invalid_high.code(), absl::StatusCode::kOutOfRange);
179#else
180 GTEST_SKIP() << "ROM tests disabled";
181#endif
182}
183
184TEST_F(Tile16EditorIntegrationTest, FlipOperationsWithROM) {
185#ifdef YAZE_ENABLE_ROM_TESTS
186 if (!rom_loaded_) {
187 GTEST_SKIP() << "ROM not loaded, skipping integration test";
188 }
189
190 // Set a valid tile first
191 auto set_result = editor_->SetCurrentTile(1);
192 ASSERT_TRUE(set_result.ok()) << "Failed to set initial tile: " << set_result.message();
193
194 // Test flip operations
195 auto flip_h_result = editor_->FlipTile16Horizontal();
196 EXPECT_TRUE(flip_h_result.ok()) << "Horizontal flip failed: " << flip_h_result.message();
197
198 auto flip_v_result = editor_->FlipTile16Vertical();
199 EXPECT_TRUE(flip_v_result.ok()) << "Vertical flip failed: " << flip_v_result.message();
200
201 auto rotate_result = editor_->RotateTile16();
202 EXPECT_TRUE(rotate_result.ok()) << "Rotation failed: " << rotate_result.message();
203#else
204 GTEST_SKIP() << "ROM tests disabled";
205#endif
206}
207
209#ifdef YAZE_ENABLE_ROM_TESTS
210 if (!rom_loaded_) {
211 GTEST_SKIP() << "ROM not loaded, skipping integration test";
212 }
213
214 // Set a tile and perform an operation to create undo state
215 auto set_result = editor_->SetCurrentTile(1);
216 ASSERT_TRUE(set_result.ok());
217
218 auto clear_result = editor_->ClearTile16();
219 ASSERT_TRUE(clear_result.ok()) << "Clear operation failed: " << clear_result.message();
220
221 // Test undo
222 auto undo_result = editor_->Undo();
223 EXPECT_TRUE(undo_result.ok()) << "Undo failed: " << undo_result.message();
224
225 // Test redo
226 auto redo_result = editor_->Redo();
227 EXPECT_TRUE(redo_result.ok()) << "Redo failed: " << redo_result.message();
228#else
229 GTEST_SKIP() << "ROM tests disabled";
230#endif
231}
232
233TEST_F(Tile16EditorIntegrationTest, PaletteOperationsWithROM) {
234#ifdef YAZE_ENABLE_ROM_TESTS
235 if (!rom_loaded_) {
236 GTEST_SKIP() << "ROM not loaded, skipping integration test";
237 }
238
239 // Test palette cycling
240 auto cycle_forward = editor_->CyclePalette(true);
241 EXPECT_TRUE(cycle_forward.ok()) << "Palette cycle forward failed: " << cycle_forward.message();
242
243 auto cycle_backward = editor_->CyclePalette(false);
244 EXPECT_TRUE(cycle_backward.ok()) << "Palette cycle backward failed: " << cycle_backward.message();
245
246 // Test valid palette preview
247 auto valid_palette = editor_->PreviewPaletteChange(3);
248 EXPECT_TRUE(valid_palette.ok()) << "Palette preview failed: " << valid_palette.message();
249
250 // Test invalid palette
251 auto invalid_palette = editor_->PreviewPaletteChange(10);
252 EXPECT_FALSE(invalid_palette.ok());
253 EXPECT_EQ(invalid_palette.code(), absl::StatusCode::kInvalidArgument);
254#else
255 GTEST_SKIP() << "ROM tests disabled";
256#endif
257}
258
259TEST_F(Tile16EditorIntegrationTest, CopyPasteOperationsWithROM) {
260#ifdef YAZE_ENABLE_ROM_TESTS
261 if (!rom_loaded_) {
262 GTEST_SKIP() << "ROM not loaded, skipping integration test";
263 }
264
265 // Set a tile first
266 auto set_result = editor_->SetCurrentTile(10);
267 ASSERT_TRUE(set_result.ok());
268
269 // Test copy operation
270 auto copy_result = editor_->CopyTile16ToClipboard(10);
271 EXPECT_TRUE(copy_result.ok()) << "Copy failed: " << copy_result.message();
272
273 // Test paste operation
274 auto paste_result = editor_->PasteTile16FromClipboard();
275 EXPECT_TRUE(paste_result.ok()) << "Paste failed: " << paste_result.message();
276#else
277 GTEST_SKIP() << "ROM tests disabled";
278#endif
279}
280
281TEST_F(Tile16EditorIntegrationTest, ScratchSpaceWithROM) {
282#ifdef YAZE_ENABLE_ROM_TESTS
283 if (!rom_loaded_) {
284 GTEST_SKIP() << "ROM not loaded, skipping integration test";
285 }
286
287 // Set a tile first
288 auto set_result = editor_->SetCurrentTile(15);
289 ASSERT_TRUE(set_result.ok());
290
291 // Test scratch space save
292 auto save_result = editor_->SaveTile16ToScratchSpace(0);
293 EXPECT_TRUE(save_result.ok()) << "Scratch save failed: " << save_result.message();
294
295 // Test scratch space load
296 auto load_result = editor_->LoadTile16FromScratchSpace(0);
297 EXPECT_TRUE(load_result.ok()) << "Scratch load failed: " << load_result.message();
298
299 // Test scratch space clear
300 auto clear_result = editor_->ClearScratchSpace(0);
301 EXPECT_TRUE(clear_result.ok()) << "Scratch clear failed: " << clear_result.message();
302#else
303 GTEST_SKIP() << "ROM tests disabled";
304#endif
305}
306
307} // namespace test
308} // namespace editor
309} // namespace yaze
static std::unique_ptr< gfx::SDL2Renderer > test_renderer_
std::unique_ptr< zelda3::Overworld > overworld_
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:32
static Arena & Get()
Definition arena.cc:15
absl::Status ShutdownWindow(Window &window)
Definition window.cc:137
absl::Status CreateWindow(Window &window, gfx::IRenderer *renderer, int flags)
Definition window.cc:57
TEST_F(Tile16EditorIntegrationTest, BasicValidation)
Tilemap CreateTilemap(IRenderer *renderer, std::vector< uint8_t > &data, int width, int height, int tile_size, int num_tiles, SnesPalette &palette)
Definition tilemap.cc:14
constexpr int kNumTile16Individual
Definition overworld.h:120
Main namespace for the application.