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 if the implementation supports exact size matching, it is used. | |
103 TEST_F(ResizingHostObserverTest, SelectExactSize) { | |
104 SetDesktopResizer( | |
105 new MockDesktopResizer(SkISize::Make(640, 480), true, NULL, 0)); | |
106 SkISize client_sizes[] = { { 200, 100 }, { 100, 200 } , { 640, 480 }, | |
107 { 480, 640 }, { 1280, 1024 } }; | |
108 VerifySizes(client_sizes, client_sizes, arraysize(client_sizes)); | |
109 } | |
110 | |
111 // Check that if the implementation supports a size that is no larger than | |
112 // the requested size, then the largest such size is used. | |
113 TEST_F(ResizingHostObserverTest, SelectBestSmallerSize) { | |
114 SkISize supported_sizes[] = { | |
115 SkISize::Make(639, 479), SkISize::Make(640, 480) }; | |
116 SetDesktopResizer( | |
117 new MockDesktopResizer(SkISize::Make(640, 480), false, | |
118 supported_sizes, arraysize(supported_sizes))); | |
119 SkISize client_sizes[] = { { 639, 479 }, { 640, 480 }, { 641, 481 }, | |
120 { 999, 999 } }; | |
121 SkISize expected_sizes[] = { supported_sizes[0], supported_sizes[1], | |
122 supported_sizes[1], supported_sizes[1] }; | |
123 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); | |
124 } | |
125 | |
126 // Check that if the implementation supports only sizes that are larger than | |
127 // the requested size, then the one that requires the least down-scaling. | |
128 TEST_F(ResizingHostObserverTest, SelectBestScaleFactor) { | |
129 SkISize supported_sizes[] = { | |
130 SkISize::Make(100, 100), SkISize::Make(200, 100) }; | |
131 SetDesktopResizer( | |
132 new MockDesktopResizer(SkISize::Make(200, 100), false, | |
133 supported_sizes, arraysize(supported_sizes))); | |
134 SkISize client_sizes[] = { { 1, 1 }, { 99, 99 }, { 199, 99 } }; | |
135 SkISize expected_sizes[] = { supported_sizes[0], supported_sizes[0], | |
136 supported_sizes[1] }; | |
137 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); | |
138 } | |
139 | |
140 // Check that if the implementation supports two sizes that have the same | |
141 // resultant scale factor, then the widest one is selected. | |
142 TEST_F(ResizingHostObserverTest, SelectWidest) { | |
143 SkISize supported_sizes[] = { | |
144 SkISize::Make(640, 480), SkISize::Make(480, 640) }; | |
145 SetDesktopResizer( | |
146 new MockDesktopResizer(SkISize::Make(480, 640), false, | |
147 supported_sizes, arraysize(supported_sizes))); | |
148 SkISize client_sizes[] = { { 100, 100 }, { 480, 480 }, { 500, 500 }, | |
149 { 640, 640 }, { 1000, 1000 } }; | |
150 SkISize expected_sizes[] = { supported_sizes[0], supported_sizes[0], | |
151 supported_sizes[0], supported_sizes[0], | |
152 supported_sizes[0] }; | |
153 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); | |
154 } | |
155 | |
156 // Check that resize-to-client is disabled if the size is changed explicitly. | |
157 TEST_F(ResizingHostObserverTest, ManualResize) { | |
Wez
2012/09/25 20:48:29
nit: Add a test for there being no sizes reported
Jamie
2012/09/25 23:20:20
Done.
| |
158 MockDesktopResizer* desktop_resizer = | |
159 new MockDesktopResizer(SkISize::Make(640, 480), true, NULL, 0); | |
160 SetDesktopResizer(desktop_resizer); | |
161 SkISize client_sizes[] = { { 1, 1 }, { 2, 2 } , { 3, 3 } }; | |
162 VerifySizes(client_sizes, client_sizes, arraysize(client_sizes)); | |
163 SkISize explicit_size = SkISize::Make(640, 480); | |
164 desktop_resizer->SetSize(explicit_size); | |
165 SkISize expected_sizes[] = { explicit_size, explicit_size, explicit_size }; | |
166 VerifySizes(client_sizes, expected_sizes, arraysize(client_sizes)); | |
167 // Make sure this behaviour doesn't persist across reconnect. | |
168 Reconnect(); | |
169 VerifySizes(client_sizes, client_sizes, arraysize(client_sizes)); | |
170 } | |
171 | |
172 } // namespace remoting | |
OLD | NEW |