[email protected] | fa8a2b1b | 2009-05-23 03:09:52 | [diff] [blame] | 1 | // 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" |
| 14 | |
| 15 | #if defined(OS_WIN) |
| 16 | #include "chrome/renderer/mock_printer_driver_win.h" |
| 17 | #endif |
| 18 | |
| 19 | struct ViewMsg_Print_Params; |
| 20 | struct ViewMsg_PrintPages_Params; |
| 21 | struct ViewHostMsg_DidPrintPage_Params; |
| 22 | |
| 23 | // A class which represents an output page used in the MockPrinter class. |
| 24 | // The MockPrinter class stores output pages in a vector, so, this class |
| 25 | // inherits the base::RefCounted<> class so that the MockPrinter class can use |
| 26 | // a smart pointer of this object (i.e. scoped_refptr<>). |
| 27 | class MockPrinterPage : public base::RefCounted<MockPrinterPage> { |
| 28 | public: |
| 29 | MockPrinterPage() |
| 30 | : width_(0), |
| 31 | height_(0), |
| 32 | source_size_(0), |
| 33 | bitmap_size_(0) { |
| 34 | } |
| 35 | |
| 36 | MockPrinterPage(int width, |
| 37 | int height, |
| 38 | const void* source_data, |
| 39 | size_t source_size, |
| 40 | const void* bitmap_data, |
| 41 | size_t bitmap_size) |
| 42 | : width_(width), |
| 43 | height_(height), |
| 44 | source_size_(source_size), |
| 45 | bitmap_size_(bitmap_size) { |
| 46 | // Create copies of the source data and the bitmap data. |
| 47 | source_data_.reset(new uint8[source_size]); |
| 48 | if (source_data_.get()) |
| 49 | memcpy(source_data_.get(), source_data, source_size); |
| 50 | bitmap_data_.reset(new uint8[bitmap_size]); |
| 51 | if (bitmap_data_.get()) |
| 52 | memcpy(bitmap_data_.get(), bitmap_data, bitmap_size); |
| 53 | } |
| 54 | |
| 55 | ~MockPrinterPage() { |
| 56 | } |
| 57 | |
| 58 | int width() { return width_; } |
| 59 | int height() { return height_; } |
| 60 | const uint8* source_data() { return source_data_.get(); } |
| 61 | const size_t source_size() { return source_size_; } |
| 62 | const uint8* bitmap_data() { return bitmap_data_.get(); } |
| 63 | const size_t bitmap_size() { return bitmap_size_; } |
| 64 | |
| 65 | private: |
| 66 | int width_; |
| 67 | int height_; |
| 68 | size_t source_size_; |
| 69 | scoped_array<uint8> source_data_; |
| 70 | size_t bitmap_size_; |
| 71 | scoped_array<uint8> bitmap_data_; |
| 72 | |
| 73 | DISALLOW_COPY_AND_ASSIGN(MockPrinterPage); |
| 74 | }; |
| 75 | |
| 76 | // A class which implements a pseudo-printer object used by the RenderViewTest |
| 77 | // class. |
| 78 | // This class consists of three parts: |
| 79 | // 1. An IPC-message hanlder sent from the RenderView class; |
| 80 | // 2. A renderer that creates a printing job into bitmaps, and; |
| 81 | // 3. A vector which saves the output pages of a printing job. |
| 82 | // A user who writes RenderViewTest cases only use the functions which |
| 83 | // retrieve output pages from this vector to verify them with expected results. |
| 84 | class MockPrinter { |
| 85 | public: |
| 86 | enum Status { |
| 87 | PRINTER_READY, |
| 88 | PRINTER_PRINTING, |
| 89 | PRINTER_ERROR, |
| 90 | }; |
| 91 | |
| 92 | MockPrinter(); |
| 93 | ~MockPrinter(); |
| 94 | |
| 95 | // Functions that changes settings of a pseudo printer. |
| 96 | void ResetPrinter(); |
| 97 | void SetDefaultPrintSettings(const ViewMsg_Print_Params& params); |
| 98 | |
| 99 | // Functions that handles IPC events. |
| 100 | void GetDefaultPrintSettings(ViewMsg_Print_Params* params); |
| 101 | void ScriptedPrint(int cookie, |
| 102 | int expected_pages_count, |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame^] | 103 | bool has_selection, |
[email protected] | fa8a2b1b | 2009-05-23 03:09:52 | [diff] [blame] | 104 | ViewMsg_PrintPages_Params* settings); |
| 105 | void SetPrintedPagesCount(int cookie, int number_pages); |
| 106 | void PrintPage(const ViewHostMsg_DidPrintPage_Params& params); |
| 107 | |
| 108 | // Functions that retrieve the output pages. |
| 109 | Status GetPrinterStatus() const { return printer_status_; } |
| 110 | int GetPrintedPages() const; |
| 111 | int GetWidth(size_t page) const; |
| 112 | int GetHeight(size_t page) const; |
| 113 | bool GetSourceChecksum(size_t page, std::string* checksum) const; |
| 114 | bool GetBitmapChecksum(size_t page, std::string* checksum) const; |
| 115 | bool GetSource(size_t page, const void** data, size_t* size) const; |
| 116 | bool GetBitmap(size_t page, const void** data, size_t* size) const; |
| 117 | bool SaveSource(size_t page, const std::wstring& filename) const; |
| 118 | bool SaveBitmap(size_t page, const std::wstring& filename) const; |
| 119 | |
| 120 | protected: |
| 121 | int CreateDocumentCookie(); |
| 122 | bool GetChecksum(const void* data, size_t size, std::string* checksum) const; |
| 123 | |
| 124 | private: |
| 125 | // In pixels according to dpi_x and dpi_y. |
| 126 | int printable_width_; |
| 127 | int printable_height_; |
| 128 | |
| 129 | // Specifies dots per inch. |
| 130 | double dpi_; |
| 131 | double max_shrink_; |
| 132 | double min_shrink_; |
| 133 | |
| 134 | // Desired apparent dpi on paper. |
| 135 | int desired_dpi_; |
| 136 | |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame^] | 137 | // Print selection. |
| 138 | bool selection_only_; |
| 139 | |
[email protected] | fa8a2b1b | 2009-05-23 03:09:52 | [diff] [blame] | 140 | // Cookie for the document to ensure correctness. |
| 141 | int document_cookie_; |
| 142 | int current_document_cookie_; |
| 143 | |
| 144 | // The current status of this printer. |
| 145 | Status printer_status_; |
| 146 | |
| 147 | // The output of a printing job. |
| 148 | int number_pages_; |
| 149 | int page_number_; |
| 150 | std::vector<scoped_refptr<MockPrinterPage> > pages_; |
| 151 | |
| 152 | #if defined(OS_WIN) |
| 153 | MockPrinterDriverWin driver_; |
| 154 | #endif |
| 155 | |
| 156 | DISALLOW_COPY_AND_ASSIGN(MockPrinter); |
| 157 | }; |
| 158 | |
| 159 | #endif // CHROME_RENDERER_MOCK_PRINTER_H_ |