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

Unified Diff: remoting/client/desktop_viewport_unittest.cc

Issue 2843373005: [Remoting Client] DesktopViewport Implementation (Closed)
Patch Set: Fix Created 3 years, 8 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: remoting/client/desktop_viewport_unittest.cc
diff --git a/remoting/client/desktop_viewport_unittest.cc b/remoting/client/desktop_viewport_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d526487bb9e33f20785850f157a90757c5d5d91f
--- /dev/null
+++ b/remoting/client/desktop_viewport_unittest.cc
@@ -0,0 +1,254 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "remoting/client/desktop_viewport.h"
+
+#include "base/bind.h"
+#include "base/location.h"
+#include "base/strings/stringprintf.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace remoting {
+
+namespace {
+
+const float EPSILON = 0.001f;
+
+} // namespace
+
+class DesktopViewportTest : public testing::Test {
joedow 2017/04/28 21:29:53 This system is a bit different than other unit tes
Yuwei 2017/04/28 23:53:40 I just reversed the order to use the more common c
+ public:
+ void SetUp() override;
+ void TearDown() override;
+
+ protected:
+ void ExpectTransformation(const tracked_objects::Location& from_here,
joedow 2017/04/28 21:29:53 I'd rename this to SetExpectedTransform() or SetEx
Yuwei 2017/04/28 23:53:40 Renamed to AssertTransformationReceived.
+ float scale,
+ float offset_x,
+ float offset_y);
+
+ DesktopViewport viewport_;
+
+ private:
+ void OnTransformationChanged(const std::array<float, 9>& matrix);
+
+ bool expect_matrix_ = false;
+ std::array<float, 9> expected_matrix_;
joedow 2017/04/28 21:29:53 expected_transfrom_? The method above doesn't men
Yuwei 2017/04/28 23:53:40 Use |received_transformation_| now
+ tracked_objects::Location expectation_location_;
+};
+
+void DesktopViewportTest::SetUp() {
+ viewport_.RegisterOnTransformationChangedCallback(
+ base::Bind(&DesktopViewportTest::OnTransformationChanged,
+ base::Unretained(this)),
+ true);
+}
+
+void DesktopViewportTest::TearDown() {
+ ASSERT_FALSE(expect_matrix_);
+}
+
+void DesktopViewportTest::ExpectTransformation(
+ const tracked_objects::Location& from_here,
+ float scale,
+ float offset_x,
+ float offset_y) {
+ ASSERT_FALSE(expect_matrix_) << "Previous matrix has not received yet.";
+ expect_matrix_ = true;
+ SimpleMatrix expected(scale, {offset_x, offset_y});
+ expected_matrix_ = expected.ToMatrixArray();
+ expectation_location_ = from_here;
+}
+
+void DesktopViewportTest::OnTransformationChanged(
+ const std::array<float, 9>& matrix) {
+ ASSERT_TRUE(expect_matrix_);
+
+ for (int i = 0; i < 9; i++) {
+ float diff = matrix[i] - expected_matrix_[i];
+ ASSERT_TRUE(diff > -EPSILON && diff < EPSILON)
+ << "Matrix doesn't match. \n"
+ << base::StringPrintf("Expected scale: %f, offset: (%f, %f)\n",
+ expected_matrix_[0], expected_matrix_[2],
+ expected_matrix_[5])
+ << base::StringPrintf("Actual scale: %f, offset: (%f, %f)\n", matrix[0],
+ matrix[2], matrix[5])
+ << "Location: " << expectation_location_.ToString();
+ }
+
+ expect_matrix_ = false;
joedow 2017/04/28 21:29:53 If SimpleMatrix had an 'isEmpty()' method, you cou
Yuwei 2017/04/28 23:53:40 Done.
+}
+
+TEST_F(DesktopViewportTest, TestViewportInitialization1) {
+ // VP < DP. Desktop shrinks to fit.
+ // +====+------+
+ // | VP | DP |
+ // | | |
+ // +====+------+
+ ExpectTransformation(FROM_HERE, 0.5f, 0.f, 0.f);
+ viewport_.SetDesktopSize(8, 6);
+ viewport_.SetSurfaceSize(2, 3);
+}
+
+TEST_F(DesktopViewportTest, TestViewportInitialization2) {
+ // VP < DP. Desktop shrinks to fit.
+ // +-----------------+
+ // | DP |
+ // | |
+ // +=================+
+ // | VP |
+ // +=================+
+ ExpectTransformation(FROM_HERE, 0.375, 0.f, 0.f);
+ viewport_.SetDesktopSize(8, 6);
+ viewport_.SetSurfaceSize(3, 2);
+}
+
+TEST_F(DesktopViewportTest, TestViewportInitialization3) {
+ // VP < DP. Desktop shrinks to fit.
+ // +========+----+
+ // | VP | DP |
+ // +========+----+
+ ExpectTransformation(FROM_HERE, 0.333f, 0.f, 0.f);
+ viewport_.SetDesktopSize(9, 3);
+ viewport_.SetSurfaceSize(2, 1);
+}
+
+TEST_F(DesktopViewportTest, TestViewportInitialization4) {
+ // VP > DP. Desktop grows to fit.
+ // +====+------+
+ // | VP | DP |
+ // | | |
+ // +====+------+
+ ExpectTransformation(FROM_HERE, 4.f, 0.f, 0.f);
+ viewport_.SetDesktopSize(2, 1);
+ viewport_.SetSurfaceSize(3, 4);
+}
+
+TEST_F(DesktopViewportTest, TestMoveDesktop) {
+ // +====+------+
+ // | VP | DP |
+ // | | |
+ // +====+------+
+ ExpectTransformation(FROM_HERE, 0.5f, 0.f, 0.f);
+ viewport_.SetDesktopSize(8, 6);
+ viewport_.SetSurfaceSize(2, 3);
+
+ // <--- DP
+ // +------+====+
+ // | DP | VP |
+ // | | |
+ // +------+====+
+ ExpectTransformation(FROM_HERE, 0.5f, -2.f, 0.f);
+ viewport_.MoveDesktop(-2.f, 0.f);
+
+ // +====+
+ // +----| VP |
+ // | DP | |
+ // | +====+
+ // +--------+
+ // Bounces back.
+ ExpectTransformation(FROM_HERE, 0.5f, -2.f, 0.f);
+ viewport_.MoveDesktop(-1.f, 1.f);
+}
+
+TEST_F(DesktopViewportTest, TestMoveAndScaleDesktop) {
+ // Number in surface coordinate.
+ //
+ // +====+------+
+ // | VP | DP |
+ // | | | 3
+ // +====+------+
+ // 4
+ ExpectTransformation(FROM_HERE, 0.5f, 0.f, 0.f);
+ viewport_.SetDesktopSize(8, 6);
+ viewport_.SetSurfaceSize(2, 3);
+
+ // Scale at pivot point (2, 3) by 1.5x.
+ // +------------------+
+ // | |
+ // | +====+ DP | 4.5
+ // | | VP | |
+ // | | | |
+ // +---+====+---------+
+ // 2 6
+ ExpectTransformation(FROM_HERE, 0.75f, -1.f, -1.5f);
+ viewport_.ScaleDesktop(2.f, 3.f, 1.5f);
+
+ // Move VP to the top-right.
+ // +-------------+====+
+ // | | VP |
+ // | DP | |
+ // | +====+ 4.5
+ // | 2 |
+ // +------------------+
+ // 6
+ ExpectTransformation(FROM_HERE, 0.75f, -4.f, 0.f);
+ viewport_.MoveDesktop(-10000.f, 10000.f);
+
+ // Scale at (2, 0) by 0.5x.
+ // VP
+ // +====+
+ // +--+----+
+ // DP | | |
+ // +--+----+
+ // +====+
+ ExpectTransformation(FROM_HERE, 0.375, -1.f, 0.375f);
+ viewport_.ScaleDesktop(2.f, 0.f, 0.5f);
+
+ // Scale all the way down.
+ // +========+
+ // | VP |
+ // +--------+
+ // | DP |
+ // +--------+
+ // | |
+ // +========+
+ ExpectTransformation(FROM_HERE, 0.25f, 0.f, 0.75f);
+ viewport_.ScaleDesktop(20.f, 0.f, 0.0001f);
+}
+
+TEST_F(DesktopViewportTest, TestSetViewportCenter) {
+ // Numbers in desktop coordinates.
+ //
+ // +====+------+
+ // | VP | DP |
+ // | | | 6
+ // +====+------+
+ // 8
+ ExpectTransformation(FROM_HERE, 0.5f, 0.f, 0.f);
+ viewport_.SetDesktopSize(8, 6);
+ viewport_.SetSurfaceSize(2, 3);
+
+ // 1.6
+ // +==+--------+
+ // |VP|2.4 DP |
+ // +==+ | 6
+ // +-----------+
+ // 8
+ ExpectTransformation(FROM_HERE, 1.25f, 0.f, 0.f);
+ viewport_.ScaleDesktop(0.f, 0.f, 2.5f);
+
+ // Move VP to center of the desktop.
+ // +------------------+
+ // | +1.6=+ |
+ // | | VP |2.4 | 6
+ // | +====+ |
+ // +------------------+
+ // 8
+ ExpectTransformation(FROM_HERE, 1.25f, -4.f, -2.25f);
+ viewport_.SetViewportCenter(4.f, 3.f);
+
+ // Move it out of bound and bounce it back.
+ // +------------------+
+ // | |
+ // | DP |
+ // | +====+
+ // | | VP |
+ // +---------------| |
+ // +====+
+ ExpectTransformation(FROM_HERE, 1.25f, -8.f, -4.5f);
+ viewport_.SetViewportCenter(1000.f, 1000.f);
+}
+
+} // namespace remoting

Powered by Google App Engine
This is Rietveld 408576698