| OLD | NEW |
| (Empty) |
| 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_service.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "chrome/browser/ui/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 bool HasBadge() OVERRIDE { return false; } | |
| 23 virtual int GetBadgeResourceID() OVERRIDE { | |
| 24 ADD_FAILURE(); | |
| 25 return 0; | |
| 26 } | |
| 27 | |
| 28 virtual bool HasMenuItem() OVERRIDE { return false; } | |
| 29 virtual int MenuItemCommandID() OVERRIDE { | |
| 30 ADD_FAILURE(); | |
| 31 return 0; | |
| 32 } | |
| 33 virtual string16 MenuItemLabel() OVERRIDE { | |
| 34 ADD_FAILURE(); | |
| 35 return string16(); | |
| 36 } | |
| 37 virtual int MenuItemIconResourceID() OVERRIDE { | |
| 38 ADD_FAILURE(); | |
| 39 return 0; | |
| 40 } | |
| 41 virtual void ExecuteMenuItem(Browser* browser) OVERRIDE { ADD_FAILURE(); } | |
| 42 | |
| 43 virtual bool HasBubbleView() OVERRIDE { return false; } | |
| 44 virtual int GetBubbleViewIconResourceID() OVERRIDE { | |
| 45 ADD_FAILURE(); | |
| 46 return 0; | |
| 47 } | |
| 48 virtual string16 GetBubbleViewTitle() OVERRIDE { | |
| 49 ADD_FAILURE(); | |
| 50 return string16(); | |
| 51 } | |
| 52 virtual string16 GetBubbleViewMessage() OVERRIDE { | |
| 53 ADD_FAILURE(); | |
| 54 return string16(); | |
| 55 } | |
| 56 virtual string16 GetBubbleViewAcceptButtonLabel() OVERRIDE { | |
| 57 ADD_FAILURE(); | |
| 58 return string16(); | |
| 59 } | |
| 60 virtual string16 GetBubbleViewCancelButtonLabel() OVERRIDE { | |
| 61 ADD_FAILURE(); | |
| 62 return string16(); | |
| 63 } | |
| 64 virtual void OnBubbleViewDidClose(Browser* browser) OVERRIDE { | |
| 65 ADD_FAILURE(); | |
| 66 } | |
| 67 virtual void BubbleViewAcceptButtonPressed(Browser* browser) OVERRIDE { | |
| 68 ADD_FAILURE(); | |
| 69 } | |
| 70 virtual void BubbleViewCancelButtonPressed(Browser* browser) OVERRIDE { | |
| 71 ADD_FAILURE(); | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 // This tracks the number BaseError objects that are currently instantiated. | |
| 76 static int count_; | |
| 77 | |
| 78 DISALLOW_COPY_AND_ASSIGN(BaseError); | |
| 79 }; | |
| 80 | |
| 81 int BaseError::count_ = 0; | |
| 82 | |
| 83 // A simple error that only has a badge. | |
| 84 class BadgeError : public BaseError { | |
| 85 public: | |
| 86 explicit BadgeError(int resource_id) : resource_id_(resource_id) {} | |
| 87 bool HasBadge() OVERRIDE { return true; } | |
| 88 virtual int GetBadgeResourceID() OVERRIDE { return resource_id_; } | |
| 89 | |
| 90 private: | |
| 91 int resource_id_; | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(BadgeError); | |
| 94 }; | |
| 95 | |
| 96 // A simple error that only has a menu item. | |
| 97 class MenuError : public BaseError { | |
| 98 public: | |
| 99 explicit MenuError(int command_id) : command_id_(command_id) {} | |
| 100 | |
| 101 virtual bool HasMenuItem() OVERRIDE { return true; } | |
| 102 virtual int MenuItemCommandID() OVERRIDE { return command_id_; } | |
| 103 virtual string16 MenuItemLabel() OVERRIDE { return string16(); } | |
| 104 virtual void ExecuteMenuItem(Browser* browser) OVERRIDE {} | |
| 105 | |
| 106 private: | |
| 107 int command_id_; | |
| 108 | |
| 109 DISALLOW_COPY_AND_ASSIGN(MenuError); | |
| 110 }; | |
| 111 | |
| 112 } // namespace | |
| 113 | |
| 114 // Test adding errors to the global error service. | |
| 115 TEST(GlobalErrorServiceTest, AddError) { | |
| 116 scoped_ptr<GlobalErrorService> service(new GlobalErrorService(NULL)); | |
| 117 EXPECT_EQ(0u, service->errors().size()); | |
| 118 | |
| 119 BaseError* error1 = new BaseError; | |
| 120 service->AddGlobalError(error1); | |
| 121 EXPECT_EQ(1u, service->errors().size()); | |
| 122 EXPECT_EQ(error1, service->errors()[0]); | |
| 123 | |
| 124 BaseError* error2 = new BaseError; | |
| 125 service->AddGlobalError(error2); | |
| 126 EXPECT_EQ(2u, service->errors().size()); | |
| 127 EXPECT_EQ(error1, service->errors()[0]); | |
| 128 EXPECT_EQ(error2, service->errors()[1]); | |
| 129 | |
| 130 // Ensure that deleting the service deletes the error objects. | |
| 131 EXPECT_EQ(2, BaseError::count()); | |
| 132 service.reset(); | |
| 133 EXPECT_EQ(0, BaseError::count()); | |
| 134 } | |
| 135 | |
| 136 // Test removing errors from the global error service. | |
| 137 TEST(GlobalErrorServiceTest, RemoveError) { | |
| 138 scoped_ptr<GlobalErrorService> service(new GlobalErrorService(NULL)); | |
| 139 BaseError error1; | |
| 140 service->AddGlobalError(&error1); | |
| 141 BaseError error2; | |
| 142 service->AddGlobalError(&error2); | |
| 143 | |
| 144 EXPECT_EQ(2u, service->errors().size()); | |
| 145 service->RemoveGlobalError(&error1); | |
| 146 EXPECT_EQ(1u, service->errors().size()); | |
| 147 EXPECT_EQ(&error2, service->errors()[0]); | |
| 148 service->RemoveGlobalError(&error2); | |
| 149 EXPECT_EQ(0u, service->errors().size()); | |
| 150 | |
| 151 // Ensure that deleting the service does not delete the error objects. | |
| 152 EXPECT_EQ(2, BaseError::count()); | |
| 153 service.reset(); | |
| 154 EXPECT_EQ(2, BaseError::count()); | |
| 155 } | |
| 156 | |
| 157 // Test finding errors by their menu item command ID. | |
| 158 TEST(GlobalErrorServiceTest, GetMenuItem) { | |
| 159 BaseError* error1 = new BaseError; | |
| 160 MenuError* error2 = new MenuError(2); | |
| 161 MenuError* error3 = new MenuError(3); | |
| 162 | |
| 163 GlobalErrorService service(NULL); | |
| 164 service.AddGlobalError(error1); | |
| 165 service.AddGlobalError(error2); | |
| 166 service.AddGlobalError(error3); | |
| 167 | |
| 168 EXPECT_EQ(error2, service.GetGlobalErrorByMenuItemCommandID(2)); | |
| 169 EXPECT_EQ(error3, service.GetGlobalErrorByMenuItemCommandID(3)); | |
| 170 EXPECT_EQ(NULL, service.GetGlobalErrorByMenuItemCommandID(4)); | |
| 171 } | |
| 172 | |
| 173 // Test getting the badge icon resource ID of the first error. | |
| 174 TEST(GlobalErrorServiceTest, GetBadgeID) { | |
| 175 BaseError* error1 = new BaseError; | |
| 176 BadgeError error2(2); | |
| 177 BadgeError* error3 = new BadgeError(3); | |
| 178 | |
| 179 GlobalErrorService service(NULL); | |
| 180 EXPECT_EQ(0, service.GetFirstBadgeResourceID()); | |
| 181 | |
| 182 service.AddGlobalError(error1); | |
| 183 EXPECT_EQ(0, service.GetFirstBadgeResourceID()); | |
| 184 | |
| 185 service.AddGlobalError(&error2); | |
| 186 EXPECT_EQ(2, service.GetFirstBadgeResourceID()); | |
| 187 | |
| 188 service.AddGlobalError(error3); | |
| 189 EXPECT_EQ(2, service.GetFirstBadgeResourceID()); | |
| 190 | |
| 191 // Remove the first error with a badge. | |
| 192 service.RemoveGlobalError(&error2); | |
| 193 // Now error3 should be the first error with a badge. | |
| 194 EXPECT_EQ(3, service.GetFirstBadgeResourceID()); | |
| 195 } | |
| OLD | NEW |