Introduce ProfilerMetricsProvider
[chromium-blink-merge.git] / chrome / browser / printing / print_job_unittest.cc
blob2346ae311378f954ba669ba404286d9d5a4e26aa
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"
15 namespace {
17 class TestSource : public printing::PrintedPagesSource {
18 public:
19 virtual base::string16 RenderSourceName() OVERRIDE {
20 return base::string16();
24 class TestPrintJobWorker : public printing::PrintJobWorker {
25 public:
26 explicit TestPrintJobWorker(printing::PrintJobWorkerOwner* owner)
27 : printing::PrintJobWorker(owner) {
29 friend class TestOwner;
32 class TestOwner : public printing::PrintJobWorkerOwner {
33 public:
34 virtual void GetSettingsDone(
35 const printing::PrintSettings& new_settings,
36 printing::PrintingContext::Result result) OVERRIDE {
37 EXPECT_FALSE(true);
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();
47 return worker;
49 virtual base::MessageLoop* message_loop() OVERRIDE {
50 EXPECT_FALSE(true);
51 return NULL;
53 virtual const printing::PrintSettings& settings() const OVERRIDE {
54 return settings_;
56 virtual int cookie() const OVERRIDE {
57 return 42;
60 private:
61 virtual ~TestOwner() {}
63 printing::PrintSettings settings_;
66 class TestPrintJob : public printing::PrintJob {
67 public:
68 explicit TestPrintJob(volatile bool* check) : check_(check) {
70 private:
71 virtual ~TestPrintJob() {
72 *check_ = true;
74 volatile bool* check_;
77 class TestPrintNotifObserv : public content::NotificationObserver {
78 public:
79 // content::NotificationObserver
80 virtual void Observe(int type,
81 const content::NotificationSource& source,
82 const content::NotificationDetails& details) OVERRIDE {
83 ADD_FAILURE();
87 } // namespace
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
93 // known lifetime.
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);
106 TestSource source;
107 job->Initialize(owner.get(), &source, 1);
108 job->Stop();
109 while (job->document()) {
110 current.RunUntilIdle();
112 EXPECT_FALSE(job->document());
113 job = NULL;
114 while (!check) {
115 current.RunUntilIdle();
117 EXPECT_TRUE(check);
120 TEST_F(PrintJobTest, SimplePrintLateInit) {
121 volatile bool check = false;
122 base::MessageLoop current;
123 scoped_refptr<printing::PrintJob> job(new TestPrintJob(&check));
124 job = NULL;
125 EXPECT_TRUE(check);
126 /* TODO(maruel): Test these.
127 job->Initialize()
128 job->Observe();
129 job->GetSettingsDone();
130 job->DetachWorker();
131 job->message_loop();
132 job->settings();
133 job->cookie();
134 job->GetSettings(printing::DEFAULTS, printing::ASK_USER, NULL);
135 job->StartPrinting();
136 job->Stop();
137 job->Cancel();
138 job->RequestMissingPages();
139 job->FlushJob(timeout);
140 job->DisconnectSource();
141 job->is_job_pending();
142 job->document();
143 // Private
144 job->UpdatePrintedDocument(NULL);
145 scoped_refptr<printing::JobEventDetails> event_details;
146 job->OnNotifyPrintJobEvent(event_details);
147 job->OnDocumentDone();
148 job->ControlledWorkerShutdown();