OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
7 #include "printing/page_overlays.h" | 7 #include "printing/page_overlays.h" |
8 #include "printing/print_settings.h" | 8 #include "printing/print_settings.h" |
9 #include "printing/printed_document.h" | 9 #include "printing/printed_document.h" |
10 #include "printing/printed_page.h" | 10 #include "printing/printed_page.h" |
11 #include "printing/printed_pages_source.h" | 11 #include "printing/printed_pages_source.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 class PageOverlaysTest : public testing::Test { | |
17 private: | |
18 MessageLoop message_loop_; | |
19 }; | |
20 | |
21 struct Keys { | 16 struct Keys { |
22 const wchar_t* key; | 17 const wchar_t* key; |
23 const wchar_t* expected; | 18 const wchar_t* expected; |
24 }; | 19 }; |
25 | 20 |
26 const Keys kOverlayKeys[] = { | 21 const Keys kOverlayKeys[] = { |
27 printing::PageOverlays::kTitle, L"Foobar Document", | 22 { printing::PageOverlays::kTitle, L"Foobar Document" }, |
28 printing::PageOverlays::kTime, L"", | 23 { printing::PageOverlays::kTime, L"" }, |
29 printing::PageOverlays::kDate, L"", | 24 { printing::PageOverlays::kDate, L"" }, |
30 printing::PageOverlays::kPage, L"1", | 25 { printing::PageOverlays::kPage, L"1" }, |
31 printing::PageOverlays::kPageCount, L"2", | 26 { printing::PageOverlays::kPageCount, L"2" }, |
32 printing::PageOverlays::kPageOnTotal, L"1/2", | 27 { printing::PageOverlays::kPageOnTotal, L"1/2" }, |
33 printing::PageOverlays::kUrl, L"http://www.perdu.com/", | 28 { printing::PageOverlays::kUrl, L"http://www.perdu.com/" }, |
34 }; | 29 }; |
35 | 30 |
36 class PagesSource : public printing::PrintedPagesSource { | 31 class PagesSource : public printing::PrintedPagesSource { |
37 public: | 32 public: |
38 virtual std::wstring RenderSourceName() { | 33 virtual std::wstring RenderSourceName() { |
39 return L"Foobar Document"; | 34 return L"Foobar Document"; |
40 } | 35 } |
41 | 36 |
42 virtual GURL RenderSourceUrl() { | 37 virtual GURL RenderSourceUrl() { |
43 return GURL(L"http://www.perdu.com"); | 38 return GURL("http://www.perdu.com"); |
44 } | 39 } |
45 }; | 40 }; |
46 | 41 |
47 } // namespace | 42 } // namespace |
48 | 43 |
| 44 class PageOverlaysTest : public testing::Test { |
| 45 private: |
| 46 MessageLoop message_loop_; |
| 47 }; |
49 | 48 |
50 TEST_F(PageOverlaysTest, StringConversion) { | 49 TEST_F(PageOverlaysTest, StringConversion) { |
51 printing::PageOverlays overlays; | 50 printing::PageOverlays overlays; |
52 overlays.GetOverlay(printing::PageOverlays::LEFT, | 51 overlays.GetOverlay(printing::PageOverlays::LEFT, |
53 printing::PageOverlays::BOTTOM); | 52 printing::PageOverlays::BOTTOM); |
54 printing::PrintSettings settings; | 53 printing::PrintSettings settings; |
55 PagesSource source; | 54 PagesSource source; |
56 int cookie = 1; | 55 int cookie = 1; |
57 scoped_refptr<printing::PrintedDocument> doc( | 56 scoped_refptr<printing::PrintedDocument> doc( |
58 new printing::PrintedDocument(settings, &source, cookie)); | 57 new printing::PrintedDocument(settings, &source, cookie)); |
59 doc->set_page_count(2); | 58 doc->set_page_count(2); |
60 gfx::Size page_size(100, 100); | 59 gfx::Size page_size(100, 100); |
61 scoped_refptr<printing::PrintedPage> page( | 60 scoped_refptr<printing::PrintedPage> page( |
62 new printing::PrintedPage(1, NULL, page_size)); | 61 new printing::PrintedPage(1, NULL, page_size)); |
63 | 62 |
64 std::wstring input; | 63 std::wstring input; |
65 std::wstring out; | 64 std::wstring out; |
66 for (int i = 0; i < arraysize(kOverlayKeys); ++i) { | 65 for (size_t i = 0; i < arraysize(kOverlayKeys); ++i) { |
67 input = StringPrintf(L"foo%lsbar", kOverlayKeys[i].key); | 66 input = StringPrintf(L"foo%lsbar", kOverlayKeys[i].key); |
68 out = printing::PageOverlays::ReplaceVariables(input, *doc.get(), | 67 out = printing::PageOverlays::ReplaceVariables(input, *doc.get(), |
69 *page.get()); | 68 *page.get()); |
70 EXPECT_FALSE(out.empty()); | 69 EXPECT_FALSE(out.empty()); |
71 if (wcslen(kOverlayKeys[i].expected) == 0) | 70 if (wcslen(kOverlayKeys[i].expected) == 0) |
72 continue; | 71 continue; |
73 EXPECT_EQ(StringPrintf(L"foo%lsbar", kOverlayKeys[i].expected), out) << | 72 std::wstring expected = StringPrintf(L"foo%lsbar", kOverlayKeys[i].expected)
; |
74 kOverlayKeys[i].key; | 73 EXPECT_EQ(expected, out) << kOverlayKeys[i].key; |
75 } | 74 } |
76 | 75 |
77 // Check if SetOverlay really sets the page overlay. | 76 // Check if SetOverlay really sets the page overlay. |
78 overlays.SetOverlay(printing::PageOverlays::LEFT, | 77 overlays.SetOverlay(printing::PageOverlays::LEFT, |
79 printing::PageOverlays::TOP, | 78 printing::PageOverlays::TOP, |
80 UTF16ToWide(L"Page {page}")); | 79 L"Page {page}"); |
81 input = overlays.GetOverlay(printing::PageOverlays::LEFT, | 80 input = overlays.GetOverlay(printing::PageOverlays::LEFT, |
82 printing::PageOverlays::TOP); | 81 printing::PageOverlays::TOP); |
83 EXPECT_EQ(input, L"Page {page}"); | 82 EXPECT_EQ(input, L"Page {page}"); |
84 | 83 |
85 // Replace the variables to see if the page number is correct. | 84 // Replace the variables to see if the page number is correct. |
86 out = printing::PageOverlays::ReplaceVariables(input, *doc.get(), | 85 out = printing::PageOverlays::ReplaceVariables(input, *doc.get(), |
87 *page.get()); | 86 *page.get()); |
88 EXPECT_EQ(out, L"Page 1"); | 87 EXPECT_EQ(out, L"Page 1"); |
89 } | 88 } |
OLD | NEW |