message_window_unittest.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2013 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. #include "base/win/message_window.h"
  5. #include <windows.h>
  6. #include "base/bind.h"
  7. #include "base/guid.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "testing/gmock/include/gmock/gmock.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. // To avoid conflicts with the macro from the Windows SDK...
  12. #undef FindWindow
  13. namespace base {
  14. namespace {
  15. bool HandleMessage(UINT message,
  16. WPARAM wparam,
  17. LPARAM lparam,
  18. LRESULT* result) {
  19. // Return |wparam| as the result of WM_USER message.
  20. if (message == WM_USER) {
  21. *result = wparam;
  22. return true;
  23. }
  24. return false;
  25. }
  26. } // namespace
  27. // Checks that a window can be created.
  28. TEST(MessageWindowTest, Create) {
  29. win::MessageWindow window;
  30. EXPECT_TRUE(window.Create(base::BindRepeating(&HandleMessage)));
  31. }
  32. // Checks that a named window can be created.
  33. TEST(MessageWindowTest, CreateNamed) {
  34. win::MessageWindow window;
  35. EXPECT_TRUE(window.CreateNamed(base::BindRepeating(&HandleMessage),
  36. UTF8ToWide("test_message_window")));
  37. }
  38. // Verifies that the created window can receive messages.
  39. TEST(MessageWindowTest, SendMessage) {
  40. win::MessageWindow window;
  41. EXPECT_TRUE(window.Create(base::BindRepeating(&HandleMessage)));
  42. EXPECT_EQ(SendMessage(window.hwnd(), WM_USER, 100, 0), 100);
  43. }
  44. // Verifies that a named window can be found by name.
  45. TEST(MessageWindowTest, FindWindow) {
  46. std::wstring name = UTF8ToWide(base::GenerateGUID());
  47. win::MessageWindow window;
  48. EXPECT_TRUE(window.CreateNamed(base::BindRepeating(&HandleMessage), name));
  49. HWND hwnd = win::MessageWindow::FindWindow(name);
  50. EXPECT_TRUE(hwnd != nullptr);
  51. EXPECT_EQ(SendMessage(hwnd, WM_USER, 200, 0), 200);
  52. }
  53. } // namespace base