1 // Copyright (c) 2011 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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h"
7 #include "chrome/browser/ui/panels/panel_mouse_watcher.h"
8 #include "chrome/browser/ui/panels/panel_mouse_watcher_observer.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/gfx/point.h"
12 class TestMouseObserver
: public PanelMouseWatcherObserver
{
14 TestMouseObserver() : mouse_movements_(0) {}
15 // Overridden from PanelMouseWatcherObserver:
16 virtual void OnMouseMove(const gfx::Point
& mouse_position
) OVERRIDE
{
22 class PanelMouseWatcherTest
: public testing::Test
{
25 TEST_F(PanelMouseWatcherTest
, StartStopWatching
) {
26 base::MessageLoop
loop(base::MessageLoop::TYPE_UI
);
28 scoped_ptr
<PanelMouseWatcher
> watcher(PanelMouseWatcher::Create());
29 EXPECT_FALSE(watcher
->IsActive());
31 scoped_ptr
<TestMouseObserver
> user1(new TestMouseObserver());
32 scoped_ptr
<TestMouseObserver
> user2(new TestMouseObserver());
35 watcher
->NotifyMouseMovement(gfx::Point(42, 101));
36 EXPECT_EQ(0, user1
->mouse_movements_
);
37 EXPECT_EQ(0, user2
->mouse_movements_
);
39 // Only one mouse observer.
40 watcher
->AddObserver(user1
.get());
41 EXPECT_TRUE(watcher
->IsActive());
42 watcher
->NotifyMouseMovement(gfx::Point(42, 101));
43 EXPECT_GE(user1
->mouse_movements_
, 1);
44 EXPECT_EQ(0, user2
->mouse_movements_
);
45 watcher
->RemoveObserver(user1
.get());
46 EXPECT_FALSE(watcher
->IsActive());
48 // More than one mouse observer.
49 watcher
->AddObserver(user1
.get());
50 EXPECT_TRUE(watcher
->IsActive());
51 watcher
->AddObserver(user2
.get());
52 watcher
->NotifyMouseMovement(gfx::Point(101, 42));
53 EXPECT_GE(user1
->mouse_movements_
, 2);
54 EXPECT_GE(user2
->mouse_movements_
, 1);
56 // Back to one observer.
57 watcher
->RemoveObserver(user1
.get());
58 EXPECT_TRUE(watcher
->IsActive());
59 int saved_count
= user1
->mouse_movements_
;
60 watcher
->NotifyMouseMovement(gfx::Point(1, 2));
61 EXPECT_EQ(saved_count
, user1
->mouse_movements_
);
62 EXPECT_GE(user2
->mouse_movements_
, 2);
63 watcher
->RemoveObserver(user2
.get());
64 EXPECT_FALSE(watcher
->IsActive());