Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4441)

Unified Diff: chrome/browser/history/history_test_utils.cc

Issue 2495053003: Wait for history backend in popup blocker test (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/history/history_test_utils.cc
diff --git a/chrome/browser/history/history_test_utils.cc b/chrome/browser/history/history_test_utils.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1f139273b2bdc96778420543d9b0c0b4dd21b83f
--- /dev/null
+++ b/chrome/browser/history/history_test_utils.cc
@@ -0,0 +1,54 @@
+// Copyright 2016 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 <memory>
+#include <utility>
+
+#include "base/macros.h"
+#include "base/message_loop/message_loop.h"
+#include "base/task/cancelable_task_tracker.h"
+#include "chrome/browser/history/history_service_factory.h"
+#include "components/history/core/browser/history_db_task.h"
+#include "components/history/core/browser/history_service.h"
+#include "content/public/test/test_utils.h"
+
+namespace {
+
+// Note: WaitableEvent is not used for synchronization between the main thread
+// and history backend thread because the history subsystem posts tasks back
+// to the main thread. Had we tried to Signal an event in such a task
+// and Wait for it on the main thread, the task would not run at all because
+// the main thread would be blocked on the Wait call, resulting in a deadlock.
+
+// A task to be scheduled on the history backend thread.
+// Notifies the main thread after all history backend thread tasks have run.
+class WaitForHistoryTask : public history::HistoryDBTask {
+ public:
+ WaitForHistoryTask() {}
+
+ bool RunOnDBThread(history::HistoryBackend* backend,
+ history::HistoryDatabase* db) override {
+ return true;
+ }
+
+ void DoneRunOnMainThread() override {
+ base::MessageLoop::current()->QuitWhenIdle();
Avi (use Gerrit) 2016/11/12 20:35:55 Use a MessageLoopRunner.
jochen (gone - plz use gerrit) 2016/11/12 20:48:21 not sure I understand? MessageLoopRunner doesn't h
Avi (use Gerrit) 2016/11/12 20:58:41 If you look at content/public/test/test_utils.cc l
jochen (gone - plz use gerrit) 2016/11/12 21:10:54 yeah, but it gives up after a few tasks and quits
Avi (use Gerrit) 2016/11/12 21:23:00 Fair enough. I'm really not a fan of hammering dir
+ }
+
+ private:
+ ~WaitForHistoryTask() override {}
+
+ DISALLOW_COPY_AND_ASSIGN(WaitForHistoryTask);
+};
+
+} // namespace
+
+void WaitForHistoryBackendToRun(Profile* profile) {
+ base::CancelableTaskTracker task_tracker;
+ std::unique_ptr<history::HistoryDBTask> task(new WaitForHistoryTask());
+ history::HistoryService* history = HistoryServiceFactory::GetForProfile(
+ profile, ServiceAccessType::EXPLICIT_ACCESS);
+ history->ScheduleDBTask(std::move(task), &task_tracker);
+ content::RunMessageLoop();
+}
« no previous file with comments | « chrome/browser/history/history_test_utils.h ('k') | chrome/browser/ui/blocked_content/popup_blocker_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698