1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <memory>
6 
7 #include "chrome/browser/lifetime/application_lifetime.h"
8 #include "chrome/browser/notifications/notification_system_observer.h"
9 #include "chrome/browser/notifications/notification_test_util.h"
10 #include "chrome/browser/notifications/notification_ui_manager.h"
11 #include "chrome/test/base/testing_browser_process.h"
12 #include "chrome/test/base/testing_profile_manager.h"
13 #include "components/crx_file/id_util.h"
14 #include "content/public/test/browser_task_environment.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/browser/unloaded_extension_reason.h"
17 #include "extensions/common/extension_builder.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 
20 namespace {
CreateGoodExtension(const std::string & name)21 scoped_refptr<const extensions::Extension> CreateGoodExtension(
22     const std::string& name) {
23   return extensions::ExtensionBuilder(name).Build();
24 }
25 }  // namespace
26 
27 class NotificationSystemObserverTest : public testing::Test {
28  protected:
29   NotificationSystemObserverTest() = default;
30   ~NotificationSystemObserverTest() override = default;
31 
32   // testing::Test:
SetUp()33   void SetUp() override {
34     profile_manager_ = std::make_unique<TestingProfileManager>(
35         TestingBrowserProcess::GetGlobal());
36     ASSERT_TRUE(profile_manager_->SetUp());
37     profile_ = profile_manager_->CreateTestingProfile("test-profile");
38     ui_manager_ = std::make_unique<StubNotificationUIManager>();
39     notification_observer_ =
40         std::make_unique<NotificationSystemObserver>(ui_manager_.get());
41   }
42 
profile_manager()43   TestingProfileManager* profile_manager() { return profile_manager_.get(); }
profile()44   TestingProfile* profile() { return profile_; }
ui_manager()45   StubNotificationUIManager* ui_manager() { return ui_manager_.get(); }
46 
47  private:
48   content::BrowserTaskEnvironment task_environment_;
49   std::unique_ptr<TestingProfileManager> profile_manager_;
50   TestingProfile* profile_ = nullptr;
51   std::unique_ptr<StubNotificationUIManager> ui_manager_;
52   std::unique_ptr<NotificationSystemObserver> notification_observer_;
53 };
54 
TEST_F(NotificationSystemObserverTest,MultiProfileExtensionUnloaded)55 TEST_F(NotificationSystemObserverTest, MultiProfileExtensionUnloaded) {
56   scoped_refptr<const extensions::Extension> extension =
57       CreateGoodExtension("foo");
58   ASSERT_NE(extension->url(), ui_manager()->last_canceled_source());
59   // Claim the extension has been unloaded.
60   extensions::ExtensionRegistry::Get(profile())->TriggerOnUnloaded(
61       extension.get(), extensions::UnloadedExtensionReason::UNINSTALL);
62   // Check the NotificationUIManger has canceled the source coming from the
63   // unloaded extension.
64   EXPECT_EQ(extension->url(), ui_manager()->last_canceled_source());
65 
66   TestingProfile* profile2 =
67       profile_manager()->CreateTestingProfile("test-profile2");
68   scoped_refptr<const extensions::Extension> extension2 =
69       CreateGoodExtension("bar");
70   // Claim the extension has been unloaded with anoter profile.
71   extensions::ExtensionRegistry::Get(profile2)->TriggerOnUnloaded(
72       extension2.get(), extensions::UnloadedExtensionReason::UNINSTALL);
73   EXPECT_EQ(extension2->url(), ui_manager()->last_canceled_source());
74 }
75