summaryrefslogtreecommitdiffstats
path: root/ui/base/win/hwnd_subclass_unittest.cc
blob: 5917ef2c101a8c1d20ba864f010da4e18eefb871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/win/hwnd_subclass.h"

#include "base/basictypes.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/win/window_impl.h"

namespace ui {

namespace {

class TestWindow : public ui::WindowImpl {
 public:
  TestWindow() : saw_message(false) {}
  virtual ~TestWindow() {}

  bool saw_message;

 private:
   // Overridden from ui::WindowImpl:
  virtual BOOL ProcessWindowMessage(HWND window,
                                    UINT message,
                                    WPARAM w_param,
                                    LPARAM l_param,
                                    LRESULT& result,
                                    DWORD msg_map_id) OVERRIDE {
    if (message == WM_NCHITTEST)
      saw_message = true;

    return FALSE;  // Results in DefWindowProc().
  }

  DISALLOW_COPY_AND_ASSIGN(TestWindow);
};

class TestMessageFilter : public HWNDMessageFilter {
 public:
  TestMessageFilter() : consume_messages(false), saw_message(false) {}
  virtual ~TestMessageFilter() {}

  // Setting to true causes the filter subclass to stop messages from reaching
  // the subclassed window procedure.
  bool consume_messages;

  // True if the message filter saw the message.
  bool saw_message;

 private:
  // Overridden from HWNDMessageFilter:
  virtual bool FilterMessage(HWND hwnd,
                             UINT message,
                             WPARAM w_param,
                             LPARAM l_param,
                             LRESULT* l_result) OVERRIDE {
    if (message == WM_NCHITTEST) {
      saw_message = true;
      return consume_messages;
    }
    return false;
  }

  DISALLOW_COPY_AND_ASSIGN(TestMessageFilter);
};

}  // namespace

TEST(HWNDSubclassTest, Filtering) {
  TestWindow window;
  window.Init(NULL, gfx::Rect(0, 0, 100, 100));
  EXPECT_TRUE(window.hwnd() != NULL);

  {
    TestMessageFilter mf;
    HWNDSubclass::AddFilterToTarget(window.hwnd(), &mf);

    // We are not filtering, so both the filter and the window should receive
    // this message:
    ::SendMessage(window.hwnd(), WM_NCHITTEST, 0, 0);

    EXPECT_TRUE(mf.saw_message);
    EXPECT_TRUE(window.saw_message);

    mf.saw_message = false;
    window.saw_message = false;

    mf.consume_messages = true;

    // We are now filtering, so only the filter should see this message:
    ::SendMessage(window.hwnd(), WM_NCHITTEST, 0, 0);

    EXPECT_TRUE(mf.saw_message);
    EXPECT_FALSE(window.saw_message);
  }
}

TEST(HWNDSubclassTest, FilteringMultipleFilters) {
  TestWindow window;
  window.Init(NULL, gfx::Rect(0, 0, 100, 100));
  EXPECT_TRUE(window.hwnd() != NULL);

  {
    TestMessageFilter mf1;
    TestMessageFilter mf2;
    HWNDSubclass::AddFilterToTarget(window.hwnd(), &mf1);
    HWNDSubclass::AddFilterToTarget(window.hwnd(), &mf2);

    // We are not filtering, so both the filter and the window should receive
    // this message:
    ::SendMessage(window.hwnd(), WM_NCHITTEST, 0, 0);

    EXPECT_TRUE(mf1.saw_message);
    EXPECT_TRUE(mf2.saw_message);
    EXPECT_TRUE(window.saw_message);

    mf1.saw_message = false;
    mf2.saw_message = false;
    window.saw_message = false;

    mf1.consume_messages = true;

    // We are now filtering, so only the filter |mf1| should see this message:
    ::SendMessage(window.hwnd(), WM_NCHITTEST, 0, 0);

    EXPECT_TRUE(mf1.saw_message);
    EXPECT_FALSE(mf2.saw_message);
    EXPECT_FALSE(window.saw_message);
  }
}

TEST(HWNDSubclassTest, RemoveFilter) {
  TestWindow window;
  window.Init(NULL, gfx::Rect(0, 0, 100, 100));
  EXPECT_TRUE(window.hwnd() != NULL);

  {
    TestMessageFilter mf1;
    TestMessageFilter mf2;
    HWNDSubclass::AddFilterToTarget(window.hwnd(), &mf1);
    HWNDSubclass::AddFilterToTarget(window.hwnd(), &mf2);

    ::SendMessage(window.hwnd(), WM_NCHITTEST, 0, 0);
    EXPECT_TRUE(mf1.saw_message);
    EXPECT_TRUE(mf2.saw_message);
    EXPECT_TRUE(window.saw_message);

    mf1.saw_message = false;
    mf2.saw_message = false;
    window.saw_message = false;

    // Remove a filter and try sending message again.
    HWNDSubclass::RemoveFilterFromAllTargets(&mf1);
    ::SendMessage(window.hwnd(), WM_NCHITTEST, 0, 0);
    EXPECT_FALSE(mf1.saw_message);
    EXPECT_TRUE(mf2.saw_message);
    EXPECT_TRUE(window.saw_message);
  }
}

}  // namespace ui