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.
4
5 #include "chrome/browser/printing/print_job.h"
6
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"
17
18 namespace {
19
20 class TestSource : public printing::PrintedPagesSource {
21 public:
RenderSourceName()22 virtual base::string16 RenderSourceName() OVERRIDE {
23 return base::string16();
24 }
25 };
26
27 class TestPrintJobWorker : public printing::PrintJobWorker {
28 public:
TestPrintJobWorker(printing::PrintJobWorkerOwner * owner)29 explicit TestPrintJobWorker(printing::PrintJobWorkerOwner* owner)
30 : printing::PrintJobWorker(content::ChildProcessHost::kInvalidUniqueID,
31 content::ChildProcessHost::kInvalidUniqueID,
32 owner) {}
33 friend class TestOwner;
34 };
35
36 class TestOwner : public printing::PrintJobWorkerOwner {
37 public:
GetSettingsDone(const printing::PrintSettings & new_settings,printing::PrintingContext::Result result)38 virtual void GetSettingsDone(
39 const printing::PrintSettings& new_settings,
40 printing::PrintingContext::Result result) OVERRIDE {
41 EXPECT_FALSE(true);
42 }
DetachWorker(printing::PrintJobWorkerOwner * new_owner)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();
51 return worker;
52 }
settings() const53 virtual const printing::PrintSettings& settings() const OVERRIDE {
54 return settings_;
55 }
cookie() const56 virtual int cookie() const OVERRIDE {
57 return 42;
58 }
59
60 private:
~TestOwner()61 virtual ~TestOwner() {}
62
63 printing::PrintSettings settings_;
64 };
65
66 class TestPrintJob : public printing::PrintJob {
67 public:
TestPrintJob(volatile bool * check)68 explicit TestPrintJob(volatile bool* check) : check_(check) {
69 }
70 private:
~TestPrintJob()71 virtual ~TestPrintJob() {
72 *check_ = true;
73 }
74 volatile bool* check_;
75 };
76
77 class TestPrintNotifObserv : public content::NotificationObserver {
78 public:
79 // content::NotificationObserver
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)80 virtual void Observe(int type,
81 const content::NotificationSource& source,
82 const content::NotificationDetails& details) OVERRIDE {
83 ADD_FAILURE();
84 }
85 };
86
87 } // namespace
88
TEST(PrintJobTest,SimplePrint)89 TEST(PrintJobTest, SimplePrint) {
90 // Test the multi-threaded nature of PrintJob to make sure we can use it with
91 // known lifetime.
92
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);
103 TestSource source;
104 job->Initialize(owner.get(), &source, 1);
105 job->Stop();
106 while (job->document()) {
107 base::MessageLoop::current()->RunUntilIdle();
108 }
109 EXPECT_FALSE(job->document());
110 job = NULL;
111 while (!check) {
112 base::MessageLoop::current()->RunUntilIdle();
113 }
114 EXPECT_TRUE(check);
115 }
116
TEST(PrintJobTest,SimplePrintLateInit)117 TEST(PrintJobTest, SimplePrintLateInit) {
118 volatile bool check = false;
119 base::MessageLoop current;
120 scoped_refptr<printing::PrintJob> job(new TestPrintJob(&check));
121 job = NULL;
122 EXPECT_TRUE(check);
123 /* TODO(maruel): Test these.
124 job->Initialize()
125 job->Observe();
126 job->GetSettingsDone();
127 job->DetachWorker();
128 job->message_loop();
129 job->settings();
130 job->cookie();
131 job->GetSettings(printing::DEFAULTS, printing::ASK_USER, NULL);
132 job->StartPrinting();
133 job->Stop();
134 job->Cancel();
135 job->RequestMissingPages();
136 job->FlushJob(timeout);
137 job->DisconnectSource();
138 job->is_job_pending();
139 job->document();
140 // Private
141 job->UpdatePrintedDocument(NULL);
142 scoped_refptr<printing::JobEventDetails> event_details;
143 job->OnNotifyPrintJobEvent(event_details);
144 job->OnDocumentDone();
145 job->ControlledWorkerShutdown();
146 */
147 }
148