• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #include "base/bind.h"
6 #include "base/guid.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/win/message_window.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace base {
13 
14 namespace {
15 
HandleMessage(UINT message,WPARAM wparam,LPARAM lparam,LRESULT * result)16 bool HandleMessage(
17     UINT message, WPARAM wparam, LPARAM lparam, LRESULT* result) {
18   // Return |wparam| as the result of WM_USER message.
19   if (message == WM_USER) {
20     *result = wparam;
21     return true;
22   }
23 
24   return false;
25 }
26 
27 }  // namespace
28 
29 // Checks that a window can be created.
TEST(MessageWindowTest,Create)30 TEST(MessageWindowTest, Create) {
31   win::MessageWindow window;
32   EXPECT_TRUE(window.Create(base::Bind(&HandleMessage)));
33 }
34 
35 // Checks that a named window can be created.
TEST(MessageWindowTest,CreateNamed)36 TEST(MessageWindowTest, CreateNamed) {
37   win::MessageWindow window;
38   EXPECT_TRUE(window.CreateNamed(base::Bind(&HandleMessage),
39               UTF8ToUTF16("test_message_window")));
40 }
41 
42 // Verifies that the created window can receive messages.
TEST(MessageWindowTest,SendMessage)43 TEST(MessageWindowTest, SendMessage) {
44   win::MessageWindow window;
45   EXPECT_TRUE(window.Create(base::Bind(&HandleMessage)));
46 
47   EXPECT_EQ(SendMessage(window.hwnd(), WM_USER, 100, 0), 100);
48 }
49 
50 // Verifies that a named window can be found by name.
TEST(MessageWindowTest,FindWindow)51 TEST(MessageWindowTest, FindWindow) {
52   string16 name = UTF8ToUTF16(base::GenerateGUID());
53   win::MessageWindow window;
54   EXPECT_TRUE(window.CreateNamed(base::Bind(&HandleMessage), name));
55 
56   HWND hwnd = win::MessageWindow::FindWindow(name);
57   EXPECT_TRUE(hwnd != NULL);
58   EXPECT_EQ(SendMessage(hwnd, WM_USER, 200, 0), 200);
59 }
60 
61 }  // namespace base
62