blob: 28e203d5b53378d2483fe41901a692a5f72d995a [file] [log] [blame]
Archana Simha6a867df2019-11-26 22:25:541// Copyright 2019 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/extensions/extension_checkup.h"
6
7#include "base/test/scoped_feature_list.h"
8#include "chrome/browser/extensions/extension_service.h"
9#include "chrome/browser/extensions/extension_service_test_base.h"
10#include "extensions/common/extension_builder.h"
11#include "extensions/common/extension_features.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace extensions {
15
16class ExtensionCheckupTest : public ExtensionServiceTestBase,
17 public testing::WithParamInterface<const char*> {
18 public:
19 ExtensionCheckupTest() {}
20 ~ExtensionCheckupTest() override {}
21
22 void SetUp() override {
23 scoped_feature_list_.InitAndEnableFeatureWithParameters(
Archana Simha7cd6dc502019-12-04 21:42:4124 extensions_features::kExtensionsCheckup,
25 {{extensions_features::kExtensionsCheckupEntryPointParameter,
Archana Simha6a867df2019-11-26 22:25:5426 GetParam()}});
27 ExtensionServiceTestBase::SetUp();
28 InitializeEmptyExtensionService();
29 service()->Init();
30 }
31
32 // Adds a user installed extension.
33 void AddUserInstalledExtension() {
34 scoped_refptr<const Extension> extension = ExtensionBuilder("foo").Build();
35 service()->AddExtension(extension.get());
36 }
37
38 void AddExemptExtensions() {
39 // Install policy extension.
40 scoped_refptr<const Extension> policy_extension =
41 ExtensionBuilder("policy")
42 .SetLocation(Manifest::EXTERNAL_POLICY)
43 .Build();
44 service()->AddExtension(policy_extension.get());
45 // Install component extension.
46 scoped_refptr<const Extension> component_extension =
47 ExtensionBuilder("component").SetLocation(Manifest::COMPONENT).Build();
48 service()->AddExtension(component_extension.get());
49 // Load a default installed extension.
50 int creation_flags = Extension::WAS_INSTALLED_BY_DEFAULT;
51 scoped_refptr<const Extension> default_install_extension =
52 ExtensionBuilder("default").AddFlags(creation_flags).Build();
53 service()->AddExtension(default_install_extension.get());
54 ASSERT_TRUE(default_install_extension);
55 }
56
57 // Verify the extensions checkup behavior.
58 bool ShouldShowExperimentCheckup() {
Archana Simha7cd6dc502019-12-04 21:42:4159 if (GetParam() == extensions_features::kNtpPromoEntryPoint)
Archana Simha6a867df2019-11-26 22:25:5460 return ShouldShowExtensionsCheckupPromo(browser_context());
61 return ShouldShowExtensionsCheckupOnStartup(browser_context());
62 }
63
64 // Verify that the opposite entry point will always return false (i.e if the
65 // user is supposed to see the middle slot promo entry point they should never
66 // see the extensions checkup upon startup).
67 void VerifyNonExperimentCheckupDisabled() {
Archana Simha7cd6dc502019-12-04 21:42:4168 if (GetParam() == extensions_features::kNtpPromoEntryPoint)
Archana Simha6a867df2019-11-26 22:25:5469 EXPECT_FALSE(ShouldShowExtensionsCheckupOnStartup(browser_context()));
70 else
71 EXPECT_FALSE(ShouldShowExtensionsCheckupPromo(browser_context()));
72 }
73
74 private:
75 base::test::ScopedFeatureList scoped_feature_list_;
76
77 DISALLOW_COPY_AND_ASSIGN(ExtensionCheckupTest);
78};
79
80// Checkup is not shown if no extensions are installed.
81TEST_P(ExtensionCheckupTest, NoInstalledExtensions) {
82 VerifyNonExperimentCheckupDisabled();
83 EXPECT_FALSE(ShouldShowExperimentCheckup());
84}
85
86// Checkup is not shown if the only extensions installed are policy
87// installed, component extensions, or installed by default.
88TEST_P(ExtensionCheckupTest, NoUserInstalledExtensions) {
89 AddExemptExtensions();
90 VerifyNonExperimentCheckupDisabled();
91 EXPECT_FALSE(ShouldShowExperimentCheckup());
92}
93
94// Checkup is shown if at least one non policy extension is installed.
95TEST_P(ExtensionCheckupTest, OnlyOneUserInstalledExtension) {
96 AddUserInstalledExtension();
97 VerifyNonExperimentCheckupDisabled();
98 EXPECT_TRUE(ShouldShowExperimentCheckup());
99}
100
101TEST_P(ExtensionCheckupTest, UserAndNonUserInstalledExtensions) {
102 AddUserInstalledExtension();
103 AddExemptExtensions();
104 VerifyNonExperimentCheckupDisabled();
105 EXPECT_TRUE(ShouldShowExperimentCheckup());
106}
107
Archana Simha7cd6dc502019-12-04 21:42:41108INSTANTIATE_TEST_SUITE_P(
Ilia Samsonov59395672019-12-09 12:01:39109 All,
Archana Simha7cd6dc502019-12-04 21:42:41110 ExtensionCheckupTest,
111 ::testing::Values(extensions_features::kNtpPromoEntryPoint,
112 extensions_features::kStartupEntryPoint));
Archana Simha6a867df2019-11-26 22:25:54113} // namespace extensions