- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / accessibility / magnification_manager_unittest.cc
1 // Copyright (c) 2013 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/accessibility/magnification_manager.h"
6
7 #include "ash/magnifier/magnifier_constants.h"
8 #include "ash/test/ash_test_base.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "content/public/browser/notification_service.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace chromeos {
17 namespace {
18
19 class MockMagnificationObserver : public content::NotificationObserver {
20  public:
21   MockMagnificationObserver() : observed_(false),
22                                 observed_enabled_(false),
23                                 observed_type_(ash::kDefaultMagnifierType) {
24     registrar_.Add(
25         this,
26         chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFIER,
27         content::NotificationService::AllSources());
28   }
29
30   bool observed() const { return observed_; }
31   bool observed_enabled() const { return observed_enabled_; }
32   ash::MagnifierType observed_type() const { return observed_type_; }
33
34   void reset() { observed_ = false; }
35
36  private:
37   // content::NotificationObserver implimentation:
38   virtual void Observe(int type,
39                        const content::NotificationSource& source,
40                        const content::NotificationDetails& details) OVERRIDE {
41     switch (type) {
42       case chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFIER:
43         AccessibilityStatusEventDetails* accessibility_status =
44             content::Details<AccessibilityStatusEventDetails>(details).ptr();
45
46         observed_ = true;
47         observed_enabled_ = accessibility_status->enabled;
48         observed_type_ = accessibility_status->magnifier_type;
49     }
50   }
51
52   bool observed_;
53   bool observed_enabled_;
54   ash::MagnifierType observed_type_;
55
56   content::NotificationRegistrar registrar_;
57 };
58
59 void EnableMagnifier() {
60   return MagnificationManager::Get()->SetMagnifierEnabled(true);
61 }
62
63 void DisableMagnifier() {
64   return MagnificationManager::Get()->SetMagnifierEnabled(false);
65 }
66
67 bool IsMagnifierEnabled() {
68   return MagnificationManager::Get()->IsMagnifierEnabled();
69 }
70
71 ash::MagnifierType GetMagnifierType() {
72   return MagnificationManager::Get()->GetMagnifierType();
73 }
74
75 void SetMagnifierType(ash::MagnifierType type) {
76   return MagnificationManager::Get()->SetMagnifierType(type);
77 }
78
79 }  // namespace
80
81 class MagnificationManagerTest : public ash::test::AshTestBase {
82  public:
83   MagnificationManagerTest() {
84   }
85
86   virtual void SetUp() OVERRIDE {
87     ash::test::AshTestBase::SetUp();
88     MagnificationManager::Initialize();
89     ASSERT_TRUE(MagnificationManager::Get());
90     MagnificationManager::Get()->SetProfileForTest(&profile_);
91   }
92
93   virtual void TearDown() OVERRIDE {
94     MagnificationManager::Shutdown();
95     ash::test::AshTestBase::TearDown();
96   }
97
98   TestingProfile profile_;
99 };
100
101 TEST_F(MagnificationManagerTest, MagnificationObserver) {
102   MockMagnificationObserver observer;
103
104   EXPECT_FALSE(observer.observed());
105
106   // Set full screen magnifier, and confirm the observer is called.
107   EnableMagnifier();
108   SetMagnifierType(ash::MAGNIFIER_FULL);
109   EXPECT_TRUE(observer.observed());
110   EXPECT_TRUE(observer.observed_enabled());
111   EXPECT_EQ(observer.observed_type(), ash::MAGNIFIER_FULL);
112   EXPECT_EQ(GetMagnifierType(), ash::MAGNIFIER_FULL);
113   observer.reset();
114
115   // Set full screen magnifier again, and confirm the observer is not called.
116   SetMagnifierType(ash::MAGNIFIER_FULL);
117   EXPECT_FALSE(observer.observed());
118   EXPECT_EQ(GetMagnifierType(), ash::MAGNIFIER_FULL);
119   observer.reset();
120 }
121
122 TEST_F(MagnificationManagerTest, ChangeType) {
123   // Set full screen magnifier, and confirm the status is set successfully.
124   EnableMagnifier();
125   SetMagnifierType(ash::MAGNIFIER_FULL);
126   EXPECT_TRUE(IsMagnifierEnabled());
127   EXPECT_EQ(GetMagnifierType(), ash::MAGNIFIER_FULL);
128
129   // Set partial screen magnifier, and confirm the change is ignored.
130   SetMagnifierType(ash::MAGNIFIER_PARTIAL);
131   EXPECT_TRUE(IsMagnifierEnabled());
132   EXPECT_EQ(GetMagnifierType(), ash::MAGNIFIER_FULL);
133
134   // Disables magnifier, and confirm the status is set successfully.
135   DisableMagnifier();
136   EXPECT_FALSE(IsMagnifierEnabled());
137   EXPECT_EQ(GetMagnifierType(), ash::MAGNIFIER_FULL);
138 }
139
140 }  // namespace chromeos