b51468264c7dd77d2a1e57d8c0d2035363dc228c
[platform/framework/web/crosswalk.git] / src / chrome / browser / printing / print_job_unittest.cc
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:
22   base::string16 RenderSourceName() override { return base::string16(); }
23 };
24
25 class TestPrintJobWorker : public printing::PrintJobWorker {
26  public:
27   explicit TestPrintJobWorker(printing::PrintJobWorkerOwner* owner)
28       : printing::PrintJobWorker(content::ChildProcessHost::kInvalidUniqueID,
29                                  content::ChildProcessHost::kInvalidUniqueID,
30                                  owner) {}
31   friend class TestOwner;
32 };
33
34 class TestOwner : public printing::PrintJobWorkerOwner {
35  public:
36   void GetSettingsDone(const printing::PrintSettings& new_settings,
37                        printing::PrintingContext::Result result) override {
38     EXPECT_FALSE(true);
39   }
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();
48     return worker;
49   }
50   const printing::PrintSettings& settings() const override { return settings_; }
51   int cookie() const override { return 42; }
52
53  private:
54   ~TestOwner() override {}
55
56   printing::PrintSettings settings_;
57 };
58
59 class TestPrintJob : public printing::PrintJob {
60  public:
61   explicit TestPrintJob(volatile bool* check) : check_(check) {
62   }
63  private:
64   ~TestPrintJob() override { *check_ = true; }
65   volatile bool* check_;
66 };
67
68 class TestPrintNotifObserv : public content::NotificationObserver {
69  public:
70   // content::NotificationObserver
71   void Observe(int type,
72                const content::NotificationSource& source,
73                const content::NotificationDetails& details) override {
74     ADD_FAILURE();
75   }
76 };
77
78 }  // namespace
79
80 TEST(PrintJobTest, SimplePrint) {
81   // Test the multi-threaded nature of PrintJob to make sure we can use it with
82   // known lifetime.
83
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);
94   TestSource source;
95   job->Initialize(owner.get(), &source, 1);
96   job->Stop();
97   while (job->document()) {
98     base::MessageLoop::current()->RunUntilIdle();
99   }
100   EXPECT_FALSE(job->document());
101   job = NULL;
102   while (!check) {
103     base::MessageLoop::current()->RunUntilIdle();
104   }
105   EXPECT_TRUE(check);
106 }
107
108 TEST(PrintJobTest, SimplePrintLateInit) {
109   volatile bool check = false;
110   base::MessageLoop current;
111   scoped_refptr<printing::PrintJob> job(new TestPrintJob(&check));
112   job = NULL;
113   EXPECT_TRUE(check);
114   /* TODO(maruel): Test these.
115   job->Initialize()
116   job->Observe();
117   job->GetSettingsDone();
118   job->DetachWorker();
119   job->message_loop();
120   job->settings();
121   job->cookie();
122   job->GetSettings(printing::DEFAULTS, printing::ASK_USER, NULL);
123   job->StartPrinting();
124   job->Stop();
125   job->Cancel();
126   job->RequestMissingPages();
127   job->FlushJob(timeout);
128   job->DisconnectSource();
129   job->is_job_pending();
130   job->document();
131   // Private
132   job->UpdatePrintedDocument(NULL);
133   scoped_refptr<printing::JobEventDetails> event_details;
134   job->OnNotifyPrintJobEvent(event_details);
135   job->OnDocumentDone();
136   job->ControlledWorkerShutdown();
137   */
138 }