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 <string> 6 #include <vector> 7 8 #include "base/compiler_specific.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "chrome/test/chromedriver/chrome/adb.h" 11 #include "chrome/test/chromedriver/chrome/device_manager.h" 12 #include "chrome/test/chromedriver/chrome/status.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace { 16 17 class FakeAdb : public Adb { 18 public: 19 FakeAdb() {} 20 virtual ~FakeAdb() {} 21 22 virtual Status GetDevices(std::vector<std::string>* devices) OVERRIDE { 23 devices->push_back("a"); 24 devices->push_back("b"); 25 return Status(kOk); 26 } 27 28 virtual Status ForwardPort(const std::string& device_serial, 29 int local_port, 30 const std::string& remote_abstract) OVERRIDE { 31 return Status(kOk); 32 } 33 34 virtual Status SetCommandLineFile(const std::string& device_serial, 35 const std::string& command_line_file, 36 const std::string& exec_name, 37 const std::string& args) OVERRIDE { 38 return Status(kOk); 39 } 40 41 virtual Status CheckAppInstalled(const std::string& device_serial, 42 const std::string& package) OVERRIDE { 43 return Status(kOk); 44 } 45 46 virtual Status ClearAppData(const std::string& device_serial, 47 const std::string& package) OVERRIDE { 48 return Status(kOk); 49 } 50 51 virtual Status Launch(const std::string& device_serial, 52 const std::string& package, 53 const std::string& activity) OVERRIDE { 54 return Status(kOk); 55 } 56 57 virtual Status ForceStop(const std::string& device_serial, 58 const std::string& package) OVERRIDE { 59 return Status(kOk); 60 } 61 62 virtual Status GetPidByName(const std::string& device_serial, 63 const std::string& process_name, 64 int* pid) OVERRIDE { 65 return Status(kOk); 66 } 67 }; 68 69 } // namespace 70 71 TEST(DeviceManager, AcquireDevice) { 72 FakeAdb adb; 73 DeviceManager device_manager(&adb); 74 scoped_ptr<Device> device1; 75 scoped_ptr<Device> device2; 76 scoped_ptr<Device> device3; 77 ASSERT_TRUE(device_manager.AcquireDevice(&device1).IsOk()); 78 ASSERT_TRUE(device_manager.AcquireDevice(&device2).IsOk()); 79 ASSERT_FALSE(device_manager.AcquireDevice(&device3).IsOk()); 80 device1.reset(NULL); 81 ASSERT_TRUE(device_manager.AcquireDevice(&device3).IsOk()); 82 ASSERT_FALSE(device_manager.AcquireDevice(&device1).IsOk()); 83 } 84 85 TEST(DeviceManager, AcquireSpecificDevice) { 86 FakeAdb adb; 87 DeviceManager device_manager(&adb); 88 scoped_ptr<Device> device1; 89 scoped_ptr<Device> device2; 90 scoped_ptr<Device> device3; 91 ASSERT_TRUE(device_manager.AcquireSpecificDevice("a", &device1).IsOk()); 92 ASSERT_FALSE(device_manager.AcquireSpecificDevice("a", &device2).IsOk()); 93 ASSERT_TRUE(device_manager.AcquireSpecificDevice("b", &device3).IsOk()); 94 device1.reset(NULL); 95 ASSERT_TRUE(device_manager.AcquireSpecificDevice("a", &device2).IsOk()); 96 ASSERT_FALSE(device_manager.AcquireSpecificDevice("a", &device1).IsOk()); 97 ASSERT_FALSE(device_manager.AcquireSpecificDevice("b", &device1).IsOk()); 98 } 99 100 TEST(Device, StartStopApp) { 101 FakeAdb adb; 102 DeviceManager device_manager(&adb); 103 scoped_ptr<Device> device1; 104 ASSERT_TRUE(device_manager.AcquireDevice(&device1).IsOk()); 105 ASSERT_TRUE(device1->TearDown().IsOk()); 106 ASSERT_TRUE(device1->SetUp("a.chrome.package", "", "", "", false, 0).IsOk()); 107 ASSERT_FALSE(device1->SetUp("a.chrome.package", "", "", "", false, 0).IsOk()); 108 ASSERT_TRUE(device1->TearDown().IsOk()); 109 ASSERT_FALSE(device1->SetUp( 110 "a.chrome.package", "an.activity", "", "", false, 0).IsOk()); 111 ASSERT_FALSE(device1->SetUp("a.package", "", "", "", false, 0).IsOk()); 112 ASSERT_TRUE(device1->SetUp( 113 "a.package", "an.activity", "", "", false, 0).IsOk()); 114 ASSERT_TRUE(device1->TearDown().IsOk()); 115 ASSERT_TRUE(device1->TearDown().IsOk()); 116 ASSERT_TRUE(device1->SetUp( 117 "a.package", "an.activity", "a.process", "", false, 0).IsOk()); 118 ASSERT_TRUE(device1->TearDown().IsOk()); 119 } 120