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

Side by Side Diff: ui/views/widget/hwnd_subclass_unittest.cc

Issue 10256008: Adds a class that makes it easy to subclass a HWND and filter messages sent to it. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 7 months 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 | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "base/basictypes.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "ui/base/win/window_impl.h"
8 #include "ui/views/widget/hwnd_subclass.h"
9
10 namespace views {
11
12 typedef testing::Test HWNDSubclassTest;
13
14 namespace {
15
16 class TestWindow : public ui::WindowImpl {
17 public:
18 TestWindow() : saw_message(false) {}
19 virtual ~TestWindow() {}
20
21 bool saw_message;
22
23 private:
24 // Overridden from ui::WindowImpl:
25 virtual BOOL ProcessWindowMessage(HWND window,
26 UINT message,
27 WPARAM w_param,
28 LPARAM l_param,
29 LRESULT& result,
30 DWORD msg_map_id) OVERRIDE {
31 if (message == WM_NCHITTEST)
32 saw_message = true;
33
34 return FALSE; // Results in DefWindowProc().
35 }
36
37 DISALLOW_COPY_AND_ASSIGN(TestWindow);
38 };
39
40 class TestMessageFilter : public HWNDSubclass {
41 public:
42 TestMessageFilter(HWND target)
43 : HWNDSubclass(target),
44 consume_messages(false),
45 saw_message(false) {}
46 virtual ~TestMessageFilter() {}
47
48 // Setting to true causes the filter subclass to stop messages from reaching
49 // the subclassed window procedure.
50 bool consume_messages;
51
52 // True if the message filter saw the message.
53 bool saw_message;
54
55 private:
56 // Overridden from HWNDSubclass:
57 virtual bool FilterMessage(HWND hwnd,
58 UINT message,
59 WPARAM w_param,
60 LPARAM l_param,
61 LRESULT* l_result) OVERRIDE {
62 if (message == WM_NCHITTEST) {
63 saw_message = true;
64 return consume_messages;
65 }
66 return false;
67 }
68
69 DISALLOW_COPY_AND_ASSIGN(TestMessageFilter);
70 };
71
72 } // namespace
73
74 TEST_F(HWNDSubclassTest, Installation) {
75 TestWindow window;
76 window.Init(NULL, gfx::Rect(0, 0, 100, 100));
77 EXPECT_TRUE(window.hwnd() != NULL);
78
79 EXPECT_EQ(HWNDSubclass::GetCurrentWndProc(window.hwnd()),
80 HWNDSubclass::GetClassWndProc(window.hwnd()));
81
82 {
83 views::HWNDSubclass subclasser(window.hwnd());
84
85 EXPECT_NE(HWNDSubclass::GetCurrentWndProc(window.hwnd()),
86 HWNDSubclass::GetClassWndProc(window.hwnd()));
87 }
88
89 EXPECT_EQ(HWNDSubclass::GetCurrentWndProc(window.hwnd()),
90 HWNDSubclass::GetClassWndProc(window.hwnd()));
91 }
92
93 TEST_F(HWNDSubclassTest, Filtering) {
94 TestWindow window;
95 window.Init(NULL, gfx::Rect(0, 0, 100, 100));
96 EXPECT_TRUE(window.hwnd() != NULL);
97
98 {
99 TestMessageFilter mf(window.hwnd());
100
101 // We are not filtering, so both the filter and the window should receive
102 // this message:
103 ::SendMessage(window.hwnd(), WM_NCHITTEST, 0, 0);
104
105 EXPECT_TRUE(mf.saw_message);
106 EXPECT_TRUE(window.saw_message);
107
108 mf.saw_message = false;
109 window.saw_message = false;
110
111 mf.consume_messages = true;
112
113 // We are now filtering, so only the filter should see this message:
114 ::SendMessage(window.hwnd(), WM_NCHITTEST, 0, 0);
115
116 EXPECT_TRUE(mf.saw_message);
117 EXPECT_FALSE(window.saw_message);
118 }
119 }
120
121 } // namespace views
OLDNEW
« ui/views/widget/hwnd_subclass.cc ('K') | « ui/views/widget/hwnd_subclass.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698