| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/rlz/rlz.h" | 5 #include "chrome/browser/rlz/rlz.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 10 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
| 12 #include "chrome/browser/google/google_brand.h" | 12 #include "chrome/browser/google/google_brand.h" |
| 13 #include "chrome/browser/omnibox/omnibox_log.h" | 13 #include "chrome/browser/omnibox/omnibox_log.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/installer/util/browser_distribution.h" | 15 #include "chrome/installer/util/browser_distribution.h" |
| 16 #include "chrome/installer/util/google_update_constants.h" | 16 #include "chrome/installer/util/google_update_constants.h" |
| 17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 17 #include "components/metrics/proto/omnibox_event.pb.h" | 18 #include "components/metrics/proto/omnibox_event.pb.h" |
| 19 #include "content/public/browser/navigation_details.h" |
| 18 #include "content/public/browser/navigation_entry.h" | 20 #include "content/public/browser/navigation_entry.h" |
| 19 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
| 20 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
| 24 #include "content/test/test_render_frame_host.h" |
| 22 #include "rlz/test/rlz_test_helpers.h" | 25 #include "rlz/test/rlz_test_helpers.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 #include "url/gurl.h" |
| 24 | 28 |
| 25 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
| 26 #include "base/win/registry.h" | 30 #include "base/win/registry.h" |
| 27 #endif | 31 #endif |
| 28 | 32 |
| 29 using content::NavigationEntry; | 33 using content::NavigationEntry; |
| 34 using content::LoadCommittedDetails; |
| 30 using testing::AssertionResult; | 35 using testing::AssertionResult; |
| 31 using testing::AssertionSuccess; | 36 using testing::AssertionSuccess; |
| 32 using testing::AssertionFailure; | 37 using testing::AssertionFailure; |
| 33 | 38 |
| 34 #if defined(OS_WIN) | 39 #if defined(OS_WIN) |
| 35 using base::win::RegKey; | 40 using base::win::RegKey; |
| 36 #endif | 41 #endif |
| 37 | 42 |
| 38 namespace { | 43 namespace { |
| 39 | 44 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 104 |
| 100 bool was_ping_sent_for_brand(const std::string& brand) const { | 105 bool was_ping_sent_for_brand(const std::string& brand) const { |
| 101 return pinged_brands_.count(brand) > 0; | 106 return pinged_brands_.count(brand) > 0; |
| 102 } | 107 } |
| 103 | 108 |
| 104 void set_assume_not_ui_thread(bool assume_not_ui_thread) { | 109 void set_assume_not_ui_thread(bool assume_not_ui_thread) { |
| 105 assume_not_ui_thread_ = assume_not_ui_thread; | 110 assume_not_ui_thread_ = assume_not_ui_thread; |
| 106 } | 111 } |
| 107 | 112 |
| 108 private: | 113 private: |
| 109 virtual void ScheduleDelayedInit(base::TimeDelta delay) override { | 114 virtual void ScheduleDelayedInit(base::TimeDelta delay) OVERRIDE { |
| 110 // If the delay is 0, invoke the delayed init now. Otherwise, | 115 // If the delay is 0, invoke the delayed init now. Otherwise, |
| 111 // don't schedule anything, it will be manually called during tests. | 116 // don't schedule anything, it will be manually called during tests. |
| 112 if (delay == base::TimeDelta()) | 117 if (delay == base::TimeDelta()) |
| 113 DelayedInit(); | 118 DelayedInit(); |
| 114 } | 119 } |
| 115 | 120 |
| 116 virtual void ScheduleFinancialPing() override { | 121 virtual void ScheduleFinancialPing() OVERRIDE { |
| 117 PingNowImpl(); | 122 PingNowImpl(); |
| 118 } | 123 } |
| 119 | 124 |
| 120 virtual bool ScheduleRecordProductEvent(rlz_lib::Product product, | 125 virtual bool ScheduleRecordProductEvent(rlz_lib::Product product, |
| 121 rlz_lib::AccessPoint point, | 126 rlz_lib::AccessPoint point, |
| 122 rlz_lib::Event event_id) override { | 127 rlz_lib::Event event_id) OVERRIDE { |
| 123 return !assume_not_ui_thread_; | 128 return !assume_not_ui_thread_; |
| 124 } | 129 } |
| 125 | 130 |
| 126 virtual bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point) override { | 131 virtual bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point) OVERRIDE { |
| 127 return !assume_not_ui_thread_; | 132 return !assume_not_ui_thread_; |
| 128 } | 133 } |
| 129 | 134 |
| 130 virtual bool ScheduleRecordFirstSearch(rlz_lib::AccessPoint point) override { | 135 virtual bool ScheduleRecordFirstSearch(rlz_lib::AccessPoint point) OVERRIDE { |
| 131 return !assume_not_ui_thread_; | 136 return !assume_not_ui_thread_; |
| 132 } | 137 } |
| 133 | 138 |
| 134 #if defined(OS_CHROMEOS) | 139 #if defined(OS_CHROMEOS) |
| 135 virtual bool ScheduleClearRlzState() override { | 140 virtual bool ScheduleClearRlzState() OVERRIDE { |
| 136 return !assume_not_ui_thread_; | 141 return !assume_not_ui_thread_; |
| 137 } | 142 } |
| 138 #endif | 143 #endif |
| 139 | 144 |
| 140 virtual bool SendFinancialPing(const std::string& brand, | 145 virtual bool SendFinancialPing(const std::string& brand, |
| 141 const base::string16& lang, | 146 const base::string16& lang, |
| 142 const base::string16& referral) override { | 147 const base::string16& referral) OVERRIDE { |
| 143 // Don't ping the server during tests, just pretend as if we did. | 148 // Don't ping the server during tests, just pretend as if we did. |
| 144 EXPECT_FALSE(brand.empty()); | 149 EXPECT_FALSE(brand.empty()); |
| 145 pinged_brands_.insert(brand); | 150 pinged_brands_.insert(brand); |
| 146 | 151 |
| 147 // Set new access points RLZ string, like the actual server ping would have | 152 // Set new access points RLZ string, like the actual server ping would have |
| 148 // done. | 153 // done. |
| 149 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), | 154 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), |
| 150 kNewOmniboxRlzString); | 155 kNewOmniboxRlzString); |
| 151 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeHomePage(), | 156 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeHomePage(), |
| 152 kNewHomepageRlzString); | 157 kNewHomepageRlzString); |
| 153 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeAppList(), | 158 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeAppList(), |
| 154 kNewAppListRlzString); | 159 kNewAppListRlzString); |
| 155 return true; | 160 return true; |
| 156 } | 161 } |
| 157 | 162 |
| 158 std::set<std::string> pinged_brands_; | 163 std::set<std::string> pinged_brands_; |
| 159 bool assume_not_ui_thread_; | 164 bool assume_not_ui_thread_; |
| 160 | 165 |
| 161 DISALLOW_COPY_AND_ASSIGN(TestRLZTracker); | 166 DISALLOW_COPY_AND_ASSIGN(TestRLZTracker); |
| 162 }; | 167 }; |
| 163 | 168 |
| 164 class RlzLibTest : public RlzLibTestNoMachineState { | 169 class RlzLibTest : public ChromeRenderViewHostTestHarness { |
| 165 protected: | 170 protected: |
| 166 virtual void SetUp() override; | 171 virtual void SetUp() OVERRIDE; |
| 172 virtual void TearDown() OVERRIDE; |
| 167 | 173 |
| 168 void SetMainBrand(const char* brand); | 174 void SetMainBrand(const char* brand); |
| 169 void SetReactivationBrand(const char* brand); | 175 void SetReactivationBrand(const char* brand); |
| 170 #if defined(OS_WIN) | 176 #if defined(OS_WIN) |
| 171 void SetRegistryBrandValue(const wchar_t* name, const char* brand); | 177 void SetRegistryBrandValue(const wchar_t* name, const char* brand); |
| 172 #endif | 178 #endif |
| 173 | 179 |
| 174 void SimulateOmniboxUsage(); | 180 void SimulateOmniboxUsage(); |
| 175 void SimulateHomepageUsage(); | 181 void SimulateHomepageUsage(); |
| 176 void SimulateAppListUsage(); | 182 void SimulateAppListUsage(); |
| 177 void InvokeDelayedInit(); | 183 void InvokeDelayedInit(); |
| 178 | 184 |
| 179 void ExpectEventRecorded(const char* event_name, bool expected); | 185 void ExpectEventRecorded(const char* event_name, bool expected); |
| 180 void ExpectRlzPingSent(bool expected); | 186 void ExpectRlzPingSent(bool expected); |
| 181 void ExpectReactivationRlzPingSent(bool expected); | 187 void ExpectReactivationRlzPingSent(bool expected); |
| 182 | 188 |
| 183 TestRLZTracker tracker_; | 189 TestRLZTracker tracker_; |
| 184 #if defined(OS_POSIX) | 190 RlzLibTestNoMachineStateHelper m_rlz_test_helper_; |
| 185 scoped_ptr<google_brand::BrandForTesting> brand_override_; | |
| 186 #endif | |
| 187 }; | 191 }; |
| 188 | 192 |
| 189 void RlzLibTest::SetUp() { | 193 void RlzLibTest::SetUp() { |
| 190 RlzLibTestNoMachineState::SetUp(); | 194 ChromeRenderViewHostTestHarness::SetUp(); |
| 195 m_rlz_test_helper_.SetUp(); |
| 191 | 196 |
| 192 // Make sure a non-organic brand code is set in the registry or the RLZTracker | 197 // Make sure a non-organic brand code is set in the registry or the RLZTracker |
| 193 // is pretty much a no-op. | 198 // is pretty much a no-op. |
| 194 SetMainBrand("TEST"); | 199 SetMainBrand("TEST"); |
| 195 SetReactivationBrand(""); | 200 SetReactivationBrand(""); |
| 196 } | 201 } |
| 197 | 202 |
| 203 void RlzLibTest::TearDown() { |
| 204 ChromeRenderViewHostTestHarness::TearDown(); |
| 205 m_rlz_test_helper_.TearDown(); |
| 206 } |
| 207 |
| 198 void RlzLibTest::SetMainBrand(const char* brand) { | 208 void RlzLibTest::SetMainBrand(const char* brand) { |
| 199 #if defined(OS_WIN) | 209 #if defined(OS_WIN) |
| 200 SetRegistryBrandValue(google_update::kRegRLZBrandField, brand); | 210 SetRegistryBrandValue(google_update::kRegRLZBrandField, brand); |
| 201 #elif defined(OS_POSIX) | 211 #elif defined(OS_POSIX) |
| 202 brand_override_.reset(new google_brand::BrandForTesting(brand)); | 212 brand_override_.reset(new google_brand::BrandForTesting(brand)); |
| 203 #endif | 213 #endif |
| 204 std::string check_brand; | 214 std::string check_brand; |
| 205 google_brand::GetBrand(&check_brand); | 215 google_brand::GetBrand(&check_brand); |
| 206 EXPECT_EQ(brand, check_brand); | 216 EXPECT_EQ(brand, check_brand); |
| 207 } | 217 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 base::TimeDelta::FromSeconds(0), 0, | 253 base::TimeDelta::FromSeconds(0), 0, |
| 244 base::TimeDelta::FromSeconds(0), | 254 base::TimeDelta::FromSeconds(0), |
| 245 AutocompleteResult()); | 255 AutocompleteResult()); |
| 246 | 256 |
| 247 tracker_.Observe(chrome::NOTIFICATION_OMNIBOX_OPENED_URL, | 257 tracker_.Observe(chrome::NOTIFICATION_OMNIBOX_OPENED_URL, |
| 248 content::NotificationService::AllSources(), | 258 content::NotificationService::AllSources(), |
| 249 content::Details<OmniboxLog>(&dummy)); | 259 content::Details<OmniboxLog>(&dummy)); |
| 250 } | 260 } |
| 251 | 261 |
| 252 void RlzLibTest::SimulateHomepageUsage() { | 262 void RlzLibTest::SimulateHomepageUsage() { |
| 253 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create()); | 263 GURL home_url = GURL("https://www.google.com/"); |
| 254 entry->SetPageID(0); | 264 GURL search_url = GURL("https://www.google.com/#q=search"); |
| 255 entry->SetTransitionType(ui::PAGE_TRANSITION_HOME_PAGE); | 265 |
| 256 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, | 266 content::TestRenderFrameHost* trfh = |
| 257 content::NotificationService::AllSources(), | 267 static_cast<content::TestRenderFrameHost*>(main_rfh()); |
| 258 content::Details<NavigationEntry>(entry.get())); | 268 |
| 269 // Simulate a navigation to homepage first. |
| 270 trfh->SendNavigateWithTransition(0, home_url, ui::PAGE_TRANSITION_HOME_PAGE); |
| 271 // Then simulate a search from homepage. |
| 272 trfh->SendNavigateWithTransition(1, search_url, ui::PAGE_TRANSITION_LINK); |
| 259 } | 273 } |
| 260 | 274 |
| 261 void RlzLibTest::SimulateAppListUsage() { | 275 void RlzLibTest::SimulateAppListUsage() { |
| 262 RLZTracker::RecordAppListSearch(); | 276 RLZTracker::RecordAppListSearch(); |
| 263 } | 277 } |
| 264 | 278 |
| 265 void RlzLibTest::InvokeDelayedInit() { | 279 void RlzLibTest::InvokeDelayedInit() { |
| 266 tracker_.DelayedInit(); | 280 tracker_.DelayedInit(); |
| 267 } | 281 } |
| 268 | 282 |
| (...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 818 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz)); | 832 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz)); |
| 819 EXPECT_STREQ(kNewOmniboxRlzString, base::UTF16ToUTF8(rlz).c_str()); | 833 EXPECT_STREQ(kNewOmniboxRlzString, base::UTF16ToUTF8(rlz).c_str()); |
| 820 EXPECT_TRUE(RLZTracker::GetAccessPointRlz( | 834 EXPECT_TRUE(RLZTracker::GetAccessPointRlz( |
| 821 RLZTracker::ChromeHomePage(), &rlz)); | 835 RLZTracker::ChromeHomePage(), &rlz)); |
| 822 EXPECT_STREQ(kNewHomepageRlzString, base::UTF16ToUTF8(rlz).c_str()); | 836 EXPECT_STREQ(kNewHomepageRlzString, base::UTF16ToUTF8(rlz).c_str()); |
| 823 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeAppList(), &rlz)); | 837 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeAppList(), &rlz)); |
| 824 EXPECT_STREQ(kNewAppListRlzString, base::UTF16ToUTF8(rlz).c_str()); | 838 EXPECT_STREQ(kNewAppListRlzString, base::UTF16ToUTF8(rlz).c_str()); |
| 825 } | 839 } |
| 826 | 840 |
| 827 TEST_F(RlzLibTest, ObserveHandlesBadArgs) { | 841 TEST_F(RlzLibTest, ObserveHandlesBadArgs) { |
| 828 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create()); | 842 scoped_ptr<LoadCommittedDetails> details(new LoadCommittedDetails()); |
| 829 entry->SetPageID(0); | 843 details->entry = NavigationEntry::Create(); |
| 830 entry->SetTransitionType(ui::PAGE_TRANSITION_LINK); | 844 details->entry->SetPageID(0); |
| 831 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, | 845 details->entry->SetTransitionType(ui::PAGE_TRANSITION_LINK); |
| 846 |
| 847 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 832 content::NotificationService::AllSources(), | 848 content::NotificationService::AllSources(), |
| 833 content::Details<NavigationEntry>(NULL)); | 849 content::Details<NavigationEntry>(NULL)); |
| 834 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, | 850 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 835 content::NotificationService::AllSources(), | 851 content::NotificationService::AllSources(), |
| 836 content::Details<NavigationEntry>(entry.get())); | 852 content::Details<LoadCommittedDetails>(details.get())); |
| 837 } | 853 } |
| 838 | 854 |
| 839 // TODO(thakis): Reactivation doesn't exist on Mac yet. | 855 // TODO(thakis): Reactivation doesn't exist on Mac yet. |
| 840 #if defined(OS_WIN) | 856 #if defined(OS_WIN) |
| 841 TEST_F(RlzLibTest, ReactivationNonOrganicNonOrganic) { | 857 TEST_F(RlzLibTest, ReactivationNonOrganicNonOrganic) { |
| 842 SetReactivationBrand("REAC"); | 858 SetReactivationBrand("REAC"); |
| 843 | 859 |
| 844 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false); | 860 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false); |
| 845 InvokeDelayedInit(); | 861 InvokeDelayedInit(); |
| 846 | 862 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 887 RLZTracker::RecordProductEvent(rlz_lib::CHROME, RLZTracker::ChromeOmnibox(), | 903 RLZTracker::RecordProductEvent(rlz_lib::CHROME, RLZTracker::ChromeOmnibox(), |
| 888 rlz_lib::FIRST_SEARCH); | 904 rlz_lib::FIRST_SEARCH); |
| 889 | 905 |
| 890 ExpectEventRecorded(kOmniboxFirstSearch, true); | 906 ExpectEventRecorded(kOmniboxFirstSearch, true); |
| 891 | 907 |
| 892 RLZTracker::ClearRlzState(); | 908 RLZTracker::ClearRlzState(); |
| 893 | 909 |
| 894 ExpectEventRecorded(kOmniboxFirstSearch, false); | 910 ExpectEventRecorded(kOmniboxFirstSearch, false); |
| 895 } | 911 } |
| 896 #endif // defined(OS_CHROMEOS) | 912 #endif // defined(OS_CHROMEOS) |
| OLD | NEW |