1 // Copyright (c) 2011 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/string16.h"
7 #include "chrome/browser/printing/print_job.h"
8 #include "chrome/browser/printing/print_job_worker.h"
9 #include "content/common/notification_registrar.h"
10 #include "content/common/notification_service.h"
11 #include "googleurl/src/gurl.h"
12 #include "printing/printed_pages_source.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 class TestSource : public printing::PrintedPagesSource {
18 public:
RenderSourceName()19 virtual string16 RenderSourceName() {
20 return string16();
21 }
RenderSourceUrl()22 virtual GURL RenderSourceUrl() {
23 return GURL();
24 }
25 };
26
27 class TestPrintJobWorker : public printing::PrintJobWorker {
28 public:
TestPrintJobWorker(printing::PrintJobWorkerOwner * owner)29 explicit TestPrintJobWorker(printing::PrintJobWorkerOwner* owner)
30 : printing::PrintJobWorker(owner) {
31 }
32 friend class TestOwner;
33 };
34
35 class TestOwner : public printing::PrintJobWorkerOwner {
36 public:
GetSettingsDone(const printing::PrintSettings & new_settings,printing::PrintingContext::Result result)37 virtual void GetSettingsDone(const printing::PrintSettings& new_settings,
38 printing::PrintingContext::Result result) {
39 EXPECT_FALSE(true);
40 }
DetachWorker(printing::PrintJobWorkerOwner * new_owner)41 virtual printing::PrintJobWorker* DetachWorker(
42 printing::PrintJobWorkerOwner* new_owner) {
43 // We're screwing up here since we're calling worker from the main thread.
44 // That's fine for testing. It is actually simulating PrinterQuery behavior.
45 TestPrintJobWorker* worker(new TestPrintJobWorker(new_owner));
46 EXPECT_TRUE(worker->Start());
47 worker->printing_context()->UseDefaultSettings();
48 settings_ = worker->printing_context()->settings();
49 return worker;
50 }
message_loop()51 virtual MessageLoop* message_loop() {
52 EXPECT_FALSE(true);
53 return NULL;
54 }
settings() const55 virtual const printing::PrintSettings& settings() const {
56 return settings_;
57 }
cookie() const58 virtual int cookie() const {
59 return 42;
60 }
61 private:
62 printing::PrintSettings settings_;
63 };
64
65 class TestPrintJob : public printing::PrintJob {
66 public:
TestPrintJob(volatile bool * check)67 explicit TestPrintJob(volatile bool* check) : check_(check) {
68 }
69 private:
~TestPrintJob()70 ~TestPrintJob() {
71 *check_ = true;
72 }
73 volatile bool* check_;
74 };
75
76 class TestPrintNotifObserv : public NotificationObserver {
77 public:
78 // NotificationObserver
Observe(NotificationType type,const NotificationSource & source,const NotificationDetails & details)79 virtual void Observe(NotificationType type,
80 const NotificationSource& source,
81 const NotificationDetails& details) {
82 EXPECT_FALSE(true);
83 }
84 };
85
86 } // namespace
87
TEST(PrintJobTest,SimplePrint)88 TEST(PrintJobTest, SimplePrint) {
89 // Test the multi-threaded nature of PrintJob to make sure we can use it with
90 // known lifetime.
91
92 // This message loop is actually never run.
93 MessageLoop current;
94
95 NotificationRegistrar registrar_;
96 TestPrintNotifObserv observ;
97 registrar_.Add(&observ, NotificationType::ALL,
98 NotificationService::AllSources());
99 volatile bool check = false;
100 scoped_refptr<printing::PrintJob> job(new TestPrintJob(&check));
101 EXPECT_EQ(MessageLoop::current(), job->message_loop());
102 scoped_refptr<TestOwner> owner(new TestOwner);
103 TestSource source;
104 job->Initialize(owner, &source, 1);
105 job->Stop();
106 job = NULL;
107 EXPECT_TRUE(check);
108 }
109
TEST(PrintJobTest,SimplePrintLateInit)110 TEST(PrintJobTest, SimplePrintLateInit) {
111 volatile bool check = false;
112 MessageLoop current;
113 scoped_refptr<printing::PrintJob> job(new TestPrintJob(&check));
114 job = NULL;
115 EXPECT_TRUE(check);
116 /* TODO(maruel): Test these.
117 job->Initialize()
118 job->Observe();
119 job->GetSettingsDone();
120 job->DetachWorker();
121 job->message_loop();
122 job->settings();
123 job->cookie();
124 job->GetSettings(printing::DEFAULTS, printing::ASK_USER, NULL);
125 job->StartPrinting();
126 job->Stop();
127 job->Cancel();
128 job->RequestMissingPages();
129 job->FlushJob(timeout_ms);
130 job->DisconnectSource();
131 job->is_job_pending();
132 job->document();
133 // Private
134 job->UpdatePrintedDocument(NULL);
135 scoped_refptr<printing::JobEventDetails> event_details;
136 job->OnNotifyPrintJobEvent(event_details);
137 job->OnDocumentDone();
138 job->ControlledWorkerShutdown();
139 */
140 }
141