summaryrefslogtreecommitdiffstats
path: root/chrome/views/view_unittest.cc
diff options
context:
space:
mode:
authorbeng@google.com <beng@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-16 00:37:56 +0000
committerbeng@google.com <beng@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-16 00:37:56 +0000
commit82739cfe200335e53c66b36e6b467c32294f7ef0 (patch)
tree32436525bb71a6ac93124da4e1dcc4b8c0bc48ed /chrome/views/view_unittest.cc
parent0ae7d14ea97bdb4170948521144de57059eb4eeb (diff)
downloadchromium_src-82739cfe200335e53c66b36e6b467c32294f7ef0.zip
chromium_src-82739cfe200335e53c66b36e6b467c32294f7ef0.tar.gz
chromium_src-82739cfe200335e53c66b36e6b467c32294f7ef0.tar.bz2
Allow Views to support an optional hit-test mask. Make hittest use this.
Make GetViewForPoint call HitTest instead of rolling its own crude hit testing. Update custom-shaped views to use this framework instead of overriding hittest themselves. B=2273 Review URL: http://codereview.chromium.org/3051 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2255 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/views/view_unittest.cc')
-rw-r--r--chrome/views/view_unittest.cc74
1 files changed, 74 insertions, 0 deletions
diff --git a/chrome/views/view_unittest.cc b/chrome/views/view_unittest.cc
index a658132..e404102 100644
--- a/chrome/views/view_unittest.cc
+++ b/chrome/views/view_unittest.cc
@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "chrome/common/gfx/chrome_canvas.h"
+#include "chrome/common/gfx/path.h"
#include "chrome/views/background.h"
#include "chrome/views/event.h"
#include "chrome/views/root_view.h"
@@ -506,3 +507,76 @@ TEST_F(ViewTest, RemoveNotification) {
NOTIFY_VIEW_REMOVED, NotificationService::AllSources());
}
+namespace {
+class HitTestView : public ChromeViews::View {
+ public:
+ explicit HitTestView(bool has_hittest_mask)
+ : has_hittest_mask_(has_hittest_mask) {
+ }
+ virtual ~HitTestView() {}
+
+ protected:
+ // Overridden from ChromeViews::View:
+ virtual bool HasHitTestMask() const {
+ return has_hittest_mask_;
+ }
+ virtual void GetHitTestMask(gfx::Path* mask) const {
+ DCHECK(has_hittest_mask_);
+ DCHECK(mask);
+
+ SkScalar w = SkIntToScalar(GetWidth());
+ SkScalar h = SkIntToScalar(GetHeight());
+
+ // Create a triangular mask within the bounds of this View.
+ mask->moveTo(w / 2, 0);
+ mask->lineTo(w, h);
+ mask->lineTo(0, h);
+ mask->close();
+ }
+
+ private:
+ bool has_hittest_mask_;
+
+ DISALLOW_COPY_AND_ASSIGN(HitTestView);
+};
+
+POINT ConvertPointToView(ChromeViews::View* view, const POINT& p) {
+ CPoint tmp = p;
+ ChromeViews::View::ConvertPointToView(view->GetRootView(), view, &tmp);
+ return tmp;
+}
+}
+
+TEST_F(ViewTest, HitTestMasks) {
+ ChromeViews::HWNDViewContainer* window = new ChromeViews::HWNDViewContainer;
+ ChromeViews::RootView* root_view = window->GetRootView();
+ root_view->SetBounds(0, 0, 500, 500);
+
+ gfx::Rect v1_bounds = gfx::Rect(0, 0, 100, 100);
+ HitTestView* v1 = new HitTestView(false);
+ v1->SetBounds(v1_bounds.ToRECT());
+ root_view->AddChildView(v1);
+
+ gfx::Rect v2_bounds = gfx::Rect(105, 0, 100, 100);
+ HitTestView* v2 = new HitTestView(true);
+ v2->SetBounds(v2_bounds.ToRECT());
+ root_view->AddChildView(v2);
+
+ POINT v1_centerpoint = v1_bounds.CenterPoint().ToPOINT();
+ POINT v2_centerpoint = v2_bounds.CenterPoint().ToPOINT();
+ POINT v1_origin = v1_bounds.origin().ToPOINT();
+ POINT v2_origin = v2_bounds.origin().ToPOINT();
+
+ // Test HitTest
+ EXPECT_EQ(true, v1->HitTest(ConvertPointToView(v1, v1_centerpoint)));
+ EXPECT_EQ(true, v2->HitTest(ConvertPointToView(v2, v2_centerpoint)));
+
+ EXPECT_EQ(true, v1->HitTest(ConvertPointToView(v1, v1_origin)));
+ EXPECT_EQ(false, v2->HitTest(ConvertPointToView(v2, v2_origin)));
+
+ // Test GetViewForPoint
+ EXPECT_EQ(v1, root_view->GetViewForPoint(v1_centerpoint));
+ EXPECT_EQ(v2, root_view->GetViewForPoint(v2_centerpoint));
+ EXPECT_EQ(v1, root_view->GetViewForPoint(v1_origin));
+ EXPECT_EQ(root_view, root_view->GetViewForPoint(v2_origin));
+}