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 <list> 6 7 #include "base/compiler_specific.h" 8 #include "base/logging.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/message_loop/message_loop.h" 11 #include "base/run_loop.h" 12 #include "remoting/host/desktop_resizer.h" 13 #include "remoting/host/resizing_host_observer.h" 14 #include "remoting/host/screen_resolution.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "third_party/skia/include/core/SkSize.h" 17 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" 18 19 std::ostream& operator<<(std::ostream& os, const SkISize& size) { 20 return os << size.width() << "x" << size.height(); 21 } 22 23 const int kDefaultDPI = 96; 24 25 namespace remoting { 26 27 class FakeDesktopResizer : public DesktopResizer { 28 public: 29 FakeDesktopResizer(const SkISize& initial_size, bool exact_size_supported, 30 const SkISize* supported_sizes, int num_supported_sizes) 31 : initial_size_(initial_size), 32 current_size_(initial_size), 33 exact_size_supported_(exact_size_supported), 34 set_size_call_count_(0) { 35 for (int i = 0; i < num_supported_sizes; ++i) { 36 supported_sizes_.push_back(supported_sizes[i]); 37 } 38 } 39 40 virtual ~FakeDesktopResizer() { 41 EXPECT_EQ(initial_size_, GetCurrentSize()); 42 } 43 44 int set_size_call_count() { return set_size_call_count_; } 45 46 // remoting::DesktopResizer interface 47 virtual SkISize GetCurrentSize() OVERRIDE { 48 return current_size_; 49 } 50 virtual std::list<SkISize> GetSupportedSizes( 51 const SkISize& preferred) OVERRIDE { 52 std::list<SkISize> result = supported_sizes_; 53 if (exact_size_supported_) { 54 result.push_back(preferred); 55 } 56 return result; 57 } 58 virtual void SetSize(const SkISize& size) OVERRIDE { 59 current_size_ = size; 60 ++set_size_call_count_; 61 } 62 virtual void RestoreSize(const SkISize& size) OVERRIDE { 63 current_size_ = size; 64 } 65 66 private: 67 SkISize initial_size_; 68 SkISize current_size_; 69 bool exact_size_supported_; 70 std::list<SkISize> supported_sizes_; 71 72 int set_size_call_count_; 73 }; 74 75 class ResizingHostObserverTest : public testing::Test { 76 public: 77 ResizingHostObserverTest() 78 : desktop_resizer_(NULL), 79 now_(base::Time::Now()) { 80 } 81 82 // This needs to be public because the derived test-case class needs to 83 // pass it to Bind, which fails if it's protected. 84 base::Time GetTime() { 85 return now_; 86 } 87 88 protected: 89 void SetDesktopResizer(scoped_ptr<FakeDesktopResizer> desktop_resizer) { 90 CHECK(!desktop_resizer_) << "Call SetDeskopResizer once per test"; 91 desktop_resizer_ = desktop_resizer.get(); 92 93 resizing_host_observer_.reset( 94 new ResizingHostObserver(desktop_resizer.PassAs<DesktopResizer>())); 95 resizing_host_observer_->SetNowFunctionForTesting( 96 base::Bind(&ResizingHostObserverTest::GetTimeAndIncrement, 97 base::Unretained(this))); 98 } 99 100 SkISize GetBestSize(const SkISize& client_size) { 101 resizing_host_observer_->SetScreenResolution(ScreenResolution( 102 webrtc::DesktopSize(client_size.width(), client_size.height()), 103 webrtc::DesktopVector(kDefaultDPI, kDefaultDPI))); 104 return desktop_resizer_->GetCurrentSize(); 105 } 106 107 void VerifySizes(const SkISize* client_sizes, const SkISize* expected_sizes, 108 int number_of_sizes) { 109 for (int i = 0; i < number_of_sizes; ++i) { 110 SkISize best_size = GetBestSize(client_sizes[i]); 111 EXPECT_EQ(expected_sizes[i], best_size) 112 << "Input size = " << client_sizes[i]; 113 } 114 } 115 116 base::Time GetTimeAndIncrement() { 117 base::Time result = now_; 118 now_ += base::TimeDelta::FromSeconds(1); 119 return result; 120 } 121 122 scoped_ptr<ResizingHostObserver> resizing_host_observer_; 123 FakeDesktopResizer* desktop_resizer_; 124 base::Time now_; 125 }; 126 127 // Check that the host is not resized if GetSupportedSizes returns an empty 128 // list (even if GetCurrentSize is supported). 129 TEST_F(ResizingHostObserverTest, EmptyGetSupportedSizes) { 130 SkISize initial = { 640, 480 }; 131 scoped_ptr<FakeDesktopResizer> desktop_resizer( 132 new FakeDesktopResizer(initial, false, NULL, 0)); 133 SetDesktopResizer(desktop_resizer.Pass()); 134 135 SkISize client_sizes[] = { { 200, 100 }, { 100, 200 } }; 136 SkISize expected_sizes[] = { initial, initial }; 137 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); 138 } 139 140 // Check that if the implementation supports exact size matching, it is used. 141 TEST_F(ResizingHostObserverTest, SelectExactSize) { 142 scoped_ptr<FakeDesktopResizer> desktop_resizer( 143 new FakeDesktopResizer(SkISize::Make(640, 480), true, NULL, 0)); 144 SetDesktopResizer(desktop_resizer.Pass()); 145 146 SkISize client_sizes[] = { { 200, 100 }, { 100, 200 } , { 640, 480 }, 147 { 480, 640 }, { 1280, 1024 } }; 148 VerifySizes(client_sizes, client_sizes, arraysize(client_sizes)); 149 } 150 151 // Check that if the implementation supports a size that is no larger than 152 // the requested size, then the largest such size is used. 153 TEST_F(ResizingHostObserverTest, SelectBestSmallerSize) { 154 SkISize supported_sizes[] = { 155 SkISize::Make(639, 479), SkISize::Make(640, 480) }; 156 scoped_ptr<FakeDesktopResizer> desktop_resizer( 157 new FakeDesktopResizer(SkISize::Make(640, 480), false, 158 supported_sizes, arraysize(supported_sizes))); 159 SetDesktopResizer(desktop_resizer.Pass()); 160 161 SkISize client_sizes[] = { { 639, 479 }, { 640, 480 }, { 641, 481 }, 162 { 999, 999 } }; 163 SkISize expected_sizes[] = { supported_sizes[0], supported_sizes[1], 164 supported_sizes[1], supported_sizes[1] }; 165 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); 166 } 167 168 // Check that if the implementation supports only sizes that are larger than 169 // the requested size, then the one that requires the least down-scaling. 170 TEST_F(ResizingHostObserverTest, SelectBestScaleFactor) { 171 SkISize supported_sizes[] = { { 100, 100 }, { 200, 100 } }; 172 scoped_ptr<FakeDesktopResizer> desktop_resizer( 173 new FakeDesktopResizer(SkISize::Make(200, 100), false, 174 supported_sizes, arraysize(supported_sizes))); 175 SetDesktopResizer(desktop_resizer.Pass()); 176 177 SkISize client_sizes[] = { { 1, 1 }, { 99, 99 }, { 199, 99 } }; 178 SkISize expected_sizes[] = { supported_sizes[0], supported_sizes[0], 179 supported_sizes[1] }; 180 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); 181 } 182 183 // Check that if the implementation supports two sizes that have the same 184 // resultant scale factor, then the widest one is selected. 185 TEST_F(ResizingHostObserverTest, SelectWidest) { 186 SkISize supported_sizes[] = { { 640, 480 }, { 480, 640 } }; 187 scoped_ptr<FakeDesktopResizer> desktop_resizer( 188 new FakeDesktopResizer(SkISize::Make(480, 640), false, 189 supported_sizes, arraysize(supported_sizes))); 190 SetDesktopResizer(desktop_resizer.Pass()); 191 192 SkISize client_sizes[] = { { 100, 100 }, { 480, 480 }, { 500, 500 }, 193 { 640, 640 }, { 1000, 1000 } }; 194 SkISize expected_sizes[] = { supported_sizes[0], supported_sizes[0], 195 supported_sizes[0], supported_sizes[0], 196 supported_sizes[0] }; 197 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); 198 } 199 200 // Check that if the best match for the client size doesn't change, then we 201 // don't call SetSize. 202 TEST_F(ResizingHostObserverTest, NoSetSizeForSameSize) { 203 SkISize supported_sizes[] = { { 640, 480 }, { 480, 640 } }; 204 FakeDesktopResizer* desktop_resizer = 205 new FakeDesktopResizer(SkISize::Make(640, 480), false, 206 supported_sizes, arraysize(supported_sizes)); 207 SetDesktopResizer(scoped_ptr<FakeDesktopResizer>(desktop_resizer)); 208 209 SkISize client_sizes[] = { { 640, 640 }, { 1024, 768 }, { 640, 480 } }; 210 SkISize expected_sizes[] = { { 640, 480 }, { 640, 480 }, { 640, 480 } }; 211 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); 212 EXPECT_EQ(desktop_resizer->set_size_call_count(), 0); 213 } 214 215 // Check that desktop resizes are rate-limited, and that if multiple resize 216 // requests are received in the time-out period, the most recent is respected. 217 TEST_F(ResizingHostObserverTest, RateLimited) { 218 FakeDesktopResizer* desktop_resizer = 219 new FakeDesktopResizer(SkISize::Make(640, 480), true, NULL, 0); 220 SetDesktopResizer(scoped_ptr<FakeDesktopResizer>(desktop_resizer)); 221 resizing_host_observer_->SetNowFunctionForTesting( 222 base::Bind(&ResizingHostObserverTest::GetTime, base::Unretained(this))); 223 224 base::MessageLoop message_loop; 225 base::RunLoop run_loop; 226 227 EXPECT_EQ(GetBestSize(SkISize::Make(100, 100)), SkISize::Make(100, 100)); 228 now_ += base::TimeDelta::FromMilliseconds(900); 229 EXPECT_EQ(GetBestSize(SkISize::Make(200, 200)), SkISize::Make(100, 100)); 230 now_ += base::TimeDelta::FromMilliseconds(99); 231 EXPECT_EQ(GetBestSize(SkISize::Make(300, 300)), SkISize::Make(100, 100)); 232 now_ += base::TimeDelta::FromMilliseconds(1); 233 234 // Due to the kMinimumResizeIntervalMs constant in resizing_host_observer.cc, 235 // We need to wait a total of 1000ms for the final resize to be processed. 236 // Since it was queued 900 + 99 ms after the first, we need to wait an 237 // additional 1ms. However, since RunLoop is not guaranteed to process tasks 238 // with the same due time in FIFO order, wait an additional 1ms for safety. 239 message_loop.PostDelayedTask( 240 FROM_HERE, 241 run_loop.QuitClosure(), 242 base::TimeDelta::FromMilliseconds(2)); 243 run_loop.Run(); 244 245 // If the QuitClosure fired before the final resize, it's a test failure. 246 EXPECT_EQ(desktop_resizer_->GetCurrentSize(), SkISize::Make(300, 300)); 247 } 248 249 } // namespace remoting 250