Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5409)

Unified Diff: chrome/browser/chromeos/hats/hats_notification_controller_unittest.cc

Issue 2169223002: Implements unittests for hats notification controller (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Implements unittests for hats notification controller Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/chromeos/hats/hats_notification_controller.h ('k') | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/chromeos/hats/hats_notification_controller_unittest.cc
diff --git a/chrome/browser/chromeos/hats/hats_notification_controller_unittest.cc b/chrome/browser/chromeos/hats/hats_notification_controller_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..da4796235653360a9c25d4613d5768e371246089
--- /dev/null
+++ b/chrome/browser/chromeos/hats/hats_notification_controller_unittest.cc
@@ -0,0 +1,234 @@
+// Copyright 2016 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/strings/string_split.h"
+#include "chrome/browser/chromeos/hats/hats_notification_controller.h"
+#include "chrome/browser/notifications/message_center_notification_manager.h"
+#include "chrome/browser/notifications/notification.h"
+#include "chrome/browser/notifications/notification_ui_manager.h"
+#include "chrome/common/pref_names.h"
+#include "chrome/test/base/browser_with_test_window_test.h"
+#include "chrome/test/base/testing_browser_process.h"
+#include "chrome/test/base/testing_profile.h"
+#include "chrome/test/base/testing_profile_manager.h"
+#include "chromeos/network/network_state.h"
+#include "chromeos/network/portal_detector/network_portal_detector.h"
+#include "components/prefs/pref_service.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "content/public/test/test_utils.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "ui/message_center/fake_message_center_tray_delegate.h"
+#include "ui/message_center/message_center.h"
+
+using testing::_;
+using testing::AtLeast;
+using testing::Invoke;
+using testing::Return;
+using testing::SaveArg;
+using testing::StrictMock;
+
+namespace chromeos {
+
+namespace {
+
+class MockNetworkPortalDetector : public NetworkPortalDetector {
+ public:
+ MockNetworkPortalDetector() {}
+ ~MockNetworkPortalDetector() override {}
+
+ MOCK_METHOD1(AddObserver,
+ void(chromeos::NetworkPortalDetector::Observer* observer));
+ MOCK_METHOD1(RemoveObserver,
+ void(chromeos::NetworkPortalDetector::Observer* observer));
+ MOCK_METHOD1(AddAndFireObserver,
+ void(chromeos::NetworkPortalDetector::Observer* observer));
+ MOCK_METHOD1(GetCaptivePortalState,
+ chromeos::NetworkPortalDetector::CaptivePortalState(
+ const std::string& service_path));
+ MOCK_METHOD0(IsEnabled, bool());
+ MOCK_METHOD1(Enable, void(bool start_detection));
+ MOCK_METHOD0(StartDetectionIfIdle, bool());
+ MOCK_METHOD1(SetStrategy,
+ void(chromeos::PortalDetectorStrategy::StrategyId id));
+ MOCK_METHOD0(OnLockScreenRequest, void());
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockNetworkPortalDetector);
+};
+
+} // namespace
+
+class HatsNotificationControllerTest : public BrowserWithTestWindowTest {
+ public:
+ HatsNotificationControllerTest() {}
+ ~HatsNotificationControllerTest() override {}
+
+ void SetUp() override {
+ BrowserWithTestWindowTest::SetUp();
+
+ profile_manager_.reset(
+ new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
+ ASSERT_TRUE(profile_manager_->SetUp());
+
+ MessageCenterNotificationManager* manager =
+ static_cast<MessageCenterNotificationManager*>(
+ g_browser_process->notification_ui_manager());
+ manager->SetMessageCenterTrayDelegateForTest(
+ new message_center::FakeMessageCenterTrayDelegate(
+ message_center::MessageCenter::Get()));
+
+ network_portal_detector::InitializeForTesting(
+ &mock_network_portal_detector_);
+ }
+
+ void TearDown() override {
+ g_browser_process->notification_ui_manager()->CancelAll();
+ profile_manager_.reset();
+ network_portal_detector::InitializeForTesting(nullptr);
+ BrowserWithTestWindowTest::TearDown();
+ }
+
+ scoped_refptr<HatsNotificationController> InstantiateHatsController() {
+ // The initialization will fail since the function IsNewDevice() will return
+ // true.
+ scoped_refptr<HatsNotificationController> hats_notification_controller =
+ new HatsNotificationController(&profile_);
+
+ // HatsController::IsNewDevice() is run on a blocking thread.
+ content::RunAllBlockingPoolTasksUntilIdle();
+
+ // Send a callback to the observer to simulate internet connectivity is
+ // present on device.
+ ON_CALL(mock_network_portal_detector_,
+ AddAndFireObserver(hats_notification_controller.get()))
+ .WillByDefault(Invoke(
+ [&hats_notification_controller](NetworkPortalDetector::Observer*) {
+ NetworkPortalDetector::CaptivePortalState online_state;
+ NetworkState network_state("");
+ online_state.status =
+ NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
+ hats_notification_controller->OnPortalDetectionCompleted(
+ &network_state, online_state);
+ }));
+
+ return hats_notification_controller;
+ }
+
+ TestingProfile profile_;
+ StrictMock<MockNetworkPortalDetector> mock_network_portal_detector_;
+
+ private:
+ std::unique_ptr<TestingProfileManager> profile_manager_;
+
+ DISALLOW_COPY_AND_ASSIGN(HatsNotificationControllerTest);
+};
+
+TEST_F(HatsNotificationControllerTest, NewDevice_ShouldNotShowNotification) {
+ int64_t initial_timestamp = base::Time::Now().ToInternalValue();
+ PrefService* pref_service = profile_.GetPrefs();
+ pref_service->SetInt64(prefs::kHatsLastInteractionTimestamp,
+ initial_timestamp);
+
+ auto hats_notification_controller = InstantiateHatsController();
+ hats_notification_controller->Initialize(true);
+
+ int64_t current_timestamp =
+ pref_service->GetInt64(prefs::kHatsLastInteractionTimestamp);
+
+ // When the device is new, the controller does not begin initialization and
+ // simply updates the timestamp to Time::Now().
+ ASSERT_TRUE(base::Time::FromInternalValue(current_timestamp) >
+ base::Time::FromInternalValue(initial_timestamp));
+
+ // Destructor for HatsController removes self from observer list.
+ EXPECT_CALL(mock_network_portal_detector_,
+ RemoveObserver(hats_notification_controller.get()))
+ .Times(1);
+
+ const Notification* notification =
+ g_browser_process->notification_ui_manager()->FindById(
+ HatsNotificationController::kDelegateId, &profile_);
+ EXPECT_FALSE(notification);
+}
+
+TEST_F(HatsNotificationControllerTest, OldDevice_ShouldShowNotification) {
+ auto hats_notification_controller = InstantiateHatsController();
+
+ // On initialization, HatsNotificationController adds itself as an observer to
+ // NetworkPortalDetector to detect internet connectivity.
+ EXPECT_CALL(mock_network_portal_detector_,
+ AddAndFireObserver(hats_notification_controller.get()))
+ .Times(1);
+ // Observer is removed if an internet connection is detected. It is called
+ // a second time when hats_notification_controller is destroyed.
+ EXPECT_CALL(mock_network_portal_detector_,
+ RemoveObserver(hats_notification_controller.get()))
+ .Times(2);
+
+ hats_notification_controller->Initialize(false);
+
+ // Finally check if notification was launched to confirm initialization.
+ const Notification* notification =
+ g_browser_process->notification_ui_manager()->FindById(
+ HatsNotificationController::kDelegateId, &profile_);
+ EXPECT_TRUE(notification != nullptr);
+}
+
+TEST_F(HatsNotificationControllerTest, NoInternet_DoNotShowNotification) {
+ auto hats_notification_controller = InstantiateHatsController();
+
+ // Upon destruction HatsNotificationController removes itself as an observer
+ // from NetworkPortalDetector. This will only be called once from the
+ EXPECT_CALL(mock_network_portal_detector_,
+ RemoveObserver(hats_notification_controller.get()))
+ .Times(1);
+
+ NetworkState network_state("");
+ NetworkPortalDetector::CaptivePortalState online_state;
+ hats_notification_controller->OnPortalDetectionCompleted(&network_state,
+ online_state);
+
+ online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE;
+ hats_notification_controller->OnPortalDetectionCompleted(&network_state,
+ online_state);
+
+ online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
+ hats_notification_controller->OnPortalDetectionCompleted(&network_state,
+ online_state);
+
+ online_state.status =
+ NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED;
+ hats_notification_controller->OnPortalDetectionCompleted(&network_state,
+ online_state);
+
+ const Notification* notification =
+ g_browser_process->notification_ui_manager()->FindById(
+ HatsNotificationController::kDelegateId, &profile_);
+ EXPECT_FALSE(notification);
+}
+
+TEST_F(HatsNotificationControllerTest, DismissNotification_ShouldUpdatePref) {
+ int64_t now_timestamp = base::Time::Now().ToInternalValue();
+ PrefService* pref_service = profile_.GetPrefs();
+ pref_service->SetInt64(prefs::kHatsLastInteractionTimestamp, now_timestamp);
+
+ auto hats_notification_controller = InstantiateHatsController();
+
+ // Simulate closing notification via user interaction.
+ hats_notification_controller->Close(true);
+
+ int64_t new_timestamp =
+ pref_service->GetInt64(prefs::kHatsLastInteractionTimestamp);
+ // The flag should be updated to a new timestamp.
+ ASSERT_TRUE(base::Time::FromInternalValue(new_timestamp) >
+ base::Time::FromInternalValue(now_timestamp));
+
+ // Destructor for HatsController removes self from observer list.
+ EXPECT_CALL(mock_network_portal_detector_,
+ RemoveObserver(hats_notification_controller.get()))
+ .Times(1);
+}
+
+} // namespace chromeos
« no previous file with comments | « chrome/browser/chromeos/hats/hats_notification_controller.h ('k') | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698