Chromium Code Reviews| 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 class ModuleWatcherTest : public testing::Test { | |
| 13 public: | |
| 14 ModuleWatcherTest() | |
| 15 : module_(nullptr), | |
| 16 module_event_count_(0), | |
| 17 module_already_loaded_event_count_(0), | |
| 18 module_loaded_event_count_(0), | |
| 19 module_unloaded_event_count_(0) {} | |
| 20 | |
| 21 void OnModuleEvent(const mojom::ModuleEvent& event) { | |
| 22 module_event_count_++; | |
|
grt (UTC plus 2)
2016/11/10 08:52:13
prefer preincrement always
chrisha
2016/11/14 16:06:45
Done.
| |
| 23 switch (event.event_type) { | |
| 24 case mojom::ModuleEventType::MODULE_ALREADY_LOADED: | |
| 25 module_already_loaded_event_count_++; | |
| 26 break; | |
| 27 case mojom::ModuleEventType::MODULE_LOADED: | |
| 28 module_loaded_event_count_++; | |
| 29 break; | |
| 30 case mojom::ModuleEventType::MODULE_UNLOADED: | |
| 31 module_unloaded_event_count_++; | |
| 32 break; | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 void TearDown() override { UnloadModule(); } | |
| 37 | |
| 38 void LoadModule() { | |
| 39 if (module_) | |
| 40 return; | |
| 41 // This module should not be a static dependency of the unit-test | |
| 42 // executable, but should be a build-system dependency or a module that is | |
| 43 // present on any Windows machine. | |
| 44 static const wchar_t kModuleName[] = L"chrome.dll"; | |
|
grt (UTC plus 2)
2016/11/10 08:52:13
nit: static constexpr
grt (UTC plus 2)
2016/11/10 08:52:13
is chrome.dll already a datadep of the unit_tests
chrisha
2016/11/14 16:06:45
Yeah, it's already a dependency of the unit_tests
chrisha
2016/11/14 16:06:45
Done.
| |
| 45 // The module should not already be loaded. | |
| 46 ASSERT_FALSE(::GetModuleHandle(kModuleName)); | |
| 47 // It should load successfully. | |
| 48 module_ = ::LoadLibrary(kModuleName); | |
| 49 ASSERT_TRUE(module_); | |
| 50 } | |
| 51 | |
| 52 void UnloadModule() { | |
| 53 if (!module_) | |
| 54 return; | |
| 55 ::FreeLibrary(module_); | |
| 56 module_ = nullptr; | |
| 57 } | |
| 58 | |
| 59 std::unique_ptr<ModuleWatcher> Create() { | |
| 60 return ModuleWatcher::Create( | |
| 61 base::Bind(&ModuleWatcherTest::OnModuleEvent, base::Unretained(this))); | |
| 62 } | |
| 63 | |
| 64 // Holds a handle to a loaded module. | |
| 65 HMODULE module_; | |
| 66 | |
| 67 // Total number of module events seen. | |
| 68 size_t module_event_count_; | |
| 69 // Total number of MODULE_ALREADY_LOADED events seen. | |
| 70 size_t module_already_loaded_event_count_; | |
| 71 // Total number of MODULE_LOADED events seen. | |
| 72 size_t module_loaded_event_count_; | |
| 73 // Total number of MODULE_UNLOADED events seen. | |
| 74 size_t module_unloaded_event_count_; | |
| 75 | |
| 76 private: | |
| 77 DISALLOW_COPY_AND_ASSIGN(ModuleWatcherTest); | |
| 78 }; | |
| 79 | |
| 80 TEST_F(ModuleWatcherTest, SingleModuleWatcherOnly) { | |
| 81 std::unique_ptr<ModuleWatcher> mw1(Create()); | |
| 82 EXPECT_TRUE(mw1.get()); | |
| 83 | |
| 84 std::unique_ptr<ModuleWatcher> mw2(Create()); | |
| 85 EXPECT_FALSE(mw2.get()); | |
| 86 } | |
| 87 | |
| 88 TEST_F(ModuleWatcherTest, ModuleEvents) { | |
| 89 // Create the module watcher. This should immediately enumerate all already | |
| 90 // loaded modules. | |
| 91 std::unique_ptr<ModuleWatcher> mw(Create()); | |
| 92 EXPECT_LT(0u, module_event_count_); | |
|
grt (UTC plus 2)
2016/11/10 08:52:13
the counts are unsigned, so just check for equalit
chrisha
2016/11/14 16:06:45
You're suggested that _NE is more readable that _L
grt (UTC plus 2)
2016/11/14 20:30:31
okay. now that i think about it, the counts should
| |
| 93 EXPECT_LT(0u, module_already_loaded_event_count_); | |
| 94 EXPECT_EQ(0u, module_loaded_event_count_); | |
| 95 EXPECT_EQ(0u, module_unloaded_event_count_); | |
| 96 | |
| 97 // Dynamically load a module and ensure it a notification is received for it. | |
| 98 LoadModule(); | |
| 99 EXPECT_EQ(1u, module_loaded_event_count_); | |
|
grt (UTC plus 2)
2016/11/10 08:52:13
does this assume that the loaded module doesn't al
chrisha
2016/11/14 16:06:45
I just want to ensure that at least one module get
grt (UTC plus 2)
2016/11/14 20:30:31
great. that addresses my concern -- that in some c
| |
| 100 | |
| 101 // Unload the module and ensure another notification is received. | |
| 102 UnloadModule(); | |
| 103 EXPECT_EQ(1u, module_unloaded_event_count_); | |
| 104 | |
| 105 // Dynamically load a module and ensure it a notification is received for it. | |
| 106 LoadModule(); | |
| 107 EXPECT_EQ(2u, module_loaded_event_count_); | |
| 108 | |
| 109 // Destroy the module watcher. | |
| 110 mw.reset(); | |
| 111 | |
| 112 // Unload the module and ensure no notification is received this time. | |
| 113 UnloadModule(); | |
| 114 EXPECT_EQ(1u, module_unloaded_event_count_); | |
| 115 } | |
| OLD | NEW |