summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/status/network_menu_icon_unittest.cc
blob: 18a82d907cb841ab22c3a697cca08030c51fc658 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/status/network_menu_icon.h"

#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/test/base/testing_browser_process.h"
#include "grit/theme_resources.h"
#include "grit/theme_resources_standard.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_bundle.h"

namespace {

// Compares each pixel in the 1x representation of two images for testing.
// TODO(pkotwicz): Compare pixels of all bitmaps contained within image.
bool CompareTwoImages(const gfx::ImageSkia& image_a,
                      const gfx::ImageSkia& image_b,
                      int log_level) {
  CHECK(!image_a.isNull());
  CHECK(!image_b.isNull());

  gfx::ImageSkiaRep image_rep_a =
      image_a.GetRepresentation(ui::SCALE_FACTOR_100P);
  CHECK_EQ(ui::SCALE_FACTOR_100P, image_rep_a.scale_factor());
  gfx::ImageSkiaRep image_rep_b =
      image_b.GetRepresentation(ui::SCALE_FACTOR_100P);
  CHECK_EQ(ui::SCALE_FACTOR_100P, image_rep_b.scale_factor());

  SkBitmap a = image_rep_a.sk_bitmap();
  SkBitmap b = image_rep_b.sk_bitmap();

  CHECK(!a.empty());
  CHECK(!b.empty());
  if (a.getSize() != b.getSize()) {
    VLOG(log_level) << "Mistmatched size: "
                    << a.getSize() << " != " << b.getSize();
    return false;
  }
  size_t bytes = a.getSize();
  SkAutoLockPixels locka(a);
  SkAutoLockPixels lockb(b);
  const char* pixa = static_cast<const char*>(a.getPixels());
  const char* pixb = static_cast<const char*>(b.getPixels());
  if (!pixa || !pixb) {
    if (!pixa)
      VLOG(log_level) << "getPixels() returned NULL for LHS";
    if (!pixb)
      VLOG(log_level) << "getPixels() returned NULL for RHS";
    return false;
  }
  size_t width = a.width();
  size_t height = a.height();
  size_t bpp = a.bytesPerPixel();
  if (width * height * bpp != bytes) {
    VLOG(log_level) << "Width: " << width << " x Height: " << height
                    << " x bpp: " << bpp << " != Size: " << bytes;
    return false;
  }
  for (int y=0; y<a.height(); ++y) {
    for (int x=0; x<a.width(); ++x) {
      for (size_t i = 0; i<bpp; ++i) {
        if (*pixa++ != *pixb++) {
          VLOG(log_level) << "Icon: " << width << "x" << height << "x" << bpp
                          << ", Mismatch at: " << x << "," << y << ":" << i;
          return false;
        }
      }
    }
  }
  return true;
}

} // namespace

namespace chromeos {

class NetworkMenuIconTest : public testing::Test {
 protected:
  NetworkMenuIconTest() : rb_(ResourceBundle::GetSharedInstance()) {}

  // testing::Test implementation.
  virtual void SetUp() OVERRIDE {
    cros_ = CrosLibrary::Get()->GetNetworkLibrary();
    // Ethernet connected = WIRED icon, no badges.
    ethernet_connected_image_ = NetworkMenuIcon::GenerateImageFromComponents(
        *rb_.GetImageSkiaNamed(IDR_STATUSBAR_WIRED),
        NULL, NULL, NULL, NULL);
    // Ethernet disonnected = WIRED icon + DISCONNECTED badge.
    ethernet_disconnected_image_ =
        NetworkMenuIcon::GenerateImageFromComponents(
            *rb_.GetImageSkiaNamed(IDR_STATUSBAR_WIRED),
            NULL, NULL, NULL,
            rb_.GetImageSkiaNamed(IDR_STATUSBAR_NETWORK_DISCONNECTED));
    // Wifi connected, strength = 100% = ARCS4 icon, no badges.
    wifi_connected_100_image_ = NetworkMenuIcon::GenerateImageFromComponents(
        NetworkMenuIcon::GetImage(
            NetworkMenuIcon::ARCS,
            NetworkMenuIcon::NumImages(NetworkMenuIcon::ARCS) - 1,
            NetworkMenuIcon::COLOR_DARK),
        NULL, NULL, NULL, NULL);
    // Wifi connected, strength = 50%, encrypted = ARCS2 icon + SECURE badge.
    wifi_encrypted_50_image_ = NetworkMenuIcon::GenerateImageFromComponents(
        NetworkMenuIcon::GetImage(NetworkMenuIcon::ARCS, 3,
                                  NetworkMenuIcon::COLOR_DARK),
        NULL, NULL, NULL,
        rb_.GetImageSkiaNamed(IDR_STATUSBAR_NETWORK_SECURE_DARK));
    // Wifi disconnected (strength = 0%) = ARCS0 icon.
    wifi_disconnected_image_ = NetworkMenuIcon::GenerateImageFromComponents(
        NetworkMenuIcon::GetImage(NetworkMenuIcon::ARCS, 0,
                                  NetworkMenuIcon::COLOR_DARK),
        NULL, NULL, NULL, NULL);
    // Wifi connecting = IDR_STATUSBAR_NETWORK_ARCS1 (faded).
    wifi_connecting_image_ = NetworkMenuIcon::GenerateConnectingImage(
        NetworkMenuIcon::GetImage(NetworkMenuIcon::ARCS, 1,
                                  NetworkMenuIcon::COLOR_DARK));
    // 4G connected, strength = 50% = BARS4 icon + 4G badge.
    wimax_connected_50_image_ =
        NetworkMenuIcon::GenerateImageFromComponents(
            NetworkMenuIcon::GetImage(
                NetworkMenuIcon::BARS, 3,
                NetworkMenuIcon::COLOR_DARK),
        rb_.GetImageSkiaNamed(IDR_STATUSBAR_NETWORK_4G_DARK), NULL, NULL, NULL);
    // 3G connected, strength = 100% = BARS4 icon + 3G badge.
    cellular_connected_100_image_ =
        NetworkMenuIcon::GenerateImageFromComponents(
            NetworkMenuIcon::GetImage(
                NetworkMenuIcon::BARS,
                NetworkMenuIcon::NumImages(NetworkMenuIcon::BARS) - 1,
                NetworkMenuIcon::COLOR_DARK),
        rb_.GetImageSkiaNamed(IDR_STATUSBAR_NETWORK_3G_DARK), NULL, NULL, NULL);
    // 3G connected, strength = 50%, roaming = BARS2 icon + roaming & 3G badges.
    cellular_roaming_50_image_ = NetworkMenuIcon::GenerateImageFromComponents(
        NetworkMenuIcon::GetImage(NetworkMenuIcon::BARS, 3,
                                  NetworkMenuIcon::COLOR_DARK),
        rb_.GetImageSkiaNamed(IDR_STATUSBAR_NETWORK_3G_DARK), NULL, NULL,
        rb_.GetImageSkiaNamed(IDR_STATUSBAR_NETWORK_ROAMING_DARK));
    // 3G disconnected (strength = 0%) = BARS0 icon + 3G badge.
    cellular_disconnected_image_ =
        NetworkMenuIcon::GenerateImageFromComponents(
            NetworkMenuIcon::GetImage(NetworkMenuIcon::BARS, 0,
                                      NetworkMenuIcon::COLOR_DARK),
            rb_.GetImageSkiaNamed(IDR_STATUSBAR_NETWORK_3G_DARK), NULL, NULL,
            NULL);
    // 3G connecting = IDR_STATUSBAR_NETWORK_BARS1 (faded).
    cellular_connecting_image_ = NetworkMenuIcon::GenerateConnectingImage(
        NetworkMenuIcon::GetImage(NetworkMenuIcon::BARS, 1,
                                  NetworkMenuIcon::COLOR_DARK));
  }

  virtual void TearDown() OVERRIDE {
  }

 protected:
  void SetConnected(Network* network) {
    Network::TestApi test_network(network);
    test_network.SetConnected();
  }

  void SetConnecting(Network* network) {
    Network::TestApi test_network(network);
    test_network.SetConnecting();
  }

  void SetDisconnected(Network* network) {
    Network::TestApi test_network(network);
    test_network.SetDisconnected();
  }

  void SetActive(Network* network, bool active) {
    if (active) {
      cros_->SetActiveNetwork(network->type(), network->service_path());
    } else {
      cros_->SetActiveNetwork(network->type(), "");
    }
  }

  void SetStrength(WirelessNetwork* network, int strength) {
    WirelessNetwork::TestApi test_network(network);
    test_network.SetStrength(strength);
  }

  void SetEncryption(WifiNetwork* network, ConnectionSecurity encryption) {
    WifiNetwork::TestApi test_network(network);
    test_network.SetEncryption(encryption);
  }

  void SetRoamingState(CellularNetwork* network, NetworkRoamingState roaming) {
    CellularNetwork::TestApi test_network(network);
    test_network.SetRoamingState(roaming);
  }

  bool CompareImages(const gfx::ImageSkia& icon, const gfx::ImageSkia& base) {
    if (CompareTwoImages(icon, base, 1))
      return true;
    EXPECT_FALSE(CompareTwoImages(icon, ethernet_connected_image_, 2));
    EXPECT_FALSE(CompareTwoImages(icon, ethernet_disconnected_image_, 2));
    EXPECT_FALSE(CompareTwoImages(icon, wifi_connected_100_image_, 2));
    EXPECT_FALSE(CompareTwoImages(icon, wifi_encrypted_50_image_, 2));
    EXPECT_FALSE(CompareTwoImages(icon, wifi_disconnected_image_, 2));
    EXPECT_FALSE(CompareTwoImages(icon, wifi_connecting_image_, 2));
    EXPECT_FALSE(CompareTwoImages(icon, cellular_connected_100_image_, 2));
    EXPECT_FALSE(CompareTwoImages(icon, cellular_roaming_50_image_, 2));
    EXPECT_FALSE(CompareTwoImages(icon, cellular_disconnected_image_, 2));
    EXPECT_FALSE(CompareTwoImages(icon, cellular_connecting_image_, 2));
    return false;
  }

 protected:
  ScopedStubCrosEnabler cros_stub_;
  NetworkLibrary* cros_;
  ResourceBundle& rb_;
  gfx::ImageSkia ethernet_connected_image_;
  gfx::ImageSkia ethernet_disconnected_image_;
  gfx::ImageSkia wifi_connected_100_image_;
  gfx::ImageSkia wifi_encrypted_50_image_;
  gfx::ImageSkia wifi_disconnected_image_;
  gfx::ImageSkia wifi_connecting_image_;
  gfx::ImageSkia wimax_connected_50_image_;
  gfx::ImageSkia cellular_connected_100_image_;
  gfx::ImageSkia cellular_roaming_50_image_;
  gfx::ImageSkia cellular_disconnected_image_;
  gfx::ImageSkia cellular_connecting_image_;
};

// Compare icon cache results against expected results fron SetUp().
TEST_F(NetworkMenuIconTest, EthernetIcon) {
  Network* network = cros_->FindNetworkByPath("eth1");
  ASSERT_NE(static_cast<const Network*>(NULL), network);
  SetConnected(network);
  gfx::ImageSkia icon = NetworkMenuIcon::GetImage(network,
                                                  NetworkMenuIcon::COLOR_DARK);
  EXPECT_TRUE(CompareImages(icon, ethernet_connected_image_));

  SetDisconnected(network);
  icon = NetworkMenuIcon::GetImage(network,
                                   NetworkMenuIcon::COLOR_DARK);
  EXPECT_TRUE(CompareImages(icon, ethernet_disconnected_image_));
}

TEST_F(NetworkMenuIconTest, WifiIcon) {
  WifiNetwork* network = cros_->FindWifiNetworkByPath("wifi1");
  ASSERT_NE(static_cast<const Network*>(NULL), network);
  gfx::ImageSkia icon = NetworkMenuIcon::GetImage(network,
                                                  NetworkMenuIcon::COLOR_DARK);
  EXPECT_TRUE(CompareImages(icon, wifi_connected_100_image_));

  SetStrength(network, 50);
  SetEncryption(network, SECURITY_RSN);
  icon = NetworkMenuIcon::GetImage(network,
                                   NetworkMenuIcon::COLOR_DARK);
  EXPECT_TRUE(CompareImages(icon, wifi_encrypted_50_image_));

  SetDisconnected(network);
  SetStrength(network, 0);
  SetEncryption(network, SECURITY_NONE);
  icon = NetworkMenuIcon::GetImage(network,
                                   NetworkMenuIcon::COLOR_DARK);
  EXPECT_TRUE(CompareImages(icon, wifi_disconnected_image_));
}

TEST_F(NetworkMenuIconTest, CellularIcon) {
  CellularNetwork* network = cros_->FindCellularNetworkByPath("cellular1");
  ASSERT_NE(static_cast<const Network*>(NULL), network);
  SetConnected(network);
  SetStrength(network, 100);
  SetRoamingState(network, ROAMING_STATE_HOME);
  gfx::ImageSkia icon = NetworkMenuIcon::GetImage(network,
                                                  NetworkMenuIcon::COLOR_DARK);
  EXPECT_TRUE(CompareImages(icon, cellular_connected_100_image_));

  SetStrength(network, 50);
  SetRoamingState(network, ROAMING_STATE_ROAMING);
  icon = NetworkMenuIcon::GetImage(network,
                                   NetworkMenuIcon::COLOR_DARK);
  EXPECT_TRUE(CompareImages(icon, cellular_roaming_50_image_));

  SetDisconnected(network);
  SetStrength(network, 0);
  SetRoamingState(network, ROAMING_STATE_HOME);
  icon = NetworkMenuIcon::GetImage(network,
                                   NetworkMenuIcon::COLOR_DARK);
  EXPECT_TRUE(CompareImages(icon, cellular_disconnected_image_));
}

namespace {

class TestNetworkMenuIcon : public NetworkMenuIcon {
 public:
  explicit TestNetworkMenuIcon(Mode mode)
      : NetworkMenuIcon(&delegate_, mode),
        animation_(0.0) {
  }
  virtual ~TestNetworkMenuIcon() {}

  // NetworkMenuIcon override.
  virtual double GetAnimation() OVERRIDE { return animation_; }

  void set_animation(double animation) { animation_ = animation; }

 private:
  class Delegate : public NetworkMenuIcon::Delegate {
   public:
    Delegate() : changed_(0) {}
    virtual void NetworkMenuIconChanged() OVERRIDE {
      ++changed_;
    }
    int changed() const { return changed_; }
   private:
    int changed_;
  };
  Delegate delegate_;
  double animation_;
};

}  // namespace

// Test Network Menu status icon logic.

// Default relevent stub state:
//  eth1: connected (active ethernet)
//  wifi1: connected (active wifi)
//  cellular1: connected  (active cellular)
// See network_library_unit_test.cc for more info.

TEST_F(NetworkMenuIconTest, StatusIconMenuMode) {
  TestNetworkMenuIcon menu_icon(NetworkMenuIcon::MENU_MODE);
  gfx::ImageSkia icon;

  // Set cellular1 to connecting.
  CellularNetwork* cellular1 = cros_->FindCellularNetworkByPath("cellular1");
  ASSERT_NE(static_cast<const Network*>(NULL), cellular1);
  SetRoamingState(cellular1, ROAMING_STATE_HOME);  // Clear romaing state
  SetConnecting(cellular1);

  // For MENU_MODE, we always display the connecting icon (cellular1).
  icon = menu_icon.GetIconAndText(NULL);
  EXPECT_TRUE(CompareImages(icon, cellular_connecting_image_));

  // Set cellular1 to connected; ethernet icon should be shown.
  SetConnected(cellular1);
  icon = menu_icon.GetIconAndText(NULL);
  EXPECT_TRUE(CompareImages(icon, ethernet_connected_image_));

  // Set ethernet to inactive/disconnected; wifi icon should be shown.
  Network* eth1 = cros_->FindNetworkByPath("eth1");
  ASSERT_NE(static_cast<const Network*>(NULL), eth1);
  SetActive(eth1, false);
  SetDisconnected(eth1);
  icon = menu_icon.GetIconAndText(NULL);
  EXPECT_TRUE(CompareImages(icon, wifi_connected_100_image_));
}

TEST_F(NetworkMenuIconTest, StatusIconDropdownMode) {
  TestNetworkMenuIcon menu_icon(NetworkMenuIcon::DROPDOWN_MODE);
  gfx::ImageSkia icon;

  // Set wifi1 to connecting.
  WifiNetwork* wifi1 = cros_->FindWifiNetworkByPath("wifi1");
  ASSERT_NE(static_cast<const Network*>(NULL), wifi1);
  SetConnecting(wifi1);

  // For DROPDOWN_MODE, we prioritize the connected network (ethernet).
  icon = menu_icon.GetIconAndText(NULL);
  EXPECT_TRUE(CompareImages(icon, ethernet_connected_image_));

  // Set ethernet to inactive/disconnected.
  Network* ethernet = cros_->FindNetworkByPath("eth1");
  ASSERT_NE(static_cast<const Network*>(NULL), ethernet);
  SetActive(ethernet, false);
  SetDisconnected(ethernet);

  // Icon should now be cellular connected icon.
  icon = menu_icon.GetIconAndText(NULL);
  EXPECT_TRUE(CompareImages(icon, cellular_connected_100_image_));

  // Set cellular1 to disconnected; Icon should now be wimax icon.
  CellularNetwork* cellular1 = cros_->FindCellularNetworkByPath("cellular1");
  ASSERT_NE(static_cast<const Network*>(NULL), cellular1);
  SetDisconnected(cellular1);
  icon = menu_icon.GetIconAndText(NULL);
  EXPECT_TRUE(CompareImages(icon, wimax_connected_50_image_));

  // Set wifi1 to connected. Icon should now be wifi connected icon.
  SetConnected(wifi1);
  icon = menu_icon.GetIconAndText(NULL);
  EXPECT_TRUE(CompareImages(icon, wifi_connected_100_image_));
}

}  // namespace chromeos