File: network_portal_notification_controller_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (200 lines) | stat: -rw-r--r-- 6,981 bytes parent folder | download
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
// Copyright 2014 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 "base/command_line.h"
#include "chrome/browser/chromeos/net/network_portal_notification_controller.h"
#include "chromeos/chromeos_switches.h"
#include "chromeos/network/network_state.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/message_center_observer.h"

using message_center::MessageCenter;

namespace chromeos {

namespace {

const char* const kNotificationId =
    NetworkPortalNotificationController::kNotificationId;

bool HasNotification() {
  MessageCenter* message_center = MessageCenter::Get();
  return message_center->FindVisibleNotificationById(kNotificationId);
}

class NotificationObserver : public message_center::MessageCenterObserver {
 public:
  NotificationObserver() : add_count_(0), remove_count_(0), update_count_(0) {}

  // Overridden from message_center::MessageCenterObserver:
  virtual void OnNotificationAdded(
      const std::string& notification_id) override {
    if (notification_id == kNotificationId)
      ++add_count_;
  }

  virtual void OnNotificationRemoved(const std::string& notification_id,
                                     bool /* by_user */) override {
    if (notification_id == kNotificationId)
      ++remove_count_;
  }

  virtual void OnNotificationUpdated(
      const std::string& notification_id) override {
    if (notification_id == kNotificationId)
      ++update_count_;
  }

  unsigned add_count() const { return add_count_; }
  unsigned remove_count() const { return remove_count_; }
  unsigned update_count() const { return update_count_; }

 private:
  unsigned add_count_;
  unsigned remove_count_;
  unsigned update_count_;

  DISALLOW_COPY_AND_ASSIGN(NotificationObserver);
};

}  // namespace

class NetworkPortalNotificationControllerTest : public testing::Test {
 public:
  NetworkPortalNotificationControllerTest() {}
  virtual ~NetworkPortalNotificationControllerTest() {}

  virtual void SetUp() override {
    base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
    cl->AppendSwitch(switches::kEnableNetworkPortalNotification);
    MessageCenter::Initialize();
    MessageCenter::Get()->AddObserver(&observer_);
  }

  virtual void TearDown() override {
    MessageCenter::Get()->RemoveObserver(&observer_);
    MessageCenter::Shutdown();
  }

 protected:
  void OnPortalDetectionCompleted(
      const NetworkState* network,
      const NetworkPortalDetector::CaptivePortalState& state) {
    controller_.OnPortalDetectionCompleted(network, state);
  }

  NotificationObserver& observer() { return observer_; }

 private:
  NetworkPortalNotificationController controller_;
  NotificationObserver observer_;

  DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationControllerTest);
};

TEST_F(NetworkPortalNotificationControllerTest, NetworkStateChanged) {
  NetworkState wifi("wifi");
  NetworkPortalDetector::CaptivePortalState wifi_state;

  // Notification is not displayed for online state.
  wifi_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
  wifi_state.response_code = 204;
  OnPortalDetectionCompleted(&wifi, wifi_state);
  ASSERT_FALSE(HasNotification());

  // Notification is displayed for portal state
  wifi_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
  wifi_state.response_code = 200;
  OnPortalDetectionCompleted(&wifi, wifi_state);
  ASSERT_TRUE(HasNotification());

  // Notification is closed for online state.
  wifi_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
  wifi_state.response_code = 204;
  OnPortalDetectionCompleted(&wifi, wifi_state);
  ASSERT_FALSE(HasNotification());
}

TEST_F(NetworkPortalNotificationControllerTest, NetworkChanged) {
  NetworkState wifi1("wifi1");
  NetworkPortalDetector::CaptivePortalState wifi1_state;
  wifi1_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
  wifi1_state.response_code = 200;
  OnPortalDetectionCompleted(&wifi1, wifi1_state);
  ASSERT_TRUE(HasNotification());

  MessageCenter::Get()->RemoveNotification(kNotificationId, true /* by_user */);
  ASSERT_FALSE(HasNotification());

  // User already closed notification about portal state for this network,
  // so notification shouldn't be displayed second time.
  OnPortalDetectionCompleted(&wifi1, wifi1_state);
  ASSERT_FALSE(HasNotification());

  NetworkState wifi2("wifi2");
  NetworkPortalDetector::CaptivePortalState wifi2_state;
  wifi2_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
  wifi2_state.response_code = 204;

  // Second network is in online state, so there shouldn't be any
  // notifications.
  OnPortalDetectionCompleted(&wifi2, wifi2_state);
  ASSERT_FALSE(HasNotification());

  // User switches back to the first network, so notification should
  // be displayed.
  OnPortalDetectionCompleted(&wifi1, wifi1_state);
  ASSERT_TRUE(HasNotification());
}

TEST_F(NetworkPortalNotificationControllerTest, NotificationUpdated) {
  NetworkPortalDetector::CaptivePortalState portal_state;
  portal_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
  portal_state.response_code = 200;

  // First network is behind a captive portal, so notification should
  // be displayed.
  NetworkState wifi1("wifi1");
  OnPortalDetectionCompleted(&wifi1, portal_state);
  ASSERT_TRUE(HasNotification());
  EXPECT_EQ(1u, observer().add_count());
  EXPECT_EQ(0u, observer().remove_count());
  EXPECT_EQ(0u, observer().update_count());

  // Second network is also behind a captive portal, so notification
  // should be updated.
  NetworkState wifi2("wifi2");
  OnPortalDetectionCompleted(&wifi2, portal_state);
  ASSERT_TRUE(HasNotification());
  EXPECT_EQ(1u, observer().add_count());
  EXPECT_EQ(0u, observer().remove_count());
  EXPECT_EQ(1u, observer().update_count());

  // User closes the notification.
  MessageCenter::Get()->RemoveNotification(kNotificationId, true /* by_user */);
  ASSERT_FALSE(HasNotification());
  EXPECT_EQ(1u, observer().add_count());
  EXPECT_EQ(1u, observer().remove_count());
  EXPECT_EQ(1u, observer().update_count());

  // Portal detector notified that second network is still behind captive
  // portal, but user already closed the notification, so there should
  // not be any notifications.
  OnPortalDetectionCompleted(&wifi2, portal_state);
  ASSERT_FALSE(HasNotification());
  EXPECT_EQ(1u, observer().add_count());
  EXPECT_EQ(1u, observer().remove_count());
  EXPECT_EQ(1u, observer().update_count());

  // Network was switched (by shill or by user) to wifi1. Notification
  // should be displayed.
  OnPortalDetectionCompleted(&wifi1, portal_state);
  ASSERT_TRUE(HasNotification());
  EXPECT_EQ(2u, observer().add_count());
  EXPECT_EQ(1u, observer().remove_count());
  EXPECT_EQ(1u, observer().update_count());
}

}  // namespace chromeos