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/ui/global_error/global_error_service.h" 6 7 #include "base/logging.h" 8 #include "base/memory/scoped_ptr.h" 9 #include "chrome/browser/ui/global_error/global_error.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace { 13 14 // Error base class that keeps track of the number of errors that exist. 15 class BaseError : public GlobalError { 16 public: 17 BaseError() { ++count_; } 18 virtual ~BaseError() { --count_; } 19 20 static int count() { return count_; } 21 22 virtual bool HasMenuItem() OVERRIDE { return false; } 23 virtual int MenuItemCommandID() OVERRIDE { 24 ADD_FAILURE(); 25 return 0; 26 } 27 virtual base::string16 MenuItemLabel() OVERRIDE { 28 ADD_FAILURE(); 29 return base::string16(); 30 } 31 virtual void ExecuteMenuItem(Browser* browser) OVERRIDE { ADD_FAILURE(); } 32 33 virtual bool HasBubbleView() OVERRIDE { return false; } 34 virtual bool HasShownBubbleView() OVERRIDE { return false; } 35 virtual void ShowBubbleView(Browser* browser) OVERRIDE { ADD_FAILURE(); } 36 virtual GlobalErrorBubbleViewBase* GetBubbleView() OVERRIDE { return NULL; } 37 38 private: 39 // This tracks the number BaseError objects that are currently instantiated. 40 static int count_; 41 42 DISALLOW_COPY_AND_ASSIGN(BaseError); 43 }; 44 45 int BaseError::count_ = 0; 46 47 // A simple error that only has a menu item. 48 class MenuError : public BaseError { 49 public: 50 explicit MenuError(int command_id, Severity severity) 51 : command_id_(command_id), 52 severity_(severity) { 53 } 54 55 virtual Severity GetSeverity() OVERRIDE { return severity_; } 56 57 virtual bool HasMenuItem() OVERRIDE { return true; } 58 virtual int MenuItemCommandID() OVERRIDE { return command_id_; } 59 virtual base::string16 MenuItemLabel() OVERRIDE { return base::string16(); } 60 virtual void ExecuteMenuItem(Browser* browser) OVERRIDE {} 61 62 private: 63 int command_id_; 64 Severity severity_; 65 66 DISALLOW_COPY_AND_ASSIGN(MenuError); 67 }; 68 69 } // namespace 70 71 // Test adding errors to the global error service. 72 TEST(GlobalErrorServiceTest, AddError) { 73 scoped_ptr<GlobalErrorService> service(new GlobalErrorService(NULL)); 74 EXPECT_EQ(0u, service->errors().size()); 75 76 BaseError* error1 = new BaseError; 77 service->AddGlobalError(error1); 78 EXPECT_EQ(1u, service->errors().size()); 79 EXPECT_EQ(error1, service->errors()[0]); 80 81 BaseError* error2 = new BaseError; 82 service->AddGlobalError(error2); 83 EXPECT_EQ(2u, service->errors().size()); 84 EXPECT_EQ(error1, service->errors()[0]); 85 EXPECT_EQ(error2, service->errors()[1]); 86 87 // Ensure that deleting the service deletes the error objects. 88 EXPECT_EQ(2, BaseError::count()); 89 service.reset(); 90 EXPECT_EQ(0, BaseError::count()); 91 } 92 93 // Test removing errors from the global error service. 94 TEST(GlobalErrorServiceTest, RemoveError) { 95 scoped_ptr<GlobalErrorService> service(new GlobalErrorService(NULL)); 96 BaseError error1; 97 service->AddGlobalError(&error1); 98 BaseError error2; 99 service->AddGlobalError(&error2); 100 101 EXPECT_EQ(2u, service->errors().size()); 102 service->RemoveGlobalError(&error1); 103 EXPECT_EQ(1u, service->errors().size()); 104 EXPECT_EQ(&error2, service->errors()[0]); 105 service->RemoveGlobalError(&error2); 106 EXPECT_EQ(0u, service->errors().size()); 107 108 // Ensure that deleting the service does not delete the error objects. 109 EXPECT_EQ(2, BaseError::count()); 110 service.reset(); 111 EXPECT_EQ(2, BaseError::count()); 112 } 113 114 // Test finding errors by their menu item command ID. 115 TEST(GlobalErrorServiceTest, GetMenuItem) { 116 MenuError* error1 = new MenuError(1, GlobalError::SEVERITY_LOW); 117 MenuError* error2 = new MenuError(2, GlobalError::SEVERITY_MEDIUM); 118 MenuError* error3 = new MenuError(3, GlobalError::SEVERITY_HIGH); 119 120 GlobalErrorService service(NULL); 121 service.AddGlobalError(error1); 122 service.AddGlobalError(error2); 123 service.AddGlobalError(error3); 124 125 EXPECT_EQ(error2, service.GetGlobalErrorByMenuItemCommandID(2)); 126 EXPECT_EQ(error3, service.GetGlobalErrorByMenuItemCommandID(3)); 127 EXPECT_EQ(NULL, service.GetGlobalErrorByMenuItemCommandID(4)); 128 } 129 130 // Test getting the error with the higest severity. 131 TEST(GlobalErrorServiceTest, HighestSeverity) { 132 MenuError* error1 = new MenuError(1, GlobalError::SEVERITY_LOW); 133 MenuError* error2 = new MenuError(2, GlobalError::SEVERITY_MEDIUM); 134 MenuError* error3 = new MenuError(3, GlobalError::SEVERITY_HIGH); 135 136 GlobalErrorService service(NULL); 137 EXPECT_EQ(NULL, service.GetHighestSeverityGlobalErrorWithWrenchMenuItem()); 138 139 service.AddGlobalError(error1); 140 EXPECT_EQ(error1, service.GetHighestSeverityGlobalErrorWithWrenchMenuItem()); 141 142 service.AddGlobalError(error2); 143 EXPECT_EQ(error2, service.GetHighestSeverityGlobalErrorWithWrenchMenuItem()); 144 145 service.AddGlobalError(error3); 146 EXPECT_EQ(error3, service.GetHighestSeverityGlobalErrorWithWrenchMenuItem()); 147 148 // Remove the highest-severity error. 149 service.RemoveGlobalError(error3); 150 delete error3; 151 152 // Now error2 should be the next highest severity error. 153 EXPECT_EQ(error2, service.GetHighestSeverityGlobalErrorWithWrenchMenuItem()); 154 } 155