| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 "chrome/browser/ui/startup/default_browser_infobar_delegate.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/run_loop.h" | |
| 13 #include "base/test/scoped_feature_list.h" | |
| 14 #include "chrome/browser/infobars/infobar_service.h" | |
| 15 #include "chrome/test/base/testing_profile.h" | |
| 16 #include "components/infobars/core/infobar.h" | |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | |
| 18 #include "content/public/test/test_web_contents_factory.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace chrome { | |
| 22 | |
| 23 // An implementation of DefaultBrowserWorker that will simply invoke the | |
| 24 // 'on_finished_callback' immediately. | |
| 25 class FakeDefaultBrowserWorker | |
| 26 : public shell_integration::DefaultBrowserWorker { | |
| 27 public: | |
| 28 FakeDefaultBrowserWorker( | |
| 29 const shell_integration::DefaultWebClientWorkerCallback& callback) | |
| 30 : shell_integration::DefaultBrowserWorker(callback) {} | |
| 31 | |
| 32 private: | |
| 33 ~FakeDefaultBrowserWorker() override = default; | |
| 34 | |
| 35 shell_integration::DefaultWebClientState CheckIsDefaultImpl() override { | |
| 36 return shell_integration::NOT_DEFAULT; | |
| 37 } | |
| 38 | |
| 39 void SetAsDefaultImpl(const base::Closure& on_finished_callback) override { | |
| 40 on_finished_callback.Run(); | |
| 41 } | |
| 42 | |
| 43 DISALLOW_COPY_AND_ASSIGN(FakeDefaultBrowserWorker); | |
| 44 }; | |
| 45 | |
| 46 class FakeDefaultBrowserInfoBarDelegate : public DefaultBrowserInfoBarDelegate { | |
| 47 public: | |
| 48 FakeDefaultBrowserInfoBarDelegate() | |
| 49 : DefaultBrowserInfoBarDelegate(nullptr) {} | |
| 50 | |
| 51 private: | |
| 52 scoped_refptr<shell_integration::DefaultBrowserWorker> | |
| 53 CreateDefaultBrowserWorker( | |
| 54 const shell_integration::DefaultWebClientWorkerCallback& callback) | |
| 55 override { | |
| 56 return new FakeDefaultBrowserWorker(callback); | |
| 57 } | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(FakeDefaultBrowserInfoBarDelegate); | |
| 60 }; | |
| 61 | |
| 62 class DefaultBrowserInfoBarDelegateTest : public ::testing::Test { | |
| 63 public: | |
| 64 DefaultBrowserInfoBarDelegateTest() | |
| 65 : profile_(new TestingProfile()), | |
| 66 web_contents_(factory_.CreateWebContents(profile_.get())) { | |
| 67 InfoBarService::CreateForWebContents(web_contents_); | |
| 68 infobar_service_ = InfoBarService::FromWebContents(web_contents_); | |
| 69 } | |
| 70 | |
| 71 protected: | |
| 72 void EnableStickyDefaultBrowserPrompt() { | |
| 73 scoped_feature_list_.InitAndEnableFeature(kStickyDefaultBrowserPrompt); | |
| 74 } | |
| 75 | |
| 76 void AddDefaultBrowserInfoBar() { | |
| 77 infobar_service_->AddInfoBar(infobar_service_->CreateConfirmInfoBar( | |
| 78 base::MakeUnique<FakeDefaultBrowserInfoBarDelegate>())); | |
| 79 } | |
| 80 | |
| 81 InfoBarService* infobar_service() { return infobar_service_; } | |
| 82 | |
| 83 private: | |
| 84 // The DefaultBrowserWorker requires a FILE thread. Also provides a | |
| 85 // SingleThreadTaskRunner for the test profile and the default browser prompt. | |
| 86 content::TestBrowserThreadBundle thread_bundle; | |
| 87 | |
| 88 // Required to get an InfoBarService. | |
| 89 std::unique_ptr<TestingProfile> profile_; | |
| 90 content::TestWebContentsFactory factory_; | |
| 91 content::WebContents* web_contents_; | |
| 92 | |
| 93 // Manages the default browser prompt. | |
| 94 InfoBarService* infobar_service_; | |
| 95 | |
| 96 base::test::ScopedFeatureList scoped_feature_list_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(DefaultBrowserInfoBarDelegateTest); | |
| 99 }; | |
| 100 | |
| 101 TEST_F(DefaultBrowserInfoBarDelegateTest, DefaultBehavior) { | |
| 102 AddDefaultBrowserInfoBar(); | |
| 103 ASSERT_EQ(1U, infobar_service()->infobar_count()); | |
| 104 | |
| 105 infobars::InfoBar* default_browser_infobar = infobar_service()->infobar_at(0); | |
| 106 ConfirmInfoBarDelegate* default_browser_infobar_delegate = | |
| 107 default_browser_infobar->delegate()->AsConfirmInfoBarDelegate(); | |
| 108 | |
| 109 // When the sticky default browser prompt experiment is not enabled, the | |
| 110 // infobar delegate should allow itself to be destroyed immediately if the | |
| 111 // user activates the default action. Accept() should return true to indicate | |
| 112 // this. | |
| 113 EXPECT_TRUE(default_browser_infobar_delegate->Accept()); | |
| 114 } | |
| 115 | |
| 116 #if defined(OS_WIN) | |
| 117 TEST_F(DefaultBrowserInfoBarDelegateTest, StickyDefaultBrowserPrompt) { | |
| 118 EnableStickyDefaultBrowserPrompt(); | |
| 119 | |
| 120 // For most Windows versions, this experiment is disabled. | |
| 121 if (!IsStickyDefaultBrowserPromptEnabled()) | |
| 122 return; | |
| 123 | |
| 124 AddDefaultBrowserInfoBar(); | |
| 125 ASSERT_EQ(1U, infobar_service()->infobar_count()); | |
| 126 | |
| 127 infobars::InfoBar* default_browser_infobar = infobar_service()->infobar_at(0); | |
| 128 ConfirmInfoBarDelegate* default_browser_infobar_delegate = | |
| 129 default_browser_infobar->delegate()->AsConfirmInfoBarDelegate(); | |
| 130 | |
| 131 // The sticky default browser prompt experiment should mean activating the | |
| 132 // infobar's default action does not result in the infobar's destruction. | |
| 133 // Instead, the infobar will ultimately be destroyed when the | |
| 134 // DefaultBrowserWorker is done. To indicate this Accept() should return | |
| 135 // false. | |
| 136 EXPECT_FALSE(default_browser_infobar_delegate->Accept()); | |
| 137 EXPECT_EQ(1U, infobar_service()->infobar_count()); | |
| 138 | |
| 139 // Spin the message loop to allow the FakeDefaultBrowserWorker to complete, | |
| 140 // which should destroy the infobar. | |
| 141 base::RunLoop().RunUntilIdle(); | |
| 142 EXPECT_EQ(0U, infobar_service()->infobar_count()); | |
| 143 } | |
| 144 #endif // defined(OS_WIN) | |
| 145 | |
| 146 } // namespace chrome | |
| OLD | NEW |