| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "chrome/browser/printing/print_job.h" | 6 #include "chrome/browser/printing/print_job.h" |
| 7 #include "chrome/browser/printing/print_job_worker.h" |
| 7 #include "chrome/browser/printing/printed_pages_source.h" | 8 #include "chrome/browser/printing/printed_pages_source.h" |
| 8 #include "chrome/common/notification_service.h" | 9 #include "chrome/common/notification_service.h" |
| 9 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 12 |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 class TestSource : public printing::PrintedPagesSource { | 15 class TestSource : public printing::PrintedPagesSource { |
| 15 public: | 16 public: |
| 16 virtual void RenderOnePrintedPage(printing::PrintedDocument* document, | |
| 17 int page_number) { | |
| 18 } | |
| 19 virtual std::wstring RenderSourceName() { | 17 virtual std::wstring RenderSourceName() { |
| 20 return L""; | 18 return L""; |
| 21 } | 19 } |
| 22 virtual GURL RenderSourceUrl() { | 20 virtual GURL RenderSourceUrl() { |
| 23 return GURL(); | 21 return GURL(); |
| 24 } | 22 } |
| 25 }; | 23 }; |
| 26 | 24 |
| 25 class TestPrintJobWorker : public printing::PrintJobWorker { |
| 26 public: |
| 27 explicit TestPrintJobWorker(printing::PrintJobWorkerOwner* owner) |
| 28 : printing::PrintJobWorker(owner) { |
| 29 } |
| 30 friend class TestOwner; |
| 31 }; |
| 32 |
| 33 class TestOwner : public printing::PrintJobWorkerOwner { |
| 34 public: |
| 35 virtual void AddRef() { |
| 36 EXPECT_FALSE(true); |
| 37 } |
| 38 virtual void Release() { |
| 39 EXPECT_FALSE(true); |
| 40 } |
| 41 virtual void GetSettingsDone(const printing::PrintSettings& new_settings, |
| 42 printing::PrintingContext::Result result) { |
| 43 EXPECT_FALSE(true); |
| 44 } |
| 45 virtual printing::PrintJobWorker* DetachWorker( |
| 46 printing::PrintJobWorkerOwner* new_owner) { |
| 47 // We're screwing up here since we're calling worker from the main thread. |
| 48 // That's fine for testing. It is actually simulating PrinterQuery behavior. |
| 49 TestPrintJobWorker* worker(new TestPrintJobWorker(new_owner)); |
| 50 EXPECT_TRUE(worker->Start()); |
| 51 worker->printing_context().UseDefaultSettings(); |
| 52 settings_ = worker->printing_context().settings(); |
| 53 return worker; |
| 54 } |
| 55 virtual MessageLoop* message_loop() { |
| 56 EXPECT_FALSE(true); |
| 57 return NULL; |
| 58 } |
| 59 virtual const printing::PrintSettings& settings() const { |
| 60 return settings_; |
| 61 } |
| 62 virtual int cookie() const { |
| 63 return 42; |
| 64 } |
| 65 private: |
| 66 printing::PrintSettings settings_; |
| 67 }; |
| 68 |
| 27 class TestPrintJob : public printing::PrintJob { | 69 class TestPrintJob : public printing::PrintJob { |
| 28 public: | 70 public: |
| 29 TestPrintJob(printing::PrintedPagesSource* source, volatile bool* check) | |
| 30 : printing::PrintJob(source), check_(check) { | |
| 31 } | |
| 32 TestPrintJob(volatile bool* check) : check_(check) { | 71 TestPrintJob(volatile bool* check) : check_(check) { |
| 33 } | 72 } |
| 34 ~TestPrintJob() { | 73 ~TestPrintJob() { |
| 35 *check_ = true; | 74 *check_ = true; |
| 36 } | 75 } |
| 37 private: | 76 private: |
| 38 volatile bool* check_; | 77 volatile bool* check_; |
| 39 }; | 78 }; |
| 40 | 79 |
| 41 class TestPrintNotifObserv : public NotificationObserver { | 80 class TestPrintNotifObserv : public NotificationObserver { |
| 42 public: | 81 public: |
| 43 // NotificationObserver | 82 // NotificationObserver |
| 44 virtual void Observe(NotificationType type, | 83 virtual void Observe(NotificationType type, |
| 45 const NotificationSource& source, | 84 const NotificationSource& source, |
| 46 const NotificationDetails& details) { | 85 const NotificationDetails& details) { |
| 47 ASSERT_EQ(NotificationType::PRINT_JOB_EVENT, type.value); | 86 EXPECT_FALSE(true); |
| 48 printing::JobEventDetails::Type event_type = | |
| 49 Details<printing::JobEventDetails>(details)->type(); | |
| 50 EXPECT_NE(printing::JobEventDetails::NEW_DOC, event_type); | |
| 51 EXPECT_NE(printing::JobEventDetails::NEW_PAGE, event_type); | |
| 52 EXPECT_NE(printing::JobEventDetails::PAGE_DONE, event_type); | |
| 53 EXPECT_NE(printing::JobEventDetails::DOC_DONE, event_type); | |
| 54 EXPECT_NE(printing::JobEventDetails::JOB_DONE, event_type); | |
| 55 EXPECT_NE(printing::JobEventDetails::ALL_PAGES_REQUESTED, event_type); | |
| 56 if (event_type == printing::JobEventDetails::USER_INIT_DONE || | |
| 57 event_type == printing::JobEventDetails::USER_INIT_CANCELED || | |
| 58 event_type == printing::JobEventDetails::DEFAULT_INIT_DONE || | |
| 59 event_type == printing::JobEventDetails::FAILED) { | |
| 60 MessageLoop::current()->Quit(); | |
| 61 return; | |
| 62 } | |
| 63 } | 87 } |
| 64 }; | 88 }; |
| 65 | 89 |
| 66 } // namespace | 90 } // namespace |
| 67 | 91 |
| 68 TEST(PrintJobTest, SimplePrint) { | 92 TEST(PrintJobTest, SimplePrint) { |
| 69 // Test the multithreaded nature of PrintJob to make sure we can use it with | 93 // Test the multithreaded nature of PrintJob to make sure we can use it with |
| 70 // known livetime. | 94 // known livetime. |
| 95 |
| 96 // This message loop is actually never run. |
| 97 MessageLoop current; |
| 98 |
| 71 TestPrintNotifObserv observ; | 99 TestPrintNotifObserv observ; |
| 72 MessageLoop current; | |
| 73 NotificationService::current()->AddObserver( | 100 NotificationService::current()->AddObserver( |
| 74 &observ, NotificationType::ALL, | 101 &observ, NotificationType::ALL, |
| 75 NotificationService::AllSources()); | 102 NotificationService::AllSources()); |
| 103 volatile bool check = false; |
| 104 scoped_refptr<printing::PrintJob> job(new TestPrintJob(&check)); |
| 105 EXPECT_EQ(MessageLoop::current(), job->message_loop()); |
| 106 TestOwner owner; |
| 76 TestSource source; | 107 TestSource source; |
| 77 volatile bool check = false; | 108 job->Initialize(&owner, &source); |
| 78 scoped_refptr<printing::PrintJob> job(new TestPrintJob(&source, &check)); | |
| 79 job->GetSettings(printing::PrintJob::DEFAULTS, NULL); | |
| 80 EXPECT_EQ(MessageLoop::current(), job->message_loop()); | |
| 81 current.Run(); | |
| 82 job->Stop(); | 109 job->Stop(); |
| 83 job = NULL; | 110 job = NULL; |
| 84 EXPECT_TRUE(check); | 111 EXPECT_TRUE(check); |
| 85 NotificationService::current()->RemoveObserver( | 112 NotificationService::current()->RemoveObserver( |
| 86 &observ, NotificationType::ALL, | 113 &observ, NotificationType::ALL, |
| 87 NotificationService::AllSources()); | 114 NotificationService::AllSources()); |
| 88 } | 115 } |
| 89 | 116 |
| 90 TEST(PrintJobTest, SimplePrintLateInit) { | 117 TEST(PrintJobTest, SimplePrintLateInit) { |
| 91 volatile bool check = false; | 118 volatile bool check = false; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 112 job->is_print_dialog_box_shown(); | 139 job->is_print_dialog_box_shown(); |
| 113 job->document(); | 140 job->document(); |
| 114 // Private | 141 // Private |
| 115 job->UpdatePrintedDocument(NULL); | 142 job->UpdatePrintedDocument(NULL); |
| 116 scoped_refptr<printing::JobEventDetails> event_details; | 143 scoped_refptr<printing::JobEventDetails> event_details; |
| 117 job->OnNotifyPrintJobEvent(event_details); | 144 job->OnNotifyPrintJobEvent(event_details); |
| 118 job->OnDocumentDone(); | 145 job->OnDocumentDone(); |
| 119 job->ControlledWorkerShutdown(); | 146 job->ControlledWorkerShutdown(); |
| 120 */ | 147 */ |
| 121 } | 148 } |
| OLD | NEW |