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

Unified Diff: chrome/browser/usb/web_usb_detector_unittest.cc

Issue 2824923002: Suppress WebUSB notifications when appropriate (Closed)
Patch Set: Test histograms Created 3 years, 8 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
Index: chrome/browser/usb/web_usb_detector_unittest.cc
diff --git a/chrome/browser/usb/web_usb_detector_unittest.cc b/chrome/browser/usb/web_usb_detector_unittest.cc
index dd410a289278e9dd92a7b055d1422952b548b174..d05471ccc4e9140f27689632d1ddde06f3b791b5 100644
--- a/chrome/browser/usb/web_usb_detector_unittest.cc
+++ b/chrome/browser/usb/web_usb_detector_unittest.cc
@@ -7,7 +7,14 @@
#include "base/macros.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
+#include "base/test/histogram_tester.h"
+#include "chrome/browser/profiles/profile_manager.h"
+#include "chrome/browser/ui/browser_list.h"
+#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/usb/web_usb_detector.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_manager.h"
#include "device/base/mock_device_client.h"
#include "device/usb/mock_usb_device.h"
#include "device/usb/mock_usb_service.h"
@@ -35,18 +42,41 @@ const char* kLandingPage_3 = "https://www.google.com/C";
} // namespace
-class WebUsbDetectorTest : public testing::Test {
+class WebUsbDetectorTest : public BrowserWithTestWindowTest {
public:
- WebUsbDetectorTest() {}
+ WebUsbDetectorTest() : profile_manager_(TestingBrowserProcess::GetGlobal()) {}
~WebUsbDetectorTest() override = default;
+ // Use the profile_manager_'s profile so that we can manage which one is most
+ // recently active.
+ TestingProfile* CreateProfile() override {
+ return profile_manager_.CreateTestingProfile("test@example.com");
+ }
+
+ // Since the profile is owned by profile_manager_, we do not need to destroy
+ // it.
+ void DestroyProfile(TestingProfile* profile) override {}
+
void SetUp() override {
message_center::MessageCenter::Initialize();
message_center_ = message_center::MessageCenter::Get();
ASSERT_TRUE(message_center_ != nullptr);
+
+ ASSERT_TRUE(profile_manager_.SetUp());
+ BrowserWithTestWindowTest::SetUp();
+ profile_manager_.UpdateLastUser(profile());
+ BrowserList::SetLastActive(browser());
+
+ web_usb_detector_.reset(new WebUsbDetector());
}
- void TearDown() override { message_center::MessageCenter::Shutdown(); }
+ void TearDown() override {
+ BrowserWithTestWindowTest::TearDown();
+ message_center::MessageCenter::Shutdown();
+ web_usb_detector_.reset(nullptr);
+ }
+
+ void Initialize() { web_usb_detector_->Initialize(); }
protected:
device::MockDeviceClient device_client_;
@@ -54,6 +84,8 @@ class WebUsbDetectorTest : public testing::Test {
private:
DISALLOW_COPY_AND_ASSIGN(WebUsbDetectorTest);
+ std::unique_ptr<WebUsbDetector> web_usb_detector_;
+ TestingProfileManager profile_manager_;
};
TEST_F(WebUsbDetectorTest, UsbDeviceAddedAndRemoved) {
@@ -63,8 +95,7 @@ TEST_F(WebUsbDetectorTest, UsbDeviceAddedAndRemoved) {
0, 1, "Google", kProductName_1, "002", landing_page));
std::string guid = device->guid();
- WebUsbDetector web_usb_detector;
- web_usb_detector.Initialize();
+ Initialize();
device_client_.usb_service()->AddDevice(device);
message_center::Notification* notification =
@@ -81,7 +112,7 @@ TEST_F(WebUsbDetectorTest, UsbDeviceAddedAndRemoved) {
device_client_.usb_service()->RemoveDevice(device);
// Device is removed, so notification should be removed from the
// message_center too.
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
}
TEST_F(WebUsbDetectorTest, UsbDeviceWithoutProductNameAddedAndRemoved) {
@@ -91,15 +122,14 @@ TEST_F(WebUsbDetectorTest, UsbDeviceWithoutProductNameAddedAndRemoved) {
0, 1, "Google", product_name, "002", landing_page));
std::string guid = device->guid();
- WebUsbDetector web_usb_detector;
- web_usb_detector.Initialize();
+ Initialize();
device_client_.usb_service()->AddDevice(device);
// For device without product name, no notification is generated.
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
device_client_.usb_service()->RemoveDevice(device);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
}
TEST_F(WebUsbDetectorTest, UsbDeviceWithoutLandingPageAddedAndRemoved) {
@@ -108,15 +138,14 @@ TEST_F(WebUsbDetectorTest, UsbDeviceWithoutLandingPageAddedAndRemoved) {
0, 1, "Google", kProductName_1, "002", landing_page));
std::string guid = device->guid();
- WebUsbDetector web_usb_detector;
- web_usb_detector.Initialize();
+ Initialize();
device_client_.usb_service()->AddDevice(device);
// For device without landing page, no notification is generated.
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
device_client_.usb_service()->RemoveDevice(device);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
}
TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndThenRemoved) {
@@ -127,13 +156,12 @@ TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndThenRemoved) {
// USB device was added before web_usb_detector was created.
device_client_.usb_service()->AddDevice(device);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
- WebUsbDetector web_usb_detector;
- web_usb_detector.Initialize();
+ Initialize();
device_client_.usb_service()->RemoveDevice(device);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
}
TEST_F(
@@ -160,18 +188,18 @@ TEST_F(
// Three usb devices were added and removed before web_usb_detector was
// created.
device_client_.usb_service()->AddDevice(device_1);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
device_client_.usb_service()->AddDevice(device_2);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
device_client_.usb_service()->AddDevice(device_3);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
device_client_.usb_service()->RemoveDevice(device_1);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
device_client_.usb_service()->RemoveDevice(device_2);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
device_client_.usb_service()->RemoveDevice(device_3);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
WebUsbDetector web_usb_detector;
web_usb_detector.Initialize();
@@ -200,21 +228,20 @@ TEST_F(
// Three usb devices were added before web_usb_detector was created.
device_client_.usb_service()->AddDevice(device_1);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
device_client_.usb_service()->AddDevice(device_2);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
device_client_.usb_service()->AddDevice(device_3);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
- WebUsbDetector web_usb_detector;
- web_usb_detector.Initialize();
+ Initialize();
device_client_.usb_service()->RemoveDevice(device_1);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
device_client_.usb_service()->RemoveDevice(device_2);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
device_client_.usb_service()->RemoveDevice(device_3);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
}
TEST_F(WebUsbDetectorTest,
@@ -239,15 +266,14 @@ TEST_F(WebUsbDetectorTest,
// Two usb devices were added before web_usb_detector was created.
device_client_.usb_service()->AddDevice(device_1);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
device_client_.usb_service()->AddDevice(device_3);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
- WebUsbDetector web_usb_detector;
- web_usb_detector.Initialize();
+ Initialize();
device_client_.usb_service()->RemoveDevice(device_1);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
device_client_.usb_service()->AddDevice(device_2);
message_center::Notification* notification =
@@ -262,10 +288,10 @@ TEST_F(WebUsbDetectorTest,
EXPECT_TRUE(notification->delegate() != nullptr);
device_client_.usb_service()->RemoveDevice(device_3);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
device_client_.usb_service()->RemoveDevice(device_2);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
}
TEST_F(WebUsbDetectorTest, ThreeUsbDevicesAddedAndRemoved) {
@@ -287,8 +313,7 @@ TEST_F(WebUsbDetectorTest, ThreeUsbDevicesAddedAndRemoved) {
6, 7, "Google", kProductName_3, "008", landing_page_3));
std::string guid_3 = device_3->guid();
- WebUsbDetector web_usb_detector;
- web_usb_detector.Initialize();
+ Initialize();
device_client_.usb_service()->AddDevice(device_1);
message_center::Notification* notification_1 =
@@ -303,7 +328,7 @@ TEST_F(WebUsbDetectorTest, ThreeUsbDevicesAddedAndRemoved) {
EXPECT_TRUE(notification_1->delegate() != nullptr);
device_client_.usb_service()->RemoveDevice(device_1);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
device_client_.usb_service()->AddDevice(device_2);
message_center::Notification* notification_2 =
@@ -318,7 +343,7 @@ TEST_F(WebUsbDetectorTest, ThreeUsbDevicesAddedAndRemoved) {
EXPECT_TRUE(notification_2->delegate() != nullptr);
device_client_.usb_service()->RemoveDevice(device_2);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
device_client_.usb_service()->AddDevice(device_3);
message_center::Notification* notification_3 =
@@ -333,7 +358,7 @@ TEST_F(WebUsbDetectorTest, ThreeUsbDevicesAddedAndRemoved) {
EXPECT_TRUE(notification_3->delegate() != nullptr);
device_client_.usb_service()->RemoveDevice(device_3);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
}
TEST_F(WebUsbDetectorTest, ThreeUsbDeviceAddedAndRemovedDifferentOrder) {
@@ -355,8 +380,7 @@ TEST_F(WebUsbDetectorTest, ThreeUsbDeviceAddedAndRemovedDifferentOrder) {
6, 7, "Google", kProductName_3, "008", landing_page_3));
std::string guid_3 = device_3->guid();
- WebUsbDetector web_usb_detector;
- web_usb_detector.Initialize();
+ Initialize();
device_client_.usb_service()->AddDevice(device_1);
message_center::Notification* notification_1 =
@@ -383,7 +407,7 @@ TEST_F(WebUsbDetectorTest, ThreeUsbDeviceAddedAndRemovedDifferentOrder) {
EXPECT_TRUE(notification_2->delegate() != nullptr);
device_client_.usb_service()->RemoveDevice(device_2);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
device_client_.usb_service()->AddDevice(device_3);
message_center::Notification* notification_3 =
@@ -398,9 +422,97 @@ TEST_F(WebUsbDetectorTest, ThreeUsbDeviceAddedAndRemovedDifferentOrder) {
EXPECT_TRUE(notification_3->delegate() != nullptr);
device_client_.usb_service()->RemoveDevice(device_1);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
device_client_.usb_service()->RemoveDevice(device_3);
- EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
+ EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
+}
+
+TEST_F(WebUsbDetectorTest, UsbDeviceAddedWhileActiveTabUrlIsLandingPage) {
+ GURL landing_page_1(kLandingPage_1);
+ scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
+ 0, 1, "Google", kProductName_1, "002", landing_page_1));
+ std::string guid_1 = device_1->guid();
+
+ Initialize();
+
+ AddTab(browser(), landing_page_1);
+
+ device_client_.usb_service()->AddDevice(device_1);
+ ASSERT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
+}
+
+TEST_F(WebUsbDetectorTest, UsbDeviceAddedBeforeActiveTabUrlIsLandingPage) {
+ GURL landing_page_1(kLandingPage_1);
+ scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
+ 0, 1, "Google", kProductName_1, "002", landing_page_1));
+ std::string guid_1 = device_1->guid();
+
+ base::HistogramTester histogram_tester;
+ Initialize();
+
+ device_client_.usb_service()->AddDevice(device_1);
+ ASSERT_TRUE(message_center_->FindVisibleNotificationById(guid_1) != nullptr);
+
+ AddTab(browser(), landing_page_1);
+ ASSERT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
+ histogram_tester.ExpectUniqueSample("WebUsb.NotificationClosed", 3, 1);
+}
+
+TEST_F(WebUsbDetectorTest,
+ NotificationClickedWhileInactiveTabUrlIsLandingPage) {
+ GURL landing_page_1(kLandingPage_1);
+ GURL landing_page_2(kLandingPage_2);
+ scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
+ 0, 1, "Google", kProductName_1, "002", landing_page_1));
+ std::string guid_1 = device_1->guid();
+ TabStripModel* tab_strip_model = browser()->tab_strip_model();
+
+ base::HistogramTester histogram_tester;
+ Initialize();
+
+ AddTab(browser(), landing_page_1);
+ AddTab(browser(), landing_page_2);
+
+ device_client_.usb_service()->AddDevice(device_1);
+ message_center::Notification* notification_1 =
+ message_center_->FindVisibleNotificationById(guid_1);
+ ASSERT_TRUE(notification_1 != nullptr);
+ EXPECT_EQ(2, tab_strip_model->count());
+
+ notification_1->Click();
+ EXPECT_EQ(2, tab_strip_model->count());
+ content::WebContents* web_contents =
+ tab_strip_model->GetWebContentsAt(tab_strip_model->active_index());
+ EXPECT_EQ(landing_page_1, web_contents->GetURL());
+ ASSERT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
+ histogram_tester.ExpectUniqueSample("WebUsb.NotificationClosed", 2, 1);
+}
+
+TEST_F(WebUsbDetectorTest, NotificationClickedWhileNoTabUrlIsLandingPage) {
+ GURL landing_page_1(kLandingPage_1);
+ GURL landing_page_2(kLandingPage_2);
+ scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
+ 0, 1, "Google", kProductName_1, "002", landing_page_1));
+ std::string guid_1 = device_1->guid();
+ TabStripModel* tab_strip_model = browser()->tab_strip_model();
+
+ base::HistogramTester histogram_tester;
+ Initialize();
+
+ device_client_.usb_service()->AddDevice(device_1);
+ message_center::Notification* notification_1 =
+ message_center_->FindVisibleNotificationById(guid_1);
+ ASSERT_TRUE(notification_1 != nullptr);
+ EXPECT_EQ(0, tab_strip_model->count());
+
+ notification_1->Click();
+ EXPECT_EQ(1, tab_strip_model->count());
+ content::WebContents* web_contents =
+ tab_strip_model->GetWebContentsAt(tab_strip_model->active_index());
+ EXPECT_EQ(landing_page_1, web_contents->GetURL());
+ ASSERT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
+ histogram_tester.ExpectUniqueSample("WebUsb.NotificationClosed", 2, 1);
}
+
#endif // !OS_WIN

Powered by Google App Engine
This is Rietveld 408576698