OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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. |
| 4 |
| 5 #include "remoting/host/resizing_host_observer.h" |
| 6 #include "remoting/host/desktop_resizer.h" |
| 7 |
| 8 #include <list> |
| 9 |
| 10 #include "base/compiler_specific.h" |
| 11 #include "base/logging.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/skia/include/core/SkSize.h" |
| 14 |
| 15 namespace { |
| 16 std::ostream& operator<<(std::ostream& os, const SkISize& size) { |
| 17 return os << size.width() << "x" << size.height(); |
| 18 } |
| 19 } // namespace |
| 20 |
| 21 namespace remoting { |
| 22 |
| 23 class MockDesktopResizer : public DesktopResizer { |
| 24 public: |
| 25 MockDesktopResizer(const SkISize& initial_size, bool exact_size_supported, |
| 26 const SkISize* supported_sizes, int num_supported_sizes) |
| 27 : initial_size_(initial_size), |
| 28 current_size_(initial_size), |
| 29 exact_size_supported_(exact_size_supported) { |
| 30 for (int i = 0; i < num_supported_sizes; ++i) { |
| 31 supported_sizes_.push_back(supported_sizes[i]); |
| 32 } |
| 33 } |
| 34 |
| 35 const SkISize& initial_size() { return initial_size_; } |
| 36 |
| 37 // remoting::DesktopResizer interface |
| 38 virtual SkISize GetCurrentSize() OVERRIDE { |
| 39 return current_size_; |
| 40 } |
| 41 virtual std::list<SkISize> GetSupportedSizes( |
| 42 const SkISize& preferred) OVERRIDE { |
| 43 std::list<SkISize> result = supported_sizes_; |
| 44 if (exact_size_supported_) { |
| 45 result.push_back(preferred); |
| 46 } |
| 47 return result; |
| 48 } |
| 49 virtual void SetSize(const SkISize& size) OVERRIDE { |
| 50 current_size_ = size; |
| 51 } |
| 52 |
| 53 private: |
| 54 SkISize initial_size_; |
| 55 SkISize current_size_; |
| 56 bool exact_size_supported_; |
| 57 std::list<SkISize> supported_sizes_; |
| 58 }; |
| 59 |
| 60 class ResizingHostObserverTest : public testing::Test { |
| 61 public: |
| 62 void SetDesktopResizer(MockDesktopResizer* desktop_resizer) { |
| 63 CHECK(!desktop_resizer_.get()) << "Call SetDeskopResizer once per test"; |
| 64 resizing_host_observer_.reset(new ResizingHostObserver(desktop_resizer)); |
| 65 desktop_resizer_.reset(desktop_resizer); |
| 66 resizing_host_observer_->OnClientAuthenticated(""); |
| 67 } |
| 68 |
| 69 SkISize GetBestSize(const SkISize& client_size) { |
| 70 resizing_host_observer_->OnClientDimensionsChanged("", client_size); |
| 71 return desktop_resizer_->GetCurrentSize(); |
| 72 } |
| 73 |
| 74 void VerifySizes(const SkISize* client_sizes, const SkISize* expected_sizes, |
| 75 int number_of_sizes) { |
| 76 for (int i = 0; i < number_of_sizes; ++i) { |
| 77 SkISize best_size = GetBestSize(client_sizes[i]); |
| 78 EXPECT_EQ(expected_sizes[i], best_size) |
| 79 << "Input size = " << client_sizes[i]; |
| 80 } |
| 81 } |
| 82 |
| 83 // testing::Test interface |
| 84 virtual void TearDown() OVERRIDE { |
| 85 resizing_host_observer_->OnClientDisconnected(""); |
| 86 EXPECT_EQ(desktop_resizer_->initial_size(), |
| 87 desktop_resizer_->GetCurrentSize()); |
| 88 } |
| 89 |
| 90 private: |
| 91 scoped_ptr<ResizingHostObserver> resizing_host_observer_; |
| 92 scoped_ptr<MockDesktopResizer> desktop_resizer_; |
| 93 }; |
| 94 |
| 95 // Check that if the implementation supports exact size matching, it is used. |
| 96 TEST_F(ResizingHostObserverTest, SelectExactSize) { |
| 97 SetDesktopResizer( |
| 98 new MockDesktopResizer(SkISize::Make(640, 480), true, NULL, 0)); |
| 99 SkISize client_sizes[] = { { 200, 100 }, { 100, 200} , { 640, 480 }, |
| 100 { 480, 640 }, { 1280, 1024 } }; |
| 101 VerifySizes(client_sizes, client_sizes, arraysize(client_sizes)); |
| 102 } |
| 103 |
| 104 // Check that if the implementation supports a size that is no larger than |
| 105 // the requested size, then the largest such size is used. |
| 106 TEST_F(ResizingHostObserverTest, SelectBestSmallerSize) { |
| 107 SkISize supported_sizes[] = { |
| 108 SkISize::Make(639, 479), SkISize::Make(640, 480) }; |
| 109 SetDesktopResizer( |
| 110 new MockDesktopResizer(SkISize::Make(640, 480), false, |
| 111 supported_sizes, arraysize(supported_sizes))); |
| 112 SkISize client_sizes[] = { { 639, 479 }, { 640, 480 }, { 641, 481 }, |
| 113 { 999, 999 } }; |
| 114 SkISize expected_sizes[] = { supported_sizes[0], supported_sizes[1], |
| 115 supported_sizes[1], supported_sizes[1] }; |
| 116 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); |
| 117 } |
| 118 |
| 119 // Check that if the implementation supports only sizes that are larger than |
| 120 // the requested size, then the one that requires the least down-scaling. |
| 121 TEST_F(ResizingHostObserverTest, SelectBestScaleFactor) { |
| 122 SkISize supported_sizes[] = { |
| 123 SkISize::Make(100, 100), SkISize::Make(200, 100) }; |
| 124 SetDesktopResizer( |
| 125 new MockDesktopResizer(SkISize::Make(200, 100), false, |
| 126 supported_sizes, arraysize(supported_sizes))); |
| 127 SkISize client_sizes[] = { { 1, 1 }, { 99, 99 }, { 199, 99 } }; |
| 128 SkISize expected_sizes[] = { supported_sizes[0], supported_sizes[0], |
| 129 supported_sizes[1] }; |
| 130 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); |
| 131 } |
| 132 |
| 133 // Check that if the implementation supports two sizes that have the same |
| 134 // resultant scale factor, then the widest one is selected. |
| 135 TEST_F(ResizingHostObserverTest, SelectWidest) { |
| 136 SkISize supported_sizes[] = { |
| 137 SkISize::Make(640, 480), SkISize::Make(480, 640) }; |
| 138 SetDesktopResizer( |
| 139 new MockDesktopResizer(SkISize::Make(480, 640), false, |
| 140 supported_sizes, arraysize(supported_sizes))); |
| 141 SkISize client_sizes[] = { { 100, 100 }, { 480, 480 }, { 500, 500 }, |
| 142 { 640, 640 }, { 1000, 1000 } }; |
| 143 SkISize expected_sizes[] = { supported_sizes[0], supported_sizes[0], |
| 144 supported_sizes[0], supported_sizes[0], |
| 145 supported_sizes[0] }; |
| 146 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); |
| 147 } |
| 148 |
| 149 } // namespace remoting |
OLD | NEW |