[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 5 | #include "chrome/browser/extensions/default_apps.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 9 | #include "build/build_config.h" |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 10 | #include "chrome/browser/extensions/external_pref_loader.h" |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 11 | #include "chrome/common/chrome_paths.h" |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 12 | #include "chrome/common/pref_names.h" |
| 13 | #include "chrome/test/base/testing_profile.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 14 | #include "components/prefs/pref_service.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 15 | #include "content/public/test/browser_task_environment.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 16 | #include "extensions/common/extension.h" |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | using default_apps::Provider; |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 20 | |
| 21 | namespace extensions { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 22 | |
| 23 | class MockExternalLoader : public ExternalLoader { |
| 24 | public: |
| 25 | MockExternalLoader() {} |
| 26 | |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 27 | void StartLoading() override {} |
| 28 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 29 | private: |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 30 | ~MockExternalLoader() override {} |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | class DefaultAppsTest : public testing::Test { |
| 34 | public: |
fdoray | 2ce6dc22 | 2017-04-27 14:39:39 | [diff] [blame] | 35 | DefaultAppsTest() |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 36 | : task_environment_(content::BrowserTaskEnvironment::IO_MAINLOOP) {} |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 37 | ~DefaultAppsTest() override {} |
| 38 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 39 | private: |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 40 | content::BrowserTaskEnvironment task_environment_; |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 41 | }; |
| 42 | |
[email protected] | 13e062e | 2014-08-09 10:21:55 | [diff] [blame] | 43 | #if !defined(OS_CHROMEOS) |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 44 | // Chrome OS has different way of installing default apps. |
[email protected] | 3ed92b8e | 2012-08-30 17:59:23 | [diff] [blame] | 45 | // Android does not currently support installing apps via Chrome. |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 46 | TEST_F(DefaultAppsTest, Install) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 47 | std::unique_ptr<TestingProfile> profile(new TestingProfile()); |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 48 | ExternalLoader* loader = new MockExternalLoader(); |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 49 | |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 50 | Provider provider(profile.get(), NULL, loader, Manifest::INTERNAL, |
| 51 | Manifest::INTERNAL, Extension::NO_FLAGS); |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 52 | |
| 53 | // The default apps should be installed if kDefaultAppsInstallState |
| 54 | // is unknown. |
| 55 | EXPECT_TRUE(provider.ShouldInstallInProfile()); |
| 56 | int state = profile->GetPrefs()->GetInteger(prefs::kDefaultAppsInstallState); |
| 57 | EXPECT_TRUE(state == default_apps::kAlreadyInstalledDefaultApps); |
| 58 | |
| 59 | // The default apps should only be installed once. |
| 60 | EXPECT_FALSE(provider.ShouldInstallInProfile()); |
| 61 | state = profile->GetPrefs()->GetInteger(prefs::kDefaultAppsInstallState); |
| 62 | EXPECT_TRUE(state == default_apps::kAlreadyInstalledDefaultApps); |
| 63 | |
| 64 | // The default apps should not be installed if the state is |
| 65 | // kNeverProvideDefaultApps |
| 66 | profile->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, |
| 67 | default_apps::kNeverInstallDefaultApps); |
| 68 | EXPECT_FALSE(provider.ShouldInstallInProfile()); |
| 69 | state = profile->GetPrefs()->GetInteger(prefs::kDefaultAppsInstallState); |
| 70 | EXPECT_TRUE(state == default_apps::kNeverInstallDefaultApps); |
| 71 | |
| 72 | // The old default apps with kAlwaysInstallDefaultAppss should be migrated. |
| 73 | profile->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, |
| 74 | default_apps::kProvideLegacyDefaultApps); |
| 75 | EXPECT_TRUE(provider.ShouldInstallInProfile()); |
| 76 | state = profile->GetPrefs()->GetInteger(prefs::kDefaultAppsInstallState); |
| 77 | EXPECT_TRUE(state == default_apps::kAlreadyInstalledDefaultApps); |
| 78 | |
| 79 | class DefaultTestingProfile : public TestingProfile { |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 80 | bool WasCreatedByVersionOrLater(const std::string& version) override { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | }; |
| 84 | profile.reset(new DefaultTestingProfile); |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 85 | Provider provider2(profile.get(), NULL, loader, Manifest::INTERNAL, |
| 86 | Manifest::INTERNAL, Extension::NO_FLAGS); |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 87 | // The old default apps with kProvideLegacyDefaultApps should be migrated |
| 88 | // even if the profile version is older than Chrome version. |
| 89 | profile->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, |
| 90 | default_apps::kProvideLegacyDefaultApps); |
| 91 | EXPECT_TRUE(provider2.ShouldInstallInProfile()); |
| 92 | state = profile->GetPrefs()->GetInteger(prefs::kDefaultAppsInstallState); |
| 93 | EXPECT_TRUE(state == default_apps::kAlreadyInstalledDefaultApps); |
| 94 | } |
| 95 | #endif |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 96 | |
| 97 | } // namespace extensions |