This source file includes following definitions.
- HasNotification
- update_count_
- OnNotificationAdded
- OnNotificationRemoved
- OnNotificationUpdated
- add_count
- remove_count
- update_count
- SetUp
- TearDown
- OnPortalDetectionCompleted
- observer
- TEST_F
- TEST_F
- TEST_F
#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* kNotificationId =
NetworkPortalNotificationController::kNotificationId;
bool HasNotification() {
return MessageCenter::Get()->HasNotification(kNotificationId);
}
class NotificationObserver : public message_center::MessageCenterObserver {
public:
NotificationObserver() : add_count_(0), remove_count_(0), update_count_(0) {}
virtual void OnNotificationAdded(
const std::string& notification_id) OVERRIDE {
if (notification_id == kNotificationId)
++add_count_;
}
virtual void OnNotificationRemoved(const std::string& notification_id,
bool ) 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);
};
}
class NetworkPortalNotificationControllerTest : public testing::Test {
public:
NetworkPortalNotificationControllerTest() {}
virtual ~NetworkPortalNotificationControllerTest() {}
virtual void SetUp() OVERRIDE {
CommandLine* cl = 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;
wifi_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
wifi_state.response_code = 204;
OnPortalDetectionCompleted(&wifi, wifi_state);
ASSERT_FALSE(HasNotification());
wifi_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
wifi_state.response_code = 200;
OnPortalDetectionCompleted(&wifi, wifi_state);
ASSERT_TRUE(HasNotification());
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 );
ASSERT_FALSE(HasNotification());
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;
OnPortalDetectionCompleted(&wifi2, wifi2_state);
ASSERT_FALSE(HasNotification());
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;
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());
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());
MessageCenter::Get()->RemoveNotification(kNotificationId, true );
ASSERT_FALSE(HasNotification());
EXPECT_EQ(1u, observer().add_count());
EXPECT_EQ(1u, observer().remove_count());
EXPECT_EQ(1u, observer().update_count());
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());
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());
}
}