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 ~BaseError() override
{ --count_
; }
20 static int count() { return count_
; }
22 bool HasMenuItem() override
{ return false; }
23 int MenuItemCommandID() override
{
27 base::string16
MenuItemLabel() override
{
29 return base::string16();
31 void ExecuteMenuItem(Browser
* browser
) override
{ ADD_FAILURE(); }
33 bool HasBubbleView() override
{ return false; }
34 bool HasShownBubbleView() override
{ return false; }
35 void ShowBubbleView(Browser
* browser
) override
{ ADD_FAILURE(); }
36 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 Severity
GetSeverity() override
{ return severity_
; }
57 bool HasMenuItem() override
{ return true; }
58 int MenuItemCommandID() override
{ return command_id_
; }
59 base::string16
MenuItemLabel() override
{ return base::string16(); }
60 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());