OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 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 #import "ios/chrome/browser/ui/ntp/notification_promo_whats_new.h" |
| 6 |
| 7 #include <map> |
| 8 |
| 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/test/user_action_tester.h" |
| 11 #include "base/time/time.h" |
| 12 #include "base/values.h" |
| 13 #include "components/metrics/metrics_pref_names.h" |
| 14 #include "components/prefs/pref_registry_simple.h" |
| 15 #include "components/prefs/testing_pref_service.h" |
| 16 #include "components/variations/variations_associated_data.h" |
| 17 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 18 #include "ios/chrome/grit/ios_chromium_strings.h" |
| 19 #include "ios/public/provider/chrome/browser/images/whats_new_icon.h" |
| 20 #include "testing/platform_test.h" |
| 21 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "url/gurl.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 // Test fixture for NotificationPromoWhatsNew. |
| 27 class NotificationPromoWhatsNewTest : public PlatformTest { |
| 28 public: |
| 29 NotificationPromoWhatsNewTest() |
| 30 : promo_(&local_state_), |
| 31 field_trial_list_(new base::FieldTrialList(NULL)) { |
| 32 ios::NotificationPromo::RegisterPrefs(local_state_.registry()); |
| 33 local_state_.registry()->RegisterInt64Pref(metrics::prefs::kInstallDate, 0); |
| 34 } |
| 35 |
| 36 ~NotificationPromoWhatsNewTest() override { |
| 37 variations::testing::ClearAllVariationParams(); |
| 38 } |
| 39 |
| 40 void TearDown() override { |
| 41 promo_.ClearAndInitFromJson(base::DictionaryValue()); |
| 42 PlatformTest::TearDown(); |
| 43 } |
| 44 |
| 45 // Sets up a mock finch trial and inits the NotificationPromoWhatsNew. All |
| 46 // parameters will be added to the list of finch parameters. |
| 47 void Init(const std::string& start, |
| 48 const std::string& end, |
| 49 const std::string& promo_text, |
| 50 const std::string& promo_id, |
| 51 const std::string& promo_type, |
| 52 const std::string& url, |
| 53 const std::string& command, |
| 54 const std::string& metric_name, |
| 55 const std::string& icon, |
| 56 const std::string& seconds_since_install, |
| 57 const std::string& max_seconds_since_install) { |
| 58 std::map<std::string, std::string> field_trial_params; |
| 59 field_trial_params["start"] = start; |
| 60 field_trial_params["end"] = end; |
| 61 field_trial_params["promo_text"] = promo_text; |
| 62 field_trial_params["promo_id"] = promo_id; |
| 63 field_trial_params["promo_type"] = promo_type; |
| 64 field_trial_params["url"] = url; |
| 65 field_trial_params["command"] = command; |
| 66 field_trial_params["metric_name"] = metric_name; |
| 67 field_trial_params["icon"] = icon; |
| 68 field_trial_params["seconds_since_install"] = seconds_since_install; |
| 69 field_trial_params["max_seconds_since_install"] = max_seconds_since_install; |
| 70 |
| 71 variations::AssociateVariationParams("IOSNTPPromotion", "Group1", |
| 72 field_trial_params); |
| 73 base::FieldTrialList::CreateFieldTrial("IOSNTPPromotion", "Group1"); |
| 74 |
| 75 promo_.Init(); |
| 76 } |
| 77 |
| 78 // Tests that |promo_text|, |promo_type|, |url|, |command_id|, and |icon| |
| 79 // equal their respective values in |promo_|, and that |valid| matches the |
| 80 // return value of |promo_|'s |CanShow()| method. |icon| is verified only if |
| 81 // |valid| is true. |
| 82 void RunTests(const std::string& promo_text, |
| 83 const std::string& promo_type, |
| 84 const std::string& url, |
| 85 int command_id, |
| 86 WhatsNewIcon icon, |
| 87 bool valid) { |
| 88 EXPECT_EQ(promo_text, promo_.promo_text()); |
| 89 EXPECT_EQ(promo_type, promo_.promo_type()); |
| 90 if (promo_type == "url") |
| 91 EXPECT_EQ(url, promo_.url().spec()); |
| 92 else |
| 93 EXPECT_EQ(command_id, promo_.command_id()); |
| 94 |
| 95 EXPECT_EQ(valid, promo_.CanShow()); |
| 96 // |icon()| is set only if the promo is valid. |
| 97 if (valid) |
| 98 EXPECT_EQ(icon, promo_.icon()); |
| 99 } |
| 100 |
| 101 protected: |
| 102 TestingPrefServiceSimple local_state_; |
| 103 NotificationPromoWhatsNew promo_; |
| 104 |
| 105 private: |
| 106 std::unique_ptr<base::FieldTrialList> field_trial_list_; |
| 107 }; |
| 108 |
| 109 // Test that a command-based, valid promo is shown with the correct text. |
| 110 TEST_F(NotificationPromoWhatsNewTest, NotificationPromoCommandTest) { |
| 111 Init("3 Aug 1999 9:26:06 GMT", "3 Aug 2199 9:26:06 GMT", |
| 112 "IDS_IOS_APP_RATING_PROMO_STRING", "0", "chrome_command", "", |
| 113 "ratethisapp", "RateThisAppPromo", "logo", "0", "0"); |
| 114 RunTests(l10n_util::GetStringUTF8(IDS_IOS_APP_RATING_PROMO_STRING), |
| 115 "chrome_command", "", IDC_RATE_THIS_APP, WHATS_NEW_LOGO, true); |
| 116 } |
| 117 |
| 118 // Test that a url-based, valid promo is shown with the correct text and icon. |
| 119 TEST_F(NotificationPromoWhatsNewTest, NotificationPromoURLTest) { |
| 120 Init("3 Aug 1999 9:26:06 GMT", "3 Aug 2199 9:26:06 GMT", "Test URL", "0", |
| 121 "url", "http://blog.chromium.org", "", "TestURLPromo", "", "0", "0"); |
| 122 RunTests("Test URL", "url", "http://blog.chromium.org/", 0, WHATS_NEW_INFO, |
| 123 true); |
| 124 } |
| 125 |
| 126 // Test that an invalid promo is not shown. |
| 127 TEST_F(NotificationPromoWhatsNewTest, NotificationPromoInvalidTest) { |
| 128 Init("3 Aug 1999 9:26:06 GMT", "3 Aug 2199 9:26:06 GMT", "Test URL", "0", |
| 129 "url", "", "", "TestURLPromo", "", "0", "0"); |
| 130 RunTests("Test URL", "url", "", 0, WHATS_NEW_INFO, false); |
| 131 } |
| 132 |
| 133 // Test that if max_seconds_since_install is set, and the current time is before |
| 134 // the cut off, the promo still shows. |
| 135 TEST_F(NotificationPromoWhatsNewTest, MaxSecondsSinceInstallSuccessTest) { |
| 136 // Init with max_seconds_since_install set to 2 days. |
| 137 Init("3 Aug 1999 9:26:06 GMT", "3 Aug 2199 9:26:06 GMT", |
| 138 "IDS_IOS_APP_RATING_PROMO_STRING", "0", "chrome_command", "", |
| 139 "ratethisapp", "RateThisAppPromo", "logo", "0", "172800"); |
| 140 // Set install date to one day before now. |
| 141 base::Time one_day_before_now_time = |
| 142 base::Time::Now() - base::TimeDelta::FromDays(1); |
| 143 int64_t one_day_before_now = one_day_before_now_time.ToTimeT(); |
| 144 local_state_.SetInt64(metrics::prefs::kInstallDate, one_day_before_now); |
| 145 // Expect the promo to show since install date was one day ago, and the promo |
| 146 // can show until 2 days after install date. |
| 147 EXPECT_TRUE(promo_.CanShow()); |
| 148 } |
| 149 |
| 150 // Test that if max_seconds_since_install is set, and the current time is after |
| 151 // the cut off, the promo does not show. |
| 152 TEST_F(NotificationPromoWhatsNewTest, MaxSecondsSinceInstallFailureTest) { |
| 153 // Init with max_seconds_since_install set to 2 days. |
| 154 Init("3 Aug 1999 9:26:06 GMT", "3 Aug 2199 9:26:06 GMT", |
| 155 "IDS_IOS_APP_RATING_PROMO_STRING", "0", "chrome_command", "", |
| 156 "ratethisapp", "RateThisAppPromo", "logo", "0", "172800"); |
| 157 // Set install date to three days before now. |
| 158 base::Time three_days_before_now_time = |
| 159 base::Time::Now() - base::TimeDelta::FromDays(3); |
| 160 int64_t three_days_before_now = three_days_before_now_time.ToTimeT(); |
| 161 local_state_.SetInt64(metrics::prefs::kInstallDate, three_days_before_now); |
| 162 // Expect the promo not to show since install date was three days ago, and |
| 163 // the promo can show until 2 days after install date. |
| 164 EXPECT_FALSE(promo_.CanShow()); |
| 165 } |
| 166 // Test that if seconds_since_install is set, and the current time is after |
| 167 // install_date + seconds_since_install, the promo still shows. |
| 168 TEST_F(NotificationPromoWhatsNewTest, SecondsSinceInstallSuccessTest) { |
| 169 // Init with seconds_since_install set to 2 days. |
| 170 Init("3 Aug 1999 9:26:06 GMT", "3 Aug 2199 9:26:06 GMT", |
| 171 "IDS_IOS_APP_RATING_PROMO_STRING", "0", "chrome_command", "", |
| 172 "ratethisapp", "RateThisAppPromo", "logo", "172800", "0"); |
| 173 // Set install date to three days before now. |
| 174 base::Time three_days_before_now_time = |
| 175 base::Time::Now() - base::TimeDelta::FromDays(3); |
| 176 int64_t three_days_before_now = three_days_before_now_time.ToTimeT(); |
| 177 local_state_.SetInt64(metrics::prefs::kInstallDate, three_days_before_now); |
| 178 // Expect the promo to show since install date was three days ago, and the |
| 179 // promo can show starting at 2 days after install date. |
| 180 EXPECT_TRUE(promo_.CanShow()); |
| 181 } |
| 182 |
| 183 // Test that if seconds_since_install is set, and the current time is before |
| 184 // install_date + seconds_since_install, the promo does not show. |
| 185 TEST_F(NotificationPromoWhatsNewTest, SecondsSinceInstallFailureTest) { |
| 186 // Init with seconds_since_install set to 2 days. |
| 187 Init("3 Aug 1999 9:26:06 GMT", "3 Aug 2199 9:26:06 GMT", |
| 188 "IDS_IOS_APP_RATING_PROMO_STRING", "0", "chrome_command", "", |
| 189 "ratethisapp", "RateThisAppPromo", "logo", "172800", "0"); |
| 190 // Set install date to one day before now. |
| 191 base::Time one_day_before_now_time = |
| 192 base::Time::Now() - base::TimeDelta::FromDays(1); |
| 193 int64_t one_day_before_now = one_day_before_now_time.ToTimeT(); |
| 194 local_state_.SetInt64(metrics::prefs::kInstallDate, one_day_before_now); |
| 195 // Expect the promo not to show since install date was one day ago, and |
| 196 // the promo can show starting at 2 days after install date. |
| 197 EXPECT_FALSE(promo_.CanShow()); |
| 198 } |
| 199 |
| 200 // Test that user actions are recorded when promo is viewed and closed. |
| 201 TEST_F(NotificationPromoWhatsNewTest, NotificationPromoMetricTest) { |
| 202 Init("3 Aug 1999 9:26:06 GMT", "3 Aug 2199 9:26:06 GMT", |
| 203 "IDS_IOS_APP_RATING_PROMO_STRING", "0", "chrome_command", "", |
| 204 "ratethisapp", "RateThisAppPromo", "logo", "0", "0"); |
| 205 |
| 206 base::UserActionTester user_action_tester; |
| 207 // Assert that promo is appropriately set up to be viewed. |
| 208 ASSERT_TRUE(promo_.CanShow()); |
| 209 promo_.HandleViewed(); |
| 210 EXPECT_EQ(1, user_action_tester.GetActionCount( |
| 211 "WhatsNewPromoViewed_RateThisAppPromo")); |
| 212 |
| 213 // Verify that the promo closed user action count is 0 before |HandleClosed()| |
| 214 // is called. |
| 215 EXPECT_EQ(0, user_action_tester.GetActionCount( |
| 216 "WhatsNewPromoClosed_RateThisAppPromo")); |
| 217 promo_.HandleClosed(); |
| 218 EXPECT_EQ(1, user_action_tester.GetActionCount( |
| 219 "WhatsNewPromoClosed_RateThisAppPromo")); |
| 220 } |
| 221 |
| 222 } // namespace |
OLD | NEW |