chromium/printing/printing_utils_unittest.cc

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "printing/printing_utils.h"

#include <stddef.h>

#include <limits>
#include <string>

#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "printing/buildflags/buildflags.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"

#if BUILDFLAG(IS_WIN)
#include "base/win/scoped_hdc.h"
#include "printing/backend/printing_info_win.h"
#include "printing/backend/win_helper.h"
#include "printing/printing_test.h"
#include "ui/gfx/geometry/rect.h"
#endif

namespace printing {

namespace {

constexpr size_t kTestLength =;

#if BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
constexpr gfx::Size kIsoA4Microns(210000, 297000);
constexpr gfx::Size kNaLetterMicrons(216000, 279000);
#endif

std::string Simplify(const std::string& title) {}

std::string Format(const std::string& owner, const std::string& title) {}

#if BUILDFLAG(IS_WIN)
// This test is automatically disabled if no printer is available.
class PrintingUtilsWinTest : public PrintingTest<testing::Test> {};
#endif  // BUILDFLAG(IS_WIN)

}  // namespace

TEST(PrintingUtilsTest, SimplifyDocumentTitle) {}

TEST(PrintingUtilsTest, FormatDocumentTitleWithOwner) {}

#if BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
TEST(PrintingUtilsTest, GetDefaultPaperSizeFromLocaleMicrons) {}

TEST(PrintingUtilsTest, SizesEqualWithinEpsilon) {}
#endif  // BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)

#if BUILDFLAG(IS_WIN)
TEST(PrintingUtilsTest, GetCenteredPageContentRect) {
  gfx::Rect page_content;

  // No centering.
  gfx::Size page_size = gfx::Size(1200, 1200);
  gfx::Rect page_content_rect = gfx::Rect(0, 0, 400, 1100);
  page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
                                            page_content_rect);
  EXPECT_EQ(0, page_content.x());
  EXPECT_EQ(0, page_content.y());
  EXPECT_EQ(400, page_content.width());
  EXPECT_EQ(1100, page_content.height());

  // X centered.
  page_size = gfx::Size(500, 1200);
  page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
                                            page_content_rect);
  EXPECT_EQ(250, page_content.x());
  EXPECT_EQ(0, page_content.y());
  EXPECT_EQ(400, page_content.width());
  EXPECT_EQ(1100, page_content.height());

  // Y centered.
  page_size = gfx::Size(1200, 500);
  page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
                                            page_content_rect);
  EXPECT_EQ(0, page_content.x());
  EXPECT_EQ(250, page_content.y());
  EXPECT_EQ(400, page_content.width());
  EXPECT_EQ(1100, page_content.height());

  // Both X and Y centered.
  page_size = gfx::Size(500, 500),
  page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
                                            page_content_rect);
  EXPECT_EQ(250, page_content.x());
  EXPECT_EQ(250, page_content.y());
  EXPECT_EQ(400, page_content.width());
  EXPECT_EQ(1100, page_content.height());
}

// Disabled - see crbug.com/1231528 for context.
TEST_F(PrintingUtilsWinTest, DISABLED_GetPrintableAreaDeviceUnits) {
  if (IsTestCaseDisabled()) {
    return;
  }

  std::wstring printer_name = GetDefaultPrinter();
  ScopedPrinterHandle printer;
  ASSERT_TRUE(printer.OpenPrinterWithName(printer_name.c_str()));

  const DEVMODE* dev_mode = nullptr;
  PrinterInfo2 info_2;
  if (info_2.Init(printer.Get())) {
    dev_mode = info_2.get()->pDevMode;
  }
  ASSERT_TRUE(dev_mode);

  base::win::ScopedCreateDC hdc(
      CreateDC(L"WINSPOOL", printer_name.c_str(), nullptr, dev_mode));
  ASSERT_TRUE(hdc.Get());

  // Check that getting printable area is successful and the resulting area is
  // non-empty.
  gfx::Rect output = GetPrintableAreaDeviceUnits(hdc.Get());
  EXPECT_FALSE(output.IsEmpty());
}
#endif  // BUILDFLAG(IS_WIN)

}  // namespace printing