File: default_browser_infobar_delegate_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (146 lines) | stat: -rw-r--r-- 5,158 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/startup/default_browser_infobar_delegate.h"

#include <memory>
#include <string>

#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/test/base/testing_profile.h"
#include "components/infobars/core/infobar.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_web_contents_factory.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chrome {

// An implementation of DefaultBrowserWorker that will simply invoke the
// 'on_finished_callback' immediately.
class FakeDefaultBrowserWorker
    : public shell_integration::DefaultBrowserWorker {
 public:
  FakeDefaultBrowserWorker(
      const shell_integration::DefaultWebClientWorkerCallback& callback)
      : shell_integration::DefaultBrowserWorker(callback) {}

 private:
  ~FakeDefaultBrowserWorker() override = default;

  shell_integration::DefaultWebClientState CheckIsDefaultImpl() override {
    return shell_integration::NOT_DEFAULT;
  }

  void SetAsDefaultImpl(const base::Closure& on_finished_callback) override {
    on_finished_callback.Run();
  }

  DISALLOW_COPY_AND_ASSIGN(FakeDefaultBrowserWorker);
};

class FakeDefaultBrowserInfoBarDelegate : public DefaultBrowserInfoBarDelegate {
 public:
  FakeDefaultBrowserInfoBarDelegate()
      : DefaultBrowserInfoBarDelegate(nullptr) {}

 private:
  scoped_refptr<shell_integration::DefaultBrowserWorker>
  CreateDefaultBrowserWorker(
      const shell_integration::DefaultWebClientWorkerCallback& callback)
      override {
    return new FakeDefaultBrowserWorker(callback);
  }

  DISALLOW_COPY_AND_ASSIGN(FakeDefaultBrowserInfoBarDelegate);
};

class DefaultBrowserInfoBarDelegateTest : public ::testing::Test {
 public:
  DefaultBrowserInfoBarDelegateTest()
      : profile_(new TestingProfile()),
        web_contents_(factory_.CreateWebContents(profile_.get())) {
    InfoBarService::CreateForWebContents(web_contents_);
    infobar_service_ = InfoBarService::FromWebContents(web_contents_);
  }

 protected:
  void EnableStickyDefaultBrowserPrompt() {
    scoped_feature_list_.InitAndEnableFeature(kStickyDefaultBrowserPrompt);
  }

  void AddDefaultBrowserInfoBar() {
    infobar_service_->AddInfoBar(infobar_service_->CreateConfirmInfoBar(
        base::MakeUnique<FakeDefaultBrowserInfoBarDelegate>()));
  }

  InfoBarService* infobar_service() { return infobar_service_; }

 private:
  // The DefaultBrowserWorker requires a FILE thread. Also provides a
  // SingleThreadTaskRunner for the test profile and the default browser prompt.
  content::TestBrowserThreadBundle thread_bundle;

  // Required to get an InfoBarService.
  std::unique_ptr<TestingProfile> profile_;
  content::TestWebContentsFactory factory_;
  content::WebContents* web_contents_;

  // Manages the default browser prompt.
  InfoBarService* infobar_service_;

  base::test::ScopedFeatureList scoped_feature_list_;

  DISALLOW_COPY_AND_ASSIGN(DefaultBrowserInfoBarDelegateTest);
};

TEST_F(DefaultBrowserInfoBarDelegateTest, DefaultBehavior) {
  AddDefaultBrowserInfoBar();
  ASSERT_EQ(1U, infobar_service()->infobar_count());

  infobars::InfoBar* default_browser_infobar = infobar_service()->infobar_at(0);
  ConfirmInfoBarDelegate* default_browser_infobar_delegate =
      default_browser_infobar->delegate()->AsConfirmInfoBarDelegate();

  // When the sticky default browser prompt experiment is not enabled, the
  // infobar delegate should allow itself to be destroyed immediately if the
  // user activates the default action. Accept() should return true to indicate
  // this.
  EXPECT_TRUE(default_browser_infobar_delegate->Accept());
}

#if defined(OS_WIN)
TEST_F(DefaultBrowserInfoBarDelegateTest, StickyDefaultBrowserPrompt) {
  EnableStickyDefaultBrowserPrompt();

  // For most Windows versions, this experiment is disabled.
  if (!IsStickyDefaultBrowserPromptEnabled())
    return;

  AddDefaultBrowserInfoBar();
  ASSERT_EQ(1U, infobar_service()->infobar_count());

  infobars::InfoBar* default_browser_infobar = infobar_service()->infobar_at(0);
  ConfirmInfoBarDelegate* default_browser_infobar_delegate =
      default_browser_infobar->delegate()->AsConfirmInfoBarDelegate();

  // The sticky default browser prompt experiment should mean activating the
  // infobar's default action does not result in the infobar's destruction.
  // Instead, the infobar will ultimately be destroyed when the
  // DefaultBrowserWorker is done. To indicate this Accept() should return
  // false.
  EXPECT_FALSE(default_browser_infobar_delegate->Accept());
  EXPECT_EQ(1U, infobar_service()->infobar_count());

  // Spin the message loop to allow the FakeDefaultBrowserWorker to complete,
  // which should destroy the infobar.
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(0U, infobar_service()->infobar_count());
}
#endif  // defined(OS_WIN)

}  // namespace chrome