summaryrefslogtreecommitdiff
path: root/chromium/chrome/common/conflicts/remote_module_watcher_win_unittest.cc
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2020-03-05 14:36:22 +0100
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2020-03-05 14:37:32 +0100
commit28db9b54de6402bd38770ecc1d620255e9d1e78f (patch)
tree469a957ff6b9b6d0ee9fb4074b9139cbaa050443 /chromium/chrome/common/conflicts/remote_module_watcher_win_unittest.cc
parent3239a38a9b35d29e483b7bd67b786b4f9d109908 (diff)
parent248b70b82a40964d5594eb04feca0fa36716185d (diff)
downloadqtwebengine-chromium-28db9b54de6402bd38770ecc1d620255e9d1e78f.tar.gz
Merge remote-tracking branch 'origin/upstream-master' into 79-based
Conflicts: chromium/chrome/common/pref_names.cc chromium/chrome/common/pref_names.h Change-Id: I9be20fb8dfd946e3db1fa298dce076db5fd1f397
Diffstat (limited to 'chromium/chrome/common/conflicts/remote_module_watcher_win_unittest.cc')
-rw-r--r--chromium/chrome/common/conflicts/remote_module_watcher_win_unittest.cc122
1 files changed, 122 insertions, 0 deletions
diff --git a/chromium/chrome/common/conflicts/remote_module_watcher_win_unittest.cc b/chromium/chrome/common/conflicts/remote_module_watcher_win_unittest.cc
new file mode 100644
index 00000000000..ca99ee16a84
--- /dev/null
+++ b/chromium/chrome/common/conflicts/remote_module_watcher_win_unittest.cc
@@ -0,0 +1,122 @@
+// Copyright 2019 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 "chrome/common/conflicts/remote_module_watcher_win.h"
+
+#include <windows.h>
+
+#include <memory>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "base/memory/scoped_refptr.h"
+#include "base/test/task_environment.h"
+#include "base/threading/thread_task_runner_handle.h"
+#include "chrome/common/conflicts/module_event_sink_win.mojom.h"
+#include "mojo/public/cpp/bindings/pending_remote.h"
+#include "mojo/public/cpp/bindings/receiver.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+class RemoteModuleWatcherTest : public testing::Test,
+ public mojom::ModuleEventSink {
+ public:
+ RemoteModuleWatcherTest() = default;
+ ~RemoteModuleWatcherTest() override = default;
+
+ mojo::PendingRemote<mojom::ModuleEventSink> Bind() {
+ return receiver_.BindNewPipeAndPassRemote();
+ }
+
+ // mojom::ModuleEventSink:
+ void OnModuleEvents(
+ const std::vector<uint64_t>& module_load_addresses) override {
+ module_event_count_ += module_load_addresses.size();
+ }
+
+ void LoadModule() {
+ if (module_handle_)
+ return;
+ // This module should not be a static dependency of the unit-test
+ // executable, but should be a build-system dependency or a module that is
+ // present on any Windows machine.
+ static constexpr wchar_t kModuleName[] = L"conflicts_dll.dll";
+ // The module should not already be loaded.
+ ASSERT_FALSE(::GetModuleHandle(kModuleName));
+ // It should load successfully.
+ module_handle_ = ::LoadLibrary(kModuleName);
+ ASSERT_TRUE(module_handle_);
+ }
+
+ void UnloadModule() {
+ if (!module_handle_)
+ return;
+ ::FreeLibrary(module_handle_);
+ module_handle_ = nullptr;
+ }
+
+ // Runs the task scheduler until no tasks are running.
+ void RunUntilIdle() { task_environment_.RunUntilIdle(); }
+ void FastForwardByIdleDelay() {
+ task_environment_.FastForwardBy(RemoteModuleWatcher::kIdleDelay);
+ }
+
+ HMODULE module_handle() { return module_handle_; }
+
+ int module_event_count() { return module_event_count_; }
+
+ private:
+ // Must be first.
+ base::test::TaskEnvironment task_environment_{
+ base::test::TaskEnvironment::TimeSource::MOCK_TIME};
+
+ // Binds a ModuleEventSinkRequest to this implementation of ModuleEventSink.
+ mojo::Receiver<mojom::ModuleEventSink> receiver_{this};
+
+ // Holds a handle to a loaded module.
+ HMODULE module_handle_ = nullptr;
+
+ // Total number of module events seen.
+ int module_event_count_ = 0;
+
+ DISALLOW_COPY_AND_ASSIGN(RemoteModuleWatcherTest);
+};
+
+} // namespace
+
+TEST_F(RemoteModuleWatcherTest, ModuleEvents) {
+ auto remote_module_watcher =
+ RemoteModuleWatcher::Create(base::ThreadTaskRunnerHandle::Get(), Bind());
+
+ // Wait until the watcher is initialized and events for already loaded modules
+ // are received.
+ RunUntilIdle();
+ // Now wait for the timer used to batch events to expire.
+ FastForwardByIdleDelay();
+
+ EXPECT_GT(module_event_count(), 0);
+
+ // Dynamically load a module and ensure a notification is received for it.
+ int previous_module_event_count = module_event_count();
+ LoadModule();
+ FastForwardByIdleDelay();
+ EXPECT_GT(module_event_count(), previous_module_event_count);
+
+ UnloadModule();
+
+ // Destroy the module watcher.
+ remote_module_watcher = nullptr;
+ RunUntilIdle();
+
+ // Load the module and ensure no notification is received this time.
+ previous_module_event_count = module_event_count();
+ LoadModule();
+ FastForwardByIdleDelay();
+
+ EXPECT_EQ(module_event_count(), previous_module_event_count);
+
+ UnloadModule();
+}