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

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

Issue 2473783005: [Win] Create ModuleWatcher. (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 unified diff | Download patch
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 "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/observer_list_threadsafe.h"
10 #include "base/run_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace conflicts {
14
15 class ModuleWatcherTest : public testing::Test {
16 public:
17 ModuleWatcherTest()
18 : module_(nullptr),
19 module_event_count_(0),
20 module_already_loaded_event_count_(0),
21 module_loaded_event_count_(0),
22 module_unloaded_event_count_(0) {}
23
24 void OnModuleEvent(const ModuleWatcher::ModuleEvent& event) {
25 module_event_count_++;
26 switch (event.event_type) {
27 case ModuleWatcher::MODULE_ALREADY_LOADED:
28 module_already_loaded_event_count_++;
29 break;
30 case ModuleWatcher::MODULE_LOADED:
31 module_loaded_event_count_++;
32 break;
33 case ModuleWatcher::MODULE_UNLOADED:
34 module_unloaded_event_count_++;
35 break;
36 }
37 }
38
39 void SetUp() override {
40 message_loop_.reset(new base::MessageLoopForUI());
41 observer_ = mw_.RegisterCallback(
42 base::Bind(&ModuleWatcherTest::OnModuleEvent, base::Unretained(this)));
43 }
44
45 void TearDown() override {
46 observer_.reset();
47 message_loop_.reset();
48 UnloadModule();
49 }
50
51 void RunLoopUntilIdle() { base::RunLoop().RunUntilIdle(); }
52
53 void AssertNoObservers() { mw_.observer_list_->AssertEmpty(); }
54
55 void LoadModule() {
56 if (module_)
57 return;
58 // This module should not be a static dependency of the unit-test
59 // executable, but should be a build-system dependency or a module that is
60 // present on any Windows machine.
61 static const wchar_t kModuleName[] = L"chrome.dll";
62 // The module should not already be loaded.
63 ASSERT_FALSE(::GetModuleHandle(kModuleName));
64 // It should have loaded successfully.
65 module_ = ::LoadLibrary(kModuleName);
66 ASSERT_TRUE(module_);
67 }
68
69 void UnloadModule() {
70 if (!module_)
71 return;
72 ::FreeLibrary(module_);
73 module_ = nullptr;
74 }
75
76 // The module watcher used by the test.
77 ModuleWatcher mw_;
78
79 // The observer object.
80 std::unique_ptr<ModuleWatcher::Observer> observer_;
81
82 // Holds a handle to a loaded module.
83 HMODULE module_;
84
85 // Total number of module events seen.
86 size_t module_event_count_;
87 // Total number of MODULE_ALREADY_LOADED events seen.
88 size_t module_already_loaded_event_count_;
89 // Total number of MODULE_LOADED events seen.
90 size_t module_loaded_event_count_;
91 // Total number of MODULE_UNLOADED events seen.
92 size_t module_unloaded_event_count_;
93
94 private:
95 std::unique_ptr<base::MessageLoopForUI> message_loop_;
96
97 DISALLOW_COPY_AND_ASSIGN(ModuleWatcherTest);
98 };
99
100 TEST_F(ModuleWatcherTest, ModuleEvents) {
101 EXPECT_FALSE(mw_.IsRunning());
102 EXPECT_EQ(0u, module_event_count_);
103
104 // Start the watcher and ensure it enumerates already loaded modules.
105 mw_.Start();
106 EXPECT_TRUE(mw_.IsRunning());
107 RunLoopUntilIdle();
108 EXPECT_LT(0u, module_event_count_);
109 EXPECT_LT(0u, module_already_loaded_event_count_);
110 EXPECT_EQ(0u, module_loaded_event_count_);
111 EXPECT_EQ(0u, module_unloaded_event_count_);
112
113 // Dynamically load a module and ensure it a notification is received for it.
114 LoadModule();
115 RunLoopUntilIdle();
116 EXPECT_EQ(1u, module_loaded_event_count_);
117
118 // Unload the module and ensure another notification is received.
119 UnloadModule();
120 RunLoopUntilIdle();
121 EXPECT_EQ(1u, module_unloaded_event_count_);
122
123 // Dynamically load a module and ensure it a notification is received for it.
124 LoadModule();
125 RunLoopUntilIdle();
126 EXPECT_EQ(2u, module_loaded_event_count_);
127
128 // Stop the module watcher.
129 mw_.Stop();
130
131 // Unload the module and ensure no notification is received this time.
132 UnloadModule();
133 RunLoopUntilIdle();
134 EXPECT_EQ(1u, module_unloaded_event_count_);
135
136 // Clean up the observer and ensure it was removed.
137 observer_.reset();
138 AssertNoObservers();
139 }
140
141 } // namespace conflicts
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698