Home | History | Annotate | Download | only in extensions
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/extensions/extension_warning_service.h"
      6 
      7 #include "chrome/browser/profiles/profile.h"
      8 #include "chrome/browser/ui/global_error/global_error_service.h"
      9 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
     10 #include "chrome/test/base/testing_profile.h"
     11 #include "testing/gmock/include/gmock/gmock.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 namespace extensions {
     15 
     16 namespace {
     17 
     18 class TestExtensionWarningService : public ExtensionWarningService {
     19  public:
     20   explicit TestExtensionWarningService(Profile* profile)
     21       : ExtensionWarningService(profile) {
     22   }
     23   virtual ~TestExtensionWarningService() {}
     24 
     25   void AddWarning(const ExtensionWarning& warning) {
     26     ExtensionWarningSet warnings;
     27     warnings.insert(warning);
     28     AddWarnings(warnings);
     29   }
     30 };
     31 
     32 class MockObserver : public ExtensionWarningService::Observer {
     33  public:
     34   virtual ~MockObserver() {}
     35   MOCK_METHOD0(ExtensionWarningsChanged, void());
     36 };
     37 
     38 const char* ext1_id = "extension1";
     39 const char* ext2_id = "extension2";
     40 const ExtensionWarning::WarningType warning_1 =
     41     ExtensionWarning::kNetworkDelay;
     42 const ExtensionWarning::WarningType warning_2 =
     43     ExtensionWarning::kNetworkConflict;
     44 
     45 }  // namespace
     46 
     47 // Check that inserting a warning triggers notifications, whereas inserting
     48 // the same warning again is silent.
     49 TEST(ExtensionWarningServiceTest, SetWarning) {
     50   TestingProfile profile;
     51   TestExtensionWarningService warning_service(&profile);
     52   MockObserver observer;
     53   warning_service.AddObserver(&observer);
     54 
     55   // Insert warning for the first time.
     56   EXPECT_CALL(observer, ExtensionWarningsChanged());
     57   warning_service.AddWarning(
     58       ExtensionWarning::CreateNetworkDelayWarning(ext1_id));
     59   testing::Mock::VerifyAndClearExpectations(&warning_service);
     60 
     61   // Second insertion of same warning does not trigger anything.
     62   warning_service.AddWarning(
     63       ExtensionWarning::CreateNetworkDelayWarning(ext1_id));
     64   testing::Mock::VerifyAndClearExpectations(&warning_service);
     65 
     66   warning_service.RemoveObserver(&observer);
     67 }
     68 
     69 // Check that ClearWarnings deletes exactly the specified warnings and
     70 // triggers notifications where appropriate.
     71 TEST(ExtensionWarningServiceTest, ClearWarnings) {
     72   TestingProfile profile;
     73   TestExtensionWarningService warning_service(&profile);
     74   MockObserver observer;
     75   warning_service.AddObserver(&observer);
     76 
     77   // Insert two unique warnings in one batch.
     78   EXPECT_CALL(observer, ExtensionWarningsChanged());
     79   ExtensionWarningSet warning_set;
     80   warning_set.insert(ExtensionWarning::CreateNetworkDelayWarning(ext1_id));
     81   warning_set.insert(ExtensionWarning::CreateNetworkConflictWarning(ext2_id));
     82   warning_service.AddWarnings(warning_set);
     83   testing::Mock::VerifyAndClearExpectations(&warning_service);
     84 
     85   // Remove one warning and check that the badge remains.
     86   EXPECT_CALL(observer, ExtensionWarningsChanged());
     87   std::set<ExtensionWarning::WarningType> to_clear;
     88   to_clear.insert(warning_2);
     89   warning_service.ClearWarnings(to_clear);
     90   testing::Mock::VerifyAndClearExpectations(&warning_service);
     91 
     92   // Check that the correct warnings appear in |warnings|.
     93   std::set<ExtensionWarning::WarningType> existing_warnings =
     94       warning_service.GetWarningTypesAffectingExtension(ext1_id);
     95   EXPECT_EQ(1u, existing_warnings.size());
     96   existing_warnings =
     97       warning_service.GetWarningTypesAffectingExtension(ext2_id);
     98   EXPECT_EQ(0u, existing_warnings.size());
     99 
    100   // Remove the other one warning.
    101   EXPECT_CALL(observer, ExtensionWarningsChanged());
    102   to_clear.insert(warning_1);
    103   warning_service.ClearWarnings(to_clear);
    104   testing::Mock::VerifyAndClearExpectations(&warning_service);
    105 
    106   // Check that not warnings remain.
    107   existing_warnings =
    108       warning_service.GetWarningTypesAffectingExtension(ext1_id);
    109   EXPECT_EQ(0u, existing_warnings.size());
    110   existing_warnings =
    111       warning_service.GetWarningTypesAffectingExtension(ext2_id);
    112   EXPECT_EQ(0u, existing_warnings.size());
    113 
    114   warning_service.RemoveObserver(&observer);
    115 }
    116 
    117 }  // namespace extensions
    118