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