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