[gbrainy] Make PdfExporter no static class and fix some unit tests



commit e0f07cb84d3fe350ab4713354a0d0f0ccfea7030
Author: Jordi Mas <jmas softcatala org>
Date:   Mon Dec 19 15:28:20 2011 +0100

    Make PdfExporter no static class and fix some unit tests

 src/Clients/Classical/Dialogs/PdfExportDialog.cs |   10 +++---
 src/Clients/Classical/gbrainy.cs                 |    2 +-
 src/Clients/WebForms/AllGames.aspx.cs            |    2 +-
 src/Core/Main/PdfExporter.cs                     |   36 +++++++++++++---------
 tests/Clients/Classical/CommandLineTest.cs       |    2 +-
 tests/Clients/Classical/gbrainyTest.cs           |   12 +++----
 6 files changed, 34 insertions(+), 30 deletions(-)
---
diff --git a/src/Clients/Classical/Dialogs/PdfExportDialog.cs b/src/Clients/Classical/Dialogs/PdfExportDialog.cs
index 7095f49..5f54d56 100644
--- a/src/Clients/Classical/Dialogs/PdfExportDialog.cs
+++ b/src/Clients/Classical/Dialogs/PdfExportDialog.cs
@@ -42,14 +42,14 @@ namespace gbrainy.Clients.Classical.Dialogs
 
 		BrowseFile file;
 		GameManager manager;
-		ITranslations translations;
+		PdfExporter pdfExporter;
 		const int COLUMN_VALUE = 1;
 		const int DEF_SIDEVALUE = 4;
 
 		public PdfExportDialog (GameManager manager, ITranslations translations) : base (translations, "PdfExportDialog.ui", "pdfexportbox")
 		{
+			pdfExporter = new PdfExporter (translations);
 			this.manager = manager;
-			this.translations = translations;
 			games_spinbutton.Value = 10;
 			checkbox_logic.Active = checkbox_calculation.Active = checkbox_verbal.Active = true;
 
@@ -91,7 +91,7 @@ namespace gbrainy.Clients.Classical.Dialogs
 			layout_combo.PackStart (layout_cell, true);
 			layout_combo.SetCellDataFunc (layout_cell, ComboBoxCellFunc);
 
-			int [] per_side = PdfExporter.PagesPerSide;
+			int [] per_side = pdfExporter.PagesPerSide;
 
 			for (int i = 0; i < per_side.Length; i++)
 				layout_store.AppendValues (per_side[i].ToString (), per_side[i]);
@@ -156,7 +156,7 @@ namespace gbrainy.Clients.Classical.Dialogs
 			MessageType msg_type;
 
 			games = new Game [num_games];
-			session = new GameSession (translations);
+			session = new GameSession (Translations);
 			session.GameManager = manager;
 			session.PlayList.ColorBlind = colorblind;
 			session.PlayList.Difficulty = difficulty;
@@ -168,7 +168,7 @@ namespace gbrainy.Clients.Classical.Dialogs
 				games [n].Translations = Translations;
 			}
 
-			if (PdfExporter.GeneratePdf (games, gamespage, filename) == true) {
+			if (pdfExporter.GeneratePdf (games, gamespage, filename) == true) {
 				msg = Catalog.GetString ("The PDF file has been exported correctly.");
 				msg_type = MessageType.Info;
 			} else {
diff --git a/src/Clients/Classical/gbrainy.cs b/src/Clients/Classical/gbrainy.cs
index 03d5a14..69fcc54 100644
--- a/src/Clients/Classical/gbrainy.cs
+++ b/src/Clients/Classical/gbrainy.cs
@@ -502,7 +502,7 @@ namespace gbrainy.Clients.Classical
 			if (String.Compare (Preferences.Get <string> (Preferences.EnglishVersionKey), Defines.VERSION, 0) == 0)
 				return false;
 
-			int percentage = ServiceLocator.Instance.GetService <ITranslations> ().TranslationPercentage;
+			int percentage = Translations.TranslationPercentage;
 			if (percentage > 0 && percentage < MIN_TRANSLATION)
 			{
 				Preferences.Set <string> (Preferences.EnglishVersionKey, Defines.VERSION);
diff --git a/src/Clients/WebForms/AllGames.aspx.cs b/src/Clients/WebForms/AllGames.aspx.cs
index cd6c312..219aada 100644
--- a/src/Clients/WebForms/AllGames.aspx.cs
+++ b/src/Clients/WebForms/AllGames.aspx.cs
@@ -125,7 +125,7 @@ namespace gbrainy.Clients.WebForms
 					continue;
 		
 				game = (gbrainy.Core.Main.Game) Activator.CreateInstance (games [i].TypeOf, true);
-				game.translations = translations;
+				game.Translations = translations;
 				game.Variant = games [i].Variant;
 				game.Begin ();								
 				string file = CreateImage (game, i);
diff --git a/src/Core/Main/PdfExporter.cs b/src/Core/Main/PdfExporter.cs
index 6dd6063..bec1267 100644
--- a/src/Core/Main/PdfExporter.cs
+++ b/src/Core/Main/PdfExporter.cs
@@ -26,20 +26,27 @@ using gbrainy.Core.Services;
 namespace gbrainy.Core.Main
 {
 	// Generates a single PDF document with the selected games
-	static public class PdfExporter
+	public class PdfExporter
 	{
-		static readonly int width = 400, height = 400, margin = 20, question_height = 100;
-		static readonly int page_margin = 20; // space between vertical and hortizontal pages
-		static readonly int page_width = width + page_margin;
-		static readonly int page_height = height + question_height + page_margin;
-		static int [] pages_side = {1, 2, 4};
+		const int width = 400, height = 400, margin = 20, question_height = 100;
+		const int page_margin = 20; // space between vertical and hortizontal pages
+		const int page_width = width + page_margin;
+		const int page_height = height + question_height + page_margin;
+		readonly int [] pages_side = {1, 2, 4};
 
-		static public int [] PagesPerSide
+		ITranslations Translations { get; set;}
+
+		public PdfExporter (ITranslations translations)
+		{
+			Translations = translations;
+		}
+
+		public int [] PagesPerSide
 		{
 			get { return pages_side; }
 		}
 
-		static public bool GeneratePdf (Game [] games, int games_page, string file)
+		public bool GeneratePdf (Game [] games, int games_page, string file)
 		{
 			int columns, rows;
 			switch (games_page) {
@@ -82,7 +89,7 @@ namespace gbrainy.Core.Main
 			}
 		}
 
-		static void GenerateQuestions (CairoContextEx cr, Game [] games, int columns, int rows)
+		void GenerateQuestions (CairoContextEx cr, Game [] games, int columns, int rows)
 		{
 			int x, y, page;
 			Game puzzle;
@@ -102,7 +109,7 @@ namespace gbrainy.Core.Main
 
 				// Translators: {0} is the game number and {1} the game question or answer
 				// The number is used as reference when looking for the game solution in the PDF
-				str = String.Format (ServiceLocator.Instance.GetService <ITranslations> ().GetString ("Game #{0}. {1}"), i + 1, puzzle.Question);
+				str = String.Format (Translations.GetString ("Game #{0}. {1}"), i + 1, puzzle.Question);
 
 				// Draw question
 				cr.SetPangoFontSize (12);
@@ -119,7 +126,7 @@ namespace gbrainy.Core.Main
 					cr.Save ();
 					cr.SetPangoFontSize (0.02);
 					cr.MoveTo (0.05, 0.95);
-					cr.ShowPangoText (String.Format (ServiceLocator.Instance.GetService <ITranslations> ().GetString ("Created by gbrainy {0}"), Defines.VERSION));
+					cr.ShowPangoText (String.Format (Translations.GetString ("Created by gbrainy {0}"), Defines.VERSION));
 					cr.Stroke ();
 					cr.Restore ();
 				}				
@@ -142,7 +149,7 @@ namespace gbrainy.Core.Main
 				cr.ShowPage ();
 		}
 
-		static void GenerateAnswers (CairoContextEx cr, Game [] games, int columns, int rows)
+		void GenerateAnswers (CairoContextEx cr, Game [] games, int columns, int rows)
 		{
 			int x, y, page;
 			string str;
@@ -154,8 +161,7 @@ namespace gbrainy.Core.Main
 
 			// Draw solution title
 			cr.SetPangoFontSize (20);
-			cr.DrawStringWithWrapping (x + margin, y + margin,
-				ServiceLocator.Instance.GetService <ITranslations> ().GetString ("Solutions"), width - margin);
+			cr.DrawStringWithWrapping (x + margin, y + margin, Translations.GetString ("Solutions"), width - margin);
 			y += space_lines;
 			cr.Stroke ();
 
@@ -163,7 +169,7 @@ namespace gbrainy.Core.Main
 			cr.UseMarkup = true;
 			for (int i = 0; i < games.Length; i++)
 			{
-				str = String.Format (ServiceLocator.Instance.GetService <ITranslations> ().GetString ("Game #{0}. {1}"), i + 1, games[i].AnswerText);
+				str = String.Format (Translations.GetString ("Game #{0}. {1}"), i + 1, games[i].AnswerText);
 
 				// Draw Solution
 				cr.DrawStringWithWrapping (x + margin, y + margin, str, width - margin);
diff --git a/tests/Clients/Classical/CommandLineTest.cs b/tests/Clients/Classical/CommandLineTest.cs
index aad4576..5a2e9d4 100644
--- a/tests/Clients/Classical/CommandLineTest.cs
+++ b/tests/Clients/Classical/CommandLineTest.cs
@@ -77,7 +77,7 @@ namespace gbrainy.Test.Clients.Classical
 					continue;
 
 				Game game = (Game) Activator.CreateInstance (games[i].TypeOf, true);
-				game.translations = translations;
+				game.Translations = translations;
 				game.Variant = games[i].Variant;
 
 				if (cand_idx > 0)
diff --git a/tests/Clients/Classical/gbrainyTest.cs b/tests/Clients/Classical/gbrainyTest.cs
index 703ec89..2dbfc91 100644
--- a/tests/Clients/Classical/gbrainyTest.cs
+++ b/tests/Clients/Classical/gbrainyTest.cs
@@ -48,9 +48,7 @@ namespace gbrainy.Test.Clients.Classical
 		[TestFixtureSetUp]
 		public void Construct ()
 		{
-			RegisterDefaultServices ();
 			translations =  new TranslationsTest ();
-			ServiceLocator.Instance.RegisterService (typeof (ITranslations), translations);
 		}
 
 		[Test]
@@ -61,7 +59,7 @@ namespace gbrainy.Test.Clients.Classical
 			Preferences.Clear ();
 
 			translations.Percentage = client.MIN_TRANSLATION;
-			Assert.AreEqual (false, client.ShowTranslationWarning ());
+			Assert.AreEqual (false, client.ShouldShowTranslationWarning ());
 		}
 
 		[Test]
@@ -72,8 +70,8 @@ namespace gbrainy.Test.Clients.Classical
 			Preferences.Clear ();
 
 			translations.Percentage = client.MIN_TRANSLATION - 1;
-			Assert.AreEqual (true, client.ShowTranslationWarning ());
-			Assert.AreEqual (false, client.ShowTranslationWarning ());
+			Assert.AreEqual (true, client.ShouldShowTranslationWarning ());
+			Assert.AreEqual (false, client.ShouldShowTranslationWarning ());
 		}
 
 		[Test]
@@ -84,9 +82,9 @@ namespace gbrainy.Test.Clients.Classical
 			Preferences.Clear ();
 
 			translations.Percentage = client.MIN_TRANSLATION - 1;
-			Assert.AreEqual (true, client.ShowTranslationWarning ());
+			Assert.AreEqual (true, client.ShouldShowTranslationWarning ());
 			Preferences.Set <string> (Preferences.EnglishVersionKey, "n.n.n");
-			Assert.AreEqual (true, client.ShowTranslationWarning ());
+			Assert.AreEqual (true, client.ShouldShowTranslationWarning ());
 		}
 	}
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]