summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authormaruel@chromium.org <maruel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-11-25 13:47:50 +0000
committermaruel@chromium.org <maruel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-11-25 13:47:50 +0000
commit27abf503d7de0fde7178f8e69fe387a27f8e1d06 (patch)
treee705a05de8c4828c259f9e6c9e1f678b97c08784 /chrome
parent46f7efe3e8aaa8f26cf3a395a4208a6c715bb6d8 (diff)
downloadchromium_src-27abf503d7de0fde7178f8e69fe387a27f8e1d06.zip
chromium_src-27abf503d7de0fde7178f8e69fe387a27f8e1d06.tar.gz
chromium_src-27abf503d7de0fde7178f8e69fe387a27f8e1d06.tar.bz2
Add superficial unit test for PrintJob. That's a start.
Add more DCHECKs. Same as r5607 except that Thread.StopSoon is fixed. Review URL: http://codereview.chromium.org/11534 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@5971 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/printing/print_job.cc12
-rw-r--r--chrome/browser/printing/print_job.h9
-rw-r--r--chrome/browser/printing/print_job_unittest.cc117
-rw-r--r--chrome/renderer/render_view.cc1
-rw-r--r--chrome/test/unit/unit_tests.scons1
-rw-r--r--chrome/test/unit/unittests.vcproj4
6 files changed, 142 insertions, 2 deletions
diff --git a/chrome/browser/printing/print_job.cc b/chrome/browser/printing/print_job.cc
index a2d07fd..14a487cd 100644
--- a/chrome/browser/printing/print_job.cc
+++ b/chrome/browser/printing/print_job.cc
@@ -24,6 +24,8 @@ PrintJob::PrintJob(PrintedPagesSource* source)
is_job_pending_(false),
is_print_dialog_box_shown_(false),
is_canceling_(false) {
+ DCHECK(ui_message_loop_);
+ ui_message_loop_->AddDestructionObserver(this);
}
PrintJob::PrintJob()
@@ -34,14 +36,18 @@ PrintJob::PrintJob()
is_job_pending_(false),
is_print_dialog_box_shown_(false),
is_canceling_(false) {
+ DCHECK(ui_message_loop_);
+ ui_message_loop_->AddDestructionObserver(this);
}
PrintJob::~PrintJob() {
+ ui_message_loop_->RemoveDestructionObserver(this);
// The job should be finished (or at least canceled) when it is destroyed.
DCHECK(!is_job_pending_);
DCHECK(!is_print_dialog_box_shown_);
DCHECK(!is_canceling_);
- DCHECK(worker_->message_loop() == NULL);
+ if (worker_.get())
+ DCHECK(worker_->message_loop() == NULL);
DCHECK_EQ(ui_message_loop_, MessageLoop::current());
}
@@ -142,6 +148,10 @@ int PrintJob::cookie() const {
return document_->cookie();
}
+void PrintJob::WillDestroyCurrentMessageLoop() {
+ NOTREACHED();
+}
+
void PrintJob::GetSettings(GetSettingsAskParam ask_user_for_settings,
HWND parent_window) {
DCHECK_EQ(ui_message_loop_, MessageLoop::current());
diff --git a/chrome/browser/printing/print_job.h b/chrome/browser/printing/print_job.h
index e18d52c..4c334b9 100644
--- a/chrome/browser/printing/print_job.h
+++ b/chrome/browser/printing/print_job.h
@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_H_
#define CHROME_BROWSER_PRINTING_PRINT_JOB_H_
+#include "base/message_loop.h"
#include "base/ref_counted.h"
#include "chrome/browser/printing/print_job_worker_owner.h"
#include "chrome/common/notification_service.h"
@@ -32,7 +33,8 @@ class PrinterQuery;
// runs in the UI thread.
class PrintJob : public base::RefCountedThreadSafe<PrintJob>,
public NotificationObserver,
- public PrintJobWorkerOwner {
+ public PrintJobWorkerOwner,
+ public MessageLoop::DestructionObserver {
public:
// GetSettings() UI parameter.
enum GetSettingsAskParam {
@@ -48,6 +50,8 @@ class PrintJob : public base::RefCountedThreadSafe<PrintJob>,
PrintJob();
virtual ~PrintJob();
+ // Grabs the ownership of the PrintJobWorker from another job, which is
+ // usually a PrinterQuery.
void Initialize(PrintJobWorkerOwner* job, PrintedPagesSource* source);
// NotificationObserver
@@ -69,6 +73,9 @@ class PrintJob : public base::RefCountedThreadSafe<PrintJob>,
virtual const PrintSettings& settings() const { return settings_; }
virtual int cookie() const;
+ // DestructionObserver
+ virtual void WillDestroyCurrentMessageLoop();
+
// Initializes the printing context. This can be done synchronously or not. It
// is fine to call this function multiple times to reinitialize the settings.
// |parent_window| parameter will be the owner of the print setting dialog
diff --git a/chrome/browser/printing/print_job_unittest.cc b/chrome/browser/printing/print_job_unittest.cc
new file mode 100644
index 0000000..6c65a5d
--- /dev/null
+++ b/chrome/browser/printing/print_job_unittest.cc
@@ -0,0 +1,117 @@
+// Copyright (c) 2008 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 "chrome/browser/printing/print_job.h"
+#include "chrome/browser/printing/printed_pages_source.h"
+#include "googleurl/src/gurl.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+class TestSource : public printing::PrintedPagesSource {
+ public:
+ virtual void RenderOnePrintedPage(printing::PrintedDocument* document,
+ int page_number) {
+ }
+ virtual std::wstring RenderSourceName() {
+ return L"";
+ }
+ virtual GURL RenderSourceUrl() {
+ return GURL();
+ }
+};
+
+class TestPrintJob : public printing::PrintJob {
+ public:
+ TestPrintJob(printing::PrintedPagesSource* source, volatile bool* check)
+ : printing::PrintJob(source), check_(check) {
+ }
+ TestPrintJob(volatile bool* check) : check_(check) {
+ }
+ ~TestPrintJob() {
+ *check_ = true;
+ }
+ private:
+ volatile bool* check_;
+};
+
+class TestPrintNotifObserv : public NotificationObserver {
+ public:
+ // NotificationObserver
+ virtual void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ ASSERT_EQ(NOTIFY_PRINT_JOB_EVENT, type);
+ printing::JobEventDetails::Type event_type =
+ Details<printing::JobEventDetails>(details)->type();
+ EXPECT_NE(printing::JobEventDetails::NEW_DOC, event_type);
+ EXPECT_NE(printing::JobEventDetails::NEW_PAGE, event_type);
+ EXPECT_NE(printing::JobEventDetails::PAGE_DONE, event_type);
+ EXPECT_NE(printing::JobEventDetails::DOC_DONE, event_type);
+ EXPECT_NE(printing::JobEventDetails::JOB_DONE, event_type);
+ EXPECT_NE(printing::JobEventDetails::ALL_PAGES_REQUESTED, event_type);
+ if (event_type == printing::JobEventDetails::USER_INIT_DONE ||
+ event_type == printing::JobEventDetails::USER_INIT_CANCELED ||
+ event_type == printing::JobEventDetails::DEFAULT_INIT_DONE ||
+ event_type == printing::JobEventDetails::FAILED) {
+ MessageLoop::current()->Quit();
+ return;
+ }
+ }
+};
+
+} // namespace
+
+TEST(PrintJobTest, SimplePrint) {
+ // Test the multithreaded nature of PrintJob to make sure we can use it with
+ // known livetime.
+ TestPrintNotifObserv observ;
+ MessageLoop current;
+ NotificationService::current()->AddObserver(
+ &observ, NOTIFY_ALL,
+ NotificationService::AllSources());
+ TestSource source;
+ volatile bool check = false;
+ scoped_refptr<printing::PrintJob> job(new TestPrintJob(&source, &check));
+ job->GetSettings(printing::PrintJob::DEFAULTS, NULL);
+ EXPECT_EQ(MessageLoop::current(), job->message_loop());
+ current.Run();
+ job->Stop();
+ job = NULL;
+ EXPECT_TRUE(check);
+}
+
+TEST(PrintJobTest, SimplePrintLateInit) {
+ volatile bool check = false;
+ MessageLoop current;
+ scoped_refptr<printing::PrintJob> 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_ms);
+ job->DisconnectSource();
+ job->is_job_pending();
+ job->is_print_dialog_box_shown();
+ job->document();
+ // Private
+ job->UpdatePrintedDocument(NULL);
+ scoped_refptr<printing::JobEventDetails> event_details;
+ job->OnNotifyPrintJobEvent(event_details);
+ job->OnDocumentDone();
+ job->ControlledWorkerShutdown();
+ */
+}
diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc
index 835e73f..d1737a6 100644
--- a/chrome/renderer/render_view.cc
+++ b/chrome/renderer/render_view.cc
@@ -453,6 +453,7 @@ int RenderView::SwitchFrameToPrintMediaType(const ViewMsg_Print_Params& params,
NOTREACHED();
pages = 0;
} else {
+ DCHECK_GT(printed_document_width_, 0);
// Force to recalculate the height, otherwise it reuse the current window
// height as the default.
float effective_shrink = printed_document_width_ / paper_width;
diff --git a/chrome/test/unit/unit_tests.scons b/chrome/test/unit/unit_tests.scons
index be3558b..c90a0be 100644
--- a/chrome/test/unit/unit_tests.scons
+++ b/chrome/test/unit/unit_tests.scons
@@ -175,6 +175,7 @@ if env['PLATFORM'] == 'win32':
'$CHROME_DIR/browser/password_form_manager_unittest.cc',
'$CHROME_DIR/browser/printing/page_number_unittest.cc',
'$CHROME_DIR/browser/printing/page_overlays_unittest.cc',
+ '$CHROME_DIR/browser/printing/print_job_unittest.cc',
'$CHROME_DIR/browser/printing/win_printing_context_unittest.cc',
'$CHROME_DIR/browser/profile_manager_unittest.cc',
'$CHROME_DIR/browser/renderer_security_policy_unittest.cc',
diff --git a/chrome/test/unit/unittests.vcproj b/chrome/test/unit/unittests.vcproj
index 0b131c0..17b9dc29 100644
--- a/chrome/test/unit/unittests.vcproj
+++ b/chrome/test/unit/unittests.vcproj
@@ -782,6 +782,10 @@
>
</File>
<File
+ RelativePath="..\..\browser\printing\print_job_unittest.cc"
+ >
+ </File>
+ <File
RelativePath="..\..\browser\printing\printing_test.h"
>
</File>