diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/graphics/engine/text.cpp | 5 | ||||
-rw-r--r-- | src/graphics/engine/text.h | 8 | ||||
-rw-r--r-- | src/graphics/opengl/gldevice.cpp | 4 | ||||
-rw-r--r-- | src/graphics/opengl/gldevice.h | 2 | ||||
-rw-r--r-- | src/object/robotmain.cpp | 2 | ||||
-rw-r--r-- | src/script/script.cpp | 30 | ||||
-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 |
18 files changed, 293 insertions, 34 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c8372b0..33fe532 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,7 @@ add_subdirectory(tools) if(${TESTS}) add_subdirectory(common/test) add_subdirectory(graphics/engine/test) + add_subdirectory(ui/test) add_subdirectory(math/test) endif() diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index 6355aed..66c73a9 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -407,10 +407,10 @@ int CText::Justify(const std::string &text, FontType font, float size, float wid if (len >= 3) ch.c3 = text[index+2]; - index += len; - if (ch.c1 == '\n') + { return index+1; + } if (ch.c1 == ' ' ) cut = index+1; @@ -421,6 +421,7 @@ int CText::Justify(const std::string &text, FontType font, float size, float wid if (cut == 0) return index; else return cut; } + index += len; } return index; diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h index 4575c37..57fad43 100644 --- a/src/graphics/engine/text.h +++ b/src/graphics/engine/text.h @@ -227,7 +227,7 @@ class CText { public: CText(CInstanceManager *iMan, CEngine* engine); - ~CText(); + virtual ~CText(); //! Sets the device to be used void SetDevice(CDevice *device); @@ -269,12 +269,12 @@ public: float GetHeight(FontType font, float size); //! Returns width of string (multi-format) - float GetStringWidth(const std::string &text, + TEST_VIRTUAL float GetStringWidth(const std::string &text, std::map<unsigned int, FontMetaChar> &format, float size); //! Returns width of string (single font) - float GetStringWidth(const std::string &text, FontType font, float size); + TEST_VIRTUAL float GetStringWidth(const std::string &text, FontType font, float size); //! Returns width of single character - float GetCharWidth(UTF8Char ch, FontType font, float size, float offset); + TEST_VIRTUAL float GetCharWidth(UTF8Char ch, FontType font, float size, float offset); //! Justifies a line of text (multi-format) int Justify(const std::string &text, std::map<unsigned int, FontMetaChar> &format, diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index 7bfd843..94b0dbc 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -48,6 +48,10 @@ // Graphics module namespace namespace Gfx { +GLDeviceConfig::GLDeviceConfig() +{ + LoadDefault(); +} void GLDeviceConfig::LoadDefault() { diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h index cda7b02..87c1247 100644 --- a/src/graphics/opengl/gldevice.h +++ b/src/graphics/opengl/gldevice.h @@ -52,7 +52,7 @@ struct GLDeviceConfig : public DeviceConfig bool hardwareAccel; //! Constructor calls LoadDefaults() - GLDeviceConfig() { LoadDefault(); } + GLDeviceConfig(); //! Loads the default values void LoadDefault(); diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp index 954d676..0f84ff7 100644 --- a/src/object/robotmain.cpp +++ b/src/object/robotmain.cpp @@ -5417,7 +5417,7 @@ void CRobotMain::FrameShowLimit(float rTime) -//! Returns a pointer to the last backslash in a filename. +//! Returns a pointer to the last slash in a filename. char* SearchLastDir(char *filename) { char* p = filename; diff --git a/src/script/script.cpp b/src/script/script.cpp index face9b7..001443b 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -3647,20 +3647,18 @@ bool CScript::ReadScript(const char* filename) { FILE* file; Ui::CEdit* edit; + std::string name; - // TODO: local user dir - std::string name = CApplication::GetInstancePointer()->GetDataFilePath(DIR_AI, filename); - - /*if ( strchr(filename, '\\') == 0 ) + if ( strchr(filename, '/') == 0 ) //we're reading non user script { - strcpy(name, "script\\"); - strcat(name, filename); + name = CApplication::GetInstancePointer()->GetDataFilePath(DIR_AI, filename); } else { -//? strcpy(name, filename); - UserDir(name, filename, ""); - }*/ + name = filename; + //TODO: is this needed? + // UserDir(name, filename, ""); + } file = fopen(name.c_str(), "rb"); if ( file == NULL ) return false; @@ -3683,22 +3681,20 @@ bool CScript::ReadScript(const char* filename) bool CScript::WriteScript(const char* filename) { Ui::CEdit* edit; + std::string name; - std::string name = CApplication::GetInstancePointer()->GetDataFilePath(DIR_AI, filename); - // TODO: local user dir - /*if ( strchr(filename, '\\') == 0 ) + if ( strchr(filename, '/') == 0 ) //we're writing non user script { - strcpy(name, "script\\"); - strcat(name, filename); + name = CApplication::GetInstancePointer()->GetDataFilePath(DIR_AI, filename); } else { - strcpy(name, filename); - }*/ + name = filename; + } if ( m_script == 0 ) { - // TODO: ? remove(filename); + remove(filename); return false; } 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 45e564d..e14b19d 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]; +} + |