File: idle_app_name_notification_view_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (128 lines) | stat: -rw-r--r-- 4,822 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/notifications/idle_app_name_notification_view.h"

#include "base/command_line.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "extensions/common/manifest_constants.h"
#include "ui/base/l10n/l10n_util.h"

namespace ash {

namespace {

const char kTestAppName[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

}  // namespace

class IdleAppNameNotificationViewTest : public BrowserWithTestWindowTest {
 public:
  IdleAppNameNotificationViewTest()
      : BrowserWithTestWindowTest(Browser::TYPE_NORMAL) {}

  IdleAppNameNotificationViewTest(const IdleAppNameNotificationViewTest&) =
      delete;
  IdleAppNameNotificationViewTest& operator=(
      const IdleAppNameNotificationViewTest&) = delete;

  ~IdleAppNameNotificationViewTest() override = default;

  void SetUp() override {
    // Add the application switch.
    base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
        ::switches::kAppId, kTestAppName);

    BrowserWithTestWindowTest::SetUp();

    base::Value::Dict manifest;
    manifest.Set(extensions::manifest_keys::kName, "Test");
    manifest.Set(extensions::manifest_keys::kVersion, "1");
    manifest.Set(extensions::manifest_keys::kManifestVersion, 2);
    manifest.Set(extensions::manifest_keys::kDescription, "Test app");
    manifest.SetByDottedPath("author.email", "Someone");

    std::string error;
    correct_extension_ = extensions::Extension::Create(
        base::FilePath(), extensions::mojom::ManifestLocation::kUnpacked,
        manifest, extensions::Extension::NO_FLAGS, kTestAppName, &error);
    base::Value::Dict manifest2;
    manifest2.Set(extensions::manifest_keys::kName, "Test");
    manifest2.Set(extensions::manifest_keys::kVersion, "1");
    manifest2.Set(extensions::manifest_keys::kDescription, "Test app");

    incorrect_extension_ = extensions::Extension::Create(
        base::FilePath(), extensions::mojom::ManifestLocation::kUnpacked,
        manifest2, extensions::Extension::NO_FLAGS, kTestAppName, &error);
  }

  void TearDown() override {
    // The destruction of the widget might be a delayed task.
    base::RunLoop().RunUntilIdle();
    BrowserWithTestWindowTest::TearDown();
  }

  extensions::Extension* correct_extension() {
    return correct_extension_.get();
  }
  extensions::Extension* incorrect_extension() {
    return incorrect_extension_.get();
  }

 private:
  // Extensions to test with.
  scoped_refptr<extensions::Extension> correct_extension_;
  scoped_refptr<extensions::Extension> incorrect_extension_;
};

// Check that creating and immediate destroying does not crash (and closes the
// message).
TEST_F(IdleAppNameNotificationViewTest, CheckTooEarlyDestruction) {
  // Create a message which is visible for 10ms and fades in/out for 5ms.
  std::make_unique<IdleAppNameNotificationView>(10, 5, correct_extension());
}

// Check that the message gets created and it destroys itself after time.
TEST_F(IdleAppNameNotificationViewTest, CheckSelfDestruction) {
  // Create a message which is visible for 10ms and fades in/out for 5ms.
  auto message =
      std::make_unique<IdleAppNameNotificationView>(10, 5, correct_extension());
  EXPECT_TRUE(message->IsVisible());

  // Wait now for some time and see that it closes itself again.
  for (int i = 0; i < 50 && message->IsVisible(); i++) {
    sleep(1);
    base::RunLoop().RunUntilIdle();
  }
  EXPECT_FALSE(message->IsVisible());
}

// Check that the shown text for a correct application is correct.
TEST_F(IdleAppNameNotificationViewTest, CheckCorrectApp) {
  // Create a message which is visible for 10ms and fades in/out for 5ms.
  auto message =
      std::make_unique<IdleAppNameNotificationView>(10, 5, correct_extension());
  std::u16string text = message->GetShownTextForTest();
  // Check that the string is the application name.
  std::u16string name = u"Test";
  EXPECT_EQ(name, text.substr(0, name.length()));
}

// Check that an invalid app gets shown accordingly.
TEST_F(IdleAppNameNotificationViewTest, CheckInvalidApp) {
  // Create a message which is visible for 10ms and fades in/out for 5ms.
  auto message = std::make_unique<IdleAppNameNotificationView>(10, 5, nullptr);
  std::u16string text = message->GetShownTextForTest();
  std::u16string error = l10n_util::GetStringUTF16(
      IDS_IDLE_APP_NAME_UNKNOWN_APPLICATION_NOTIFICATION);
  EXPECT_EQ(error, text);
}

}  // namespace ash