Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / ui / idle_app_name_notification_view_unittest.cc
blob8502eae6afa22bb99185b862f3fa7d194efc21b0
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/grit/generated_resources.h"
13 #include "chrome/test/base/browser_with_test_window_test.h"
14 #include "extensions/common/manifest_constants.h"
15 #include "ui/base/l10n/l10n_util.h"
17 namespace {
18 const char kTestAppName[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
19 } // namespace
21 class IdleAppNameNotificationViewTest : public BrowserWithTestWindowTest {
22 public:
23 IdleAppNameNotificationViewTest()
24 : BrowserWithTestWindowTest(
25 Browser::TYPE_TABBED,
26 chrome::HOST_DESKTOP_TYPE_ASH,
27 false) {
30 ~IdleAppNameNotificationViewTest() override {}
32 void SetUp() override {
33 // Add the application switch.
34 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
35 ::switches::kAppId, kTestAppName);
37 BrowserWithTestWindowTest::SetUp();
39 base::DictionaryValue manifest;
40 manifest.SetString(extensions::manifest_keys::kName, "Test");
41 manifest.SetString(extensions::manifest_keys::kVersion, "1");
42 manifest.SetString(extensions::manifest_keys::kDescription, "Test app");
43 manifest.SetString("author.email", "Someone");
45 std::string error;
46 correct_extension_ =
47 extensions::Extension::Create(base::FilePath(),
48 extensions::Manifest::UNPACKED,
49 manifest,
50 extensions::Extension::NO_FLAGS,
51 kTestAppName,
52 &error);
53 base::DictionaryValue manifest2;
54 manifest2.SetString(extensions::manifest_keys::kName, "Test");
55 manifest2.SetString(extensions::manifest_keys::kVersion, "1");
56 manifest2.SetString(extensions::manifest_keys::kDescription, "Test app");
58 incorrect_extension_ =
59 extensions::Extension::Create(base::FilePath(),
60 extensions::Manifest::UNPACKED,
61 manifest2,
62 extensions::Extension::NO_FLAGS,
63 kTestAppName,
64 &error);
67 void TearDown() override {
68 // The destruction of the widget might be a delayed task.
69 base::MessageLoop::current()->RunUntilIdle();
70 BrowserWithTestWindowTest::TearDown();
73 extensions::Extension* correct_extension() {
74 return correct_extension_.get();
76 extensions::Extension* incorrect_extension() {
77 return incorrect_extension_.get();
80 private:
81 // Extensions to test with.
82 scoped_refptr<extensions::Extension> correct_extension_;
83 scoped_refptr<extensions::Extension> incorrect_extension_;
85 DISALLOW_COPY_AND_ASSIGN(IdleAppNameNotificationViewTest);
88 // Check that creating and immediate destroying does not crash (and closes the
89 // message).
90 TEST_F(IdleAppNameNotificationViewTest, CheckTooEarlyDestruction) {
91 // Create a message which is visible for 10ms and fades in/out for 5ms.
92 scoped_ptr<chromeos::IdleAppNameNotificationView> message(
93 new chromeos::IdleAppNameNotificationView(10, 5, correct_extension()));
96 // Check that the message gets created and it destroys itself after time.
97 TEST_F(IdleAppNameNotificationViewTest, CheckSelfDestruction) {
98 // Create a message which is visible for 10ms and fades in/out for 5ms.
99 scoped_ptr<chromeos::IdleAppNameNotificationView> message(
100 new chromeos::IdleAppNameNotificationView(10, 5, correct_extension()));
101 EXPECT_TRUE(message->IsVisible());
103 // Wait now for some time and see that it closes itself again.
104 for (int i = 0; i < 50 && message->IsVisible(); i++) {
105 sleep(1);
106 base::MessageLoop::current()->RunUntilIdle();
108 EXPECT_FALSE(message->IsVisible());
111 // Check that the shown text for a correct application is correct.
112 TEST_F(IdleAppNameNotificationViewTest, CheckCorrectApp) {
113 // Create a message which is visible for 10ms and fades in/out for 5ms.
114 scoped_ptr<chromeos::IdleAppNameNotificationView> message(
115 new chromeos::IdleAppNameNotificationView(10, 5, correct_extension()));
116 base::string16 text = message->GetShownTextForTest();
117 // Check that the string is the application name.
118 base::string16 name = base::ASCIIToUTF16("Test");
119 EXPECT_EQ(name, text.substr(0, name.length()));
122 // Check that an invalid app gets shown accordingly.
123 TEST_F(IdleAppNameNotificationViewTest, CheckInvalidApp) {
124 // Create a message which is visible for 10ms and fades in/out for 5ms.
125 scoped_ptr<chromeos::IdleAppNameNotificationView> message(
126 new chromeos::IdleAppNameNotificationView(10, 5, NULL));
127 base::string16 text = message->GetShownTextForTest();
128 base::string16 error = l10n_util::GetStringUTF16(
129 IDS_IDLE_APP_NAME_UNKNOWN_APPLICATION_NOTIFICATION);
130 EXPECT_EQ(error, text);