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. 4 5#include "chrome/browser/chromeos/ui/idle_app_name_notification_view.h" 6 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" 16 17namespace { 18const char kTestAppName[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 19} // namespace 20 21class IdleAppNameNotificationViewTest : public BrowserWithTestWindowTest { 22 public: 23 IdleAppNameNotificationViewTest() 24 : BrowserWithTestWindowTest( 25 Browser::TYPE_TABBED, 26 chrome::HOST_DESKTOP_TYPE_ASH, 27 false) { 28 } 29 30 virtual ~IdleAppNameNotificationViewTest() { 31 } 32 33 virtual void SetUp() OVERRIDE { 34 // Add the application switch. 35 CommandLine::ForCurrentProcess()->AppendSwitchASCII(::switches::kAppId, 36 kTestAppName); 37 38 BrowserWithTestWindowTest::SetUp(); 39 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.email", "Someone"); 45 46 std::string error; 47 correct_extension_ = 48 extensions::Extension::Create(base::FilePath(), 49 extensions::Manifest::UNPACKED, 50 manifest, 51 extensions::Extension::NO_FLAGS, 52 kTestAppName, 53 &error); 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"); 58 59 incorrect_extension_ = 60 extensions::Extension::Create(base::FilePath(), 61 extensions::Manifest::UNPACKED, 62 manifest2, 63 extensions::Extension::NO_FLAGS, 64 kTestAppName, 65 &error); 66 } 67 68 virtual void TearDown() OVERRIDE { 69 // The destruction of the widget might be a delayed task. 70 base::MessageLoop::current()->RunUntilIdle(); 71 BrowserWithTestWindowTest::TearDown(); 72 } 73 74 extensions::Extension* correct_extension() { 75 return correct_extension_.get(); 76 } 77 extensions::Extension* incorrect_extension() { 78 return incorrect_extension_.get(); 79 } 80 81 private: 82 // Extensions to test with. 83 scoped_refptr<extensions::Extension> correct_extension_; 84 scoped_refptr<extensions::Extension> incorrect_extension_; 85 86 DISALLOW_COPY_AND_ASSIGN(IdleAppNameNotificationViewTest); 87}; 88 89// Check that creating and immediate destroying does not crash (and closes the 90// message). 91TEST_F(IdleAppNameNotificationViewTest, CheckTooEarlyDestruction) { 92 // Create a message which is visible for 10ms and fades in/out for 5ms. 93 scoped_ptr<chromeos::IdleAppNameNotificationView> message( 94 new chromeos::IdleAppNameNotificationView(10, 5, correct_extension())); 95} 96 97// Check that the message gets created and it destroys itself after time. 98TEST_F(IdleAppNameNotificationViewTest, CheckSelfDestruction) { 99 // Create a message which is visible for 10ms and fades in/out for 5ms. 100 scoped_ptr<chromeos::IdleAppNameNotificationView> message( 101 new chromeos::IdleAppNameNotificationView(10, 5, correct_extension())); 102 EXPECT_TRUE(message->IsVisible()); 103 104 // Wait now for some time and see that it closes itself again. 105 for (int i = 0; i < 50 && message->IsVisible(); i++) { 106 sleep(1); 107 base::MessageLoop::current()->RunUntilIdle(); 108 } 109 EXPECT_FALSE(message->IsVisible()); 110} 111 112// Check that the shown text for a correct application is correct. 113TEST_F(IdleAppNameNotificationViewTest, CheckCorrectApp) { 114 // Create a message which is visible for 10ms and fades in/out for 5ms. 115 scoped_ptr<chromeos::IdleAppNameNotificationView> message( 116 new chromeos::IdleAppNameNotificationView(10, 5, correct_extension())); 117 base::string16 text = message->GetShownTextForTest(); 118 // Check that the string is the application name. 119 base::string16 name = base::ASCIIToUTF16("Test"); 120 EXPECT_EQ(name, text.substr(0, name.length())); 121} 122 123// Check that an invalid app gets shown accordingly. 124TEST_F(IdleAppNameNotificationViewTest, CheckInvalidApp) { 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, NULL)); 128 base::string16 text = message->GetShownTextForTest(); 129 base::string16 error = l10n_util::GetStringUTF16( 130 IDS_IDLE_APP_NAME_UNKNOWN_APPLICATION_NOTIFICATION); 131 EXPECT_EQ(error, text); 132} 133