| 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/feature_list.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/run_loop.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 base::FeatureList::ClearInstanceForTesting(); |
| 74 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 75 feature_list->InitializeFromCommandLine(kStickyDefaultBrowserPrompt.name, |
| 76 std::string()); |
| 77 base::FeatureList::SetInstance(std::move(feature_list)); |
| 78 } |
| 79 |
| 80 void AddDefaultBrowserInfoBar() { |
| 81 infobar_service_->AddInfoBar(infobar_service_->CreateConfirmInfoBar( |
| 82 base::WrapUnique(new FakeDefaultBrowserInfoBarDelegate()))); |
| 83 } |
| 84 |
| 85 InfoBarService* infobar_service() { return infobar_service_; } |
| 86 |
| 87 private: |
| 88 // The DefaultBrowserWorker requires a FILE thread. Also provides a |
| 89 // SingleThreadTaskRunner for the test profile and the default browser prompt. |
| 90 content::TestBrowserThreadBundle thread_bundle; |
| 91 |
| 92 // Required to get an InfoBarService. |
| 93 std::unique_ptr<TestingProfile> profile_; |
| 94 content::TestWebContentsFactory factory_; |
| 95 content::WebContents* web_contents_; |
| 96 |
| 97 // Manages the default browser prompt. |
| 98 InfoBarService* infobar_service_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(DefaultBrowserInfoBarDelegateTest); |
| 101 }; |
| 102 |
| 103 TEST_F(DefaultBrowserInfoBarDelegateTest, DefaultBehavior) { |
| 104 AddDefaultBrowserInfoBar(); |
| 105 ASSERT_EQ(1U, infobar_service()->infobar_count()); |
| 106 |
| 107 infobars::InfoBar* default_browser_infobar = infobar_service()->infobar_at(0); |
| 108 ConfirmInfoBarDelegate* default_browser_infobar_delegate = |
| 109 default_browser_infobar->delegate()->AsConfirmInfoBarDelegate(); |
| 110 |
| 111 // When the sticky default browser prompt experiment is not enabled, the |
| 112 // infobar delegate should allow itself to be destroyed immediately if the |
| 113 // user activates the default action. Accept() should return true to indicate |
| 114 // this. |
| 115 EXPECT_TRUE(default_browser_infobar_delegate->Accept()); |
| 116 } |
| 117 |
| 118 #if defined(OS_WIN) |
| 119 TEST_F(DefaultBrowserInfoBarDelegateTest, StickyDefaultBrowserPrompt) { |
| 120 EnableStickyDefaultBrowserPrompt(); |
| 121 |
| 122 // For most Windows versions, this experiment is disabled. |
| 123 if (!IsStickyDefaultBrowserPromptEnabled()) |
| 124 return; |
| 125 |
| 126 AddDefaultBrowserInfoBar(); |
| 127 ASSERT_EQ(1U, infobar_service()->infobar_count()); |
| 128 |
| 129 infobars::InfoBar* default_browser_infobar = infobar_service()->infobar_at(0); |
| 130 ConfirmInfoBarDelegate* default_browser_infobar_delegate = |
| 131 default_browser_infobar->delegate()->AsConfirmInfoBarDelegate(); |
| 132 |
| 133 // The sticky default browser prompt experiment should mean activating the |
| 134 // infobar's default action does not result in the infobar's destruction. |
| 135 // Instead, the infobar will ultimately be destroyed when the |
| 136 // DefaultBrowserWorker is done. To indicate this Accept() should return |
| 137 // false. |
| 138 EXPECT_FALSE(default_browser_infobar_delegate->Accept()); |
| 139 EXPECT_EQ(1U, infobar_service()->infobar_count()); |
| 140 |
| 141 // Spin the message loop to allow the FakeDefaultBrowserWorker to complete, |
| 142 // which should destroy the infobar. |
| 143 base::RunLoop().RunUntilIdle(); |
| 144 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 145 } |
| 146 #endif // defined(OS_WIN) |
| 147 |
| 148 } // namespace chrome |
| OLD | NEW |