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 "base/message_loop/message_loop.h"
6 #include "base/strings/string16.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/printing/print_job.h"
9 #include "chrome/browser/printing/print_job_worker.h"
10 #include "content/public/browser/notification_registrar.h"
11 #include "content/public/browser/notification_service.h"
12 #include "printing/printed_pages_source.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 class TestSource : public printing::PrintedPagesSource {
19 virtual base::string16 RenderSourceName() OVERRIDE {
20 return base::string16();
24 class TestPrintJobWorker : public printing::PrintJobWorker {
26 explicit TestPrintJobWorker(printing::PrintJobWorkerOwner* owner)
27 : printing::PrintJobWorker(owner) {
29 friend class TestOwner;
32 class TestOwner : public printing::PrintJobWorkerOwner {
34 virtual void GetSettingsDone(
35 const printing::PrintSettings& new_settings,
36 printing::PrintingContext::Result result) OVERRIDE {
39 virtual printing::PrintJobWorker* DetachWorker(
40 printing::PrintJobWorkerOwner* new_owner) OVERRIDE {
41 // We're screwing up here since we're calling worker from the main thread.
42 // That's fine for testing. It is actually simulating PrinterQuery behavior.
43 TestPrintJobWorker* worker(new TestPrintJobWorker(new_owner));
44 EXPECT_TRUE(worker->Start());
45 worker->printing_context()->UseDefaultSettings();
46 settings_ = worker->printing_context()->settings();
49 virtual base::MessageLoop* message_loop() OVERRIDE {
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 typedef testing::Test PrintJobTest;
91 TEST_F(PrintJobTest, SimplePrint) {
92 // Test the multi-threaded nature of PrintJob to make sure we can use it with
95 // This message loop is actually never run.
96 base::MessageLoop current;
98 content::NotificationRegistrar registrar_;
99 TestPrintNotifObserv observ;
100 registrar_.Add(&observ, content::NOTIFICATION_ALL,
101 content::NotificationService::AllSources());
102 volatile bool check = false;
103 scoped_refptr<printing::PrintJob> job(new TestPrintJob(&check));
104 EXPECT_EQ(base::MessageLoop::current(), job->message_loop());
105 scoped_refptr<TestOwner> owner(new TestOwner);
107 job->Initialize(owner.get(), &source, 1);
109 while (job->document()) {
110 current.RunUntilIdle();
112 EXPECT_FALSE(job->document());
115 current.RunUntilIdle();
120 TEST_F(PrintJobTest, SimplePrintLateInit) {
121 volatile bool check = false;
122 base::MessageLoop current;
123 scoped_refptr<printing::PrintJob> job(new TestPrintJob(&check));
126 /* TODO(maruel): Test these.
129 job->GetSettingsDone();
134 job->GetSettings(printing::DEFAULTS, printing::ASK_USER, NULL);
135 job->StartPrinting();
138 job->RequestMissingPages();
139 job->FlushJob(timeout);
140 job->DisconnectSource();
141 job->is_job_pending();
144 job->UpdatePrintedDocument(NULL);
145 scoped_refptr<printing::JobEventDetails> event_details;
146 job->OnNotifyPrintJobEvent(event_details);
147 job->OnDocumentDone();
148 job->ControlledWorkerShutdown();