blob: 80f6c4b6b6dd66ec2a76868fb11e1b24a8f6fccc [file] [log] [blame]
[email protected]fa8a2b1b2009-05-23 03:09:521// Copyright (c) 2009 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
5#ifndef CHROME_RENDERER_MOCK_PRINTER_H_
6#define CHROME_RENDERER_MOCK_PRINTER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/ref_counted.h"
13#include "base/scoped_ptr.h"
[email protected]aa82249f2009-07-16 17:23:5814#include "printing/image.h"
[email protected]fa8a2b1b2009-05-23 03:09:5215
16struct ViewMsg_Print_Params;
17struct ViewMsg_PrintPages_Params;
18struct ViewHostMsg_DidPrintPage_Params;
19
20// A class which represents an output page used in the MockPrinter class.
21// The MockPrinter class stores output pages in a vector, so, this class
22// inherits the base::RefCounted<> class so that the MockPrinter class can use
23// a smart pointer of this object (i.e. scoped_refptr<>).
24class MockPrinterPage : public base::RefCounted<MockPrinterPage> {
25 public:
[email protected]aa82249f2009-07-16 17:23:5826 MockPrinterPage(const void* source_data,
[email protected]fa8a2b1b2009-05-23 03:09:5227 size_t source_size,
[email protected]aa82249f2009-07-16 17:23:5828 const printing::Image& image)
29 : source_size_(source_size),
30 image_(image) {
31 // Create copies of the source data
[email protected]fa8a2b1b2009-05-23 03:09:5232 source_data_.reset(new uint8[source_size]);
33 if (source_data_.get())
34 memcpy(source_data_.get(), source_data, source_size);
[email protected]fa8a2b1b2009-05-23 03:09:5235 }
36
37 ~MockPrinterPage() {
38 }
39
[email protected]aa82249f2009-07-16 17:23:5840 int width() const { return image_.size().width(); }
41 int height() const { return image_.size().height(); }
42 const uint8* source_data() const { return source_data_.get(); }
43 const size_t source_size() const { return source_size_; }
44 const printing::Image& image() const { return image_; }
[email protected]fa8a2b1b2009-05-23 03:09:5245
46 private:
[email protected]fa8a2b1b2009-05-23 03:09:5247 size_t source_size_;
48 scoped_array<uint8> source_data_;
[email protected]aa82249f2009-07-16 17:23:5849 printing::Image image_;
[email protected]fa8a2b1b2009-05-23 03:09:5250
51 DISALLOW_COPY_AND_ASSIGN(MockPrinterPage);
52};
53
54// A class which implements a pseudo-printer object used by the RenderViewTest
55// class.
56// This class consists of three parts:
57// 1. An IPC-message hanlder sent from the RenderView class;
58// 2. A renderer that creates a printing job into bitmaps, and;
59// 3. A vector which saves the output pages of a printing job.
60// A user who writes RenderViewTest cases only use the functions which
61// retrieve output pages from this vector to verify them with expected results.
62class MockPrinter {
63 public:
64 enum Status {
65 PRINTER_READY,
66 PRINTER_PRINTING,
67 PRINTER_ERROR,
68 };
69
70 MockPrinter();
71 ~MockPrinter();
72
73 // Functions that changes settings of a pseudo printer.
74 void ResetPrinter();
75 void SetDefaultPrintSettings(const ViewMsg_Print_Params& params);
76
77 // Functions that handles IPC events.
78 void GetDefaultPrintSettings(ViewMsg_Print_Params* params);
79 void ScriptedPrint(int cookie,
80 int expected_pages_count,
[email protected]c8ad40c2009-06-08 17:05:2181 bool has_selection,
[email protected]fa8a2b1b2009-05-23 03:09:5282 ViewMsg_PrintPages_Params* settings);
83 void SetPrintedPagesCount(int cookie, int number_pages);
84 void PrintPage(const ViewHostMsg_DidPrintPage_Params& params);
85
86 // Functions that retrieve the output pages.
87 Status GetPrinterStatus() const { return printer_status_; }
88 int GetPrintedPages() const;
[email protected]aa82249f2009-07-16 17:23:5889
90 // Get a pointer to the printed page, returns NULL if pageno has not been
91 // printed. The pointer is for read only view and should not be deleted.
92 const MockPrinterPage* GetPrintedPage(size_t pageno) const;
93
[email protected]fa8a2b1b2009-05-23 03:09:5294 int GetWidth(size_t page) const;
95 int GetHeight(size_t page) const;
[email protected]fa8a2b1b2009-05-23 03:09:5296 bool GetBitmapChecksum(size_t page, std::string* checksum) const;
97 bool GetSource(size_t page, const void** data, size_t* size) const;
98 bool GetBitmap(size_t page, const void** data, size_t* size) const;
[email protected]72f966b2009-10-14 20:02:2799 bool SaveSource(size_t page, const FilePath& filepath) const;
100 bool SaveBitmap(size_t page, const FilePath& filepath) const;
[email protected]fa8a2b1b2009-05-23 03:09:52101
102 protected:
103 int CreateDocumentCookie();
104 bool GetChecksum(const void* data, size_t size, std::string* checksum) const;
105
106 private:
107 // In pixels according to dpi_x and dpi_y.
108 int printable_width_;
109 int printable_height_;
110
111 // Specifies dots per inch.
112 double dpi_;
113 double max_shrink_;
114 double min_shrink_;
115
116 // Desired apparent dpi on paper.
117 int desired_dpi_;
118
[email protected]c8ad40c2009-06-08 17:05:21119 // Print selection.
120 bool selection_only_;
121
[email protected]fa8a2b1b2009-05-23 03:09:52122 // Cookie for the document to ensure correctness.
123 int document_cookie_;
124 int current_document_cookie_;
125
126 // The current status of this printer.
127 Status printer_status_;
128
129 // The output of a printing job.
130 int number_pages_;
131 int page_number_;
132 std::vector<scoped_refptr<MockPrinterPage> > pages_;
133
[email protected]fa8a2b1b2009-05-23 03:09:52134 DISALLOW_COPY_AND_ASSIGN(MockPrinter);
135};
136
137#endif // CHROME_RENDERER_MOCK_PRINTER_H_