| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 #include "base/command_line.h" | 4 #include "base/command_line.h" |
| 5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" | 6 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" |
| 7 #include "chrome/browser/extensions/extension_apitest.h" | 7 #include "chrome/browser/extensions/extension_apitest.h" |
| 8 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 8 #include "chrome/common/chrome_switches.h" | 9 #include "chrome/common/chrome_switches.h" |
| 10 #include "chrome/test/base/ui_test_utils.h" |
| 9 | 11 |
| 10 namespace extensions { | 12 namespace extensions { |
| 11 | 13 |
| 12 using api::experimental_system_info_cpu::CpuInfo; | 14 using api::experimental_system_info_cpu::CpuInfo; |
| 13 | 15 |
| 14 class MockCpuInfoProviderImpl : public CpuInfoProvider { | 16 class MockCpuInfoProviderImpl : public CpuInfoProvider { |
| 15 public: | 17 public: |
| 16 MockCpuInfoProviderImpl() {} | 18 MockCpuInfoProviderImpl() : num_of_processors_(4) { |
| 19 // Set sampling interval to 200ms for testing. |
| 20 sampling_interval_ = 200; |
| 21 } |
| 17 ~MockCpuInfoProviderImpl() {} | 22 ~MockCpuInfoProviderImpl() {} |
| 18 | 23 |
| 19 virtual bool QueryInfo(CpuInfo* info) OVERRIDE { | 24 virtual bool QueryInfo(CpuInfo* info) OVERRIDE { |
| 20 if (!info) return false; | 25 if (!info) return false; |
| 21 | 26 |
| 22 info->num_of_processors = 4; | 27 info->num_of_processors = num_of_processors_; |
| 23 info->arch_name = "x86"; | 28 info->arch_name = "x86"; |
| 24 info->model_name = "unknown"; | 29 info->model_name = "unknown"; |
| 25 | 30 |
| 26 return true; | 31 return true; |
| 27 } | 32 } |
| 33 |
| 34 private: |
| 35 virtual bool QueryCpuTimePerProcessor(std::vector<CpuTime>* times) OVERRIDE { |
| 36 DCHECK(times); |
| 37 |
| 38 times->clear(); |
| 39 static int user_step = 3; |
| 40 static int kernel_step = 2; |
| 41 static int idle_step = 1; |
| 42 static int count = 0; |
| 43 for (int i = 0; i < num_of_processors_; i++) { |
| 44 CpuTime time; |
| 45 time.user += user_step * count; |
| 46 time.kernel += kernel_step * count; |
| 47 time.idle += idle_step * count; |
| 48 times->push_back(time); |
| 49 |
| 50 count++; |
| 51 } |
| 52 return true; |
| 53 } |
| 54 |
| 55 int num_of_processors_; |
| 28 }; | 56 }; |
| 29 | 57 |
| 30 class SystemInfoCpuApiTest: public ExtensionApiTest { | 58 class SystemInfoCpuApiTest: public ExtensionApiTest { |
| 31 public: | 59 public: |
| 32 SystemInfoCpuApiTest() {} | 60 SystemInfoCpuApiTest() {} |
| 33 ~SystemInfoCpuApiTest() {} | 61 ~SystemInfoCpuApiTest() {} |
| 34 | 62 |
| 35 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 63 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 36 ExtensionApiTest::SetUpCommandLine(command_line); | 64 ExtensionApiTest::SetUpCommandLine(command_line); |
| 37 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | 65 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
| 38 } | 66 } |
| 39 | 67 |
| 40 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | 68 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
| 41 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); | 69 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); |
| 42 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_UI)); | 70 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_UI)); |
| 43 | |
| 44 CpuInfoProvider* provider = new MockCpuInfoProviderImpl(); | |
| 45 // The provider is owned by the single CpuInfoProvider instance. | |
| 46 CpuInfoProvider::InitializeForTesting(provider); | |
| 47 } | 71 } |
| 48 | 72 |
| 49 private: | 73 private: |
| 50 scoped_ptr<MessageLoop> message_loop_; | 74 scoped_ptr<MessageLoop> message_loop_; |
| 51 }; | 75 }; |
| 52 | 76 |
| 53 IN_PROC_BROWSER_TEST_F(SystemInfoCpuApiTest, Cpu) { | 77 IN_PROC_BROWSER_TEST_F(SystemInfoCpuApiTest, Cpu) { |
| 78 CpuInfoProvider* provider = new MockCpuInfoProviderImpl(); |
| 79 // The provider is owned by the single CpuInfoProvider instance. |
| 80 CpuInfoProvider::InitializeForTesting(provider); |
| 54 ASSERT_TRUE(RunExtensionTest("systeminfo/cpu")) << message_; | 81 ASSERT_TRUE(RunExtensionTest("systeminfo/cpu")) << message_; |
| 55 } | 82 } |
| 56 | 83 |
| 57 } // namespace extensions | 84 } // namespace extensions |
| OLD | NEW |