Archana Simha | 6a867df | 2019-11-26 22:25:54 | [diff] [blame] | 1 | // 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 | |
| 14 | namespace extensions { |
| 15 | |
| 16 | class 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 Simha | 7cd6dc50 | 2019-12-04 21:42:41 | [diff] [blame] | 24 | extensions_features::kExtensionsCheckup, |
| 25 | {{extensions_features::kExtensionsCheckupEntryPointParameter, |
Archana Simha | 6a867df | 2019-11-26 22:25:54 | [diff] [blame] | 26 | 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 Simha | 7cd6dc50 | 2019-12-04 21:42:41 | [diff] [blame] | 59 | if (GetParam() == extensions_features::kNtpPromoEntryPoint) |
Archana Simha | 6a867df | 2019-11-26 22:25:54 | [diff] [blame] | 60 | 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 Simha | 7cd6dc50 | 2019-12-04 21:42:41 | [diff] [blame] | 68 | if (GetParam() == extensions_features::kNtpPromoEntryPoint) |
Archana Simha | 6a867df | 2019-11-26 22:25:54 | [diff] [blame] | 69 | 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. |
| 81 | TEST_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. |
| 88 | TEST_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. |
| 95 | TEST_P(ExtensionCheckupTest, OnlyOneUserInstalledExtension) { |
| 96 | AddUserInstalledExtension(); |
| 97 | VerifyNonExperimentCheckupDisabled(); |
| 98 | EXPECT_TRUE(ShouldShowExperimentCheckup()); |
| 99 | } |
| 100 | |
| 101 | TEST_P(ExtensionCheckupTest, UserAndNonUserInstalledExtensions) { |
| 102 | AddUserInstalledExtension(); |
| 103 | AddExemptExtensions(); |
| 104 | VerifyNonExperimentCheckupDisabled(); |
| 105 | EXPECT_TRUE(ShouldShowExperimentCheckup()); |
| 106 | } |
| 107 | |
Archana Simha | 7cd6dc50 | 2019-12-04 21:42:41 | [diff] [blame] | 108 | INSTANTIATE_TEST_SUITE_P( |
Ilia Samsonov | 5939567 | 2019-12-09 12:01:39 | [diff] [blame] | 109 | All, |
Archana Simha | 7cd6dc50 | 2019-12-04 21:42:41 | [diff] [blame] | 110 | ExtensionCheckupTest, |
| 111 | ::testing::Values(extensions_features::kNtpPromoEntryPoint, |
| 112 | extensions_features::kStartupEntryPoint)); |
Archana Simha | 6a867df | 2019-11-26 22:25:54 | [diff] [blame] | 113 | } // namespace extensions |