Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2011 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 #include "base/basictypes.h"
      6 #include "base/command_line.h"
      7 #include "base/utf_string_conversions.h"
      8 #include "chrome/browser/background_page_tracker.h"
      9 #include "chrome/test/testing_browser_process.h"
     10 #include "chrome/test/testing_browser_process_test.h"
     11 #include "chrome/test/testing_pref_service.h"
     12 #include "content/common/notification_service.h"
     13 #include "content/common/notification_type.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 #include "testing/platform_test.h"
     16 
     17 class MockBackgroundPageTracker : public BackgroundPageTracker {
     18  public:
     19   MockBackgroundPageTracker() {
     20     BackgroundPageTracker::RegisterPrefs(&prefs_);
     21   }
     22   ~MockBackgroundPageTracker() {}
     23   // Overridden from BackgroundPageTracker to mock out functionality.
     24   virtual bool IsEnabled() { return true; }
     25   virtual PrefService* GetPrefService() { return &prefs_; }
     26  private:
     27   TestingPrefService prefs_;
     28 };
     29 
     30 class BackgroundPageTrackerTest : public TestingBrowserProcessTest {
     31 };
     32 
     33 TEST_F(BackgroundPageTrackerTest, Create) {
     34   MockBackgroundPageTracker tracker;
     35   EXPECT_EQ(0, tracker.GetBackgroundPageCount());
     36   EXPECT_EQ(0, tracker.GetUnacknowledgedBackgroundPageCount());
     37 }
     38 
     39 TEST_F(BackgroundPageTrackerTest, OnBackgroundPageLoaded) {
     40   MockBackgroundPageTracker tracker;
     41   EXPECT_EQ(0, tracker.GetBackgroundPageCount());
     42   EXPECT_EQ(0, tracker.GetUnacknowledgedBackgroundPageCount());
     43   std::string app1 = "app_id_1";
     44   std::string app2 = "app_id_2";
     45   tracker.OnBackgroundPageLoaded(app1);
     46   EXPECT_EQ(1, tracker.GetBackgroundPageCount());
     47   EXPECT_EQ(1, tracker.GetUnacknowledgedBackgroundPageCount());
     48   tracker.OnBackgroundPageLoaded(app1);
     49   EXPECT_EQ(1, tracker.GetBackgroundPageCount());
     50   EXPECT_EQ(1, tracker.GetUnacknowledgedBackgroundPageCount());
     51   tracker.OnBackgroundPageLoaded(app2);
     52   EXPECT_EQ(2, tracker.GetBackgroundPageCount());
     53   EXPECT_EQ(2, tracker.GetUnacknowledgedBackgroundPageCount());
     54 
     55   tracker.OnExtensionUnloaded(app1);
     56   EXPECT_EQ(1, tracker.GetBackgroundPageCount());
     57   EXPECT_EQ(1, tracker.GetUnacknowledgedBackgroundPageCount());
     58 
     59   tracker.OnExtensionUnloaded(app2);
     60   EXPECT_EQ(0, tracker.GetBackgroundPageCount());
     61   EXPECT_EQ(0, tracker.GetUnacknowledgedBackgroundPageCount());
     62 }
     63 
     64 TEST_F(BackgroundPageTrackerTest, AcknowledgeBackgroundPages) {
     65   MockBackgroundPageTracker tracker;
     66   EXPECT_EQ(0, tracker.GetBackgroundPageCount());
     67   EXPECT_EQ(0, tracker.GetUnacknowledgedBackgroundPageCount());
     68   std::string app1 = "app_id_1";
     69   tracker.OnBackgroundPageLoaded(app1);
     70   EXPECT_EQ(1, tracker.GetBackgroundPageCount());
     71   EXPECT_EQ(1, tracker.GetUnacknowledgedBackgroundPageCount());
     72   tracker.AcknowledgeBackgroundPages();
     73   EXPECT_EQ(1, tracker.GetBackgroundPageCount());
     74   EXPECT_EQ(0, tracker.GetUnacknowledgedBackgroundPageCount());
     75   tracker.OnBackgroundPageLoaded(app1);
     76   EXPECT_EQ(1, tracker.GetBackgroundPageCount());
     77   EXPECT_EQ(0, tracker.GetUnacknowledgedBackgroundPageCount());
     78 }
     79 
     80 class BadgeChangedNotificationCounter : public NotificationObserver {
     81  public:
     82   BadgeChangedNotificationCounter()
     83       : count_(0) {
     84     registrar_.Add(this,
     85                    NotificationType::BACKGROUND_PAGE_TRACKER_CHANGED,
     86                    NotificationService::AllSources());
     87   }
     88   // # notifications received.
     89   int count() { return count_; }
     90   // NotificationObserver implementation.
     91   virtual void Observe(NotificationType type,
     92                        const NotificationSource& source,
     93                        const NotificationDetails& details) {
     94     EXPECT_EQ(type.value,
     95               NotificationType::BACKGROUND_PAGE_TRACKER_CHANGED);
     96     count_++;
     97   }
     98  private:
     99   int count_;
    100   NotificationRegistrar registrar_;
    101 };
    102 
    103 TEST_F(BackgroundPageTrackerTest, TestTrackerChangedNotifications) {
    104   MockBackgroundPageTracker tracker;
    105   BadgeChangedNotificationCounter counter;
    106   std::string app1 = "app_id_1";
    107   std::string app2 = "app_id_2";
    108   std::string app3 = "app_id_3";
    109   // New extension should generate notification
    110   tracker.OnBackgroundPageLoaded(app1);
    111   EXPECT_EQ(1, counter.count());
    112   // Same extension should not generate notification
    113   tracker.OnBackgroundPageLoaded(app1);
    114   EXPECT_EQ(1, counter.count());
    115   // New extension should generate notification
    116   tracker.OnBackgroundPageLoaded(app2);
    117   EXPECT_EQ(2, counter.count());
    118   // Acknowledging pages should generate notification.
    119   tracker.AcknowledgeBackgroundPages();
    120   EXPECT_EQ(3, counter.count());
    121   tracker.OnBackgroundPageLoaded(app1);
    122   EXPECT_EQ(3, counter.count());
    123   tracker.OnBackgroundPageLoaded(app3);
    124   EXPECT_EQ(4, counter.count());
    125 }
    126