Merge pull request #2646 from ludovic-henry/fix-processwatch-dispose
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Printing / PrintingServicesUnixTest.cs
1 //
2 // PrintingServicesUnix class unit tests
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Collections.Generic;
31 using System.Drawing;
32 using System.Drawing.Printing;
33 using System.Runtime.InteropServices;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Drawing.Printing {
37
38         [TestFixture]
39         public class PrintingServicesUnixTest {
40
41                 [DllImport ("gdiplus.dll")]
42                 static extern Status GdipGetPostScriptSavePage (IntPtr graphics);
43
44                 [Test]
45                 public void BuiltInPrinting ()
46                 {
47                         // ensure libgdiplus is built with printing support enabled
48                         if (GDIPlus.RunningOnWindows ())
49                                 Assert.Ignore ("Running on Windows.");
50
51                         Assert.AreEqual (Status.InvalidParameter, GdipGetPostScriptSavePage (IntPtr.Zero), "Missing printing support");
52                 }
53
54                 #region Novell Bug #602934
55
56                 #region CUPS methods and structs
57
58                 [StructLayout (LayoutKind.Sequential)]
59                 struct CUPS_DEST
60                 {
61                         public string Name;
62                         public string Instance;
63                         public int IsDefault;
64                         public int NumOptions;
65                         public IntPtr Options;
66                 }
67
68                 [StructLayout (LayoutKind.Sequential)]
69                 struct CUPS_OPTION
70                 {
71                         public string Name;
72                         public string Value;
73                 }
74
75                 readonly IntPtr CUPS_HTTP_DEFAULT = IntPtr.Zero;
76
77                 [DllImport ("libcups")]
78                 static extern IntPtr cupsGetNamedDest (IntPtr http, string name, string instance);
79
80                 [DllImport ("libcups")]
81                 static extern void cupsFreeDests (int num_dests, IntPtr dests);
82
83                 [DllImport ("libcups")]
84                 static extern void cupsFreeDests (int num_dests, ref CUPS_DEST dests);
85
86                 #endregion
87
88                 Dictionary<string, string> GetOptionsOfFirstPrinterThroughCups ()
89                 {
90                         var options = new Dictionary<string, string> ();
91
92                         var destPtr = cupsGetNamedDest (CUPS_HTTP_DEFAULT, PrinterSettings.InstalledPrinters [0], null);
93                         var dest = (CUPS_DEST)Marshal.PtrToStructure (destPtr, typeof(CUPS_DEST));
94                         var optionPtr = dest.Options;
95                         int cupsOptionSize = Marshal.SizeOf (typeof(CUPS_OPTION));
96                         for (int i = 0; i < dest.NumOptions; i++) {
97                                 var cupsOption = (CUPS_OPTION)Marshal.PtrToStructure (optionPtr, typeof(CUPS_OPTION));
98                                 options.Add (cupsOption.Name, cupsOption.Value);
99                                 optionPtr = (IntPtr)((long)optionPtr + cupsOptionSize);
100                         }
101                         cupsFreeDests (1, destPtr);
102                         return options;
103                 }
104
105                 [Test]
106                 [Platform (Exclude = "Win", Reason = "Depends on CUPS which is usually not installed on Windows")]
107                 public void Bug602934_PrinterSettingsReturnActualValues ()
108                 {
109                         if (PrinterSettings.InstalledPrinters.Count < 1)
110                                 Assert.Ignore ("Need at least one printer installed.");
111
112                         var options = GetOptionsOfFirstPrinterThroughCups ();
113
114                         var settings = new PrinterSettings () { PrinterName = PrinterSettings.InstalledPrinters [0] };
115                         Assert.AreEqual (options ["PageSize"], settings.DefaultPageSettings.PaperSize.PaperName,
116                                 "Bug #602934 (https://bugzilla.novell.com/show_bug.cgi?id=602934) not fixed (PaperSize)");
117                         if (options.ContainsKey("Resolution"))
118                                 Assert.AreEqual (options ["Resolution"], string.Format ("{0}dpi", settings.DefaultPageSettings.PrinterResolution.X),
119                                         "Bug #602934 (https://bugzilla.novell.com/show_bug.cgi?id=602934) not fixed (Resolution)");
120                 }
121
122                 #endregion
123
124         }
125 }