Home | History | Annotate | Download | only in system_display
      1 // Copyright 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/extensions/api/system_display/system_display_api.h"
      6 
      7 #include "base/strings/string_number_conversions.h"
      8 #include "chrome/browser/extensions/api/system_display/display_info_provider.h"
      9 #include "chrome/browser/extensions/extension_apitest.h"
     10 #include "chrome/browser/extensions/extension_function_test_utils.h"
     11 
     12 namespace utils = extension_function_test_utils;
     13 
     14 namespace extensions {
     15 
     16 using api::system_display::Bounds;
     17 using api::system_display::DisplayUnitInfo;
     18 
     19 class MockDisplayInfoProvider : public DisplayInfoProvider {
     20  public:
     21   MockDisplayInfoProvider() {}
     22 
     23   virtual bool QueryInfo() OVERRIDE {
     24     info_.clear();
     25     for (int i = 0; i < 4; i++) {
     26       linked_ptr<DisplayUnitInfo> unit(new DisplayUnitInfo());
     27       unit->id = base::IntToString(i);
     28       unit->name = "DISPLAY NAME FOR " + unit->id;
     29       if (i == 1)
     30         unit->mirroring_source_id = "0";
     31       unit->is_primary = i == 0 ? true : false;
     32       unit->is_internal = i == 0 ? true : false;
     33       unit->is_enabled = true;
     34       unit->rotation = (90 * i) % 360;
     35       unit->dpi_x = 96.0;
     36       unit->dpi_y = 96.0;
     37       unit->bounds.left = 0;
     38       unit->bounds.top = 0;
     39       unit->bounds.width = 1280;
     40       unit->bounds.height = 720;
     41       if (i == 0) {
     42         unit->overscan.left = 20;
     43         unit->overscan.top = 40;
     44         unit->overscan.right = 60;
     45         unit->overscan.bottom = 80;
     46       }
     47       unit->work_area.left = 0;
     48       unit->work_area.top = 0;
     49       unit->work_area.width = 960;
     50       unit->work_area.height = 720;
     51       info_.push_back(unit);
     52     }
     53     return true;
     54   }
     55 
     56   virtual void SetInfo(
     57       const std::string& display_id,
     58       const api::system_display::DisplayProperties& params,
     59       const SetInfoCallback& callback) OVERRIDE {
     60     // Should get called only once per test case.
     61     EXPECT_FALSE(set_info_value_);
     62     set_info_value_ = params.ToValue();
     63     set_info_display_id_ = display_id;
     64     callback.Run(true, std::string());
     65   }
     66 
     67   scoped_ptr<base::DictionaryValue> GetSetInfoValue() {
     68     return set_info_value_.Pass();
     69   }
     70 
     71   std::string GetSetInfoDisplayId() const {
     72     return set_info_display_id_;
     73   }
     74 
     75  private:
     76   virtual ~MockDisplayInfoProvider() {}
     77 
     78   scoped_ptr<base::DictionaryValue> set_info_value_;
     79   std::string set_info_display_id_;
     80 
     81   DISALLOW_COPY_AND_ASSIGN(MockDisplayInfoProvider);
     82 };
     83 
     84 class SystemDisplayApiTest: public ExtensionApiTest {
     85  public:
     86   SystemDisplayApiTest() {}
     87   virtual ~SystemDisplayApiTest() {}
     88 
     89   virtual void SetUpOnMainThread() OVERRIDE {
     90     ExtensionApiTest::SetUpOnMainThread();
     91     provider_ = new MockDisplayInfoProvider();
     92     // The |provider| will be co-owned by the singleton instance.
     93     DisplayInfoProvider::InitializeForTesting(provider_);
     94   }
     95 
     96   virtual void CleanUpOnMainThread() OVERRIDE {
     97     // Has to be released before the main thread is gone.
     98     provider_ = NULL;
     99     ExtensionApiTest::CleanUpOnMainThread();
    100   }
    101 
    102  protected:
    103   scoped_refptr<MockDisplayInfoProvider> provider_;
    104 
    105   DISALLOW_COPY_AND_ASSIGN(SystemDisplayApiTest);
    106 };
    107 
    108 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, GetDisplay) {
    109   ASSERT_TRUE(RunPlatformAppTest("system/display")) << message_;
    110 }
    111 
    112 #if !defined(OS_CHROMEOS)
    113 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplay) {
    114   scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
    115       set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
    116 
    117   set_info_function->set_has_callback(true);
    118 
    119   EXPECT_EQ("Function available only on ChromeOS.",
    120             utils::RunFunctionAndReturnError(set_info_function.get(),
    121                                              "[\"display_id\", {}]",
    122                                              browser()));
    123 
    124   scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
    125   EXPECT_FALSE(set_info);
    126 }
    127 #endif  // !defined(OS_CHROMEOS)
    128 
    129 #if defined(OS_CHROMEOS)
    130 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayNotKioskEnabled) {
    131   scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary(
    132       "{\n"
    133       "  \"name\": \"Test\",\n"
    134       "  \"version\": \"1.0\",\n"
    135       "  \"app\": {\n"
    136       "    \"background\": {\n"
    137       "      \"scripts\": [\"background.js\"]\n"
    138       "    }\n"
    139       "  }\n"
    140       "}"));
    141   scoped_refptr<Extension> test_extension(
    142       utils::CreateExtension(test_extension_value.get()));
    143 
    144   scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
    145       set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
    146 
    147   set_info_function->set_extension(test_extension.get());
    148   set_info_function->set_has_callback(true);
    149 
    150   EXPECT_EQ("The extension needs to be kiosk enabled to use the function.",
    151             utils::RunFunctionAndReturnError(set_info_function.get(),
    152                                              "[\"display_id\", {}]",
    153                                              browser()));
    154 
    155   scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
    156   EXPECT_FALSE(set_info);
    157 }
    158 
    159 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayKioskEnabled) {
    160   scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary(
    161       "{\n"
    162       "  \"name\": \"Test\",\n"
    163       "  \"version\": \"1.0\",\n"
    164       "  \"app\": {\n"
    165       "    \"background\": {\n"
    166       "      \"scripts\": [\"background.js\"]\n"
    167       "    }\n"
    168       "  },\n"
    169       "  \"kiosk_enabled\": true\n"
    170       "}"));
    171   scoped_refptr<Extension> test_extension(
    172       utils::CreateExtension(test_extension_value.get()));
    173 
    174   scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
    175       set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
    176 
    177   set_info_function->set_has_callback(true);
    178   set_info_function->set_extension(test_extension.get());
    179 
    180   ASSERT_TRUE(utils::RunFunction(
    181       set_info_function.get(),
    182       "[\"display_id\", {\n"
    183       "  \"isPrimary\": true,\n"
    184       "  \"mirroringSourceId\": \"mirroringId\",\n"
    185       "  \"boundsOriginX\": 100,\n"
    186       "  \"boundsOriginY\": 200,\n"
    187       "  \"rotation\": 90,\n"
    188       "  \"overscan\": {\"left\": 1, \"top\": 2, \"right\": 3, \"bottom\": 4}\n"
    189       "}]",
    190       browser(),
    191       utils::NONE));
    192 
    193   scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
    194   ASSERT_TRUE(set_info);
    195   EXPECT_TRUE(utils::GetBoolean(set_info.get(), "isPrimary"));
    196   EXPECT_EQ("mirroringId",
    197             utils::GetString(set_info.get(), "mirroringSourceId"));
    198   EXPECT_EQ(100, utils::GetInteger(set_info.get(), "boundsOriginX"));
    199   EXPECT_EQ(200, utils::GetInteger(set_info.get(), "boundsOriginY"));
    200   EXPECT_EQ(90, utils::GetInteger(set_info.get(), "rotation"));
    201   base::DictionaryValue* overscan;
    202   ASSERT_TRUE(set_info->GetDictionary("overscan", &overscan));
    203   EXPECT_EQ(1, utils::GetInteger(overscan, "left"));
    204   EXPECT_EQ(2, utils::GetInteger(overscan, "top"));
    205   EXPECT_EQ(3, utils::GetInteger(overscan, "right"));
    206   EXPECT_EQ(4, utils::GetInteger(overscan, "bottom"));
    207 
    208   EXPECT_EQ("display_id", provider_->GetSetInfoDisplayId());
    209 }
    210 #endif  // defined(OS_CHROMEOS)
    211 
    212 } // namespace extensions
    213