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