1 // Copyright (c) 2012 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.
5 #include "chrome/browser/printing/print_job.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/string16.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/printing/print_job_worker.h"
11 #include "content/public/browser/notification_registrar.h"
12 #include "content/public/browser/notification_service.h"
13 #include "content/public/common/child_process_host.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "printing/printed_pages_source.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 class TestSource
: public printing::PrintedPagesSource
{
22 virtual base::string16
RenderSourceName() OVERRIDE
{
23 return base::string16();
27 class TestPrintJobWorker
: public printing::PrintJobWorker
{
29 explicit TestPrintJobWorker(printing::PrintJobWorkerOwner
* owner
)
30 : printing::PrintJobWorker(content::ChildProcessHost::kInvalidUniqueID
,
31 content::ChildProcessHost::kInvalidUniqueID
,
33 friend class TestOwner
;
36 class TestOwner
: public printing::PrintJobWorkerOwner
{
38 virtual void GetSettingsDone(
39 const printing::PrintSettings
& new_settings
,
40 printing::PrintingContext::Result result
) OVERRIDE
{
43 virtual printing::PrintJobWorker
* DetachWorker(
44 printing::PrintJobWorkerOwner
* new_owner
) OVERRIDE
{
45 // We're screwing up here since we're calling worker from the main thread.
46 // That's fine for testing. It is actually simulating PrinterQuery behavior.
47 TestPrintJobWorker
* worker(new TestPrintJobWorker(new_owner
));
48 EXPECT_TRUE(worker
->Start());
49 worker
->printing_context()->UseDefaultSettings();
50 settings_
= worker
->printing_context()->settings();
53 virtual const printing::PrintSettings
& settings() const OVERRIDE
{
56 virtual int cookie() const OVERRIDE
{
61 virtual ~TestOwner() {}
63 printing::PrintSettings settings_
;
66 class TestPrintJob
: public printing::PrintJob
{
68 explicit TestPrintJob(volatile bool* check
) : check_(check
) {
71 virtual ~TestPrintJob() {
74 volatile bool* check_
;
77 class TestPrintNotifObserv
: public content::NotificationObserver
{
79 // content::NotificationObserver
80 virtual void Observe(int type
,
81 const content::NotificationSource
& source
,
82 const content::NotificationDetails
& details
) OVERRIDE
{
89 TEST(PrintJobTest
, SimplePrint
) {
90 // Test the multi-threaded nature of PrintJob to make sure we can use it with
93 content::TestBrowserThreadBundle thread_bundle_
;
94 content::NotificationRegistrar registrar_
;
95 TestPrintNotifObserv observ
;
96 registrar_
.Add(&observ
,
97 content::NOTIFICATION_ALL
,
98 content::NotificationService::AllSources());
99 volatile bool check
= false;
100 scoped_refptr
<printing::PrintJob
> job(new TestPrintJob(&check
));
101 EXPECT_TRUE(job
->RunsTasksOnCurrentThread());
102 scoped_refptr
<TestOwner
> owner(new TestOwner
);
104 job
->Initialize(owner
.get(), &source
, 1);
106 while (job
->document()) {
107 base::MessageLoop::current()->RunUntilIdle();
109 EXPECT_FALSE(job
->document());
112 base::MessageLoop::current()->RunUntilIdle();
117 TEST(PrintJobTest
, SimplePrintLateInit
) {
118 volatile bool check
= false;
119 base::MessageLoop current
;
120 scoped_refptr
<printing::PrintJob
> job(new TestPrintJob(&check
));
123 /* TODO(maruel): Test these.
126 job->GetSettingsDone();
131 job->GetSettings(printing::DEFAULTS, printing::ASK_USER, NULL);
132 job->StartPrinting();
135 job->RequestMissingPages();
136 job->FlushJob(timeout);
137 job->DisconnectSource();
138 job->is_job_pending();
141 job->UpdatePrintedDocument(NULL);
142 scoped_refptr<printing::JobEventDetails> event_details;
143 job->OnNotifyPrintJobEvent(event_details);
144 job->OnDocumentDone();
145 job->ControlledWorkerShutdown();