Index: chrome/browser/engagement/site_engagement_service_unittest.cc |
diff --git a/chrome/browser/engagement/site_engagement_service_unittest.cc b/chrome/browser/engagement/site_engagement_service_unittest.cc |
index e43d6d63f38d409f1452d918097426f4ac572b93..fd08ff48ec4d8f37768009c396b79fb6831fc70d 100644 |
--- a/chrome/browser/engagement/site_engagement_service_unittest.cc |
+++ b/chrome/browser/engagement/site_engagement_service_unittest.cc |
@@ -97,6 +97,46 @@ std::unique_ptr<KeyedService> BuildTestHistoryService( |
} // namespace |
+class ObserverTester : public SiteEngagementObserver { |
+ public: |
+ ObserverTester(SiteEngagementService* service, const GURL& url, double score) |
+ : SiteEngagementObserver(service), |
+ url_(url), |
+ score_(score), |
+ callback_called_(false), |
+ run_loop_() {} |
+ |
+ void OnEngagementIncreased(const SiteEngagementService* service, |
+ const GURL& url, |
+ double score) override { |
+ EXPECT_EQ(url_, url); |
+ EXPECT_EQ(score_, score); |
+ set_callback_called(true); |
+ run_loop_.Quit(); |
+ } |
+ |
+ void Wait() { run_loop_.Run(); } |
+ |
+ bool callback_called() { return callback_called_; } |
+ void set_callback_called(bool callback_called) { |
+ callback_called_ = callback_called; |
+ } |
+ |
+ void UpdateExpectations(const GURL& url, double score) { |
calamity
2016/05/24 08:09:33
Unused.
dominickn
2016/05/25 07:21:32
Done.
|
+ url_ = url; |
+ score_ = score; |
+ set_callback_called(false); |
+ } |
+ |
+ private: |
+ GURL url_; |
+ double score_; |
+ bool callback_called_; |
+ base::RunLoop run_loop_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ObserverTester); |
+}; |
+ |
class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { |
public: |
void SetUp() override { |
@@ -983,6 +1023,54 @@ TEST_F(SiteEngagementServiceTest, EngagementLevel) { |
url2, SiteEngagementService::ENGAGEMENT_LEVEL_MAX)); |
} |
+TEST_F(SiteEngagementServiceTest, Observers) { |
+ SiteEngagementService* service = SiteEngagementService::Get(profile()); |
+ |
+ GURL url_score_1("http://www.google.com/maps"); |
+ GURL url_score_2("http://www.google.com/drive"); |
+ GURL url_score_3("http://www.google.com/"); |
+ GURL url_not_called("https://www.google.com/"); |
+ |
+ // Create an observer and Observe(nullptr). |
+ ObserverTester tester_not_called(service, url_not_called, 1); |
+ tester_not_called.Observe(nullptr); |
+ |
+ { |
+ // Create an observer for score 1. |
+ ObserverTester tester(service, url_score_1, 1); |
+ service->AddPoints(url_score_1, 1); |
+ tester.Wait(); |
+ EXPECT_TRUE(tester.callback_called()); |
+ EXPECT_FALSE(tester_not_called.callback_called()); |
+ tester.Observe(nullptr); |
+ } |
+ |
+ { |
+ // Update observer for score 1.05 |
+ ObserverTester tester(service, url_score_2, 1.05); |
+ service->AddPoints(url_score_2, 0.05); |
+ tester.Wait(); |
+ EXPECT_TRUE(tester.callback_called()); |
+ EXPECT_FALSE(tester_not_called.callback_called()); |
+ tester.Observe(nullptr); |
+ } |
+ |
+ // Add two observers for score 1.55. |
+ { |
+ ObserverTester tester_1(service, url_score_3, 1.55); |
+ ObserverTester tester_2(service, url_score_3, 1.55); |
+ service->AddPoints(url_score_3, 0.5); |
+ tester_1.Wait(); |
+ tester_2.Wait(); |
+ |
+ EXPECT_TRUE(tester_1.callback_called()); |
+ EXPECT_TRUE(tester_2.callback_called()); |
+ EXPECT_FALSE(tester_not_called.callback_called()); |
+ tester_1.Observe(nullptr); |
+ tester_2.Observe(nullptr); |
+ } |
+} |
+ |
TEST_F(SiteEngagementServiceTest, ScoreDecayHistograms) { |
base::SimpleTestClock* clock = new base::SimpleTestClock(); |
std::unique_ptr<SiteEngagementService> service( |