15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/workspace/magnetism_matcher.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ash {
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Trivial test case verifying assertions on left edge.
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(MagnetismMatcherTest, TrivialLeft) {
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const int distance = MagnetismMatcher::kMagneticDistance;
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const gfx::Rect initial_bounds(20, 10, 50, 60);
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MagnetismMatcher matcher(initial_bounds, kAllMagnetismEdges);
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.AreEdgesObscured());
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MatchedEdge edge;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.ShouldAttach(
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   gfx::Rect(initial_bounds.x() - distance - 10,
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             initial_bounds.y() - distance - 10, 2, 3), &edge));
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.AreEdgesObscured());
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(matcher.ShouldAttach(
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  gfx::Rect(initial_bounds.x() - 2, initial_bounds.y(), 1, 1),
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  &edge));
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(MAGNETISM_EDGE_LEFT, edge.primary_edge);
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_LEADING, edge.secondary_edge);
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(matcher.ShouldAttach(
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  gfx::Rect(initial_bounds.x() - 2,
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            initial_bounds.y() + distance + 1 , 1, 1),
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  &edge));
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(MAGNETISM_EDGE_LEFT, edge.primary_edge);
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE, edge.secondary_edge);
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Trivial test case verifying assertions on bottom edge.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(MagnetismMatcherTest, TrivialBottom) {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const int distance = MagnetismMatcher::kMagneticDistance;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const gfx::Rect initial_bounds(20, 10, 50, 60);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MagnetismMatcher matcher(initial_bounds, kAllMagnetismEdges);
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.AreEdgesObscured());
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MatchedEdge edge;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.ShouldAttach(
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   gfx::Rect(initial_bounds.x() - distance - 10,
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             initial_bounds.y() - distance - 10, 2, 3), &edge));
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.AreEdgesObscured());
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(matcher.ShouldAttach(
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  gfx::Rect(initial_bounds.x() - 2,
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            initial_bounds.bottom() + 4, 10, 1), &edge));
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(MAGNETISM_EDGE_BOTTOM, edge.primary_edge);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_LEADING, edge.secondary_edge);
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(matcher.ShouldAttach(
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  gfx::Rect(initial_bounds.x() + distance + 1,
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            initial_bounds.bottom() + 4, 10, 1), &edge));
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(MAGNETISM_EDGE_BOTTOM, edge.primary_edge);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE, edge.secondary_edge);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(matcher.ShouldAttach(
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  gfx::Rect(initial_bounds.right() - 10 - 1,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            initial_bounds.bottom() + 4, 10, 1), &edge));
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(MAGNETISM_EDGE_BOTTOM, edge.primary_edge);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_TRAILING, edge.secondary_edge);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Verifies we don't match an obscured corner.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(MagnetismMatcherTest, ObscureLeading) {
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const int distance = MagnetismMatcher::kMagneticDistance;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const gfx::Rect initial_bounds(20, 10, 150, 160);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MagnetismMatcher matcher(initial_bounds, kAllMagnetismEdges);
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MatchedEdge edge;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overlap with the upper right corner.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.ShouldAttach(
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   gfx::Rect(initial_bounds.right() - distance * 2,
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             initial_bounds.y() - distance - 2,
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             distance * 3,
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             (distance + 2) * 2), &edge));
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.AreEdgesObscured());
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Verify doesn't match the following which is obscured by first.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.ShouldAttach(
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   gfx::Rect(initial_bounds.right() + 1,
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             initial_bounds.y(),
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             distance,
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             5), &edge));
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should match the following which extends into non-overlapping region.
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(matcher.ShouldAttach(
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   gfx::Rect(initial_bounds.right() + 1,
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             initial_bounds.y() + distance + 1,
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             distance,
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             15), &edge));
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(MAGNETISM_EDGE_RIGHT, edge.primary_edge);
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE, edge.secondary_edge);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Verifies obscuring one side doesn't obscure the other.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(MagnetismMatcherTest, DontObscureOtherSide) {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const int distance = MagnetismMatcher::kMagneticDistance;
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const gfx::Rect initial_bounds(20, 10, 150, 160);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MagnetismMatcher matcher(initial_bounds, kAllMagnetismEdges);
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MatchedEdge edge;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overlap with the left side.
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.ShouldAttach(
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   gfx::Rect(initial_bounds.x() - distance + 1,
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             initial_bounds.y() + 2,
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             distance * 2 + 2,
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             initial_bounds.height() + distance * 4), &edge));
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.AreEdgesObscured());
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should match the right side since it isn't obscured.
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(matcher.ShouldAttach(
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   gfx::Rect(initial_bounds.right() - 1,
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             initial_bounds.y() + distance + 1,
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             distance,
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             5), &edge));
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(MAGNETISM_EDGE_RIGHT, edge.primary_edge);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE, edge.secondary_edge);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Verifies we don't match an obscured center.
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(MagnetismMatcherTest, ObscureCenter) {
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const int distance = MagnetismMatcher::kMagneticDistance;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const gfx::Rect initial_bounds(20, 10, 150, 160);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MagnetismMatcher matcher(initial_bounds, kAllMagnetismEdges);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MatchedEdge edge;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overlap with the center bottom edge.
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.ShouldAttach(
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   gfx::Rect(100, initial_bounds.bottom() - distance - 2,
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             20,
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             (distance + 2) * 2), &edge));
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.AreEdgesObscured());
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Verify doesn't match the following which is obscured by first.
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.ShouldAttach(
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   gfx::Rect(110, initial_bounds.bottom() + 1,
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             10, 5), &edge));
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should match the following which extends into non-overlapping region.
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(matcher.ShouldAttach(
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  gfx::Rect(90,
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            initial_bounds.bottom() + 1,
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            10, 5), &edge));
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(MAGNETISM_EDGE_BOTTOM, edge.primary_edge);
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE, edge.secondary_edge);
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Verifies we don't match an obscured trailing edge.
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(MagnetismMatcherTest, ObscureTrailing) {
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const int distance = MagnetismMatcher::kMagneticDistance;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const gfx::Rect initial_bounds(20, 10, 150, 160);
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MagnetismMatcher matcher(initial_bounds, kAllMagnetismEdges);
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MatchedEdge edge;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overlap with the trailing left edge.
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.ShouldAttach(
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   gfx::Rect(initial_bounds.x() - distance - 2,
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             150,
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             (distance + 2) * 2,
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             50), &edge));
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.AreEdgesObscured());
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Verify doesn't match the following which is obscured by first.
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(matcher.ShouldAttach(
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   gfx::Rect(initial_bounds.x() - 4,
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             160, 3, 20), &edge));
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should match the following which extends into non-overlapping region.
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(matcher.ShouldAttach(
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   gfx::Rect(initial_bounds.x() - 4,
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             140, 3, 20), &edge));
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(MAGNETISM_EDGE_LEFT, edge.primary_edge);
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE, edge.secondary_edge);
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace ash
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
170