-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(EC-514): Handle Motion Sensor Leakage when started but not stopp…
…ed (#30)
- Loading branch information
1 parent
daea639
commit adfea75
Showing
8 changed files
with
202 additions
and
2 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
swift-lang/src/main/java/io/ecocode/ios/swift/checks/sobriety/MotionSensorLeakCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* ecoCode iOS plugin - Help the earth, adopt this green plugin for your applications | ||
* Copyright © 2023 green-code-initiative (https://www.ecocode.io/) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package io.ecocode.ios.swift.checks.sobriety; | ||
|
||
import io.ecocode.ios.swift.SwiftRuleCheck; | ||
import io.ecocode.ios.swift.antlr.generated.Swift5Parser; | ||
import org.antlr.v4.runtime.tree.ParseTree; | ||
import org.antlr.v4.runtime.tree.TerminalNodeImpl; | ||
import org.sonar.check.Rule; | ||
|
||
@Rule(key = "EC514") | ||
public class MotionSensorLeakCheck extends SwiftRuleCheck { | ||
private static final String DEFAULT_ISSUE_MESSAGE = "Any motion sensor started should be stopped."; | ||
private boolean motionSensorStarted = false; | ||
private boolean motionSensorStopped = false; | ||
Swift5Parser.ExpressionContext id; | ||
|
||
@Override | ||
public void apply(ParseTree tree) { | ||
if (tree instanceof Swift5Parser.ExpressionContext && tree.getText().contains("startAccelerometerUpdates")) { | ||
id = (Swift5Parser.ExpressionContext) tree; | ||
motionSensorStarted = true; | ||
} | ||
|
||
if (tree instanceof Swift5Parser.ExpressionContext | ||
&& (tree.getText().contains("stopAccelerometerUpdates"))) { | ||
motionSensorStopped = true; | ||
} | ||
|
||
if (tree instanceof TerminalNodeImpl && tree.getText().equals("<EOF>")) { | ||
if (motionSensorStarted && !motionSensorStopped) { | ||
this.recordIssue(id.getStart().getStartIndex(), DEFAULT_ISSUE_MESSAGE); | ||
} | ||
motionSensorStarted = false; | ||
motionSensorStopped = false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,14 @@ | |
"EC509", | ||
"EC512", | ||
"EC513", | ||
"EC514", | ||
"EC519", | ||
"EC520", | ||
"EC522", | ||
"EC524", | ||
"EC530", | ||
"EC533", | ||
"EC534", | ||
"EC603" | ||
] | ||
} |
45 changes: 45 additions & 0 deletions
45
swift-lang/src/main/resources/io/ecocode/rules/swift/EC514.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<img src="http://www.neomades.com/extern/partage/ecoCode/2sur5_1x.png"> | ||
<p>Most iOS devices have built-in sensors that measure motion, orientation, and various environmental conditions. | ||
Additionally, they have image sensors (a.k.a. Camera) and geo-positioning sensors (a.k.a. GPS). | ||
|
||
The common point of all these sensors is that they consume significant power while in use. Their common issue is | ||
processing data unnecessarily when the app is in an idle state, typically when it enters the background or becomes | ||
inactive. | ||
|
||
<code>Consequently, calls to start and stop sensor updates must be carefully managed for motion sensor: | ||
CMMotionManager#startAccelerometerUpdates()/CMMotionManager#stopAccelerometerUpdates(). | ||
Failing to do so can drain the battery quickly.</code></p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
import CoreMotion | ||
|
||
let motionManager = CMMotionManager() | ||
|
||
func startMotionUpdates() { | ||
if motionManager.isAccelerometerAvailable { | ||
motionManager.startAccelerometerUpdates(to: .main) { data, error in | ||
// Handle accelerometer updates | ||
} | ||
} | ||
} | ||
</pre> | ||
<h2>Compliant Code Example</h2> | ||
<pre> | ||
import CoreMotion | ||
|
||
let motionManager = CMMotionManager() | ||
|
||
func startMotionUpdates() { | ||
if motionManager.isAccelerometerAvailable { | ||
motionManager.startAccelerometerUpdates(to: .main) { data, error in | ||
// Handle accelerometer updates | ||
} | ||
} | ||
} | ||
|
||
func stopMotionUpdates() { | ||
if motionManager.isAccelerometerActive { | ||
motionManager.stopAccelerometerUpdates() | ||
} | ||
} | ||
</pre> |
18 changes: 18 additions & 0 deletions
18
swift-lang/src/main/resources/io/ecocode/rules/swift/EC514.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"key": "EC514", | ||
"title": "Motion Sensor Leakage", | ||
"defaultSeverity": "Major", | ||
"description": "Any motion sensor started should be stopped.", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"ecocode", | ||
"environment", | ||
"sobriety", | ||
"eco-design" | ||
], | ||
"type": "CODE_SMELL" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
swift-lang/src/test/java/io/ecocode/ios/swift/checks/sobriety/MotionSensorLeakCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* ecoCode iOS plugin - Help the earth, adopt this green plugin for your applications | ||
* Copyright © 2023 green-code-initiative (https://www.ecocode.io/) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package io.ecocode.ios.swift.checks.sobriety; | ||
|
||
import io.ecocode.ios.swift.checks.CheckTestHelper; | ||
import org.junit.Test; | ||
import org.sonar.api.batch.sensor.internal.SensorContextTester; | ||
import org.sonar.api.batch.sensor.issue.Issue; | ||
import org.sonar.api.batch.sensor.issue.IssueLocation; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class MotionSensorLeakCheckTest { | ||
@Test | ||
public void SensorLeak_trigger() { | ||
SensorContextTester context = CheckTestHelper.analyzeTestFile("checks/sobriety/MotionSensorLeak_trigger.swift"); | ||
assertThat(context.allIssues()).hasSize(1); | ||
Optional<Issue> issue = context.allIssues().stream().findFirst(); | ||
issue.ifPresent(i -> { | ||
assertThat(i.ruleKey().rule()).isEqualTo("EC514"); | ||
|
||
assertThat(i.ruleKey().repository()).isEqualTo("ecoCode-swift"); | ||
IssueLocation location = i.primaryLocation(); | ||
assertThat(location.textRange().start().line()).isEqualTo(7); | ||
|
||
}); | ||
} | ||
|
||
@Test | ||
public void SensorLeak_no_trigger() { | ||
SensorContextTester context = CheckTestHelper.analyzeTestFile("checks/sobriety/MotionSensorLeak_no_trigger.swift"); | ||
assertThat(context.allIssues()).isEmpty(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
swift-lang/src/test/resources/checks/sobriety/MotionSensorLeak_no_trigger.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import CoreMotion | ||
|
||
let motionManager = CMMotionManager() | ||
|
||
func startMotionUpdates() { | ||
if motionManager.isAccelerometerAvailable { | ||
motionManager.startAccelerometerUpdates(to: .main) { data, error in | ||
// Handle accelerometer updates | ||
} | ||
} | ||
motionManager.magnetometerUpdateInterval = 0.1 | ||
} | ||
|
||
func stopMotionUpdates() { | ||
if motionManager.isAccelerometerActive { | ||
motionManager.stopAccelerometerUpdates() | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
swift-lang/src/test/resources/checks/sobriety/MotionSensorLeak_trigger.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import CoreMotion | ||
|
||
let motionManager = CMMotionManager() | ||
|
||
func startMotionUpdates() { | ||
if motionManager.isAccelerometerAvailable { | ||
motionManager.startAccelerometerUpdates(to: .main) { data, error in | ||
// Handle accelerometer updates | ||
} | ||
} | ||
motionManager.magnetometerUpdateInterval = 0.1 | ||
} |