summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/apps_promo_unittest.cc
blob: b9e2e5e8fedb60cd5e072d002ace6a51d28a57e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/logging.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/apps_promo.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_pref_service.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

const char kPromoId[] = "23123123";
const char kPromoHeader[] = "Get great apps!";
const char kPromoButton[] = "Click for apps!";
const char kPromoLink[] = "http://apps.com";
const char kPromoLogo[] = "chrome://theme/IDR_WEBSTORE_ICON";
const char kPromoExpire[] = "No thanks.";
const int kPromoUserGroup =
    AppsPromo::USERS_NEW | AppsPromo::USERS_EXISTING;

} // namespace

class ExtensionAppsPromo : public testing::Test {
 public:
  TestingPrefService* prefs() { return &prefs_; }
  AppsPromo* apps_promo() { return &apps_promo_; }

 protected:
  explicit ExtensionAppsPromo();

  // testing::Test
  virtual void SetUp();

 private:
  TestingPrefService prefs_;
  ScopedTestingLocalState local_state_;
  AppsPromo apps_promo_;
};

ExtensionAppsPromo::ExtensionAppsPromo()
    : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)),
      apps_promo_(&prefs_) {
}

void ExtensionAppsPromo::SetUp() {
  browser::RegisterUserPrefs(&prefs_);
}

// TODO(dpolukhin): On Chrome OS all apps are installed via external extensions,
// and the web store promo is never shown.
#if !defined(OS_CHROMEOS)

TEST_F(ExtensionAppsPromo, HappyPath) {
  const extensions::ExtensionIdSet& default_app_ids =
      apps_promo()->old_default_apps();

  EXPECT_GT(default_app_ids.size(), 0u);

  // The promo counter should default to the max, since we only use the counter
  // if they were installed from older versions of Chrome.
  EXPECT_EQ(AppsPromo::kDefaultAppsCounterMax + 1,
            apps_promo()->GetPromoCounter());

  // The app launcher and promo should not be shown if there are no extensions
  // installed and no promo is set.
  extensions::ExtensionIdSet installed_ids;
  bool promo_just_expired = false;
  EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale());
  EXPECT_FALSE(apps_promo()->ShouldShowAppLauncher(installed_ids));
  EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
                                             &promo_just_expired));
  EXPECT_FALSE(promo_just_expired);

  // Make sure the web store can be supported even when the promo is not active.
  AppsPromo::SetWebStoreSupportedForLocale(true);
  EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale());
  EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
  EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
                                             &promo_just_expired));

  // We should be able to disable the web store as well.
  AppsPromo::SetWebStoreSupportedForLocale(false);
  EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale());
  EXPECT_FALSE(apps_promo()->ShouldShowAppLauncher(installed_ids));
  EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
                                             &promo_just_expired));

  // Once the promo is set, we show both the promo and app launcher.
  AppsPromo::PromoData promo_data(kPromoId, kPromoHeader, kPromoButton,
                                  GURL(kPromoLink), kPromoExpire, GURL(""),
                                  kPromoUserGroup);
  AppsPromo::SetPromo(promo_data);
  AppsPromo::SetWebStoreSupportedForLocale(true);
  EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
  EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
  EXPECT_TRUE(apps_promo()->ShouldShowPromo(installed_ids,
                                            &promo_just_expired));
  EXPECT_FALSE(promo_just_expired);

  // Now install an app and the promo should not be shown.
  installed_ids.insert(*(default_app_ids.begin()));
  EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
  EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
  EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
                                             &promo_just_expired));
  EXPECT_FALSE(promo_just_expired);

  // Even if the user installs the exact set of default apps, we still don't
  // show the promo.
  installed_ids = default_app_ids;
  EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
  EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
  EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
                                             &promo_just_expired));
  EXPECT_FALSE(promo_just_expired);

  // If the user then uninstalls the apps, the app launcher should remain
  // and the promo should return.
  installed_ids.clear();
  EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
  EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
  EXPECT_TRUE(apps_promo()->ShouldShowPromo(installed_ids,
                                            &promo_just_expired));
  EXPECT_FALSE(promo_just_expired);
}

// Tests get and set of promo content.
TEST_F(ExtensionAppsPromo, PromoPrefs) {
  // Store a promo....
  AppsPromo::PromoData promo_data(kPromoId, kPromoHeader, kPromoButton,
                                  GURL(kPromoLink), kPromoExpire, GURL(""),
                                  kPromoUserGroup);
  AppsPromo::SetPromo(promo_data);

  // ... then make sure AppsPromo can access it.
  AppsPromo::PromoData actual_data = AppsPromo::GetPromo();
  EXPECT_EQ(kPromoId, actual_data.id);
  EXPECT_EQ(kPromoHeader, actual_data.header);
  EXPECT_EQ(kPromoButton, actual_data.button);
  EXPECT_EQ(GURL(kPromoLink), actual_data.link);
  EXPECT_EQ(kPromoExpire, actual_data.expire);
  EXPECT_EQ(kPromoUserGroup, actual_data.user_group);
  // The promo logo should be the default value.
  EXPECT_EQ(GURL(kPromoLogo), actual_data.logo);
  EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());

  AppsPromo::ClearPromo();
  actual_data = AppsPromo::GetPromo();
  EXPECT_EQ("", actual_data.id);
  EXPECT_EQ("", actual_data.header);
  EXPECT_EQ("", actual_data.button);
  EXPECT_EQ(GURL(""), actual_data.link);
  EXPECT_EQ("", actual_data.expire);
  EXPECT_EQ(AppsPromo::USERS_NONE, actual_data.user_group);
  EXPECT_EQ(GURL(kPromoLogo), actual_data.logo);
  EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale());

  // Make sure we can set the logo to something other than the default.
  std::string promo_logo = "data:image/png;base64,iVBORw0kGgoAAAN";
  promo_data.logo = GURL(promo_logo);
  AppsPromo::SetPromo(promo_data);
  EXPECT_EQ(GURL(promo_logo), AppsPromo::GetPromo().logo);
  EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());

  // Verify that the default is returned instead of HTTP or HTTPS URLs.
  promo_data.logo = GURL("http://google.com/logo.png");
  AppsPromo::SetPromo(promo_data);
  EXPECT_EQ(GURL(kPromoLogo), AppsPromo::GetPromo().logo);
  EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());

  promo_data.logo = GURL("https://google.com/logo.png");
  AppsPromo::SetPromo(promo_data);
  EXPECT_EQ(GURL(kPromoLogo), AppsPromo::GetPromo().logo);
  EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());

  // Try an invalid URL.
  promo_data.logo = GURL("sldkfjlsdn");
  AppsPromo::SetPromo(promo_data);
  EXPECT_EQ(GURL(kPromoLogo), AppsPromo::GetPromo().logo);
  EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());

  // Try the web store supported flag.
  EXPECT_FALSE(AppsPromo::IsWebStoreSupportedForLocale());
  AppsPromo::SetWebStoreSupportedForLocale(true);
  EXPECT_TRUE(AppsPromo::IsWebStoreSupportedForLocale());
  AppsPromo::SetWebStoreSupportedForLocale(false);
  EXPECT_FALSE(AppsPromo::IsWebStoreSupportedForLocale());

  // Try setting and getting the source logo URL.
  GURL expected_source("https://www.google.com/images/test.png");
  AppsPromo::SetSourcePromoLogoURL(expected_source);
  EXPECT_EQ(expected_source, AppsPromo::GetSourcePromoLogoURL());
}

TEST_F(ExtensionAppsPromo, PromoHiddenByPref) {
  prefs()->SetInteger(prefs::kAppsPromoCounter, 0);
  prefs()->SetBoolean(prefs::kDefaultAppsInstalled, true);

  // When the "hide" pref is false, the promo should still appear.
  prefs()->SetBoolean(prefs::kNtpHideWebStorePromo, false);
  AppsPromo::PromoData promo_data(
      kPromoId, kPromoHeader, kPromoButton, GURL(kPromoLink), kPromoExpire,
      GURL(""), AppsPromo::USERS_NEW | AppsPromo::USERS_EXISTING);
  AppsPromo::SetPromo(promo_data);
  bool just_expired;
  bool show_promo = apps_promo()->ShouldShowPromo(
      apps_promo()->old_default_apps(), &just_expired);
  EXPECT_TRUE(show_promo);

  // When the "hide" pref is true, the promo should NOT appear.
  prefs()->SetBoolean(prefs::kNtpHideWebStorePromo, true);
  show_promo = apps_promo()->ShouldShowPromo(
      apps_promo()->old_default_apps(), &just_expired);
  EXPECT_FALSE(show_promo);
}

#endif // OS_CHROMEOS