// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "base/message_loop.h" #include "base/string16.h" #include "chrome/browser/printing/print_job.h" #include "chrome/browser/printing/print_job_worker.h" #include "chrome/common/chrome_notification_types.h" #include "content/public/browser/notification_registrar.h" #include "content/public/browser/notification_service.h" #include "printing/printed_pages_source.h" #include "testing/gtest/include/gtest/gtest.h" namespace { class TestSource : public printing::PrintedPagesSource { public: virtual string16 RenderSourceName() OVERRIDE { return string16(); } }; class TestPrintJobWorker : public printing::PrintJobWorker { public: explicit TestPrintJobWorker(printing::PrintJobWorkerOwner* owner) : printing::PrintJobWorker(owner) { } friend class TestOwner; }; class TestOwner : public printing::PrintJobWorkerOwner { public: virtual void GetSettingsDone(const printing::PrintSettings& new_settings, printing::PrintingContext::Result result) { EXPECT_FALSE(true); } virtual printing::PrintJobWorker* DetachWorker( printing::PrintJobWorkerOwner* new_owner) { // We're screwing up here since we're calling worker from the main thread. // That's fine for testing. It is actually simulating PrinterQuery behavior. TestPrintJobWorker* worker(new TestPrintJobWorker(new_owner)); EXPECT_TRUE(worker->Start()); worker->printing_context()->UseDefaultSettings(); settings_ = worker->printing_context()->settings(); return worker; } virtual MessageLoop* message_loop() { EXPECT_FALSE(true); return NULL; } virtual const printing::PrintSettings& settings() const { return settings_; } virtual int cookie() const { return 42; } private: virtual ~TestOwner() {} printing::PrintSettings settings_; }; class TestPrintJob : public printing::PrintJob { public: explicit TestPrintJob(volatile bool* check) : check_(check) { } private: ~TestPrintJob() { *check_ = true; } volatile bool* check_; }; class TestPrintNotifObserv : public content::NotificationObserver { public: // content::NotificationObserver virtual void Observe(int type, const content::NotificationSource& source, const content::NotificationDetails& details) { ADD_FAILURE(); } }; } // namespace typedef testing::Test PrintJobTest; TEST_F(PrintJobTest, SimplePrint) { // Test the multi-threaded nature of PrintJob to make sure we can use it with // known lifetime. // This message loop is actually never run. MessageLoop current; content::NotificationRegistrar registrar_; TestPrintNotifObserv observ; registrar_.Add(&observ, content::NOTIFICATION_ALL, content::NotificationService::AllSources()); volatile bool check = false; scoped_refptr job(new TestPrintJob(&check)); EXPECT_EQ(MessageLoop::current(), job->message_loop()); scoped_refptr owner(new TestOwner); TestSource source; job->Initialize(owner, &source, 1); job->Stop(); job = NULL; EXPECT_TRUE(check); } TEST_F(PrintJobTest, SimplePrintLateInit) { volatile bool check = false; MessageLoop current; scoped_refptr job(new TestPrintJob(&check)); job = NULL; EXPECT_TRUE(check); /* TODO(maruel): Test these. job->Initialize() job->Observe(); job->GetSettingsDone(); job->DetachWorker(); job->message_loop(); job->settings(); job->cookie(); job->GetSettings(printing::DEFAULTS, printing::ASK_USER, NULL); job->StartPrinting(); job->Stop(); job->Cancel(); job->RequestMissingPages(); job->FlushJob(timeout); job->DisconnectSource(); job->is_job_pending(); job->document(); // Private job->UpdatePrintedDocument(NULL); scoped_refptr event_details; job->OnNotifyPrintJobEvent(event_details); job->OnDocumentDone(); job->ControlledWorkerShutdown(); */ }