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 base::string16
RenderSourceName() override
{ return base::string16(); }
25 class TestPrintJobWorker
: public printing::PrintJobWorker
{
27 explicit TestPrintJobWorker(printing::PrintJobWorkerOwner
* owner
)
28 : printing::PrintJobWorker(content::ChildProcessHost::kInvalidUniqueID
,
29 content::ChildProcessHost::kInvalidUniqueID
,
31 friend class TestOwner
;
34 class TestOwner
: public printing::PrintJobWorkerOwner
{
36 void GetSettingsDone(const printing::PrintSettings
& new_settings
,
37 printing::PrintingContext::Result result
) override
{
40 printing::PrintJobWorker
* DetachWorker(
41 printing::PrintJobWorkerOwner
* new_owner
) override
{
42 // We're screwing up here since we're calling worker from the main thread.
43 // That's fine for testing. It is actually simulating PrinterQuery behavior.
44 TestPrintJobWorker
* worker(new TestPrintJobWorker(new_owner
));
45 EXPECT_TRUE(worker
->Start());
46 worker
->printing_context()->UseDefaultSettings();
47 settings_
= worker
->printing_context()->settings();
50 const printing::PrintSettings
& settings() const override
{ return settings_
; }
51 int cookie() const override
{ return 42; }
54 ~TestOwner() override
{}
56 printing::PrintSettings settings_
;
59 class TestPrintJob
: public printing::PrintJob
{
61 explicit TestPrintJob(volatile bool* check
) : check_(check
) {
64 ~TestPrintJob() override
{ *check_
= true; }
65 volatile bool* check_
;
68 class TestPrintNotifObserv
: public content::NotificationObserver
{
70 // content::NotificationObserver
71 void Observe(int type
,
72 const content::NotificationSource
& source
,
73 const content::NotificationDetails
& details
) override
{
80 TEST(PrintJobTest
, SimplePrint
) {
81 // Test the multi-threaded nature of PrintJob to make sure we can use it with
84 content::TestBrowserThreadBundle thread_bundle_
;
85 content::NotificationRegistrar registrar_
;
86 TestPrintNotifObserv observ
;
87 registrar_
.Add(&observ
,
88 content::NOTIFICATION_ALL
,
89 content::NotificationService::AllSources());
90 volatile bool check
= false;
91 scoped_refptr
<printing::PrintJob
> job(new TestPrintJob(&check
));
92 EXPECT_TRUE(job
->RunsTasksOnCurrentThread());
93 scoped_refptr
<TestOwner
> owner(new TestOwner
);
95 job
->Initialize(owner
.get(), &source
, 1);
97 while (job
->document()) {
98 base::MessageLoop::current()->RunUntilIdle();
100 EXPECT_FALSE(job
->document());
103 base::MessageLoop::current()->RunUntilIdle();
108 TEST(PrintJobTest
, SimplePrintLateInit
) {
109 volatile bool check
= false;
110 base::MessageLoop current
;
111 scoped_refptr
<printing::PrintJob
> job(new TestPrintJob(&check
));
114 /* TODO(maruel): Test these.
117 job->GetSettingsDone();
122 job->GetSettings(printing::DEFAULTS, printing::ASK_USER, NULL);
123 job->StartPrinting();
126 job->RequestMissingPages();
127 job->FlushJob(timeout);
128 job->DisconnectSource();
129 job->is_job_pending();
132 job->UpdatePrintedDocument(NULL);
133 scoped_refptr<printing::JobEventDetails> event_details;
134 job->OnNotifyPrintJobEvent(event_details);
135 job->OnDocumentDone();
136 job->ControlledWorkerShutdown();