Home | History | Annotate | Download | only in printing
      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 "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"
     14 
     15 namespace {
     16 
     17 class TestSource : public printing::PrintedPagesSource {
     18  public:
     19   virtual string16 RenderSourceName() OVERRIDE {
     20     return string16();
     21   }
     22 };
     23 
     24 class TestPrintJobWorker : public printing::PrintJobWorker {
     25  public:
     26   explicit TestPrintJobWorker(printing::PrintJobWorkerOwner* owner)
     27       : printing::PrintJobWorker(owner) {
     28   }
     29   friend class TestOwner;
     30 };
     31 
     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);
     38   }
     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;
     48   }
     49   virtual base::MessageLoop* message_loop() OVERRIDE {
     50     EXPECT_FALSE(true);
     51     return NULL;
     52   }
     53   virtual const printing::PrintSettings& settings() const OVERRIDE {
     54     return settings_;
     55   }
     56   virtual int cookie() const OVERRIDE {
     57     return 42;
     58   }
     59 
     60  private:
     61   virtual ~TestOwner() {}
     62 
     63   printing::PrintSettings settings_;
     64 };
     65 
     66 class TestPrintJob : public printing::PrintJob {
     67  public:
     68   explicit TestPrintJob(volatile bool* check) : check_(check) {
     69   }
     70  private:
     71   virtual ~TestPrintJob() {
     72     *check_ = true;
     73   }
     74   volatile bool* check_;
     75 };
     76 
     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();
     84   }
     85 };
     86 
     87 }  // namespace
     88 
     89 typedef testing::Test PrintJobTest;
     90 
     91 TEST_F(PrintJobTest, SimplePrint) {
     92   // Test the multi-threaded nature of PrintJob to make sure we can use it with
     93   // known lifetime.
     94 
     95   // This message loop is actually never run.
     96   base::MessageLoop current;
     97 
     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   EXPECT_FALSE(job->is_stopped());
    110   EXPECT_TRUE(job->is_stopping());
    111   while (!job->is_stopped())
    112   {
    113     current.RunUntilIdle();
    114   }
    115   EXPECT_TRUE(job->is_stopped());
    116   EXPECT_FALSE(job->is_stopping());
    117   job = NULL;
    118   EXPECT_TRUE(check);
    119 }
    120 
    121 TEST_F(PrintJobTest, SimplePrintLateInit) {
    122   volatile bool check = false;
    123   base::MessageLoop current;
    124   scoped_refptr<printing::PrintJob> job(new TestPrintJob(&check));
    125   job = NULL;
    126   EXPECT_TRUE(check);
    127   /* TODO(maruel): Test these.
    128   job->Initialize()
    129   job->Observe();
    130   job->GetSettingsDone();
    131   job->DetachWorker();
    132   job->message_loop();
    133   job->settings();
    134   job->cookie();
    135   job->GetSettings(printing::DEFAULTS, printing::ASK_USER, NULL);
    136   job->StartPrinting();
    137   job->Stop();
    138   job->Cancel();
    139   job->RequestMissingPages();
    140   job->FlushJob(timeout);
    141   job->DisconnectSource();
    142   job->is_job_pending();
    143   job->document();
    144   // Private
    145   job->UpdatePrintedDocument(NULL);
    146   scoped_refptr<printing::JobEventDetails> event_details;
    147   job->OnNotifyPrintJobEvent(event_details);
    148   job->OnDocumentDone();
    149   job->ControlledWorkerShutdown();
    150   */
    151 }
    152