diff options
author | Zaba999 <qrwfw5rp> | 2012-10-11 23:09:29 +0200 |
---|---|---|
committer | Zaba999 <qrwfw5rp> | 2012-10-11 23:09:29 +0200 |
commit | 8a932fed3e5700af283017deab3cb1da11cb0dce (patch) | |
tree | 892d7a4068c843cb41bd202f078b042027a0a3d0 /src/ui | |
parent | 3f7f013168e62693601ab57108d329ffaa924439 (diff) | |
download | colobot-8a932fed3e5700af283017deab3cb1da11cb0dce.tar.gz colobot-8a932fed3e5700af283017deab3cb1da11cb0dce.tar.bz2 colobot-8a932fed3e5700af283017deab3cb1da11cb0dce.zip |
Added saving user scripts on game save. Fixed bug in writing script to file.
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/control.cpp | 1 | ||||
-rw-r--r-- | src/ui/control.h | 7 | ||||
-rw-r--r-- | src/ui/edit.cpp | 5 | ||||
-rw-r--r-- | src/ui/edit.h | 2 | ||||
-rw-r--r-- | src/ui/test/CMakeLists.txt | 33 | ||||
-rw-r--r-- | src/ui/test/edit_test.cpp | 73 | ||||
-rw-r--r-- | src/ui/test/mocks/text_mock.h | 21 | ||||
-rw-r--r-- | src/ui/test/stubs/app_stub.cpp | 26 | ||||
-rw-r--r-- | src/ui/test/stubs/engine_stub.cpp | 79 | ||||
-rw-r--r-- | src/ui/test/stubs/restext_stub.cpp | 11 | ||||
-rw-r--r-- | src/ui/test/stubs/robotmain_stub.cpp | 17 |
11 files changed, 266 insertions, 9 deletions
diff --git a/src/ui/control.cpp b/src/ui/control.cpp index 16769d1..718ad3b 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -21,7 +21,6 @@ namespace Ui { // Object's constructor. - CControl::CControl() { m_iMan = CInstanceManager::GetInstancePointer(); diff --git a/src/ui/control.h b/src/ui/control.h index e08c34d..635ae12 100644 --- a/src/ui/control.h +++ b/src/ui/control.h @@ -1,4 +1,4 @@ -// * This file is part of the COLOBOT source code +// * This file is part of the COLOBOT source code; // * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch // * Copyright (C) 2012 Polish Portal of Colobot (PPC) // * @@ -65,8 +65,7 @@ namespace Ui { class CControl { public: - // CControl(CInstanceManager* iMan); - CControl (); + CControl(); virtual ~CControl(); virtual bool Create(Math::Point pos, Math::Point dim, int icon, EventType eventType); @@ -117,9 +116,9 @@ namespace Ui { protected: CInstanceManager* m_iMan; Gfx::CEngine* m_engine; + Gfx::CParticle* m_particle; CEventQueue* m_event; CRobotMain* m_main; - Gfx::CParticle* m_particle; CSoundInterface* m_sound; Math::Point m_pos; // corner upper / left diff --git a/src/ui/edit.cpp b/src/ui/edit.cpp index ca53a7a..547e73a 100644 --- a/src/ui/edit.cpp +++ b/src/ui/edit.cpp @@ -71,8 +71,6 @@ bool IsSep(int character) //! Object's constructor. - -//CEdit::CEdit(CInstanceManager* iMan) : CControl(iMan) CEdit::CEdit () : CControl () { Math::Point pos; @@ -3081,7 +3079,7 @@ void CEdit::Justif() if ( m_format.size() == 0 ) { // TODO check if good - + i += m_engine->GetText()->Justify(m_text+i, m_fontType, m_fontSize, width); } @@ -3137,6 +3135,7 @@ void CEdit::Justif() } if ( m_lineTotal >= EDITLINEMAX-2 ) break; } + if ( m_len > 0 && m_text[m_len-1] == '\n' ) { m_lineOffset[m_lineTotal] = m_len; diff --git a/src/ui/edit.h b/src/ui/edit.h index 7414372..35d8b2c 100644 --- a/src/ui/edit.h +++ b/src/ui/edit.h @@ -124,8 +124,8 @@ struct HyperHistory class CEdit : public CControl { public: -// CEdit(CInstanceManager* iMan); CEdit (); + virtual ~CEdit(); bool Create(Math::Point pos, Math::Point dim, int icon, EventType eventType); diff --git a/src/ui/test/CMakeLists.txt b/src/ui/test/CMakeLists.txt new file mode 100644 index 0000000..e49e6af --- /dev/null +++ b/src/ui/test/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 2.8) + +set(CMAKE_BUILD_TYPE debug) +set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -Wold-style-cast -std=gnu++0x") + +include_directories( +. +../.. +../../.. +${GTEST_DIR}/include +) + + +add_executable(edit_test + ../../common/event.cpp + ../../common/logger.cpp + ../../common/misc.cpp + ../../common/iman.cpp + ../../common/stringutils.cpp + ../../graphics/engine/particle.cpp + ../../graphics/engine/text.cpp + ../button.cpp + ../control.cpp + ../edit.cpp + ../scroll.cpp + stubs/app_stub.cpp + stubs/engine_stub.cpp + stubs/restext_stub.cpp + stubs/robotmain_stub.cpp + edit_test.cpp) +target_link_libraries(edit_test gtest gmock ${SDL_LIBRARY} ${SDLTTF_LIBRARY}) + +add_test(edit_test ./edit_test) diff --git a/src/ui/test/edit_test.cpp b/src/ui/test/edit_test.cpp new file mode 100644 index 0000000..489b873 --- /dev/null +++ b/src/ui/test/edit_test.cpp @@ -0,0 +1,73 @@ +#include "../edit.h" +#include "../../app/app.h" +#include "mocks/text_mock.h" +#include <gtest/gtest.h> +#include <gmock/gmock.h> +#include <fstream> + +class CEditTest : public testing::Test +{ +public: + CEditTest(){}; + + virtual void SetUp() + { + m_engine = new Gfx::CEngine(&m_iMan, NULL); + + m_iMan.AddInstance(CLASS_ENGINE, m_engine); + m_edit = new Ui::CEdit; + } + + virtual void TearDown() + { + m_iMan.DeleteInstance(CLASS_ENGINE, m_engine); + delete m_engine; + m_engine = NULL; + delete m_edit; + m_edit = NULL; + + } + virtual ~CEditTest() + { + + }; + +protected: + CInstanceManager m_iMan; + CApplication m_app; + Gfx::CEngine * m_engine; + Ui::CEdit * m_edit; + CLogger m_logger; +}; + +using ::testing::_; +using ::testing::Return; + +TEST_F(CEditTest, WriteTest) +{ + ASSERT_TRUE(true); + CTextMock * text = dynamic_cast<CTextMock *>(m_engine->GetText()); + EXPECT_CALL(*text, GetCharWidth(_, _, _, _)).WillRepeatedly(Return(1.0f)); + EXPECT_CALL(*text, GetStringWidth(_, _, _)).WillOnce(Return(1.0f)); + std::string filename = "test.file"; + m_edit->SetMaxChar(Ui::EDITSTUDIOMAX); + m_edit->SetAutoIndent(true); + std::string inputScript = "{\ntext1\ntext2\n\ntext3\n{\ntext4\n}\n}"; + std::string expectedScript = "{\r\n\ttext1\r\n\ttext2\r\n\t\r\n\ttext3\r\n\t{\r\n\t\ttext4\r\n\t}\r\n}"; + m_edit->SetText(inputScript.c_str(), true); + GetLogger()->Info("Writing text \n"); + m_edit->WriteText("script.txt"); + + std::fstream scriptFile; + + scriptFile.open("script.txt", std::ios_base::binary | std::ios_base::in); + std::string outputScript((std::istreambuf_iterator<char>(scriptFile)), std::istreambuf_iterator<char>()); + ASSERT_STREQ(expectedScript.c_str(), outputScript.c_str()); +} + +int main(int argc, char *argv[]) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} + diff --git a/src/ui/test/mocks/text_mock.h b/src/ui/test/mocks/text_mock.h new file mode 100644 index 0000000..59a6c48 --- /dev/null +++ b/src/ui/test/mocks/text_mock.h @@ -0,0 +1,21 @@ +#include "../../graphics/engine/text.h" +#include <gmock/gmock.h> +#include "../../common/logger.h" + + +class CTextMock : public Gfx::CText +{ +public: + CTextMock(CInstanceManager *iMan, Gfx::CEngine* engine) : CText(iMan, engine) + { + } + + virtual ~CTextMock() + { + }; + + MOCK_METHOD4(GetCharWidth, float(Gfx::UTF8Char, Gfx::FontType, float, float)); + MOCK_METHOD3(GetStringWidth, float(const std::string &, Gfx::FontType, float)); + +}; + diff --git a/src/ui/test/stubs/app_stub.cpp b/src/ui/test/stubs/app_stub.cpp new file mode 100644 index 0000000..5dd79e4 --- /dev/null +++ b/src/ui/test/stubs/app_stub.cpp @@ -0,0 +1,26 @@ +#include "../../app/app.h" +#include "../../graphics/opengl/gldevice.h" + +template<> CApplication* CSingleton<CApplication>::mInstance = nullptr; + +namespace Gfx { + +GLDeviceConfig::GLDeviceConfig() +{ +} + +} /* Gfx */ +CApplication::CApplication() +{ +} + +CApplication::~CApplication() +{ +} + +std::string CApplication::GetDataFilePath(DataDir /* dataDir */, const std::string& subpath) +{ + return subpath; +} + + diff --git a/src/ui/test/stubs/engine_stub.cpp b/src/ui/test/stubs/engine_stub.cpp new file mode 100644 index 0000000..6ec6006 --- /dev/null +++ b/src/ui/test/stubs/engine_stub.cpp @@ -0,0 +1,79 @@ +#include "../../graphics/engine/engine.h" +#include "../../graphics/engine/text.h" +#include "../mocks/text_mock.h" + +namespace Gfx { + +CEngine::CEngine(CInstanceManager* iMan, CApplication* app) : + m_iMan(iMan), m_app(app) +{ + m_text = new CTextMock(m_iMan, this); + m_text->Create(); +} + +CEngine::~CEngine() +{ + delete m_text; + m_text = NULL; +} + +Math::Point CEngine::WindowToInterfaceSize(Math::IntPoint size) +{ + return Math::Point(size.x, size.y); +} + +void CEngine::SetState(int state, const Color& color) +{ + if (state == m_lastState && color == m_lastColor) + return; + + m_lastState = state; + m_lastColor = color; +} + +Math::IntPoint CEngine::GetWindowSize() +{ + return m_size; +} + +void CEngine::AddStatisticTriangle(int count) +{ + m_statisticTriangle += count; +} + +void CEngine::SetMouseType(EngineMouseType type) +{ + m_mouseType = type; +} + +bool CEngine::SetTexture(const std::string& /* name */, int /* stage */) +{ + return true; +} + +CText* CEngine::GetText() +{ + return m_text; +} + +CDevice* CEngine::GetDevice() +{ + return m_device; +} + +int CEngine::GetEditIndentValue() +{ + return m_editIndentValue; +} + +void CEngine::DeleteTexture(const std::string& /* texName */) +{ +} +Texture CEngine::LoadTexture(const std::string& /* name */) +{ + Texture texture; + return texture; +} + +} /* Gfx */ + diff --git a/src/ui/test/stubs/restext_stub.cpp b/src/ui/test/stubs/restext_stub.cpp new file mode 100644 index 0000000..c1986ca --- /dev/null +++ b/src/ui/test/stubs/restext_stub.cpp @@ -0,0 +1,11 @@ +#include "../../common/restext.h" +bool GetResource(ResType /* type */, int /* num */, char* /* text */) +{ + return true; +} + +bool SearchKey(const char * /* cmd */, InputSlot & /* key */) +{ + return true; +} + diff --git a/src/ui/test/stubs/robotmain_stub.cpp b/src/ui/test/stubs/robotmain_stub.cpp new file mode 100644 index 0000000..93e0e82 --- /dev/null +++ b/src/ui/test/stubs/robotmain_stub.cpp @@ -0,0 +1,17 @@ +#include "../../object/robotmain.h" + + +template<> CRobotMain* CSingleton<CRobotMain>::mInstance = nullptr; + +bool CRobotMain::GetGlint() +{ + return false; +} + +const InputBinding& CRobotMain::GetInputBinding(InputSlot slot) +{ + unsigned int index = static_cast<unsigned int>(slot); + assert(index >= 0 && index < INPUT_SLOT_MAX); + return m_inputBindings[index]; +} + |