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.
5 #include "chrome/browser/ui/global_error/global_error_service.h"
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"
14 // Error base class that keeps track of the number of errors that exist.
15 class BaseError
: public GlobalError
{
17 BaseError() { ++count_
; }
18 virtual ~BaseError() { --count_
; }
20 static int count() { return count_
; }
22 virtual bool HasMenuItem() OVERRIDE
{ return false; }
23 virtual int MenuItemCommandID() OVERRIDE
{
27 virtual base::string16
MenuItemLabel() OVERRIDE
{
29 return base::string16();
31 virtual void ExecuteMenuItem(Browser
* browser
) OVERRIDE
{ ADD_FAILURE(); }
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
; }
39 // This tracks the number BaseError objects that are currently instantiated.
42 DISALLOW_COPY_AND_ASSIGN(BaseError
);
45 int BaseError::count_
= 0;
47 // A simple error that only has a menu item.
48 class MenuError
: public BaseError
{
50 explicit MenuError(int command_id
, Severity severity
)
51 : command_id_(command_id
),
55 virtual Severity
GetSeverity() OVERRIDE
{ return severity_
; }
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
{}
66 DISALLOW_COPY_AND_ASSIGN(MenuError
);
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());
76 BaseError
* error1
= new BaseError
;
77 service
->AddGlobalError(error1
);
78 EXPECT_EQ(1u, service
->errors().size());
79 EXPECT_EQ(error1
, service
->errors()[0]);
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]);
87 // Ensure that deleting the service deletes the error objects.
88 EXPECT_EQ(2, BaseError::count());
90 EXPECT_EQ(0, BaseError::count());
93 // Test removing errors from the global error service.
94 TEST(GlobalErrorServiceTest
, RemoveError
) {
95 scoped_ptr
<GlobalErrorService
> service(new GlobalErrorService(NULL
));
97 service
->AddGlobalError(&error1
);
99 service
->AddGlobalError(&error2
);
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());
108 // Ensure that deleting the service does not delete the error objects.
109 EXPECT_EQ(2, BaseError::count());
111 EXPECT_EQ(2, BaseError::count());
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
);
120 GlobalErrorService
service(NULL
);
121 service
.AddGlobalError(error1
);
122 service
.AddGlobalError(error2
);
123 service
.AddGlobalError(error3
);
125 EXPECT_EQ(error2
, service
.GetGlobalErrorByMenuItemCommandID(2));
126 EXPECT_EQ(error3
, service
.GetGlobalErrorByMenuItemCommandID(3));
127 EXPECT_EQ(NULL
, service
.GetGlobalErrorByMenuItemCommandID(4));
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
);
136 GlobalErrorService
service(NULL
);
137 EXPECT_EQ(NULL
, service
.GetHighestSeverityGlobalErrorWithWrenchMenuItem());
139 service
.AddGlobalError(error1
);
140 EXPECT_EQ(error1
, service
.GetHighestSeverityGlobalErrorWithWrenchMenuItem());
142 service
.AddGlobalError(error2
);
143 EXPECT_EQ(error2
, service
.GetHighestSeverityGlobalErrorWithWrenchMenuItem());
145 service
.AddGlobalError(error3
);
146 EXPECT_EQ(error3
, service
.GetHighestSeverityGlobalErrorWithWrenchMenuItem());
148 // Remove the highest-severity error.
149 service
.RemoveGlobalError(error3
);
152 // Now error2 should be the next highest severity error.
153 EXPECT_EQ(error2
, service
.GetHighestSeverityGlobalErrorWithWrenchMenuItem());