Home | History | Annotate | Download | only in ui
      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/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"
     16 
     17 namespace {
     18 const char kTestAppName[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
     19 }  // namespace
     20 
     21 class 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() { return correct_extension_; }
     75   extensions::Extension* incorrect_extension() { return incorrect_extension_; }
     76 
     77  private:
     78   // Extensions to test with.
     79   scoped_refptr<extensions::Extension> correct_extension_;
     80   scoped_refptr<extensions::Extension> incorrect_extension_;
     81 
     82   DISALLOW_COPY_AND_ASSIGN(IdleAppNameNotificationViewTest);
     83 };
     84 
     85 // Check that creating and immediate destroying does not crash (and closes the
     86 // message).
     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()));
     91 }
     92 
     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());
     99 
    100   // Wait now for some time and see that it closes itself again.
    101   for (int i = 0; i < 50 && message->IsVisible(); i++) {
    102     sleep(1);
    103     base::MessageLoop::current()->RunUntilIdle();
    104   }
    105   EXPECT_FALSE(message->IsVisible());
    106 }
    107 
    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 is the application name.
    115   base::string16 name = base::ASCIIToUTF16("Test");
    116   EXPECT_EQ(name, text.substr(0, name.length()));
    117 }
    118 
    119 // Check that an invalid app gets shown accordingly.
    120 TEST_F(IdleAppNameNotificationViewTest, CheckInvalidApp) {
    121   // Create a message which is visible for 10ms and fades in/out for 5ms.
    122   scoped_ptr<chromeos::IdleAppNameNotificationView> message(
    123       new chromeos::IdleAppNameNotificationView(10, 5, NULL));
    124   base::string16 text = message->GetShownTextForTest();
    125   base::string16 error = l10n_util::GetStringUTF16(
    126       IDS_IDLE_APP_NAME_UNKNOWN_APPLICATION_NOTIFICATION);
    127   EXPECT_EQ(error, text);
    128 }
    129