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

Side by Side Diff: chrome/common/conflicts/module_watcher_win_unittest.cc

Issue 2473783005: [Win] Create ModuleWatcher. (Closed)
Patch Set: Add conflits_dll for testing. Created 4 years 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 unified diff | Download patch
« no previous file with comments | « chrome/common/conflicts/module_watcher_win.cc ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_;
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 constexpr wchar_t kModuleName[] = L"conflicts_dll.dll";
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 int module_event_count_;
69 // Total number of MODULE_ALREADY_LOADED events seen.
70 int module_already_loaded_event_count_;
71 // Total number of MODULE_LOADED events seen.
72 int module_loaded_event_count_;
73 // Total number of MODULE_UNLOADED events seen.
74 int 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(0, module_event_count_);
93 EXPECT_LT(0, module_already_loaded_event_count_);
94 EXPECT_EQ(0, module_loaded_event_count_);
95 EXPECT_EQ(0, module_unloaded_event_count_);
96
97 // Dynamically load a module and ensure a notification is received for it.
98 int previous_module_loaded_event_count = module_loaded_event_count_;
99 LoadModule();
100 EXPECT_LT(previous_module_loaded_event_count, module_loaded_event_count_);
101
102 // Unload the module and ensure another notification is received.
103 int previous_module_unloaded_event_count = module_unloaded_event_count_;
104 UnloadModule();
105 EXPECT_LT(previous_module_unloaded_event_count, module_unloaded_event_count_);
106
107 // Dynamically load a module and ensure a notification is received for it.
108 previous_module_loaded_event_count = module_loaded_event_count_;
109 LoadModule();
110 EXPECT_LT(previous_module_loaded_event_count, module_loaded_event_count_);
111
112 // Destroy the module watcher.
113 mw.reset();
114
115 // Unload the module and ensure no notification is received this time.
116 previous_module_unloaded_event_count = module_unloaded_event_count_;
117 UnloadModule();
118 EXPECT_EQ(previous_module_unloaded_event_count, module_unloaded_event_count_);
119 }
OLDNEW
« no previous file with comments | « chrome/common/conflicts/module_watcher_win.cc ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698