1 // Copyright 2014 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/chromeos/ui/idle_app_name_notification_view.h"
7 #include "base/command_line.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/test_extension_system.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/test/base/browser_with_test_window_test.h"
13 #include "extensions/common/manifest_constants.h"
14 #include "grit/generated_resources.h"
15 #include "ui/base/l10n/l10n_util.h"
18 const char kTestAppName[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
21 class IdleAppNameNotificationViewTest : public BrowserWithTestWindowTest {
23 IdleAppNameNotificationViewTest()
24 : BrowserWithTestWindowTest(
26 chrome::HOST_DESKTOP_TYPE_ASH,
30 virtual ~IdleAppNameNotificationViewTest() {
33 virtual void SetUp() OVERRIDE {
34 // Add the application switch.
35 CommandLine::ForCurrentProcess()->AppendSwitchASCII(::switches::kAppId,
38 BrowserWithTestWindowTest::SetUp();
40 base::DictionaryValue manifest;
41 manifest.SetString(extensions::manifest_keys::kName, "Test");
42 manifest.SetString(extensions::manifest_keys::kVersion, "1");
43 manifest.SetString(extensions::manifest_keys::kDescription, "Test app");
44 manifest.SetString("author", "Someone");
48 extensions::Extension::Create(base::FilePath(),
49 extensions::Manifest::UNPACKED,
51 extensions::Extension::NO_FLAGS,
54 base::DictionaryValue manifest2;
55 manifest2.SetString(extensions::manifest_keys::kName, "Test");
56 manifest2.SetString(extensions::manifest_keys::kVersion, "1");
57 manifest2.SetString(extensions::manifest_keys::kDescription, "Test app");
59 incorrect_extension_ =
60 extensions::Extension::Create(base::FilePath(),
61 extensions::Manifest::UNPACKED,
63 extensions::Extension::NO_FLAGS,
68 virtual void TearDown() OVERRIDE {
69 // The destruction of the widget might be a delayed task.
70 base::MessageLoop::current()->RunUntilIdle();
71 BrowserWithTestWindowTest::TearDown();
74 extensions::Extension* correct_extension() { return correct_extension_; }
75 extensions::Extension* incorrect_extension() { return incorrect_extension_; }
78 // Extensions to test with.
79 scoped_refptr<extensions::Extension> correct_extension_;
80 scoped_refptr<extensions::Extension> incorrect_extension_;
82 DISALLOW_COPY_AND_ASSIGN(IdleAppNameNotificationViewTest);
85 // Check that creating and immediate destroying does not crash (and closes the
87 TEST_F(IdleAppNameNotificationViewTest, CheckTooEarlyDestruction) {
88 // Create a message which is visible for 10ms and fades in/out for 5ms.
89 scoped_ptr<chromeos::IdleAppNameNotificationView> message(
90 new chromeos::IdleAppNameNotificationView(10, 5, correct_extension()));
93 // Check that the message gets created and it destroys itself after time.
94 TEST_F(IdleAppNameNotificationViewTest, CheckSelfDestruction) {
95 // Create a message which is visible for 10ms and fades in/out for 5ms.
96 scoped_ptr<chromeos::IdleAppNameNotificationView> message(
97 new chromeos::IdleAppNameNotificationView(10, 5, correct_extension()));
98 EXPECT_TRUE(message->IsVisible());
100 // Wait now for some time and see that it closes itself again.
101 for (int i = 0; i < 50 && message->IsVisible(); i++) {
103 base::MessageLoop::current()->RunUntilIdle();
105 EXPECT_FALSE(message->IsVisible());
108 // Check that the shown text for a correct application is correct.
109 TEST_F(IdleAppNameNotificationViewTest, CheckCorrectApp) {
110 // Create a message which is visible for 10ms and fades in/out for 5ms.
111 scoped_ptr<chromeos::IdleAppNameNotificationView> message(
112 new chromeos::IdleAppNameNotificationView(10, 5, correct_extension()));
113 base::string16 text = message->GetShownTextForTest();
114 // Check that the string starts with the application name followed by a space.
115 base::string16 name = base::ASCIIToUTF16("Test ");
116 EXPECT_EQ(name, text.substr(0, name.length()));
117 // Check that the string ends with a space + author's name.
118 base::string16 author = base::ASCIIToUTF16(" Someone");
120 text.substr(text.length() - author.length(), author.length()));
123 // Check that an invalid author gets shown accordingly.
124 TEST_F(IdleAppNameNotificationViewTest, CheckInvalidAuthor) {
125 // Create a message which is visible for 10ms and fades in/out for 5ms.
126 scoped_ptr<chromeos::IdleAppNameNotificationView> message(
127 new chromeos::IdleAppNameNotificationView(10, 5, incorrect_extension()));
128 base::string16 text = message->GetShownTextForTest();
129 // Check that the string starts with the application name followed by a space.
130 base::string16 name = base::ASCIIToUTF16("Test ");
131 EXPECT_EQ(name, text.substr(0, name.length()));
132 // Check that it ends in an invalid author notification.
133 base::string16 author = l10n_util::GetStringUTF16(
134 IDS_IDLE_APP_NAME_INVALID_AUTHOR_NOTIFICATION);
136 text.substr(text.length() - author.length(), author.length()));
139 // Check that an invalid app gets shown accordingly.
140 TEST_F(IdleAppNameNotificationViewTest, CheckInvalidApp) {
141 // Create a message which is visible for 10ms and fades in/out for 5ms.
142 scoped_ptr<chromeos::IdleAppNameNotificationView> message(
143 new chromeos::IdleAppNameNotificationView(10, 5, NULL));
144 base::string16 text = message->GetShownTextForTest();
145 base::string16 error = l10n_util::GetStringUTF16(
146 IDS_IDLE_APP_NAME_UNKNOWN_APPLICATION_NOTIFICATION);
147 EXPECT_EQ(error, text);