Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2011 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 "base/logging.h"
      6 #include "chrome/browser/extensions/apps_promo.h"
      7 #include "chrome/browser/prefs/browser_prefs.h"
      8 #include "chrome/browser/ui/webui/shown_sections_handler.h"
      9 #include "chrome/common/extensions/extension.h"
     10 #include "chrome/common/pref_names.h"
     11 #include "chrome/test/testing_browser_process.h"
     12 #include "chrome/test/testing_pref_service.h"
     13 #include "googleurl/src/gurl.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 
     16 namespace {
     17 
     18 const char kPromoId[] = "23123123";
     19 const char kPromoHeader[] = "Get great apps!";
     20 const char kPromoButton[] = "Click for apps!";
     21 const char kPromoLink[] = "http://apps.com";
     22 const char kPromoExpire[] = "No thanks.";
     23 
     24 } // namespace
     25 
     26 class ExtensionAppsPromo : public testing::Test {
     27  public:
     28   TestingPrefService* local_state() { return &local_state_; }
     29   TestingPrefService* prefs() { return &prefs_; }
     30   AppsPromo* apps_promo() { return apps_promo_; }
     31 
     32  protected:
     33   explicit ExtensionAppsPromo();
     34   virtual ~ExtensionAppsPromo();
     35 
     36   // testing::Test
     37   virtual void SetUp();
     38   virtual void TearDown();
     39 
     40  private:
     41   TestingPrefService local_state_;
     42   TestingPrefService prefs_;
     43   AppsPromo* apps_promo_;
     44 };
     45 
     46 ExtensionAppsPromo::ExtensionAppsPromo() :
     47     apps_promo_(new AppsPromo(&prefs_)) {
     48 }
     49 
     50 ExtensionAppsPromo::~ExtensionAppsPromo() {
     51   delete apps_promo_;
     52 }
     53 
     54 void ExtensionAppsPromo::SetUp() {
     55   browser::RegisterLocalState(&local_state_);
     56   browser::RegisterUserPrefs(&prefs_);
     57 
     58   TestingBrowserProcess* testing_browser_process =
     59       static_cast<TestingBrowserProcess*>(g_browser_process);
     60   testing_browser_process->SetPrefService(&local_state_);
     61 }
     62 
     63 void ExtensionAppsPromo::TearDown() {
     64   TestingBrowserProcess* testing_browser_process =
     65       static_cast<TestingBrowserProcess*>(g_browser_process);
     66   testing_browser_process->SetPrefService(NULL);
     67 }
     68 
     69 // TODO(dpolukhin): On Chrome OS all apps are installed via external extensions,
     70 // and the web store promo is never shown.
     71 #if !defined(OS_CHROMEOS)
     72 TEST_F(ExtensionAppsPromo, HappyPath) {
     73   const ExtensionIdSet& default_app_ids = apps_promo()->old_default_apps();
     74 
     75   EXPECT_GT(default_app_ids.size(), 0u);
     76 
     77   // The promo counter should default to the max, since we only use the counter
     78   // if they were installed from older versions of Chrome.
     79   EXPECT_EQ(AppsPromo::kDefaultAppsCounterMax + 1,
     80             apps_promo()->GetPromoCounter());
     81 
     82   // The app launcher and promo should not be shown if there are no extensions
     83   // installed and no promo is set.
     84   ExtensionIdSet installed_ids;
     85   bool promo_just_expired = false;
     86   EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale());
     87   EXPECT_FALSE(apps_promo()->ShouldShowAppLauncher(installed_ids));
     88   EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
     89                                              &promo_just_expired));
     90   EXPECT_FALSE(promo_just_expired);
     91 
     92   // Once the promo is set, we show both the promo and app launcher.
     93   AppsPromo::SetPromo(
     94       kPromoId, kPromoHeader, kPromoButton, GURL(kPromoLink), kPromoExpire);
     95 
     96   EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
     97   EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
     98   EXPECT_TRUE(apps_promo()->ShouldShowPromo(installed_ids,
     99                                            &promo_just_expired));
    100   EXPECT_FALSE(promo_just_expired);
    101 
    102 
    103   // Now install an app and the promo should not be shown.
    104   installed_ids.insert(*(default_app_ids.begin()));
    105   EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
    106   EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
    107   EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
    108                                              &promo_just_expired));
    109   EXPECT_FALSE(promo_just_expired);
    110 
    111   // Even if the user installs the exact set of default apps, we still don't
    112   // show the promo.
    113   installed_ids = default_app_ids;
    114   EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
    115   EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
    116   EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
    117                                              &promo_just_expired));
    118   EXPECT_FALSE(promo_just_expired);
    119 
    120   // If the user then uninstalls the apps, the app launcher should remain
    121   // and the promo should return.
    122   installed_ids.clear();
    123   EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
    124   EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
    125   EXPECT_TRUE(apps_promo()->ShouldShowPromo(installed_ids,
    126                                             &promo_just_expired));
    127   EXPECT_FALSE(promo_just_expired);
    128 }
    129 
    130 // Tests get and set of promo content.
    131 TEST_F(ExtensionAppsPromo, PromoPrefs) {
    132   // Store a promo....
    133   AppsPromo::SetPromo(
    134       kPromoId, kPromoHeader, kPromoButton, GURL(kPromoLink), kPromoExpire);
    135 
    136   // ... then make sure AppsPromo can access it.
    137   EXPECT_EQ(kPromoId, AppsPromo::GetPromoId());
    138   EXPECT_EQ(kPromoHeader, AppsPromo::GetPromoHeaderText());
    139   EXPECT_EQ(kPromoButton, AppsPromo::GetPromoButtonText());
    140   EXPECT_EQ(GURL(kPromoLink), AppsPromo::GetPromoLink());
    141   EXPECT_EQ(kPromoExpire, AppsPromo::GetPromoExpireText());
    142   EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
    143 
    144   AppsPromo::ClearPromo();
    145   EXPECT_EQ("", AppsPromo::GetPromoId());
    146   EXPECT_EQ("", AppsPromo::GetPromoHeaderText());
    147   EXPECT_EQ("", AppsPromo::GetPromoButtonText());
    148   EXPECT_EQ(GURL(""), AppsPromo::GetPromoLink());
    149   EXPECT_EQ("", AppsPromo::GetPromoExpireText());
    150   EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale());
    151 }
    152 
    153 // Tests that the apps section is maxmized when showing a promo for the first
    154 // time.
    155 TEST_F(ExtensionAppsPromo, UpdatePromoFocus) {
    156   ExtensionIdSet installed_ids;
    157 
    158   bool promo_just_expired = false;
    159   EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
    160                                              &promo_just_expired));
    161   EXPECT_FALSE(promo_just_expired);
    162 
    163   // Set the promo content.
    164   AppsPromo::SetPromo(
    165       kPromoId, kPromoHeader, kPromoButton, GURL(kPromoLink), kPromoExpire);
    166 
    167   // After asking if we should show the promo, the
    168   EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
    169   EXPECT_TRUE(apps_promo()->ShouldShowPromo(installed_ids,
    170                                             &promo_just_expired));
    171   apps_promo()->MaximizeAppsIfFirstView();
    172 
    173   EXPECT_TRUE(
    174       (ShownSectionsHandler::GetShownSections(prefs()) & APPS) != 0);
    175   EXPECT_FALSE(
    176       (ShownSectionsHandler::GetShownSections(prefs()) & THUMB) != 0);
    177 
    178   apps_promo()->HidePromo();
    179 
    180   EXPECT_TRUE((ShownSectionsHandler::GetShownSections(prefs()) &
    181                (MENU_APPS | THUMB)) != 0);
    182 }
    183 #endif  // OS_CHROMEOS
    184