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 std::ostream& operator<<(std::ostream& os, const SkISize& size) { |
| 16 return os << size.width() << "x" << size.height(); |
| 17 } |
| 18 |
| 19 namespace remoting { |
| 20 |
| 21 class MockDesktopResizer : public DesktopResizer { |
| 22 public: |
| 23 MockDesktopResizer(const SkISize& initial_size, bool exact_size_supported, |
| 24 const SkISize* supported_sizes, int num_supported_sizes) |
| 25 : initial_size_(initial_size), |
| 26 current_size_(initial_size), |
| 27 exact_size_supported_(exact_size_supported) { |
| 28 for (int i = 0; i < num_supported_sizes; ++i) { |
| 29 supported_sizes_.push_back(supported_sizes[i]); |
| 30 } |
| 31 } |
| 32 |
| 33 const SkISize& initial_size() { return initial_size_; } |
| 34 |
| 35 // remoting::DesktopResizer interface |
| 36 virtual SkISize GetCurrentSize() OVERRIDE { |
| 37 return current_size_; |
| 38 } |
| 39 virtual std::list<SkISize> GetSupportedSizes( |
| 40 const SkISize& preferred) OVERRIDE { |
| 41 std::list<SkISize> result = supported_sizes_; |
| 42 if (exact_size_supported_) { |
| 43 result.push_back(preferred); |
| 44 } |
| 45 return result; |
| 46 } |
| 47 virtual void SetSize(const SkISize& size) OVERRIDE { |
| 48 current_size_ = size; |
| 49 } |
| 50 virtual void RestoreSize(const SkISize& size) OVERRIDE { |
| 51 current_size_ = size; |
| 52 } |
| 53 |
| 54 private: |
| 55 SkISize initial_size_; |
| 56 SkISize current_size_; |
| 57 bool exact_size_supported_; |
| 58 std::list<SkISize> supported_sizes_; |
| 59 }; |
| 60 |
| 61 class ResizingHostObserverTest : public testing::Test { |
| 62 public: |
| 63 void SetDesktopResizer(MockDesktopResizer* desktop_resizer) { |
| 64 CHECK(!desktop_resizer_.get()) << "Call SetDeskopResizer once per test"; |
| 65 resizing_host_observer_.reset(new ResizingHostObserver(desktop_resizer, |
| 66 NULL)); |
| 67 desktop_resizer_.reset(desktop_resizer); |
| 68 resizing_host_observer_->OnClientAuthenticated(""); |
| 69 } |
| 70 |
| 71 SkISize GetBestSize(const SkISize& client_size) { |
| 72 resizing_host_observer_->OnClientDimensionsChanged("", client_size); |
| 73 return desktop_resizer_->GetCurrentSize(); |
| 74 } |
| 75 |
| 76 void VerifySizes(const SkISize* client_sizes, const SkISize* expected_sizes, |
| 77 int number_of_sizes) { |
| 78 for (int i = 0; i < number_of_sizes; ++i) { |
| 79 SkISize best_size = GetBestSize(client_sizes[i]); |
| 80 EXPECT_EQ(expected_sizes[i], best_size) |
| 81 << "Input size = " << client_sizes[i]; |
| 82 } |
| 83 } |
| 84 |
| 85 void Reconnect() { |
| 86 resizing_host_observer_->OnClientDisconnected(""); |
| 87 resizing_host_observer_->OnClientAuthenticated(""); |
| 88 } |
| 89 |
| 90 // testing::Test interface |
| 91 virtual void TearDown() OVERRIDE { |
| 92 resizing_host_observer_->OnClientDisconnected(""); |
| 93 EXPECT_EQ(desktop_resizer_->initial_size(), |
| 94 desktop_resizer_->GetCurrentSize()); |
| 95 } |
| 96 |
| 97 private: |
| 98 scoped_ptr<ResizingHostObserver> resizing_host_observer_; |
| 99 scoped_ptr<MockDesktopResizer> desktop_resizer_; |
| 100 }; |
| 101 |
| 102 // Check that the host is not resized if it reports an initial size of zero |
| 103 // (even if it GetSupportedSizes does not return an empty list). |
| 104 TEST_F(ResizingHostObserverTest, ZeroGetCurrentSize) { |
| 105 SkISize zero = { 0, 0 }; |
| 106 SetDesktopResizer( |
| 107 new MockDesktopResizer(zero, true, NULL, 0)); |
| 108 SkISize client_sizes[] = { { 200, 100 }, { 100, 200 } }; |
| 109 SkISize expected_sizes[] = { zero, zero }; |
| 110 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); |
| 111 } |
| 112 |
| 113 // Check that the host is not resized if GetSupportedSizes returns an empty |
| 114 // list (even if GetCurrentSize is supported). |
| 115 TEST_F(ResizingHostObserverTest, EmptyGetSupportedSizes) { |
| 116 SkISize initial = { 640, 480 }; |
| 117 SetDesktopResizer( |
| 118 new MockDesktopResizer(initial, false, NULL, 0)); |
| 119 SkISize client_sizes[] = { { 200, 100 }, { 100, 200 } }; |
| 120 SkISize expected_sizes[] = { initial, initial }; |
| 121 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); |
| 122 } |
| 123 |
| 124 // Check that if the implementation supports exact size matching, it is used. |
| 125 TEST_F(ResizingHostObserverTest, SelectExactSize) { |
| 126 SetDesktopResizer( |
| 127 new MockDesktopResizer(SkISize::Make(640, 480), true, NULL, 0)); |
| 128 SkISize client_sizes[] = { { 200, 100 }, { 100, 200 } , { 640, 480 }, |
| 129 { 480, 640 }, { 1280, 1024 } }; |
| 130 VerifySizes(client_sizes, client_sizes, arraysize(client_sizes)); |
| 131 } |
| 132 |
| 133 // Check that if the implementation supports a size that is no larger than |
| 134 // the requested size, then the largest such size is used. |
| 135 TEST_F(ResizingHostObserverTest, SelectBestSmallerSize) { |
| 136 SkISize supported_sizes[] = { |
| 137 SkISize::Make(639, 479), SkISize::Make(640, 480) }; |
| 138 SetDesktopResizer( |
| 139 new MockDesktopResizer(SkISize::Make(640, 480), false, |
| 140 supported_sizes, arraysize(supported_sizes))); |
| 141 SkISize client_sizes[] = { { 639, 479 }, { 640, 480 }, { 641, 481 }, |
| 142 { 999, 999 } }; |
| 143 SkISize expected_sizes[] = { supported_sizes[0], supported_sizes[1], |
| 144 supported_sizes[1], supported_sizes[1] }; |
| 145 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); |
| 146 } |
| 147 |
| 148 // Check that if the implementation supports only sizes that are larger than |
| 149 // the requested size, then the one that requires the least down-scaling. |
| 150 TEST_F(ResizingHostObserverTest, SelectBestScaleFactor) { |
| 151 SkISize supported_sizes[] = { |
| 152 SkISize::Make(100, 100), SkISize::Make(200, 100) }; |
| 153 SetDesktopResizer( |
| 154 new MockDesktopResizer(SkISize::Make(200, 100), false, |
| 155 supported_sizes, arraysize(supported_sizes))); |
| 156 SkISize client_sizes[] = { { 1, 1 }, { 99, 99 }, { 199, 99 } }; |
| 157 SkISize expected_sizes[] = { supported_sizes[0], supported_sizes[0], |
| 158 supported_sizes[1] }; |
| 159 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); |
| 160 } |
| 161 |
| 162 // Check that if the implementation supports two sizes that have the same |
| 163 // resultant scale factor, then the widest one is selected. |
| 164 TEST_F(ResizingHostObserverTest, SelectWidest) { |
| 165 SkISize supported_sizes[] = { |
| 166 SkISize::Make(640, 480), SkISize::Make(480, 640) }; |
| 167 SetDesktopResizer( |
| 168 new MockDesktopResizer(SkISize::Make(480, 640), false, |
| 169 supported_sizes, arraysize(supported_sizes))); |
| 170 SkISize client_sizes[] = { { 100, 100 }, { 480, 480 }, { 500, 500 }, |
| 171 { 640, 640 }, { 1000, 1000 } }; |
| 172 SkISize expected_sizes[] = { supported_sizes[0], supported_sizes[0], |
| 173 supported_sizes[0], supported_sizes[0], |
| 174 supported_sizes[0] }; |
| 175 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); |
| 176 } |
| 177 |
| 178 // Check that resize-to-client is disabled if the size is changed explicitly. |
| 179 TEST_F(ResizingHostObserverTest, ManualResize) { |
| 180 MockDesktopResizer* desktop_resizer = |
| 181 new MockDesktopResizer(SkISize::Make(640, 480), true, NULL, 0); |
| 182 SetDesktopResizer(desktop_resizer); |
| 183 SkISize client_sizes[] = { { 1, 1 }, { 2, 2 } , { 3, 3 } }; |
| 184 VerifySizes(client_sizes, client_sizes, arraysize(client_sizes)); |
| 185 SkISize explicit_size = SkISize::Make(640, 480); |
| 186 desktop_resizer->SetSize(explicit_size); |
| 187 SkISize expected_sizes[] = { explicit_size, explicit_size, explicit_size }; |
| 188 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); |
| 189 // Make sure this behaviour doesn't persist across reconnect. |
| 190 Reconnect(); |
| 191 VerifySizes(client_sizes, client_sizes, arraysize(client_sizes)); |
| 192 } |
| 193 |
| 194 } // namespace remoting |
OLD | NEW |