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

Side by Side Diff: chrome/browser/ui/startup/default_browser_infobar_delegate_unittest.cc

Issue 1974153002: Add an experiment for the default browser infobar on Windows 10 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pkasting comments Created 4 years, 6 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 unified diff | Download patch
OLDNEW
(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 // Check if Chrome is the default browser.
34 shell_integration::DefaultWebClientState CheckIsDefaultImpl() override {
35 return shell_integration::NOT_DEFAULT;
36 }
37
38 // Set Chrome as the default browser.
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_);
Peter Kasting 2016/06/01 19:32:10 I think this class leaks |web_contents_| and |info
Patrick Monette 2016/06/01 22:07:40 The TestWebContentsFactory's documentation is pret
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_;
Peter Kasting 2016/06/01 19:32:10 Heh, I was suggesting subclassing this, but actual
Patrick Monette 2016/06/01 22:07:40 I was pleasantly surprised as well!
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 // The infobar delegate should allow itself to be destroyed immediately if the
112 // user activates the default action. Accept() should return true to indicate
113 // this.
114 EXPECT_TRUE(default_browser_infobar_delegate->Accept());
115 }
116
117 #if defined(OS_WIN)
118 TEST_F(DefaultBrowserInfoBarDelegateTest, StickyDefaultBrowserPrompt) {
119 EnableStickyDefaultBrowserPrompt();
120
121 // For most Windows versions, this experiment is disabled.
122 if (!IsStickyDefaultBrowserPromptEnabled())
123 return;
124
125 AddDefaultBrowserInfoBar();
126 ASSERT_EQ(1U, infobar_service()->infobar_count());
127
128 infobars::InfoBar* default_browser_infobar = infobar_service()->infobar_at(0);
129 ConfirmInfoBarDelegate* default_browser_infobar_delegate =
130 default_browser_infobar->delegate()->AsConfirmInfoBarDelegate();
131
132 // The sticky default browser prompt experiment should mean activating the
133 // infobar's default action does not result in the infobar's destruction.
134 // Instead, the infobar will ultimately be destroyed when the
135 // DefaultBrowserWorker is done. To indicate this Accept() should return
136 // false.
137 EXPECT_FALSE(default_browser_infobar_delegate->Accept());
138 EXPECT_EQ(1U, infobar_service()->infobar_count());
139
140 // Spin the message loop to allow the FakeDefaultBrowserWorker to complete,
141 // which should destroy the infobar.
142 base::RunLoop().RunUntilIdle();
143 EXPECT_EQ(0U, infobar_service()->infobar_count());
144 }
145 #endif // defined(OS_WIN)
146
147 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698