print_job_unittest.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2008 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 "base/message_loop.h"
6#include "chrome/browser/printing/print_job.h"
7#include "chrome/browser/printing/print_job_worker.h"
8#include "chrome/common/notification_registrar.h"
9#include "chrome/common/notification_service.h"
10#include "printing/printed_pages_source.h"
11#include "googleurl/src/gurl.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace {
15
16class TestSource : public printing::PrintedPagesSource {
17 public:
18  virtual std::wstring RenderSourceName() {
19    return L"";
20  }
21  virtual GURL RenderSourceUrl() {
22    return GURL();
23  }
24};
25
26class TestPrintJobWorker : public printing::PrintJobWorker {
27 public:
28  explicit TestPrintJobWorker(printing::PrintJobWorkerOwner* owner)
29      : printing::PrintJobWorker(owner) {
30  }
31  friend class TestOwner;
32};
33
34class TestOwner : public printing::PrintJobWorkerOwner {
35 public:
36  virtual void GetSettingsDone(const printing::PrintSettings& new_settings,
37                               printing::PrintingContext::Result result) {
38    EXPECT_FALSE(true);
39  }
40  virtual printing::PrintJobWorker* DetachWorker(
41      printing::PrintJobWorkerOwner* new_owner) {
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();
48    return worker;
49  }
50  virtual MessageLoop* message_loop() {
51    EXPECT_FALSE(true);
52    return NULL;
53  }
54  virtual const printing::PrintSettings& settings() const {
55    return settings_;
56  }
57  virtual int cookie() const {
58    return 42;
59  }
60 private:
61  printing::PrintSettings settings_;
62};
63
64class TestPrintJob : public printing::PrintJob {
65 public:
66  explicit TestPrintJob(volatile bool* check) : check_(check) {
67  }
68 private:
69  ~TestPrintJob() {
70    *check_ = true;
71  }
72  volatile bool* check_;
73};
74
75class TestPrintNotifObserv : public NotificationObserver {
76 public:
77  // NotificationObserver
78  virtual void Observe(NotificationType type,
79                       const NotificationSource& source,
80                       const NotificationDetails& details) {
81    EXPECT_FALSE(true);
82  }
83};
84
85}  // namespace
86
87TEST(PrintJobTest, SimplePrint) {
88  // Test the multithreaded nature of PrintJob to make sure we can use it with
89  // known livetime.
90
91  // This message loop is actually never run.
92  MessageLoop current;
93
94  NotificationRegistrar registrar_;
95  TestPrintNotifObserv observ;
96  registrar_.Add(&observ, NotificationType::ALL,
97                 NotificationService::AllSources());
98  volatile bool check = false;
99  scoped_refptr<printing::PrintJob> job(new TestPrintJob(&check));
100  EXPECT_EQ(MessageLoop::current(), job->message_loop());
101  scoped_refptr<TestOwner> owner(new TestOwner);
102  TestSource source;
103  job->Initialize(owner, &source);
104  job->Stop();
105  job = NULL;
106  EXPECT_TRUE(check);
107}
108
109TEST(PrintJobTest, SimplePrintLateInit) {
110  volatile bool check = false;
111  MessageLoop current;
112  scoped_refptr<printing::PrintJob> job(new TestPrintJob(&check));
113  job = NULL;
114  EXPECT_TRUE(check);
115  /* TODO(maruel): Test these.
116  job->Initialize()
117  job->Observe();
118  job->GetSettingsDone();
119  job->DetachWorker();
120  job->message_loop();
121  job->settings();
122  job->cookie();
123  job->GetSettings(printing::DEFAULTS, printing::ASK_USER, NULL);
124  job->StartPrinting();
125  job->Stop();
126  job->Cancel();
127  job->RequestMissingPages();
128  job->FlushJob(timeout_ms);
129  job->DisconnectSource();
130  job->is_job_pending();
131  job->is_print_dialog_box_shown();
132  job->document();
133  // Private
134  job->UpdatePrintedDocument(NULL);
135  scoped_refptr<printing::JobEventDetails> event_details;
136  job->OnNotifyPrintJobEvent(event_details);
137  job->OnDocumentDone();
138  job->ControlledWorkerShutdown();
139  */
140}
141