Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / chromeos / status / network_menu_icon_unittest.cc
blobb17a0f5b81741649e73f60b08ea275c56452332d
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.
5 #include "chrome/browser/chromeos/status/network_menu_icon.h"
7 #include "chrome/browser/chromeos/cros/cros_library.h"
8 #include "chrome/test/base/testing_browser_process.h"
9 #include "grit/theme_resources.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/base/resource/resource_bundle.h"
13 namespace {
15 // Compares each pixel in the 1x representation of two images for testing.
16 // TODO(pkotwicz): Compare pixels of all bitmaps contained within image.
17 bool CompareTwoImages(const gfx::ImageSkia& image_a,
18 const gfx::ImageSkia& image_b,
19 int log_level) {
20 CHECK(!image_a.isNull());
21 CHECK(!image_b.isNull());
23 gfx::ImageSkiaRep image_rep_a =
24 image_a.GetRepresentation(ui::SCALE_FACTOR_100P);
25 CHECK_EQ(ui::SCALE_FACTOR_100P, image_rep_a.scale_factor());
26 gfx::ImageSkiaRep image_rep_b =
27 image_b.GetRepresentation(ui::SCALE_FACTOR_100P);
28 CHECK_EQ(ui::SCALE_FACTOR_100P, image_rep_b.scale_factor());
30 SkBitmap a = image_rep_a.sk_bitmap();
31 SkBitmap b = image_rep_b.sk_bitmap();
33 CHECK(!a.empty());
34 CHECK(!b.empty());
35 if (a.getSize() != b.getSize()) {
36 VLOG(log_level) << "Mistmatched size: "
37 << a.getSize() << " != " << b.getSize();
38 return false;
40 size_t bytes = a.getSize();
41 SkAutoLockPixels locka(a);
42 SkAutoLockPixels lockb(b);
43 const char* pixa = static_cast<const char*>(a.getPixels());
44 const char* pixb = static_cast<const char*>(b.getPixels());
45 if (!pixa || !pixb) {
46 if (!pixa)
47 VLOG(log_level) << "getPixels() returned NULL for LHS";
48 if (!pixb)
49 VLOG(log_level) << "getPixels() returned NULL for RHS";
50 return false;
52 size_t width = a.width();
53 size_t height = a.height();
54 size_t bpp = a.bytesPerPixel();
55 if (width * height * bpp != bytes) {
56 VLOG(log_level) << "Width: " << width << " x Height: " << height
57 << " x bpp: " << bpp << " != Size: " << bytes;
58 return false;
60 for (int y=0; y<a.height(); ++y) {
61 for (int x=0; x<a.width(); ++x) {
62 for (size_t i = 0; i<bpp; ++i) {
63 if (*pixa++ != *pixb++) {
64 VLOG(log_level) << "Icon: " << width << "x" << height << "x" << bpp
65 << ", Mismatch at: " << x << "," << y << ":" << i;
66 return false;
71 return true;
74 } // namespace
76 namespace chromeos {
78 class NetworkMenuIconTest : public testing::Test {
79 protected:
80 NetworkMenuIconTest() : rb_(ResourceBundle::GetSharedInstance()) {}
82 // testing::Test implementation.
83 virtual void SetUp() OVERRIDE {
84 cros_ = CrosLibrary::Get()->GetNetworkLibrary();
85 // Ethernet connected = WIRED icon, no badges.
86 ethernet_connected_image_ = NetworkMenuIcon::GenerateImageFromComponents(
87 *rb_.GetImageSkiaNamed(IDR_STATUSBAR_WIRED),
88 NULL, NULL, NULL, NULL);
89 // Wifi connected, strength = 100% = ARCS4 icon, no badges.
90 wifi_connected_100_image_ = NetworkMenuIcon::GenerateImageFromComponents(
91 NetworkMenuIcon::GetImage(
92 NetworkMenuIcon::ARCS,
93 NetworkMenuIcon::NumImages(NetworkMenuIcon::ARCS) - 1,
94 NetworkMenuIcon::COLOR_DARK),
95 NULL, NULL, NULL, NULL);
96 // Wifi connected, strength = 50%, encrypted = ARCS2 icon + SECURE badge.
97 wifi_encrypted_50_image_ = NetworkMenuIcon::GenerateImageFromComponents(
98 NetworkMenuIcon::GetImage(NetworkMenuIcon::ARCS, 3,
99 NetworkMenuIcon::COLOR_DARK),
100 NULL, NULL, NULL,
101 rb_.GetImageSkiaNamed(IDR_STATUSBAR_NETWORK_SECURE_DARK));
102 // Wifi connecting = IDR_STATUSBAR_NETWORK_ARCS1 (faded).
103 wifi_connecting_image_ = NetworkMenuIcon::GenerateConnectingImage(
104 NetworkMenuIcon::GetImage(NetworkMenuIcon::ARCS, 1,
105 NetworkMenuIcon::COLOR_DARK));
106 // 4G connected, strength = 50% = BARS4 icon + 4G badge.
107 wimax_connected_50_image_ =
108 NetworkMenuIcon::GenerateImageFromComponents(
109 NetworkMenuIcon::GetImage(
110 NetworkMenuIcon::BARS, 3,
111 NetworkMenuIcon::COLOR_DARK),
112 rb_.GetImageSkiaNamed(IDR_STATUSBAR_NETWORK_4G_DARK), NULL, NULL, NULL);
113 // 3G connected, strength = 100% = BARS4 icon + 3G badge.
114 cellular_connected_100_image_ =
115 NetworkMenuIcon::GenerateImageFromComponents(
116 NetworkMenuIcon::GetImage(
117 NetworkMenuIcon::BARS,
118 NetworkMenuIcon::NumImages(NetworkMenuIcon::BARS) - 1,
119 NetworkMenuIcon::COLOR_DARK),
120 rb_.GetImageSkiaNamed(IDR_STATUSBAR_NETWORK_3G_DARK), NULL, NULL, NULL);
121 // 3G connected, strength = 50%, roaming = BARS2 icon + roaming & 3G badges.
122 cellular_roaming_50_image_ = NetworkMenuIcon::GenerateImageFromComponents(
123 NetworkMenuIcon::GetImage(NetworkMenuIcon::BARS, 3,
124 NetworkMenuIcon::COLOR_DARK),
125 rb_.GetImageSkiaNamed(IDR_STATUSBAR_NETWORK_3G_DARK), NULL, NULL,
126 rb_.GetImageSkiaNamed(IDR_STATUSBAR_NETWORK_ROAMING_DARK));
127 // 3G connecting = IDR_STATUSBAR_NETWORK_BARS1 (faded).
128 cellular_connecting_image_ = NetworkMenuIcon::GenerateConnectingImage(
129 NetworkMenuIcon::GetImage(NetworkMenuIcon::BARS, 1,
130 NetworkMenuIcon::COLOR_DARK));
131 // Disconnected = ARCS0 icon.
132 disconnected_image_ = NetworkMenuIcon::GenerateImageFromComponents(
133 NetworkMenuIcon::GetImage(NetworkMenuIcon::ARCS, 0,
134 NetworkMenuIcon::COLOR_DARK),
135 NULL, NULL, NULL, NULL);
138 virtual void TearDown() OVERRIDE {
141 protected:
142 void SetConnected(Network* network) {
143 Network::TestApi test_network(network);
144 test_network.SetConnected();
147 void SetConnecting(Network* network) {
148 Network::TestApi test_network(network);
149 test_network.SetConnecting();
152 void SetDisconnected(Network* network) {
153 Network::TestApi test_network(network);
154 test_network.SetDisconnected();
157 void SetActive(Network* network, bool active) {
158 if (active) {
159 cros_->SetActiveNetwork(network->type(), network->service_path());
160 } else {
161 cros_->SetActiveNetwork(network->type(), "");
165 void SetStrength(WirelessNetwork* network, int strength) {
166 WirelessNetwork::TestApi test_network(network);
167 test_network.SetStrength(strength);
170 void SetEncryption(WifiNetwork* network, ConnectionSecurity encryption) {
171 WifiNetwork::TestApi test_network(network);
172 test_network.SetEncryption(encryption);
175 void SetRoamingState(CellularNetwork* network, NetworkRoamingState roaming) {
176 CellularNetwork::TestApi test_network(network);
177 test_network.SetRoamingState(roaming);
180 bool CompareImages(const gfx::ImageSkia& icon, const gfx::ImageSkia& base) {
181 if (CompareTwoImages(icon, base, 1))
182 return true;
183 EXPECT_FALSE(CompareTwoImages(icon, ethernet_connected_image_, 2));
184 EXPECT_FALSE(CompareTwoImages(icon, wifi_connected_100_image_, 2));
185 EXPECT_FALSE(CompareTwoImages(icon, wifi_encrypted_50_image_, 2));
186 EXPECT_FALSE(CompareTwoImages(icon, wifi_connecting_image_, 2));
187 EXPECT_FALSE(CompareTwoImages(icon, cellular_connected_100_image_, 2));
188 EXPECT_FALSE(CompareTwoImages(icon, cellular_roaming_50_image_, 2));
189 EXPECT_FALSE(CompareTwoImages(icon, cellular_connecting_image_, 2));
190 EXPECT_FALSE(CompareTwoImages(icon, disconnected_image_, 2));
191 return false;
194 protected:
195 ScopedStubCrosEnabler cros_stub_;
196 NetworkLibrary* cros_;
197 ResourceBundle& rb_;
198 gfx::ImageSkia ethernet_connected_image_;
199 gfx::ImageSkia wifi_connected_100_image_;
200 gfx::ImageSkia wifi_encrypted_50_image_;
201 gfx::ImageSkia wifi_connecting_image_;
202 gfx::ImageSkia wimax_connected_50_image_;
203 gfx::ImageSkia cellular_connected_100_image_;
204 gfx::ImageSkia cellular_roaming_50_image_;
205 gfx::ImageSkia cellular_connecting_image_;
206 gfx::ImageSkia disconnected_image_;
209 // Compare icon cache results against expected results fron SetUp().
210 TEST_F(NetworkMenuIconTest, EthernetIcon) {
211 Network* network = cros_->FindNetworkByPath("eth1");
212 ASSERT_NE(static_cast<const Network*>(NULL), network);
213 SetConnected(network);
214 gfx::ImageSkia icon = NetworkMenuIcon::GetImage(network,
215 NetworkMenuIcon::COLOR_DARK);
216 EXPECT_TRUE(CompareImages(icon, ethernet_connected_image_));
219 TEST_F(NetworkMenuIconTest, WifiIcon) {
220 WifiNetwork* network = cros_->FindWifiNetworkByPath("wifi1");
221 ASSERT_NE(static_cast<const Network*>(NULL), network);
222 gfx::ImageSkia icon = NetworkMenuIcon::GetImage(network,
223 NetworkMenuIcon::COLOR_DARK);
224 EXPECT_TRUE(CompareImages(icon, wifi_connected_100_image_));
226 SetStrength(network, 50);
227 SetEncryption(network, SECURITY_RSN);
228 icon = NetworkMenuIcon::GetImage(network,
229 NetworkMenuIcon::COLOR_DARK);
230 EXPECT_TRUE(CompareImages(icon, wifi_encrypted_50_image_));
233 TEST_F(NetworkMenuIconTest, CellularIcon) {
234 CellularNetwork* network = cros_->FindCellularNetworkByPath("cellular1");
235 ASSERT_NE(static_cast<const Network*>(NULL), network);
236 SetConnected(network);
237 SetStrength(network, 100);
238 SetRoamingState(network, ROAMING_STATE_HOME);
239 gfx::ImageSkia icon = NetworkMenuIcon::GetImage(network,
240 NetworkMenuIcon::COLOR_DARK);
241 EXPECT_TRUE(CompareImages(icon, cellular_connected_100_image_));
243 SetStrength(network, 50);
244 SetRoamingState(network, ROAMING_STATE_ROAMING);
245 icon = NetworkMenuIcon::GetImage(network,
246 NetworkMenuIcon::COLOR_DARK);
247 EXPECT_TRUE(CompareImages(icon, cellular_roaming_50_image_));
250 namespace {
252 class TestNetworkMenuIcon : public NetworkMenuIcon {
253 public:
254 explicit TestNetworkMenuIcon(Mode mode)
255 : NetworkMenuIcon(&delegate_, mode),
256 animation_(0.0) {
258 virtual ~TestNetworkMenuIcon() {}
260 // NetworkMenuIcon override.
261 virtual double GetAnimation() OVERRIDE { return animation_; }
263 void set_animation(double animation) { animation_ = animation; }
265 private:
266 class Delegate : public NetworkMenuIcon::Delegate {
267 public:
268 Delegate() : changed_(0) {}
269 virtual void NetworkMenuIconChanged() OVERRIDE {
270 ++changed_;
272 int changed() const { return changed_; }
273 private:
274 int changed_;
276 Delegate delegate_;
277 double animation_;
280 } // namespace
282 // Test Network Menu status icon logic.
284 // Default relevent stub state:
285 // eth1: connected (active ethernet)
286 // wifi1: connected (active wifi)
287 // cellular1: connected (active cellular)
288 // wimax2: connected (active wimax)
289 // See network_library_unit_test.cc for more info.
291 TEST_F(NetworkMenuIconTest, StatusIconMenuMode) {
292 TestNetworkMenuIcon menu_icon(NetworkMenuIcon::MENU_MODE);
293 gfx::ImageSkia icon;
295 // Set cellular1 to connecting.
296 CellularNetwork* cellular1 = cros_->FindCellularNetworkByPath("cellular1");
297 ASSERT_NE(static_cast<const Network*>(NULL), cellular1);
298 SetRoamingState(cellular1, ROAMING_STATE_HOME); // Clear romaing state
299 SetConnecting(cellular1);
301 // For MENU_MODE, we always display the connecting icon (cellular1).
302 icon = menu_icon.GetIconAndText(NULL);
303 EXPECT_TRUE(CompareImages(icon, cellular_connecting_image_));
305 // Set cellular1 to connected; ethernet icon should be shown.
306 SetConnected(cellular1);
307 icon = menu_icon.GetIconAndText(NULL);
308 EXPECT_TRUE(CompareImages(icon, ethernet_connected_image_));
310 // Set ethernet to inactive/disconnected; wifi icon should be shown.
311 Network* eth1 = cros_->FindNetworkByPath("eth1");
312 ASSERT_NE(static_cast<const Network*>(NULL), eth1);
313 SetActive(eth1, false);
314 SetDisconnected(eth1);
315 icon = menu_icon.GetIconAndText(NULL);
316 EXPECT_TRUE(CompareImages(icon, wifi_connected_100_image_));
318 // Set all networks to disconnected; disconnected icon should be shown.
319 SetActive(cellular1, false);
320 SetDisconnected(cellular1);
321 WifiNetwork* wifi1 = cros_->FindWifiNetworkByPath("wifi1");
322 SetActive(wifi1, false);
323 SetDisconnected(wifi1);
324 WimaxNetwork* wimax2 = cros_->FindWimaxNetworkByPath("wimax2");
325 SetActive(wimax2, false);
326 SetDisconnected(wimax2);
327 icon = menu_icon.GetIconAndText(NULL);
328 EXPECT_TRUE(CompareImages(icon, disconnected_image_));
331 TEST_F(NetworkMenuIconTest, StatusIconDropdownMode) {
332 TestNetworkMenuIcon menu_icon(NetworkMenuIcon::DROPDOWN_MODE);
333 gfx::ImageSkia icon;
335 // Set wifi1 to connecting.
336 WifiNetwork* wifi1 = cros_->FindWifiNetworkByPath("wifi1");
337 ASSERT_NE(static_cast<const Network*>(NULL), wifi1);
338 SetConnecting(wifi1);
340 // For DROPDOWN_MODE, we prioritize the connected network (ethernet).
341 icon = menu_icon.GetIconAndText(NULL);
342 EXPECT_TRUE(CompareImages(icon, ethernet_connected_image_));
344 // Set ethernet to inactive/disconnected.
345 Network* ethernet = cros_->FindNetworkByPath("eth1");
346 ASSERT_NE(static_cast<const Network*>(NULL), ethernet);
347 SetActive(ethernet, false);
348 SetDisconnected(ethernet);
350 // Icon should now be cellular connected icon.
351 icon = menu_icon.GetIconAndText(NULL);
352 EXPECT_TRUE(CompareImages(icon, cellular_connected_100_image_));
354 // Set cellular1 to disconnected; Icon should now be wimax icon.
355 CellularNetwork* cellular1 = cros_->FindCellularNetworkByPath("cellular1");
356 ASSERT_NE(static_cast<const Network*>(NULL), cellular1);
357 SetDisconnected(cellular1);
358 icon = menu_icon.GetIconAndText(NULL);
359 EXPECT_TRUE(CompareImages(icon, wimax_connected_50_image_));
361 // Set wifi1 to connected. Icon should now be wifi connected icon.
362 SetConnected(wifi1);
363 icon = menu_icon.GetIconAndText(NULL);
364 EXPECT_TRUE(CompareImages(icon, wifi_connected_100_image_));
367 } // namespace chromeos