Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Unified Diff: ui/v2/src/view_unittest.cc

Issue 25757007: V2: Implement reparenting, bounds and visibility change notifications, and write more tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/v2/src/view_unittest.cc
diff --git a/ui/v2/src/view_unittest.cc b/ui/v2/src/view_unittest.cc
index 73a3837d6b1db17d01ef7e1d727b582c0cab6e55..fa93ca594f6fa4234cc93e9f9760a8d47bcc5970 100644
--- a/ui/v2/src/view_unittest.cc
+++ b/ui/v2/src/view_unittest.cc
@@ -29,6 +29,17 @@ TEST_F(ViewTest, RemoveChild) {
EXPECT_EQ(0U, v1.children().size());
}
+TEST_F(ViewTest, Reparent) {
+ View v1;
+ View v2;
+ View* v11 = new View;
+ v1.AddChild(v11);
+ EXPECT_EQ(1U, v1.children().size());
+ v2.AddChild(v11);
+ EXPECT_EQ(1U, v2.children().size());
+ EXPECT_EQ(0U, v1.children().size());
+}
+
TEST_F(ViewTest, Contains) {
View v1;
@@ -122,6 +133,7 @@ class TreeChangeObserver : public ViewObserver {
DISALLOW_COPY_AND_ASSIGN(TreeChangeObserver);
};
+// Adds/Removes v11 to v1.
TEST_F(ViewObserverTest, TreeChange_SimpleAddRemove) {
View v1;
TreeChangeObserver o1(&v1);
@@ -178,6 +190,13 @@ TEST_F(ViewObserverTest, TreeChange_SimpleAddRemove) {
EXPECT_TRUE(TreeChangeParamsMatch(p11, o11.received_params().back()));
}
+// Creates these two trees:
+// v1
+// +- v11
+// v111
+// +- v1111
+// +- v1112
+// Then adds/removes v111 from v11.
TEST_F(ViewObserverTest, TreeChange_NestedAddRemove) {
View v1, v11, v111, v1111, v1112;
@@ -238,6 +257,191 @@ TEST_F(ViewObserverTest, TreeChange_NestedAddRemove) {
EXPECT_TRUE(TreeChangeParamsMatch(p1112, o1112.received_params().back()));
// Remove.
+ o1.Reset();
+ o11.Reset();
+ o111.Reset();
+ o1111.Reset();
+ o1112.Reset();
+ EXPECT_TRUE(o1.received_params().empty());
+ EXPECT_TRUE(o11.received_params().empty());
+ EXPECT_TRUE(o111.received_params().empty());
+ EXPECT_TRUE(o1111.received_params().empty());
+ EXPECT_TRUE(o1112.received_params().empty());
+
+ v11.RemoveChild(&v111);
+
+ EXPECT_EQ(1U, o1.received_params().size());
+ p1.target = &v111;
+ p1.receiver = &v1;
+ p1.old_parent = &v11;
+ p1.new_parent = NULL;
+ p1.phase = ViewObserver::DISPOSITION_CHANGING;
+ EXPECT_TRUE(TreeChangeParamsMatch(p1, o1.received_params().back()));
+
+ EXPECT_EQ(1U, o11.received_params().size());
+ p11 = p1;
+ p11.receiver = &v11;
+ EXPECT_TRUE(TreeChangeParamsMatch(p11, o11.received_params().back()));
+
+ EXPECT_EQ(2U, o111.received_params().size());
+ p111 = p11;
+ p111.receiver = &v111;
+ p111.phase = ViewObserver::DISPOSITION_CHANGING;
+ EXPECT_TRUE(TreeChangeParamsMatch(p111, o111.received_params().front()));
+ p111.phase = ViewObserver::DISPOSITION_CHANGED;
+ EXPECT_TRUE(TreeChangeParamsMatch(p111, o111.received_params().back()));
+
+ EXPECT_EQ(2U, o1111.received_params().size());
+ p1111 = p111;
+ p1111.receiver = &v1111;
+ p1111.phase = ViewObserver::DISPOSITION_CHANGING;
+ EXPECT_TRUE(TreeChangeParamsMatch(p1111, o1111.received_params().front()));
+ p1111.phase = ViewObserver::DISPOSITION_CHANGED;
+ EXPECT_TRUE(TreeChangeParamsMatch(p1111, o1111.received_params().back()));
+
+ EXPECT_EQ(2U, o1112.received_params().size());
+ p1112 = p111;
+ p1112.receiver = &v1112;
+ p1112.phase = ViewObserver::DISPOSITION_CHANGING;
+ EXPECT_TRUE(TreeChangeParamsMatch(p1112, o1112.received_params().front()));
+ p1112.phase = ViewObserver::DISPOSITION_CHANGED;
+ EXPECT_TRUE(TreeChangeParamsMatch(p1112, o1112.received_params().back()));
+}
+
+TEST_F(ViewObserverTest, TreeChange_Reparent) {
+ View v1, v11, v12, v111;
+ v11.set_owned_by_parent(false);
+ v111.set_owned_by_parent(false);
+ v12.set_owned_by_parent(false);
+ v1.AddChild(&v11);
+ v1.AddChild(&v12);
+ v11.AddChild(&v111);
+
+ TreeChangeObserver o1(&v1), o11(&v11), o12(&v12), o111(&v111);
+
+ // Reparent.
+ v12.AddChild(&v111);
+
+ // v1 (root) should see both changing and changed notifications.
+ EXPECT_EQ(2U, o1.received_params().size());
+ ViewObserver::TreeChangeParams p1;
+ p1.target = &v111;
+ p1.receiver = &v1;
+ p1.old_parent = &v11;
+ p1.new_parent = &v12;
+ p1.phase = ViewObserver::DISPOSITION_CHANGING;
+ EXPECT_TRUE(TreeChangeParamsMatch(p1, o1.received_params().front()));
+ p1.phase = ViewObserver::DISPOSITION_CHANGED;
+ EXPECT_TRUE(TreeChangeParamsMatch(p1, o1.received_params().back()));
+
+ // v11 should see changing notifications.
+ EXPECT_EQ(1U, o11.received_params().size());
+ ViewObserver::TreeChangeParams p11;
+ p11 = p1;
+ p11.receiver = &v11;
+ p11.phase = ViewObserver::DISPOSITION_CHANGING;
+ EXPECT_TRUE(TreeChangeParamsMatch(p11, o11.received_params().back()));
+
+ // v12 should see changed notifications.
+ EXPECT_EQ(1U, o12.received_params().size());
+ ViewObserver::TreeChangeParams p12;
+ p12 = p1;
+ p12.receiver = &v12;
+ p12.phase = ViewObserver::DISPOSITION_CHANGED;
+ EXPECT_TRUE(TreeChangeParamsMatch(p12, o12.received_params().back()));
+
+ // v111 should see both changing and changed notifications.
+ EXPECT_EQ(2U, o111.received_params().size());
+ ViewObserver::TreeChangeParams p111;
+ p111 = p1;
+ p111.receiver = &v111;
+ p111.phase = ViewObserver::DISPOSITION_CHANGING;
+ EXPECT_TRUE(TreeChangeParamsMatch(p111, o111.received_params().front()));
+ p111.phase = ViewObserver::DISPOSITION_CHANGED;
+ EXPECT_TRUE(TreeChangeParamsMatch(p111, o111.received_params().back()));
+}
+
+class VisibilityObserver : public ViewObserver {
+ public:
+ typedef std::pair<ViewObserver::DispositionChangePhase, bool> LogEntry;
+ typedef std::vector<LogEntry> LogEntries;
+
+ VisibilityObserver(View* view) : view_(view) {
+ view_->AddObserver(this);
+ }
+ virtual ~VisibilityObserver() {
+ view_->RemoveObserver(this);
+ }
+
+ const LogEntries& log_entries() const { return log_entries_; }
+
+ private:
+ // Overridden from ViewObserver:
+ virtual void OnViewVisibilityChange(
+ View* view,
+ ViewObserver::DispositionChangePhase phase) OVERRIDE {
+ DCHECK_EQ(view_, view);
+ log_entries_.push_back(std::make_pair(phase, view->visible()));
+ }
+
+ View* view_;
+ LogEntries log_entries_;
+
+ DISALLOW_COPY_AND_ASSIGN(VisibilityObserver);
+};
+
+TEST_F(ViewObserverTest, VisibilityChange) {
+ View v1; // Starts out visible.
+
+ VisibilityObserver o1(&v1);
+
+ v1.SetVisible(false);
+
+ EXPECT_EQ(2U, o1.log_entries().size());
+ EXPECT_EQ(ViewObserver::DISPOSITION_CHANGING, o1.log_entries().front().first);
+ EXPECT_EQ(o1.log_entries().front().second, true);
+ EXPECT_EQ(ViewObserver::DISPOSITION_CHANGED, o1.log_entries().back().first);
+ EXPECT_EQ(o1.log_entries().back().second, false);
+}
+
+class BoundsObserver : public ViewObserver {
+ public:
+ typedef std::pair<gfx::Rect, gfx::Rect> BoundsChange;
+ typedef std::vector<BoundsChange> BoundsChanges;
+
+ explicit BoundsObserver(View* view) : view_(view) {
+ view_->AddObserver(this);
+ }
+ virtual ~BoundsObserver() {
+ view_->RemoveObserver(this);
+ }
+
+ const BoundsChanges& bounds_changes() const { return bounds_changes_; }
+
+ private:
+ virtual void OnViewBoundsChanged(View* view,
+ const gfx::Rect& old_bounds,
+ const gfx::Rect& new_bounds) OVERRIDE {
+ DCHECK_EQ(view_, view);
+ bounds_changes_.push_back(std::make_pair(old_bounds, new_bounds));
+ }
+
+ View* view_;
+ BoundsChanges bounds_changes_;
+
+ DISALLOW_COPY_AND_ASSIGN(BoundsObserver);
+};
+
+TEST_F(ViewObserverTest, BoundsChanged) {
+ View v1;
+ BoundsObserver o1(&v1);
+
+ gfx::Rect new_bounds(0, 0, 10, 10);
+
+ v1.SetBounds(new_bounds);
+ EXPECT_EQ(1U, o1.bounds_changes().size());
+ EXPECT_EQ(gfx::Rect(), o1.bounds_changes().front().first);
+ EXPECT_EQ(new_bounds, o1.bounds_changes().front().second);
}
} // namespace v2
« ui/v2/src/view_private.h ('K') | « ui/v2/src/view_private.cc ('k') | ui/v2/v2.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698