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

Side by Side Diff: remoting/host/resizing_host_observer_unittest.cc

Issue 2038183002: Refactor ResizingHostObserver tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 4 years, 6 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "remoting/host/resizing_host_observer.h" 5 #include "remoting/host/resizing_host_observer.h"
6 6
7 #include <list> 7 #include <list>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
(...skipping 22 matching lines...) Expand all
33 33
34 const int kDefaultDPI = 96; 34 const int kDefaultDPI = 96;
35 35
36 ScreenResolution MakeResolution(int width, int height) { 36 ScreenResolution MakeResolution(int width, int height) {
37 return ScreenResolution(webrtc::DesktopSize(width, height), 37 return ScreenResolution(webrtc::DesktopSize(width, height),
38 webrtc::DesktopVector(kDefaultDPI, kDefaultDPI)); 38 webrtc::DesktopVector(kDefaultDPI, kDefaultDPI));
39 } 39 }
40 40
41 class FakeDesktopResizer : public DesktopResizer { 41 class FakeDesktopResizer : public DesktopResizer {
42 public: 42 public:
43 FakeDesktopResizer(const ScreenResolution& initial_resolution, 43 struct CallCounts {
44 bool exact_size_supported, 44 int set_resolution = 0;
45 const ScreenResolution* supported_resolutions, 45 int restore_resolution = 0;
46 int num_supported_resolutions, 46 };
47 int* restore_resolution_call_count) 47
48 : initial_resolution_(initial_resolution), 48 FakeDesktopResizer(bool exact_size_supported,
49 current_resolution_(initial_resolution), 49 std::vector<ScreenResolution> supported_resolutions,
50 exact_size_supported_(exact_size_supported), 50 ScreenResolution* current_resolution,
51 set_resolution_call_count_(0), 51 CallCounts* call_counts)
52 restore_resolution_call_count_(restore_resolution_call_count) { 52 : exact_size_supported_(exact_size_supported),
53 for (int i = 0; i < num_supported_resolutions; ++i) { 53 initial_resolution_(*current_resolution),
54 supported_resolutions_.push_back(supported_resolutions[i]); 54 current_resolution_(current_resolution),
55 } 55 supported_resolutions_(std::move(supported_resolutions)),
56 call_counts_(call_counts) {
56 } 57 }
57 58
58 ~FakeDesktopResizer() override { 59 ~FakeDesktopResizer() override {
59 EXPECT_EQ(initial_resolution_, GetCurrentResolution()); 60 EXPECT_EQ(initial_resolution_, GetCurrentResolution());
60 } 61 }
61 62
62 int set_resolution_call_count() { return set_resolution_call_count_; }
63
64 // remoting::DesktopResizer interface 63 // remoting::DesktopResizer interface
65 ScreenResolution GetCurrentResolution() override { 64 ScreenResolution GetCurrentResolution() override {
66 return current_resolution_; 65 return *current_resolution_;
67 } 66 }
68 std::list<ScreenResolution> GetSupportedResolutions( 67 std::list<ScreenResolution> GetSupportedResolutions(
69 const ScreenResolution& preferred) override { 68 const ScreenResolution& preferred) override {
70 std::list<ScreenResolution> result = supported_resolutions_; 69 std::list<ScreenResolution> result(supported_resolutions_.begin(),
70 supported_resolutions_.end());
71 if (exact_size_supported_) { 71 if (exact_size_supported_) {
72 result.push_back(preferred); 72 result.push_back(preferred);
73 } 73 }
74 return result; 74 return result;
75 } 75 }
76 void SetResolution(const ScreenResolution& resolution) override { 76 void SetResolution(const ScreenResolution& resolution) override {
77 current_resolution_ = resolution; 77 *current_resolution_ = resolution;
78 ++set_resolution_call_count_; 78 ++call_counts_->set_resolution;
79 } 79 }
80 void RestoreResolution(const ScreenResolution& resolution) override { 80 void RestoreResolution(const ScreenResolution& resolution) override {
81 current_resolution_ = resolution; 81 *current_resolution_ = resolution;
82 if (restore_resolution_call_count_) 82 ++call_counts_->restore_resolution;
83 ++(*restore_resolution_call_count_);
84 } 83 }
85 84
86 private: 85 private:
86 bool exact_size_supported_;
87 ScreenResolution initial_resolution_; 87 ScreenResolution initial_resolution_;
88 ScreenResolution current_resolution_; 88 ScreenResolution *current_resolution_;
89 bool exact_size_supported_; 89 std::vector<ScreenResolution> supported_resolutions_;
90 std::list<ScreenResolution> supported_resolutions_; 90 CallCounts* call_counts_;
91
92 int set_resolution_call_count_;
93 int* restore_resolution_call_count_;
94 }; 91 };
95 92
96 class ResizingHostObserverTest : public testing::Test { 93 class ResizingHostObserverTest : public testing::Test {
97 public: 94 public:
98 ResizingHostObserverTest() 95 ResizingHostObserverTest()
99 : desktop_resizer_(nullptr), 96 : now_(base::Time::Now()) {
100 now_(base::Time::Now()) {
101 } 97 }
102 98
103 // This needs to be public because the derived test-case class needs to 99 // This needs to be public because the derived test-case class needs to
104 // pass it to Bind, which fails if it's protected. 100 // pass it to Bind, which fails if it's protected.
105 base::Time GetTime() { 101 base::Time GetTime() {
106 return now_; 102 return now_;
107 } 103 }
108 104
109 protected: 105 protected:
110 void SetDesktopResizer(std::unique_ptr<FakeDesktopResizer> desktop_resizer) { 106 void InitDesktopResizer(const ScreenResolution& initial_resolution,
111 CHECK(!desktop_resizer_) << "Call SetDeskopResizer once per test"; 107 bool exact_size_supported,
112 desktop_resizer_ = desktop_resizer.get(); 108 std::vector<ScreenResolution> supported_resolutions) {
113 109 current_resolution_ = initial_resolution;
114 resizing_host_observer_.reset( 110 call_counts_ = FakeDesktopResizer::CallCounts();
115 new ResizingHostObserver(std::move(desktop_resizer))); 111 resizing_host_observer_ = base::MakeUnique<ResizingHostObserver>(
112 base::MakeUnique<FakeDesktopResizer>(exact_size_supported,
113 std::move(supported_resolutions),
114 &current_resolution_,
115 &call_counts_));
116 resizing_host_observer_->SetNowFunctionForTesting( 116 resizing_host_observer_->SetNowFunctionForTesting(
117 base::Bind(&ResizingHostObserverTest::GetTimeAndIncrement, 117 base::Bind(&ResizingHostObserverTest::GetTimeAndIncrement,
118 base::Unretained(this))); 118 base::Unretained(this)));
119 } 119 }
120 120
121 ScreenResolution GetBestResolution(const ScreenResolution& client_size) { 121 ScreenResolution GetBestResolution(const ScreenResolution& client_size) {
122 resizing_host_observer_->SetScreenResolution(client_size); 122 resizing_host_observer_->SetScreenResolution(client_size);
123 return desktop_resizer_->GetCurrentResolution(); 123 return current_resolution_;
124 } 124 }
125 125
126 void VerifySizes(const ScreenResolution* client_sizes, 126 void VerifySizes(const std::vector<ScreenResolution>& client_sizes,
127 const ScreenResolution* expected_sizes, 127 const std::vector<ScreenResolution>& expected_sizes) {
128 int number_of_sizes) { 128 CHECK_EQ(client_sizes.length(), expected_sizes.length())
129 for (int i = 0; i < number_of_sizes; ++i) { 129 << "Client and expected vectors must have the same length";
rkjnsn 2016/06/06 18:26:47 Is CHECK the appropriate thing to use here?
Jamie 2016/06/06 19:12:42 See my other comment. Since a failure here indicat
130 ScreenResolution best_size = GetBestResolution(client_sizes[i]); 130 for (auto client = client_sizes.begin(), expected = expected_sizes.begin();
131 EXPECT_EQ(expected_sizes[i], best_size) 131 client != client_sizes.end() && expected != expected_sizes.end();
132 << "Input resolution = " << client_sizes[i]; 132 ++client, ++expected) {
133 ScreenResolution best_size = GetBestResolution(*client);
134 EXPECT_EQ(*expected, best_size) << "Input resolution = " << *client;
133 } 135 }
134 } 136 }
135 137
136 base::Time GetTimeAndIncrement() { 138 base::Time GetTimeAndIncrement() {
137 base::Time result = now_; 139 base::Time result = now_;
138 now_ += base::TimeDelta::FromSeconds(1); 140 now_ += base::TimeDelta::FromSeconds(1);
139 return result; 141 return result;
140 } 142 }
141 143
144 ScreenResolution current_resolution_;
145 FakeDesktopResizer::CallCounts call_counts_;
142 std::unique_ptr<ResizingHostObserver> resizing_host_observer_; 146 std::unique_ptr<ResizingHostObserver> resizing_host_observer_;
143 FakeDesktopResizer* desktop_resizer_;
144 base::Time now_; 147 base::Time now_;
145 }; 148 };
146 149
147 // Check that the resolution isn't restored if it wasn't changed by this class. 150 // Check that the resolution isn't restored if it wasn't changed by this class.
148 TEST_F(ResizingHostObserverTest, NoRestoreResolution) { 151 TEST_F(ResizingHostObserverTest, NoRestoreResolution) {
149 int restore_resolution_call_count = 0; 152 InitDesktopResizer(MakeResolution(640, 480), false, {});
150 ScreenResolution initial = MakeResolution(640, 480);
151 std::unique_ptr<FakeDesktopResizer> desktop_resizer(new FakeDesktopResizer(
152 initial, false, nullptr, 0, &restore_resolution_call_count));
153 SetDesktopResizer(std::move(desktop_resizer));
154 VerifySizes(nullptr, nullptr, 0);
155 resizing_host_observer_.reset(); 153 resizing_host_observer_.reset();
156 EXPECT_EQ(0, restore_resolution_call_count); 154 EXPECT_EQ(0, call_counts_.restore_resolution);
157 } 155 }
158 156
159 // Check that the host is not resized if GetSupportedSizes returns an empty 157 // Check that the host is not resized if GetSupportedSizes returns an empty
160 // list (even if GetCurrentSize is supported). 158 // list (even if GetCurrentSize is supported).
161 TEST_F(ResizingHostObserverTest, EmptyGetSupportedSizes) { 159 TEST_F(ResizingHostObserverTest, EmptyGetSupportedSizes) {
162 int restore_resolution_call_count = 0;
163 ScreenResolution initial = MakeResolution(640, 480); 160 ScreenResolution initial = MakeResolution(640, 480);
164 std::unique_ptr<FakeDesktopResizer> desktop_resizer(new FakeDesktopResizer( 161 InitDesktopResizer(initial, false, {});
165 initial, false, nullptr, 0, &restore_resolution_call_count)); 162 VerifySizes({MakeResolution(200, 100), MakeResolution(100, 200)},
166 SetDesktopResizer(std::move(desktop_resizer)); 163 {initial, initial});
167
168 ScreenResolution client_sizes[] = { MakeResolution(200, 100),
169 MakeResolution(100, 200) };
170 ScreenResolution expected_sizes[] = { initial, initial };
171 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes));
172
173 resizing_host_observer_.reset(); 164 resizing_host_observer_.reset();
174 EXPECT_EQ(0, restore_resolution_call_count); 165 EXPECT_EQ(0, call_counts_.set_resolution);
166 EXPECT_EQ(0, call_counts_.restore_resolution);
175 } 167 }
176 168
177 // Check that if the implementation supports exact size matching, it is used. 169 // Check that if the implementation supports exact size matching, it is used.
178 TEST_F(ResizingHostObserverTest, SelectExactSize) { 170 TEST_F(ResizingHostObserverTest, SelectExactSize) {
179 int restore_resolution_call_count = 0; 171 InitDesktopResizer(MakeResolution(640, 480), true, {});
180 std::unique_ptr<FakeDesktopResizer> desktop_resizer( 172 std::vector<ScreenResolution> client_sizes = {MakeResolution(200, 100),
181 new FakeDesktopResizer(MakeResolution(640, 480), true, nullptr, 0, 173 MakeResolution(100, 200),
182 &restore_resolution_call_count)); 174 MakeResolution(640, 480),
183 SetDesktopResizer(std::move(desktop_resizer)); 175 MakeResolution(480, 640),
184 176 MakeResolution(1280, 1024)};
185 ScreenResolution client_sizes[] = { MakeResolution(200, 100), 177 VerifySizes(client_sizes, client_sizes);
186 MakeResolution(100, 200),
187 MakeResolution(640, 480),
188 MakeResolution(480, 640),
189 MakeResolution(1280, 1024) };
190 VerifySizes(client_sizes, client_sizes, arraysize(client_sizes));
191 resizing_host_observer_.reset(); 178 resizing_host_observer_.reset();
192 EXPECT_EQ(1, restore_resolution_call_count); 179 EXPECT_EQ(1, call_counts_.restore_resolution);
193 } 180 }
194 181
195 // Check that if the implementation supports a size that is no larger than 182 // Check that if the implementation supports a size that is no larger than
196 // the requested size, then the largest such size is used. 183 // the requested size, then the largest such size is used.
197 TEST_F(ResizingHostObserverTest, SelectBestSmallerSize) { 184 TEST_F(ResizingHostObserverTest, SelectBestSmallerSize) {
198 ScreenResolution supported_sizes[] = { MakeResolution(639, 479), 185 std::vector<ScreenResolution> supported_sizes = {MakeResolution(639, 479),
199 MakeResolution(640, 480) }; 186 MakeResolution(640, 480)};
200 std::unique_ptr<FakeDesktopResizer> desktop_resizer( 187 InitDesktopResizer(MakeResolution(640, 480), false, supported_sizes);
201 new FakeDesktopResizer(MakeResolution(640, 480), false, supported_sizes, 188 VerifySizes({MakeResolution(639, 479),
202 arraysize(supported_sizes), nullptr)); 189 MakeResolution(640, 480),
203 SetDesktopResizer(std::move(desktop_resizer)); 190 MakeResolution(641, 481),
204 191 MakeResolution(999, 999)},
205 ScreenResolution client_sizes[] = { MakeResolution(639, 479), 192 {supported_sizes[0],
206 MakeResolution(640, 480), 193 supported_sizes[1],
207 MakeResolution(641, 481), 194 supported_sizes[1],
208 MakeResolution(999, 999) }; 195 supported_sizes[1]});
209 ScreenResolution expected_sizes[] = { supported_sizes[0], supported_sizes[1],
210 supported_sizes[1], supported_sizes[1] };
211 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes));
212 } 196 }
213 197
214 // Check that if the implementation supports only sizes that are larger than 198 // Check that if the implementation supports only sizes that are larger than
215 // the requested size, then the one that requires the least down-scaling. 199 // the requested size, then the one that requires the least down-scaling.
216 TEST_F(ResizingHostObserverTest, SelectBestScaleFactor) { 200 TEST_F(ResizingHostObserverTest, SelectBestScaleFactor) {
217 ScreenResolution supported_sizes[] = { MakeResolution(100, 100), 201 std::vector<ScreenResolution> supported_sizes = {MakeResolution(100, 100),
218 MakeResolution(200, 100) }; 202 MakeResolution(200, 100)};
219 std::unique_ptr<FakeDesktopResizer> desktop_resizer( 203 InitDesktopResizer(MakeResolution(200, 100), false, supported_sizes);
220 new FakeDesktopResizer(MakeResolution(200, 100), false, supported_sizes, 204 VerifySizes({MakeResolution(1, 1),
221 arraysize(supported_sizes), nullptr)); 205 MakeResolution(99, 99),
222 SetDesktopResizer(std::move(desktop_resizer)); 206 MakeResolution(199, 99)},
223 207 {supported_sizes[0],
224 ScreenResolution client_sizes[] = { MakeResolution(1, 1), 208 supported_sizes[0],
225 MakeResolution(99, 99), 209 supported_sizes[1]});
226 MakeResolution(199, 99) };
227 ScreenResolution expected_sizes[] = { supported_sizes[0], supported_sizes[0],
228 supported_sizes[1] };
229 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes));
230 } 210 }
231 211
232 // Check that if the implementation supports two sizes that have the same 212 // Check that if the implementation supports two sizes that have the same
233 // resultant scale factor, then the widest one is selected. 213 // resultant scale factor, then the widest one is selected.
234 TEST_F(ResizingHostObserverTest, SelectWidest) { 214 TEST_F(ResizingHostObserverTest, SelectWidest) {
235 ScreenResolution supported_sizes[] = { MakeResolution(640, 480), 215 std::vector<ScreenResolution> supported_sizes = {MakeResolution(640, 480),
236 MakeResolution(480, 640) }; 216 MakeResolution(480, 640)};
237 std::unique_ptr<FakeDesktopResizer> desktop_resizer( 217 InitDesktopResizer(MakeResolution(480, 640), false, supported_sizes);
238 new FakeDesktopResizer(MakeResolution(480, 640), false, supported_sizes, 218 VerifySizes({MakeResolution(100, 100),
239 arraysize(supported_sizes), nullptr)); 219 MakeResolution(480, 480),
240 SetDesktopResizer(std::move(desktop_resizer)); 220 MakeResolution(500, 500),
241 221 MakeResolution(640, 640),
242 ScreenResolution client_sizes[] = { MakeResolution(100, 100), 222 MakeResolution(1000, 1000)},
243 MakeResolution(480, 480), 223 {supported_sizes[0],
244 MakeResolution(500, 500), 224 supported_sizes[0],
245 MakeResolution(640, 640), 225 supported_sizes[0],
246 MakeResolution(1000, 1000) }; 226 supported_sizes[0],
247 ScreenResolution expected_sizes[] = { supported_sizes[0], supported_sizes[0], 227 supported_sizes[0]});
248 supported_sizes[0], supported_sizes[0],
249 supported_sizes[0] };
250 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes));
251 } 228 }
252 229
253 // Check that if the best match for the client size doesn't change, then we 230 // Check that if the best match for the client size doesn't change, then we
254 // don't call SetSize. 231 // don't call SetSize.
255 TEST_F(ResizingHostObserverTest, NoSetSizeForSameSize) { 232 TEST_F(ResizingHostObserverTest, NoSetSizeForSameSize) {
256 ScreenResolution supported_sizes[] = { MakeResolution(640, 480), 233 std::vector<ScreenResolution> supported_sizes = {MakeResolution(640, 480),
257 MakeResolution(480, 640) }; 234 MakeResolution(480, 640)};
258 SetDesktopResizer(base::WrapUnique( 235 InitDesktopResizer(MakeResolution(480, 640), false, supported_sizes);
259 new FakeDesktopResizer(MakeResolution(480, 640), false, supported_sizes, 236 VerifySizes({MakeResolution(640, 640),
260 arraysize(supported_sizes), nullptr))); 237 MakeResolution(1024, 768),
261 238 MakeResolution(640, 480)},
262 ScreenResolution client_sizes[] = { MakeResolution(640, 640), 239 {supported_sizes[0],
263 MakeResolution(1024, 768), 240 supported_sizes[0],
264 MakeResolution(640, 480) }; 241 supported_sizes[0]});
265 ScreenResolution expected_sizes[] = { MakeResolution(640, 480), 242 EXPECT_EQ(1, call_counts_.set_resolution);
266 MakeResolution(640, 480),
267 MakeResolution(640, 480) };
268 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes));
269 EXPECT_EQ(desktop_resizer_->set_resolution_call_count(), 1);
270 } 243 }
271 244
272 // Check that desktop resizes are rate-limited, and that if multiple resize 245 // Check that desktop resizes are rate-limited, and that if multiple resize
273 // requests are received in the time-out period, the most recent is respected. 246 // requests are received in the time-out period, the most recent is respected.
274 TEST_F(ResizingHostObserverTest, RateLimited) { 247 TEST_F(ResizingHostObserverTest, RateLimited) {
275 SetDesktopResizer(base::WrapUnique(new FakeDesktopResizer( 248 InitDesktopResizer(MakeResolution(640, 480), true, {});
276 MakeResolution(640, 480), true, nullptr, 0, nullptr)));
277 resizing_host_observer_->SetNowFunctionForTesting( 249 resizing_host_observer_->SetNowFunctionForTesting(
278 base::Bind(&ResizingHostObserverTest::GetTime, base::Unretained(this))); 250 base::Bind(&ResizingHostObserverTest::GetTime, base::Unretained(this)));
279 251
280 base::MessageLoop message_loop; 252 base::MessageLoop message_loop;
281 base::RunLoop run_loop; 253 base::RunLoop run_loop;
282 254
283 EXPECT_EQ(GetBestResolution(MakeResolution(100, 100)), 255 EXPECT_EQ(GetBestResolution(MakeResolution(100, 100)),
284 MakeResolution(100, 100)); 256 MakeResolution(100, 100));
285 now_ += base::TimeDelta::FromMilliseconds(900); 257 now_ += base::TimeDelta::FromMilliseconds(900);
286 EXPECT_EQ(GetBestResolution(MakeResolution(200, 200)), 258 EXPECT_EQ(GetBestResolution(MakeResolution(200, 200)),
287 MakeResolution(100, 100)); 259 MakeResolution(100, 100));
288 now_ += base::TimeDelta::FromMilliseconds(99); 260 now_ += base::TimeDelta::FromMilliseconds(99);
289 EXPECT_EQ(GetBestResolution(MakeResolution(300, 300)), 261 EXPECT_EQ(GetBestResolution(MakeResolution(300, 300)),
290 MakeResolution(100, 100)); 262 MakeResolution(100, 100));
291 now_ += base::TimeDelta::FromMilliseconds(1); 263 now_ += base::TimeDelta::FromMilliseconds(1);
292 264
293 // Due to the kMinimumResizeIntervalMs constant in resizing_host_observer.cc, 265 // Due to the kMinimumResizeIntervalMs constant in resizing_host_observer.cc,
294 // We need to wait a total of 1000ms for the final resize to be processed. 266 // We need to wait a total of 1000ms for the final resize to be processed.
295 // Since it was queued 900 + 99 ms after the first, we need to wait an 267 // Since it was queued 900 + 99 ms after the first, we need to wait an
296 // additional 1ms. However, since RunLoop is not guaranteed to process tasks 268 // additional 1ms. However, since RunLoop is not guaranteed to process tasks
297 // with the same due time in FIFO order, wait an additional 1ms for safety. 269 // with the same due time in FIFO order, wait an additional 1ms for safety.
298 message_loop.PostDelayedTask( 270 message_loop.PostDelayedTask(
299 FROM_HERE, 271 FROM_HERE,
300 run_loop.QuitClosure(), 272 run_loop.QuitClosure(),
301 base::TimeDelta::FromMilliseconds(2)); 273 base::TimeDelta::FromMilliseconds(2));
302 run_loop.Run(); 274 run_loop.Run();
303 275
304 // If the QuitClosure fired before the final resize, it's a test failure. 276 // If the QuitClosure fired before the final resize, it's a test failure.
305 EXPECT_EQ(desktop_resizer_->GetCurrentResolution(), MakeResolution(300, 300)); 277 EXPECT_EQ(current_resolution_, MakeResolution(300, 300));
306 } 278 }
307 279
308 } // namespace remoting 280 } // namespace remoting
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698