| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/ui/ntp/notification_promo_whats_new.h" | 5 #import "ios/chrome/browser/ui/ntp/notification_promo_whats_new.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 9 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| 10 #include "base/test/user_action_tester.h" | 11 #include "base/metrics/user_metrics.h" |
| 11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "components/metrics/metrics_pref_names.h" | 14 #include "components/metrics/metrics_pref_names.h" |
| 14 #include "components/prefs/pref_registry_simple.h" | 15 #include "components/prefs/pref_registry_simple.h" |
| 15 #include "components/prefs/testing_pref_service.h" | 16 #include "components/prefs/testing_pref_service.h" |
| 16 #include "components/variations/variations_associated_data.h" | 17 #include "components/variations/variations_associated_data.h" |
| 17 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" | 18 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 18 #include "ios/chrome/grit/ios_chromium_strings.h" | 19 #include "ios/chrome/grit/ios_chromium_strings.h" |
| 19 #include "ios/public/provider/chrome/browser/images/whats_new_icon.h" | 20 #include "ios/public/provider/chrome/browser/images/whats_new_icon.h" |
| 20 #include "testing/platform_test.h" | 21 #include "testing/platform_test.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 // Test fixture for NotificationPromoWhatsNew. | 27 // Test fixture for NotificationPromoWhatsNew. |
| 27 class NotificationPromoWhatsNewTest : public PlatformTest { | 28 class NotificationPromoWhatsNewTest : public PlatformTest { |
| 28 public: | 29 public: |
| 29 NotificationPromoWhatsNewTest() | 30 NotificationPromoWhatsNewTest() |
| 30 : promo_(&local_state_), | 31 : promo_(&local_state_), |
| 32 action_callback_( |
| 33 base::Bind(&NotificationPromoWhatsNewTest::OnUserAction, |
| 34 base::Unretained(this))), |
| 31 field_trial_list_(new base::FieldTrialList(NULL)) { | 35 field_trial_list_(new base::FieldTrialList(NULL)) { |
| 32 ios::NotificationPromo::RegisterPrefs(local_state_.registry()); | 36 ios::NotificationPromo::RegisterPrefs(local_state_.registry()); |
| 33 local_state_.registry()->RegisterInt64Pref(metrics::prefs::kInstallDate, 0); | 37 local_state_.registry()->RegisterInt64Pref(metrics::prefs::kInstallDate, 0); |
| 38 base::AddActionCallback(action_callback_); |
| 34 } | 39 } |
| 35 | 40 |
| 36 ~NotificationPromoWhatsNewTest() override { | 41 ~NotificationPromoWhatsNewTest() override { |
| 42 base::RemoveActionCallback(action_callback_); |
| 37 variations::testing::ClearAllVariationParams(); | 43 variations::testing::ClearAllVariationParams(); |
| 38 } | 44 } |
| 39 | 45 |
| 40 void TearDown() override { | 46 void TearDown() override { |
| 41 promo_.ClearAndInitFromJson(base::DictionaryValue()); | 47 promo_.ClearAndInitFromJson(base::DictionaryValue()); |
| 42 PlatformTest::TearDown(); | 48 PlatformTest::TearDown(); |
| 43 } | 49 } |
| 44 | 50 |
| 45 // Sets up a mock finch trial and inits the NotificationPromoWhatsNew. All | 51 // Sets up a mock finch trial and inits the NotificationPromoWhatsNew. All |
| 46 // parameters will be added to the list of finch parameters. | 52 // parameters will be added to the list of finch parameters. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 EXPECT_EQ(url, promo_.url().spec()); | 97 EXPECT_EQ(url, promo_.url().spec()); |
| 92 else | 98 else |
| 93 EXPECT_EQ(command_id, promo_.command_id()); | 99 EXPECT_EQ(command_id, promo_.command_id()); |
| 94 | 100 |
| 95 EXPECT_EQ(valid, promo_.CanShow()); | 101 EXPECT_EQ(valid, promo_.CanShow()); |
| 96 // |icon()| is set only if the promo is valid. | 102 // |icon()| is set only if the promo is valid. |
| 97 if (valid) | 103 if (valid) |
| 98 EXPECT_EQ(icon, promo_.icon()); | 104 EXPECT_EQ(icon, promo_.icon()); |
| 99 } | 105 } |
| 100 | 106 |
| 107 void OnUserAction(const std::string& user_action) { |
| 108 user_action_count_map_[user_action]++; |
| 109 } |
| 110 |
| 111 int GetUserActionCount(const std::string& user_action) { |
| 112 return user_action_count_map_[user_action]; |
| 113 } |
| 114 |
| 101 protected: | 115 protected: |
| 102 TestingPrefServiceSimple local_state_; | 116 TestingPrefServiceSimple local_state_; |
| 103 NotificationPromoWhatsNew promo_; | 117 NotificationPromoWhatsNew promo_; |
| 118 base::ActionCallback action_callback_; |
| 119 std::map<std::string, int> user_action_count_map_; |
| 104 | 120 |
| 105 private: | 121 private: |
| 106 std::unique_ptr<base::FieldTrialList> field_trial_list_; | 122 std::unique_ptr<base::FieldTrialList> field_trial_list_; |
| 107 }; | 123 }; |
| 108 | 124 |
| 109 // Test that a command-based, valid promo is shown with the correct text. | 125 // Test that a command-based, valid promo is shown with the correct text. |
| 110 TEST_F(NotificationPromoWhatsNewTest, NotificationPromoCommandTest) { | 126 TEST_F(NotificationPromoWhatsNewTest, NotificationPromoCommandTest) { |
| 111 Init("3 Aug 1999 9:26:06 GMT", "3 Aug 2199 9:26:06 GMT", | 127 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", "", | 128 "IDS_IOS_APP_RATING_PROMO_STRING", "0", "chrome_command", "", |
| 113 "ratethisapp", "RateThisAppPromo", "logo", "0", "0"); | 129 "ratethisapp", "RateThisAppPromo", "logo", "0", "0"); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 // the promo can show starting at 2 days after install date. | 212 // the promo can show starting at 2 days after install date. |
| 197 EXPECT_FALSE(promo_.CanShow()); | 213 EXPECT_FALSE(promo_.CanShow()); |
| 198 } | 214 } |
| 199 | 215 |
| 200 // Test that user actions are recorded when promo is viewed and closed. | 216 // Test that user actions are recorded when promo is viewed and closed. |
| 201 TEST_F(NotificationPromoWhatsNewTest, NotificationPromoMetricTest) { | 217 TEST_F(NotificationPromoWhatsNewTest, NotificationPromoMetricTest) { |
| 202 Init("3 Aug 1999 9:26:06 GMT", "3 Aug 2199 9:26:06 GMT", | 218 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", "", | 219 "IDS_IOS_APP_RATING_PROMO_STRING", "0", "chrome_command", "", |
| 204 "ratethisapp", "RateThisAppPromo", "logo", "0", "0"); | 220 "ratethisapp", "RateThisAppPromo", "logo", "0", "0"); |
| 205 | 221 |
| 206 base::UserActionTester user_action_tester; | |
| 207 // Assert that promo is appropriately set up to be viewed. | 222 // Assert that promo is appropriately set up to be viewed. |
| 208 ASSERT_TRUE(promo_.CanShow()); | 223 ASSERT_TRUE(promo_.CanShow()); |
| 209 promo_.HandleViewed(); | 224 promo_.HandleViewed(); |
| 210 EXPECT_EQ(1, user_action_tester.GetActionCount( | 225 EXPECT_EQ(1, GetUserActionCount("WhatsNewPromoViewed_RateThisAppPromo")); |
| 211 "WhatsNewPromoViewed_RateThisAppPromo")); | |
| 212 | 226 |
| 213 // Verify that the promo closed user action count is 0 before |HandleClosed()| | 227 // Verify that the promo closed user action count is 0 before |HandleClosed()| |
| 214 // is called. | 228 // is called. |
| 215 EXPECT_EQ(0, user_action_tester.GetActionCount( | 229 EXPECT_EQ(0, GetUserActionCount("WhatsNewPromoClosed_RateThisAppPromo")); |
| 216 "WhatsNewPromoClosed_RateThisAppPromo")); | |
| 217 promo_.HandleClosed(); | 230 promo_.HandleClosed(); |
| 218 EXPECT_EQ(1, user_action_tester.GetActionCount( | 231 EXPECT_EQ(1, GetUserActionCount("WhatsNewPromoClosed_RateThisAppPromo")); |
| 219 "WhatsNewPromoClosed_RateThisAppPromo")); | |
| 220 } | 232 } |
| 221 | 233 |
| 222 } // namespace | 234 } // namespace |
| OLD | NEW |