emf_win_unittest.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "printing/emf_win.h"
  5. // For quick access.
  6. #include <stdint.h>
  7. #include <wingdi.h>
  8. #include <winspool.h>
  9. #include <memory>
  10. #include <string>
  11. #include <utility>
  12. #include "base/files/file_path.h"
  13. #include "base/files/file_util.h"
  14. #include "base/files/scoped_temp_dir.h"
  15. #include "base/path_service.h"
  16. #include "base/win/scoped_hdc.h"
  17. #include "printing/mojom/print.mojom.h"
  18. #include "printing/printing_context.h"
  19. #include "printing/printing_context_win.h"
  20. #include "testing/gtest/include/gtest/gtest.h"
  21. #include "ui/gfx/geometry/point.h"
  22. #include "ui/gfx/geometry/size.h"
  23. namespace printing {
  24. namespace {
  25. // This test is automatically disabled if no printer named "UnitTest Printer" is
  26. // available.
  27. class EmfPrintingTest : public testing::Test, public PrintingContext::Delegate {
  28. public:
  29. typedef testing::Test Parent;
  30. static bool IsTestCaseDisabled() {
  31. // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700.
  32. HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", nullptr, nullptr);
  33. if (!hdc)
  34. return true;
  35. DeleteDC(hdc);
  36. return false;
  37. }
  38. // PrintingContext::Delegate methods.
  39. gfx::NativeView GetParentView() override { return nullptr; }
  40. std::string GetAppLocale() override { return std::string(); }
  41. };
  42. const uint32_t EMF_HEADER_SIZE = 128;
  43. } // namespace
  44. TEST(EmfTest, DC) {
  45. // Simplest use case.
  46. std::vector<char> data;
  47. {
  48. Emf emf;
  49. EXPECT_TRUE(emf.Init());
  50. EXPECT_TRUE(emf.context());
  51. // An empty EMF is invalid, so we put at least a rectangle in it.
  52. ::Rectangle(emf.context(), 10, 10, 190, 190);
  53. EXPECT_TRUE(emf.FinishDocument());
  54. uint32_t size = emf.GetDataSize();
  55. EXPECT_GT(size, EMF_HEADER_SIZE);
  56. EXPECT_TRUE(emf.GetDataAsVector(&data));
  57. ASSERT_EQ(data.size(), size);
  58. }
  59. // Playback the data.
  60. Emf emf;
  61. // TODO(thestig): Make `data` uint8_t and avoid the base::as_bytes() call.
  62. EXPECT_TRUE(emf.InitFromData(base::as_bytes(base::make_span(data))));
  63. HDC hdc = CreateCompatibleDC(nullptr);
  64. EXPECT_TRUE(hdc);
  65. RECT output_rect = {0, 0, 10, 10};
  66. EXPECT_TRUE(emf.Playback(hdc, &output_rect));
  67. EXPECT_TRUE(DeleteDC(hdc));
  68. }
  69. // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598.
  70. TEST_F(EmfPrintingTest, Enumerate) {
  71. if (IsTestCaseDisabled())
  72. return;
  73. auto settings = std::make_unique<PrintSettings>();
  74. // My test case is a HP Color LaserJet 4550 PCL.
  75. settings->set_device_name(u"UnitTest Printer");
  76. // Initialize it.
  77. PrintingContextWin context(this);
  78. EXPECT_EQ(mojom::ResultCode::kSuccess,
  79. context.InitWithSettingsForTest(std::move(settings)));
  80. base::FilePath emf_file;
  81. EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &emf_file));
  82. emf_file = emf_file.Append(FILE_PATH_LITERAL("printing"))
  83. .Append(FILE_PATH_LITERAL("test"))
  84. .Append(FILE_PATH_LITERAL("data"))
  85. .Append(FILE_PATH_LITERAL("emf"))
  86. .Append(FILE_PATH_LITERAL("test4.emf"));
  87. // Load any EMF with an image.
  88. std::string emf_data;
  89. base::ReadFileToString(emf_file, &emf_data);
  90. ASSERT_TRUE(emf_data.size());
  91. Emf emf;
  92. EXPECT_TRUE(emf.InitFromData(base::as_bytes(base::make_span(emf_data))));
  93. // This will print to file. The reason is that when running inside a
  94. // unit_test, PrintingContext automatically dumps its files to the
  95. // current directory.
  96. // TODO(maruel): Clean the .PRN file generated in current directory.
  97. context.NewDocument(u"EmfTest.Enumerate");
  98. // Process one at a time.
  99. RECT page_bounds = emf.GetPageBounds(1).ToRECT();
  100. Emf::Enumerator emf_enum(emf, context.context(), &page_bounds);
  101. for (Emf::Enumerator::const_iterator itr = emf_enum.begin();
  102. itr != emf_enum.end(); ++itr) {
  103. // To help debugging.
  104. ptrdiff_t index = itr - emf_enum.begin();
  105. // If you get this assert, you need to lookup iType in wingdi.h. It starts
  106. // with EMR_HEADER.
  107. EMR_HEADER;
  108. EXPECT_TRUE(itr->SafePlayback(&emf_enum.context_))
  109. << " index: " << index << " type: " << itr->record()->iType;
  110. }
  111. context.DocumentDone();
  112. }
  113. // Disabled if no "UnitTest printer" exists.
  114. TEST_F(EmfPrintingTest, PageBreak) {
  115. base::win::ScopedCreateDC dc(
  116. CreateDC(L"WINSPOOL", L"UnitTest Printer", nullptr, nullptr));
  117. if (!dc.Get())
  118. return;
  119. std::vector<char> data;
  120. {
  121. Emf emf;
  122. EXPECT_TRUE(emf.Init());
  123. EXPECT_TRUE(emf.context());
  124. int pages = 3;
  125. while (pages) {
  126. emf.StartPage(gfx::Size(), gfx::Rect(), 1,
  127. mojom::PageOrientation::kUpright);
  128. ::Rectangle(emf.context(), 10, 10, 190, 190);
  129. EXPECT_TRUE(emf.FinishPage());
  130. --pages;
  131. }
  132. EXPECT_EQ(3U, emf.GetPageCount());
  133. EXPECT_TRUE(emf.FinishDocument());
  134. uint32_t size = emf.GetDataSize();
  135. EXPECT_TRUE(emf.GetDataAsVector(&data));
  136. ASSERT_EQ(data.size(), size);
  137. }
  138. // Playback the data.
  139. DOCINFO di = {0};
  140. di.cbSize = sizeof(DOCINFO);
  141. di.lpszDocName = L"Test Job";
  142. int job_id = ::StartDoc(dc.Get(), &di);
  143. Emf emf;
  144. // TODO(thestig): Make `data` uint8_t and avoid the base::as_bytes() call.
  145. EXPECT_TRUE(emf.InitFromData(base::as_bytes(base::make_span(data))));
  146. EXPECT_TRUE(emf.SafePlayback(dc.Get()));
  147. ::EndDoc(dc.Get());
  148. // Since presumably the printer is not real, let us just delete the job from
  149. // the queue.
  150. HANDLE printer = nullptr;
  151. if (::OpenPrinter(const_cast<LPTSTR>(L"UnitTest Printer"), &printer,
  152. nullptr)) {
  153. ::SetJob(printer, job_id, 0, nullptr, JOB_CONTROL_DELETE);
  154. ClosePrinter(printer);
  155. }
  156. }
  157. TEST(EmfTest, FileBackedEmf) {
  158. // Simplest use case.
  159. base::ScopedTempDir scratch_metafile_dir;
  160. ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir());
  161. base::FilePath metafile_path;
  162. EXPECT_TRUE(base::CreateTemporaryFileInDir(scratch_metafile_dir.GetPath(),
  163. &metafile_path));
  164. uint32_t size;
  165. std::vector<char> data;
  166. {
  167. Emf emf;
  168. EXPECT_TRUE(emf.InitToFile(metafile_path));
  169. EXPECT_TRUE(emf.context());
  170. // An empty EMF is invalid, so we put at least a rectangle in it.
  171. ::Rectangle(emf.context(), 10, 10, 190, 190);
  172. EXPECT_TRUE(emf.FinishDocument());
  173. size = emf.GetDataSize();
  174. EXPECT_GT(size, EMF_HEADER_SIZE);
  175. EXPECT_TRUE(emf.GetDataAsVector(&data));
  176. EXPECT_EQ(data.size(), size);
  177. }
  178. int64_t file_size = 0;
  179. base::GetFileSize(metafile_path, &file_size);
  180. EXPECT_EQ(size, file_size);
  181. // Playback the data.
  182. HDC hdc = CreateCompatibleDC(nullptr);
  183. EXPECT_TRUE(hdc);
  184. Emf emf;
  185. EXPECT_TRUE(emf.InitFromFile(metafile_path));
  186. RECT output_rect = {0, 0, 10, 10};
  187. EXPECT_TRUE(emf.Playback(hdc, &output_rect));
  188. EXPECT_TRUE(DeleteDC(hdc));
  189. }
  190. } // namespace printing