| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/common/conflicts/module_watcher_win.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace conflicts { |
| 13 |
| 14 class ModuleWatcherTest : public testing::Test { |
| 15 public: |
| 16 ModuleWatcherTest() |
| 17 : module_(nullptr), |
| 18 module_event_count_(0), |
| 19 module_already_loaded_event_count_(0), |
| 20 module_loaded_event_count_(0), |
| 21 module_unloaded_event_count_(0) {} |
| 22 |
| 23 void OnModuleEvent(const mojom::ModuleEvent& event) { |
| 24 module_event_count_++; |
| 25 switch (event.event_type) { |
| 26 case mojom::ModuleEventType::MODULE_ALREADY_LOADED: |
| 27 module_already_loaded_event_count_++; |
| 28 break; |
| 29 case mojom::ModuleEventType::MODULE_LOADED: |
| 30 module_loaded_event_count_++; |
| 31 break; |
| 32 case mojom::ModuleEventType::MODULE_UNLOADED: |
| 33 module_unloaded_event_count_++; |
| 34 break; |
| 35 } |
| 36 } |
| 37 |
| 38 void TearDown() override { |
| 39 UnloadModule(); |
| 40 } |
| 41 |
| 42 void LoadModule() { |
| 43 if (module_) |
| 44 return; |
| 45 // This module should not be a static dependency of the unit-test |
| 46 // executable, but should be a build-system dependency or a module that is |
| 47 // present on any Windows machine. |
| 48 static const wchar_t kModuleName[] = L"chrome.dll"; |
| 49 // The module should not already be loaded. |
| 50 ASSERT_FALSE(::GetModuleHandle(kModuleName)); |
| 51 // It should load successfully. |
| 52 module_ = ::LoadLibrary(kModuleName); |
| 53 ASSERT_TRUE(module_); |
| 54 } |
| 55 |
| 56 void UnloadModule() { |
| 57 if (!module_) |
| 58 return; |
| 59 ::FreeLibrary(module_); |
| 60 module_ = nullptr; |
| 61 } |
| 62 |
| 63 // Holds a handle to a loaded module. |
| 64 HMODULE module_; |
| 65 |
| 66 // Total number of module events seen. |
| 67 size_t module_event_count_; |
| 68 // Total number of MODULE_ALREADY_LOADED events seen. |
| 69 size_t module_already_loaded_event_count_; |
| 70 // Total number of MODULE_LOADED events seen. |
| 71 size_t module_loaded_event_count_; |
| 72 // Total number of MODULE_UNLOADED events seen. |
| 73 size_t module_unloaded_event_count_; |
| 74 |
| 75 private: |
| 76 DISALLOW_COPY_AND_ASSIGN(ModuleWatcherTest); |
| 77 }; |
| 78 |
| 79 TEST_F(ModuleWatcherTest, ModuleEvents) { |
| 80 // Create the module watcher. This should immediately enumerate all already |
| 81 // loaded modules. |
| 82 std::unique_ptr<ModuleWatcher> mw(new ModuleWatcher( |
| 83 base::Bind(&ModuleWatcherTest::OnModuleEvent, base::Unretained(this)))); |
| 84 EXPECT_LT(0u, module_event_count_); |
| 85 EXPECT_LT(0u, module_already_loaded_event_count_); |
| 86 EXPECT_EQ(0u, module_loaded_event_count_); |
| 87 EXPECT_EQ(0u, module_unloaded_event_count_); |
| 88 |
| 89 // Dynamically load a module and ensure it a notification is received for it. |
| 90 LoadModule(); |
| 91 EXPECT_EQ(1u, module_loaded_event_count_); |
| 92 |
| 93 // Unload the module and ensure another notification is received. |
| 94 UnloadModule(); |
| 95 EXPECT_EQ(1u, module_unloaded_event_count_); |
| 96 |
| 97 // Dynamically load a module and ensure it a notification is received for it. |
| 98 LoadModule(); |
| 99 EXPECT_EQ(2u, module_loaded_event_count_); |
| 100 |
| 101 // Destroy the module watcher. |
| 102 mw.reset(); |
| 103 |
| 104 // Unload the module and ensure no notification is received this time. |
| 105 UnloadModule(); |
| 106 EXPECT_EQ(1u, module_unloaded_event_count_); |
| 107 } |
| 108 |
| 109 } // namespace conflicts |
| OLD | NEW |